- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / base / asyncpacketsocket.h
1 /*
2  * libjingle
3  * Copyright 2004--2005, 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_ASYNCPACKETSOCKET_H_
29 #define TALK_BASE_ASYNCPACKETSOCKET_H_
30
31 #include "talk/base/dscp.h"
32 #include "talk/base/sigslot.h"
33 #include "talk/base/socket.h"
34
35 namespace talk_base {
36
37 // Provides the ability to receive packets asynchronously. Sends are not
38 // buffered since it is acceptable to drop packets under high load.
39 class AsyncPacketSocket : public sigslot::has_slots<> {
40  public:
41   enum State {
42     STATE_CLOSED,
43     STATE_BINDING,
44     STATE_BOUND,
45     STATE_CONNECTING,
46     STATE_CONNECTED
47   };
48
49   AsyncPacketSocket() { }
50   virtual ~AsyncPacketSocket() { }
51
52   // Returns current local address. Address may be set to NULL if the
53   // socket is not bound yet (GetState() returns STATE_BINDING).
54   virtual SocketAddress GetLocalAddress() const = 0;
55
56   // Returns remote address. Returns zeroes if this is not a client TCP socket.
57   virtual SocketAddress GetRemoteAddress() const = 0;
58
59   // Send a packet.
60   virtual int Send(const void *pv, size_t cb, DiffServCodePoint dscp) = 0;
61   virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr,
62                      DiffServCodePoint) = 0;
63
64   // Close the socket.
65   virtual int Close() = 0;
66
67   // Returns current state of the socket.
68   virtual State GetState() const = 0;
69
70   // Get/set options.
71   virtual int GetOption(Socket::Option opt, int* value) = 0;
72   virtual int SetOption(Socket::Option opt, int value) = 0;
73
74   // Get/Set current error.
75   // TODO: Remove SetError().
76   virtual int GetError() const = 0;
77   virtual void SetError(int error) = 0;
78
79   // Emitted each time a packet is read. Used only for UDP and
80   // connected TCP sockets.
81   sigslot::signal4<AsyncPacketSocket*, const char*, size_t,
82                    const SocketAddress&> SignalReadPacket;
83
84   // Emitted when the socket is currently able to send.
85   sigslot::signal1<AsyncPacketSocket*> SignalReadyToSend;
86
87   // Emitted after address for the socket is allocated, i.e. binding
88   // is finished. State of the socket is changed from BINDING to BOUND
89   // (for UDP and server TCP sockets) or CONNECTING (for client TCP
90   // sockets).
91   sigslot::signal2<AsyncPacketSocket*, const SocketAddress&> SignalAddressReady;
92
93   // Emitted for client TCP sockets when state is changed from
94   // CONNECTING to CONNECTED.
95   sigslot::signal1<AsyncPacketSocket*> SignalConnect;
96
97   // Emitted for client TCP sockets when state is changed from
98   // CONNECTED to CLOSED.
99   sigslot::signal2<AsyncPacketSocket*, int> SignalClose;
100
101   // Used only for listening TCP sockets.
102   sigslot::signal2<AsyncPacketSocket*, AsyncPacketSocket*> SignalNewConnection;
103
104  private:
105   DISALLOW_EVIL_CONSTRUCTORS(AsyncPacketSocket);
106 };
107
108 }  // namespace talk_base
109
110 #endif  // TALK_BASE_ASYNCPACKETSOCKET_H_