henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_ASYNCSOCKET_H_ |
| 12 | #define RTC_BASE_ASYNCSOCKET_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "rtc_base/sigslot.h" |
| 15 | #include "rtc_base/socket.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 16 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 17 | namespace rtc { |
| 18 | |
| 19 | // TODO: Remove Socket and rename AsyncSocket to Socket. |
| 20 | |
| 21 | // Provides the ability to perform socket I/O asynchronously. |
| 22 | class 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 | |
| 43 | class 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 82 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 83 | #endif // RTC_BASE_ASYNCSOCKET_H_ |