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/socket.h" |
Artem Titov | e41c433 | 2018-07-25 15:04:28 +0200 | [diff] [blame] | 15 | #include "rtc_base/third_party/sigslot/sigslot.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 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 34 | sigslot::signal1<AsyncSocket*, sigslot::multi_threaded_local> SignalReadEvent; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 35 | // ready to write |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 36 | sigslot::signal1<AsyncSocket*, sigslot::multi_threaded_local> |
| 37 | SignalWriteEvent; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 38 | sigslot::signal1<AsyncSocket*> SignalConnectEvent; // connected |
| 39 | sigslot::signal2<AsyncSocket*, int> SignalCloseEvent; // closed |
| 40 | }; |
| 41 | |
| 42 | class 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 81 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 82 | #endif // RTC_BASE_ASYNCSOCKET_H_ |