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_
31 #include "talk/base/common.h"
32 #include "talk/base/sigslot.h"
33 #include "talk/base/socket.h"
37 // TODO: Remove Socket and rename AsyncSocket to Socket.
39 // Provides the ability to perform socket I/O asynchronously.
40 class AsyncSocket : public Socket {
43 virtual ~AsyncSocket();
45 virtual AsyncSocket* Accept(SocketAddress* paddr) = 0;
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.
52 sigslot::signal1<AsyncSocket*,
53 sigslot::multi_threaded_local> SignalReadEvent;
55 sigslot::signal1<AsyncSocket*,
56 sigslot::multi_threaded_local> SignalWriteEvent;
57 sigslot::signal1<AsyncSocket*> SignalConnectEvent; // connected
58 sigslot::signal2<AsyncSocket*, int> SignalCloseEvent; // closed
61 class AsyncSocketAdapter : public AsyncSocket, public sigslot::has_slots<> {
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();
73 virtual SocketAddress GetRemoteAddress() const {
74 return socket_->GetRemoteAddress();
76 virtual int Bind(const SocketAddress& addr) {
77 return socket_->Bind(addr);
79 virtual int Connect(const SocketAddress& addr) {
80 return socket_->Connect(addr);
82 virtual int Send(const void* pv, size_t cb) {
83 return socket_->Send(pv, cb);
85 virtual int SendTo(const void* pv, size_t cb, const SocketAddress& addr) {
86 return socket_->SendTo(pv, cb, addr);
88 virtual int Recv(void* pv, size_t cb) {
89 return socket_->Recv(pv, cb);
91 virtual int RecvFrom(void* pv, size_t cb, SocketAddress* paddr) {
92 return socket_->RecvFrom(pv, cb, paddr);
94 virtual int Listen(int backlog) {
95 return socket_->Listen(backlog);
97 virtual AsyncSocket* Accept(SocketAddress* paddr) {
98 return socket_->Accept(paddr);
100 virtual int Close() {
101 return socket_->Close();
103 virtual int GetError() const {
104 return socket_->GetError();
106 virtual void SetError(int error) {
107 return socket_->SetError(error);
109 virtual ConnState GetState() const {
110 return socket_->GetState();
112 virtual int EstimateMTU(uint16* mtu) {
113 return socket_->EstimateMTU(mtu);
115 virtual int GetOption(Option opt, int* value) {
116 return socket_->GetOption(opt, value);
118 virtual int SetOption(Option opt, int value) {
119 return socket_->SetOption(opt, value);
123 virtual void OnConnectEvent(AsyncSocket* socket) {
124 SignalConnectEvent(this);
126 virtual void OnReadEvent(AsyncSocket* socket) {
127 SignalReadEvent(this);
129 virtual void OnWriteEvent(AsyncSocket* socket) {
130 SignalWriteEvent(this);
132 virtual void OnCloseEvent(AsyncSocket* socket, int err) {
133 SignalCloseEvent(this, err);
136 AsyncSocket* socket_;
139 } // namespace talk_base
141 #endif // TALK_BASE_ASYNCSOCKET_H_