2854558adc7de967eab99897d96f16b66ad6af78
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / base / asyncsocket.h
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 #ifndef __native_client__
31
32 #include "talk/base/common.h"
33 #include "talk/base/sigslot.h"
34 #include "talk/base/socket.h"
35
36 namespace talk_base {
37
38 // TODO: Remove Socket and rename AsyncSocket to Socket.
39
40 // Provides the ability to perform socket I/O asynchronously.
41 class AsyncSocket : public Socket {
42  public:
43   AsyncSocket();
44   virtual ~AsyncSocket();
45
46   virtual AsyncSocket* Accept(SocketAddress* paddr) = 0;
47
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.
52   // ready to read
53   sigslot::signal1<AsyncSocket*,
54                    sigslot::multi_threaded_local> SignalReadEvent;
55   // ready to write
56   sigslot::signal1<AsyncSocket*,
57                    sigslot::multi_threaded_local> SignalWriteEvent;
58   sigslot::signal1<AsyncSocket*> SignalConnectEvent;     // connected
59   sigslot::signal2<AsyncSocket*, int> SignalCloseEvent;  // closed
60 };
61
62 class AsyncSocketAdapter : public AsyncSocket, public sigslot::has_slots<> {
63  public:
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();
73   }
74   virtual SocketAddress GetRemoteAddress() const {
75     return socket_->GetRemoteAddress();
76   }
77   virtual int Bind(const SocketAddress& addr) {
78     return socket_->Bind(addr);
79   }
80   virtual int Connect(const SocketAddress& addr) {
81     return socket_->Connect(addr);
82   }
83   virtual int Send(const void* pv, size_t cb) {
84     return socket_->Send(pv, cb);
85   }
86   virtual int SendTo(const void* pv, size_t cb, const SocketAddress& addr) {
87     return socket_->SendTo(pv, cb, addr);
88   }
89   virtual int Recv(void* pv, size_t cb) {
90     return socket_->Recv(pv, cb);
91   }
92   virtual int RecvFrom(void* pv, size_t cb, SocketAddress* paddr) {
93     return socket_->RecvFrom(pv, cb, paddr);
94   }
95   virtual int Listen(int backlog) {
96     return socket_->Listen(backlog);
97   }
98   virtual AsyncSocket* Accept(SocketAddress* paddr) {
99     return socket_->Accept(paddr);
100   }
101   virtual int Close() {
102     return socket_->Close();
103   }
104   virtual int GetError() const {
105     return socket_->GetError();
106   }
107   virtual void SetError(int error) {
108     return socket_->SetError(error);
109   }
110   virtual ConnState GetState() const {
111     return socket_->GetState();
112   }
113   virtual int EstimateMTU(uint16* mtu) {
114     return socket_->EstimateMTU(mtu);
115   }
116   virtual int GetOption(Option opt, int* value) {
117     return socket_->GetOption(opt, value);
118   }
119   virtual int SetOption(Option opt, int value) {
120     return socket_->SetOption(opt, value);
121   }
122
123  protected:
124   virtual void OnConnectEvent(AsyncSocket* socket) {
125     SignalConnectEvent(this);
126   }
127   virtual void OnReadEvent(AsyncSocket* socket) {
128     SignalReadEvent(this);
129   }
130   virtual void OnWriteEvent(AsyncSocket* socket) {
131     SignalWriteEvent(this);
132   }
133   virtual void OnCloseEvent(AsyncSocket* socket, int err) {
134     SignalCloseEvent(this, err);
135   }
136
137   AsyncSocket* socket_;
138 };
139
140 }  // namespace talk_base
141
142 #endif  //  __native_client__
143 #endif  // TALK_BASE_ASYNCSOCKET_H_