blob: c018c23f077fcfc58d14124376b757217c819de5 [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/sigslot.h"
15#include "rtc_base/socket.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
34 sigslot::signal1<AsyncSocket*,
35 sigslot::multi_threaded_local> SignalReadEvent;
36 // ready to write
37 sigslot::signal1<AsyncSocket*,
38 sigslot::multi_threaded_local> SignalWriteEvent;
39 sigslot::signal1<AsyncSocket*> SignalConnectEvent; // connected
40 sigslot::signal2<AsyncSocket*, int> SignalCloseEvent; // closed
41};
42
43class AsyncSocketAdapter : public AsyncSocket, public sigslot::has_slots<> {
44 public:
45 // The adapted socket may explicitly be null, and later assigned using Attach.
46 // However, subclasses which support detached mode must override any methods
47 // that will be called during the detached period (usually GetState()), to
48 // avoid dereferencing a null pointer.
49 explicit AsyncSocketAdapter(AsyncSocket* socket);
50 ~AsyncSocketAdapter() override;
51 void Attach(AsyncSocket* socket);
52 SocketAddress GetLocalAddress() const override;
53 SocketAddress GetRemoteAddress() const override;
54 int Bind(const SocketAddress& addr) override;
55 int Connect(const SocketAddress& addr) override;
56 int Send(const void* pv, size_t cb) override;
57 int SendTo(const void* pv, size_t cb, const SocketAddress& addr) override;
58 int Recv(void* pv, size_t cb, int64_t* timestamp) override;
59 int RecvFrom(void* pv,
60 size_t cb,
61 SocketAddress* paddr,
62 int64_t* timestamp) override;
63 int Listen(int backlog) override;
64 AsyncSocket* Accept(SocketAddress* paddr) override;
65 int Close() override;
66 int GetError() const override;
67 void SetError(int error) override;
68 ConnState GetState() const override;
69 int GetOption(Option opt, int* value) override;
70 int SetOption(Option opt, int value) override;
71
72 protected:
73 virtual void OnConnectEvent(AsyncSocket* socket);
74 virtual void OnReadEvent(AsyncSocket* socket);
75 virtual void OnWriteEvent(AsyncSocket* socket);
76 virtual void OnCloseEvent(AsyncSocket* socket, int err);
77
78 AsyncSocket* socket_;
79};
80
81} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000082
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020083#endif // RTC_BASE_ASYNCSOCKET_H_