henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2004--2010, Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #ifndef TALK_BASE_ASYNCSOCKET_H_ |
| 29 | #define TALK_BASE_ASYNCSOCKET_H_ |
| 30 | |
| 31 | #include "talk/base/common.h" |
| 32 | #include "talk/base/sigslot.h" |
| 33 | #include "talk/base/socket.h" |
| 34 | |
| 35 | namespace talk_base { |
| 36 | |
| 37 | // TODO: Remove Socket and rename AsyncSocket to Socket. |
| 38 | |
| 39 | // Provides the ability to perform socket I/O asynchronously. |
| 40 | class AsyncSocket : public Socket { |
| 41 | public: |
| 42 | AsyncSocket(); |
| 43 | virtual ~AsyncSocket(); |
| 44 | |
| 45 | virtual AsyncSocket* Accept(SocketAddress* paddr) = 0; |
| 46 | |
wu@webrtc.org | d371a29 | 2013-10-23 23:56:09 | [diff] [blame] | 47 | // SignalReadEvent and SignalWriteEvent use multi_threaded_local to allow |
| 48 | // access concurrently from different thread. |
| 49 | // For example SignalReadEvent::connect will be called in AsyncUDPSocket ctor |
| 50 | // but at the same time the SocketDispatcher maybe signaling the read event. |
| 51 | // ready to read |
| 52 | sigslot::signal1<AsyncSocket*, |
| 53 | sigslot::multi_threaded_local> SignalReadEvent; |
| 54 | // ready to write |
| 55 | sigslot::signal1<AsyncSocket*, |
| 56 | sigslot::multi_threaded_local> SignalWriteEvent; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 57 | sigslot::signal1<AsyncSocket*> SignalConnectEvent; // connected |
| 58 | sigslot::signal2<AsyncSocket*, int> SignalCloseEvent; // closed |
| 59 | }; |
| 60 | |
| 61 | class AsyncSocketAdapter : public AsyncSocket, public sigslot::has_slots<> { |
| 62 | public: |
| 63 | // The adapted socket may explicitly be NULL, and later assigned using Attach. |
| 64 | // However, subclasses which support detached mode must override any methods |
| 65 | // that will be called during the detached period (usually GetState()), to |
| 66 | // avoid dereferencing a null pointer. |
| 67 | explicit AsyncSocketAdapter(AsyncSocket* socket); |
| 68 | virtual ~AsyncSocketAdapter(); |
| 69 | void Attach(AsyncSocket* socket); |
| 70 | virtual SocketAddress GetLocalAddress() const { |
| 71 | return socket_->GetLocalAddress(); |
| 72 | } |
| 73 | virtual SocketAddress GetRemoteAddress() const { |
| 74 | return socket_->GetRemoteAddress(); |
| 75 | } |
| 76 | virtual int Bind(const SocketAddress& addr) { |
| 77 | return socket_->Bind(addr); |
| 78 | } |
| 79 | virtual int Connect(const SocketAddress& addr) { |
| 80 | return socket_->Connect(addr); |
| 81 | } |
| 82 | virtual int Send(const void* pv, size_t cb) { |
| 83 | return socket_->Send(pv, cb); |
| 84 | } |
| 85 | virtual int SendTo(const void* pv, size_t cb, const SocketAddress& addr) { |
| 86 | return socket_->SendTo(pv, cb, addr); |
| 87 | } |
| 88 | virtual int Recv(void* pv, size_t cb) { |
| 89 | return socket_->Recv(pv, cb); |
| 90 | } |
| 91 | virtual int RecvFrom(void* pv, size_t cb, SocketAddress* paddr) { |
| 92 | return socket_->RecvFrom(pv, cb, paddr); |
| 93 | } |
| 94 | virtual int Listen(int backlog) { |
| 95 | return socket_->Listen(backlog); |
| 96 | } |
| 97 | virtual AsyncSocket* Accept(SocketAddress* paddr) { |
| 98 | return socket_->Accept(paddr); |
| 99 | } |
| 100 | virtual int Close() { |
| 101 | return socket_->Close(); |
| 102 | } |
| 103 | virtual int GetError() const { |
| 104 | return socket_->GetError(); |
| 105 | } |
| 106 | virtual void SetError(int error) { |
| 107 | return socket_->SetError(error); |
| 108 | } |
| 109 | virtual ConnState GetState() const { |
| 110 | return socket_->GetState(); |
| 111 | } |
| 112 | virtual int EstimateMTU(uint16* mtu) { |
| 113 | return socket_->EstimateMTU(mtu); |
| 114 | } |
| 115 | virtual int GetOption(Option opt, int* value) { |
| 116 | return socket_->GetOption(opt, value); |
| 117 | } |
| 118 | virtual int SetOption(Option opt, int value) { |
| 119 | return socket_->SetOption(opt, value); |
| 120 | } |
| 121 | |
| 122 | protected: |
| 123 | virtual void OnConnectEvent(AsyncSocket* socket) { |
| 124 | SignalConnectEvent(this); |
| 125 | } |
| 126 | virtual void OnReadEvent(AsyncSocket* socket) { |
| 127 | SignalReadEvent(this); |
| 128 | } |
| 129 | virtual void OnWriteEvent(AsyncSocket* socket) { |
| 130 | SignalWriteEvent(this); |
| 131 | } |
| 132 | virtual void OnCloseEvent(AsyncSocket* socket, int err) { |
| 133 | SignalCloseEvent(this, err); |
| 134 | } |
| 135 | |
| 136 | AsyncSocket* socket_; |
| 137 | }; |
| 138 | |
| 139 | } // namespace talk_base |
| 140 | |
| 141 | #endif // TALK_BASE_ASYNCSOCKET_H_ |