blob: 0abdc27750eeb90ab863d4d9e8e954d748222e61 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_ASYNCSOCKET_H_
12#define RTC_BASE_ASYNCSOCKET_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/socket.h"
Artem Titove41c4332018-07-25 15:04:28 +020015#include "rtc_base/third_party/sigslot/sigslot.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020017namespace rtc {
18
19// TODO: Remove Socket and rename AsyncSocket to Socket.
20
21// Provides the ability to perform socket I/O asynchronously.
22class AsyncSocket : public Socket {
23 public:
24 AsyncSocket();
25 ~AsyncSocket() override;
26
27 AsyncSocket* Accept(SocketAddress* paddr) override = 0;
28
29 // SignalReadEvent and SignalWriteEvent use multi_threaded_local to allow
30 // access concurrently from different thread.
31 // For example SignalReadEvent::connect will be called in AsyncUDPSocket ctor
32 // but at the same time the SocketDispatcher maybe signaling the read event.
33 // ready to read
Yves Gerey665174f2018-06-19 15:03:05 +020034 sigslot::signal1<AsyncSocket*, sigslot::multi_threaded_local> SignalReadEvent;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020035 // ready to write
Yves Gerey665174f2018-06-19 15:03:05 +020036 sigslot::signal1<AsyncSocket*, sigslot::multi_threaded_local>
37 SignalWriteEvent;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020038 sigslot::signal1<AsyncSocket*> SignalConnectEvent; // connected
39 sigslot::signal2<AsyncSocket*, int> SignalCloseEvent; // closed
40};
41
42class AsyncSocketAdapter : public AsyncSocket, public sigslot::has_slots<> {
43 public:
44 // The adapted socket may explicitly be null, and later assigned using Attach.
45 // However, subclasses which support detached mode must override any methods
46 // that will be called during the detached period (usually GetState()), to
47 // avoid dereferencing a null pointer.
48 explicit AsyncSocketAdapter(AsyncSocket* socket);
49 ~AsyncSocketAdapter() override;
50 void Attach(AsyncSocket* socket);
51 SocketAddress GetLocalAddress() const override;
52 SocketAddress GetRemoteAddress() const override;
53 int Bind(const SocketAddress& addr) override;
54 int Connect(const SocketAddress& addr) override;
55 int Send(const void* pv, size_t cb) override;
56 int SendTo(const void* pv, size_t cb, const SocketAddress& addr) override;
57 int Recv(void* pv, size_t cb, int64_t* timestamp) override;
58 int RecvFrom(void* pv,
59 size_t cb,
60 SocketAddress* paddr,
61 int64_t* timestamp) override;
62 int Listen(int backlog) override;
63 AsyncSocket* Accept(SocketAddress* paddr) override;
64 int Close() override;
65 int GetError() const override;
66 void SetError(int error) override;
67 ConnState GetState() const override;
68 int GetOption(Option opt, int* value) override;
69 int SetOption(Option opt, int value) override;
70
71 protected:
72 virtual void OnConnectEvent(AsyncSocket* socket);
73 virtual void OnReadEvent(AsyncSocket* socket);
74 virtual void OnWriteEvent(AsyncSocket* socket);
75 virtual void OnCloseEvent(AsyncSocket* socket, int err);
76
77 AsyncSocket* socket_;
78};
79
80} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000081
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020082#endif // RTC_BASE_ASYNCSOCKET_H_