3 * Copyright 2004--2010, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #ifndef TALK_BASE_ASYNCSOCKET_H_
29 #define TALK_BASE_ASYNCSOCKET_H_
30 #ifndef __native_client__
32 #include "talk/base/common.h"
33 #include "talk/base/sigslot.h"
34 #include "talk/base/socket.h"
38 // TODO: Remove Socket and rename AsyncSocket to Socket.
40 // Provides the ability to perform socket I/O asynchronously.
41 class AsyncSocket : public Socket {
44 virtual ~AsyncSocket();
46 virtual AsyncSocket* Accept(SocketAddress* paddr) = 0;
48 // SignalReadEvent and SignalWriteEvent use multi_threaded_local to allow
49 // access concurrently from different thread.
50 // For example SignalReadEvent::connect will be called in AsyncUDPSocket ctor
51 // but at the same time the SocketDispatcher maybe signaling the read event.
53 sigslot::signal1<AsyncSocket*,
54 sigslot::multi_threaded_local> SignalReadEvent;
56 sigslot::signal1<AsyncSocket*,
57 sigslot::multi_threaded_local> SignalWriteEvent;
58 sigslot::signal1<AsyncSocket*> SignalConnectEvent; // connected
59 sigslot::signal2<AsyncSocket*, int> SignalCloseEvent; // closed
62 class AsyncSocketAdapter : public AsyncSocket, public sigslot::has_slots<> {
64 // The adapted socket may explicitly be NULL, and later assigned using Attach.
65 // However, subclasses which support detached mode must override any methods
66 // that will be called during the detached period (usually GetState()), to
67 // avoid dereferencing a null pointer.
68 explicit AsyncSocketAdapter(AsyncSocket* socket);
69 virtual ~AsyncSocketAdapter();
70 void Attach(AsyncSocket* socket);
71 virtual SocketAddress GetLocalAddress() const {
72 return socket_->GetLocalAddress();
74 virtual SocketAddress GetRemoteAddress() const {
75 return socket_->GetRemoteAddress();
77 virtual int Bind(const SocketAddress& addr) {
78 return socket_->Bind(addr);
80 virtual int Connect(const SocketAddress& addr) {
81 return socket_->Connect(addr);
83 virtual int Send(const void* pv, size_t cb) {
84 return socket_->Send(pv, cb);
86 virtual int SendTo(const void* pv, size_t cb, const SocketAddress& addr) {
87 return socket_->SendTo(pv, cb, addr);
89 virtual int Recv(void* pv, size_t cb) {
90 return socket_->Recv(pv, cb);
92 virtual int RecvFrom(void* pv, size_t cb, SocketAddress* paddr) {
93 return socket_->RecvFrom(pv, cb, paddr);
95 virtual int Listen(int backlog) {
96 return socket_->Listen(backlog);
98 virtual AsyncSocket* Accept(SocketAddress* paddr) {
99 return socket_->Accept(paddr);
101 virtual int Close() {
102 return socket_->Close();
104 virtual int GetError() const {
105 return socket_->GetError();
107 virtual void SetError(int error) {
108 return socket_->SetError(error);
110 virtual ConnState GetState() const {
111 return socket_->GetState();
113 virtual int EstimateMTU(uint16* mtu) {
114 return socket_->EstimateMTU(mtu);
116 virtual int GetOption(Option opt, int* value) {
117 return socket_->GetOption(opt, value);
119 virtual int SetOption(Option opt, int value) {
120 return socket_->SetOption(opt, value);
124 virtual void OnConnectEvent(AsyncSocket* socket) {
125 SignalConnectEvent(this);
127 virtual void OnReadEvent(AsyncSocket* socket) {
128 SignalReadEvent(this);
130 virtual void OnWriteEvent(AsyncSocket* socket) {
131 SignalWriteEvent(this);
133 virtual void OnCloseEvent(AsyncSocket* socket, int err) {
134 SignalCloseEvent(this, err);
137 AsyncSocket* socket_;
140 } // namespace talk_base
142 #endif // __native_client__
143 #endif // TALK_BASE_ASYNCSOCKET_H_