xbmc
NptSockets.h
1 /*****************************************************************
2 |
3 | Neptune - Network Sockets
4 |
5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC.
6 | All rights reserved.
7 |
8 | Redistribution and use in source and binary forms, with or without
9 | modification, are permitted provided that the following conditions are met:
10 | * Redistributions of source code must retain the above copyright
11 | notice, this list of conditions and the following disclaimer.
12 | * Redistributions in binary form must reproduce the above copyright
13 | notice, this list of conditions and the following disclaimer in the
14 | documentation and/or other materials provided with the distribution.
15 | * Neither the name of Axiomatic Systems nor the
16 | names of its contributors may be used to endorse or promote products
17 | derived from this software without specific prior written permission.
18 |
19 | THIS SOFTWARE IS PROVIDED BY AXIOMATIC SYSTEMS ''AS IS'' AND ANY
20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | DISCLAIMED. IN NO EVENT SHALL AXIOMATIC SYSTEMS BE LIABLE FOR ANY
23 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 |
30  ****************************************************************/
31 
32 #ifndef _NPT_SOCKETS_H_
33 #define _NPT_SOCKETS_H_
34 
35 /*----------------------------------------------------------------------
36 | includes
37 +---------------------------------------------------------------------*/
38 #include "NptTypes.h"
39 #include "NptConstants.h"
40 #include "NptStreams.h"
41 #include "NptStrings.h"
42 #include "NptDataBuffer.h"
43 #include "NptNetwork.h"
44 #include "NptThreads.h"
45 
46 /*----------------------------------------------------------------------
47 | constants
48 +---------------------------------------------------------------------*/
49 const int NPT_ERROR_CONNECTION_RESET = NPT_ERROR_BASE_SOCKET - 0;
50 const int NPT_ERROR_CONNECTION_ABORTED = NPT_ERROR_BASE_SOCKET - 1;
51 const int NPT_ERROR_CONNECTION_REFUSED = NPT_ERROR_BASE_SOCKET - 2;
52 const int NPT_ERROR_CONNECTION_FAILED = NPT_ERROR_BASE_SOCKET - 3;
53 const int NPT_ERROR_HOST_UNKNOWN = NPT_ERROR_BASE_SOCKET - 4;
54 const int NPT_ERROR_SOCKET_FAILED = NPT_ERROR_BASE_SOCKET - 5;
55 const int NPT_ERROR_GETSOCKOPT_FAILED = NPT_ERROR_BASE_SOCKET - 6;
56 const int NPT_ERROR_SETSOCKOPT_FAILED = NPT_ERROR_BASE_SOCKET - 7;
57 const int NPT_ERROR_SOCKET_CONTROL_FAILED = NPT_ERROR_BASE_SOCKET - 8;
58 const int NPT_ERROR_BIND_FAILED = NPT_ERROR_BASE_SOCKET - 9;
59 const int NPT_ERROR_LISTEN_FAILED = NPT_ERROR_BASE_SOCKET - 10;
60 const int NPT_ERROR_ACCEPT_FAILED = NPT_ERROR_BASE_SOCKET - 11;
61 const int NPT_ERROR_ADDRESS_IN_USE = NPT_ERROR_BASE_SOCKET - 12;
62 const int NPT_ERROR_NETWORK_DOWN = NPT_ERROR_BASE_SOCKET - 13;
63 const int NPT_ERROR_NETWORK_UNREACHABLE = NPT_ERROR_BASE_SOCKET - 14;
64 const int NPT_ERROR_HOST_UNREACHABLE = NPT_ERROR_BASE_SOCKET - 15;
65 const int NPT_ERROR_NOT_CONNECTED = NPT_ERROR_BASE_SOCKET - 16;
66 
67 const unsigned int NPT_SOCKET_FLAG_CANCELLABLE = 1; // make the socket cancellable
68 
69 /*----------------------------------------------------------------------
70 | forward references
71 +---------------------------------------------------------------------*/
72 class NPT_Socket;
73 
74 /*----------------------------------------------------------------------
75 | NPT_SocketAddress
76 +---------------------------------------------------------------------*/
78 {
79 public:
80  // constructors and destructor
81  NPT_SocketAddress() : m_Port(0) {}
82  NPT_SocketAddress(const NPT_IpAddress& address, NPT_IpPort port) :
83  m_IpAddress(address),
84  m_Port(port) {}
85 
86  // methods
87  NPT_Result SetIpAddress(const NPT_IpAddress& address) {
88  m_IpAddress = address;
89  return NPT_SUCCESS;
90  }
91  const NPT_IpAddress& GetIpAddress() const {
92  return m_IpAddress;
93  }
94  NPT_Result SetPort(NPT_IpPort port) {
95  m_Port = port;
96  return NPT_SUCCESS;
97  }
98  NPT_IpPort GetPort() const {
99  return m_Port;
100  }
101  NPT_String ToString() const;
102 
103  // operators
104  bool operator==(const NPT_SocketAddress& other) const;
105 
106 private:
107  // members
108  NPT_IpAddress m_IpAddress;
109  NPT_IpPort m_Port;
110 };
111 
112 /*----------------------------------------------------------------------
113 | NPT_SocketInfo
114 +---------------------------------------------------------------------*/
115 typedef struct {
116  NPT_SocketAddress local_address;
117  NPT_SocketAddress remote_address;
119 
120 /*----------------------------------------------------------------------
121 | NPT_SocketInterface
122 +---------------------------------------------------------------------*/
124 {
125  public:
126  virtual ~NPT_SocketInterface() {}
127 
128  // interface methods
129  virtual NPT_Result Bind(const NPT_SocketAddress& address, bool reuse_address = true) = 0;
130  virtual NPT_Result Connect(const NPT_SocketAddress& address, NPT_Timeout timeout) = 0;
131  virtual NPT_Result WaitForConnection(NPT_Timeout timeout) = 0;
132  virtual NPT_Result GetInputStream(NPT_InputStreamReference& stream) = 0;
133  virtual NPT_Result GetOutputStream(NPT_OutputStreamReference& stream) = 0;
134  virtual NPT_Result GetInfo(NPT_SocketInfo& info) = 0;
135  virtual NPT_Result SetReadTimeout(NPT_Timeout timeout) = 0;
136  virtual NPT_Result SetWriteTimeout(NPT_Timeout timeout) = 0;
137  virtual NPT_Result Cancel(bool shutdown=true) = 0;
138 };
139 
140 /*----------------------------------------------------------------------
141 | NPT_UdpSocketInterface
142 +---------------------------------------------------------------------*/
144 {
145  public:
146  virtual ~NPT_UdpSocketInterface() {}
147 
148  // methods
149  virtual NPT_Result Send(const NPT_DataBuffer& packet,
150  const NPT_SocketAddress* address = NULL) = 0;
151  virtual NPT_Result Receive(NPT_DataBuffer& packet,
152  NPT_SocketAddress* address = NULL) = 0;
153 };
154 
155 /*----------------------------------------------------------------------
156 | NPT_UdpMulticastSocketInterface
157 +---------------------------------------------------------------------*/
159 {
160  public:
161  virtual ~NPT_UdpMulticastSocketInterface() {}
162 
163  // methods
164  virtual NPT_Result JoinGroup(const NPT_IpAddress& group,
165  const NPT_IpAddress& iface) = 0;
166  virtual NPT_Result LeaveGroup(const NPT_IpAddress& group,
167  const NPT_IpAddress& iface) = 0;
168  virtual NPT_Result SetTimeToLive(unsigned char ttl) = 0;
169  virtual NPT_Result SetInterface(const NPT_IpAddress& iface) = 0;
170 };
171 
172 /*----------------------------------------------------------------------
173 | NPT_TcpServerSocketInterface
174 +---------------------------------------------------------------------*/
176 {
177  public:
178  virtual ~NPT_TcpServerSocketInterface() {}
179 
180  // interface methods
181  virtual NPT_Result Listen(unsigned int max_clients) = 0;
182  virtual NPT_Result WaitForNewClient(NPT_Socket*& client,
183  NPT_Timeout timeout,
184  NPT_Flags flags) = 0;
185 };
186 
187 /*----------------------------------------------------------------------
188 | NPT_Socket
189 +---------------------------------------------------------------------*/
191 {
192 public:
193  // static methods
194  static NPT_Result CancelBlockerSocket(NPT_Thread::ThreadId thread_id);
195 
196  // constructor and destructor
197  explicit NPT_Socket(NPT_SocketInterface* delegate) : m_SocketDelegate(delegate) {}
198  ~NPT_Socket() override;
199 
200  // delegate NPT_SocketInterface methods
201  NPT_Result Bind(const NPT_SocketAddress& address, bool reuse_address = true) override {
202  return m_SocketDelegate->Bind(address, reuse_address);
203  }
204  NPT_Result Connect(const NPT_SocketAddress& address,
205  NPT_Timeout timeout = NPT_TIMEOUT_INFINITE) override {
206  return m_SocketDelegate->Connect(address, timeout);
207  }
208  NPT_Result WaitForConnection(NPT_Timeout timeout = NPT_TIMEOUT_INFINITE) override {
209  return m_SocketDelegate->WaitForConnection(timeout);
210  }
211  NPT_Result GetInputStream(NPT_InputStreamReference& stream) override {
212  return m_SocketDelegate->GetInputStream(stream);
213  }
214  NPT_Result GetOutputStream(NPT_OutputStreamReference& stream) override {
215  return m_SocketDelegate->GetOutputStream(stream);
216  }
217  NPT_Result GetInfo(NPT_SocketInfo& info) override {
218  return m_SocketDelegate->GetInfo(info);
219  }
220  NPT_Result SetReadTimeout(NPT_Timeout timeout) override {
221  return m_SocketDelegate->SetReadTimeout(timeout);
222  }
223  NPT_Result SetWriteTimeout(NPT_Timeout timeout) override {
224  return m_SocketDelegate->SetWriteTimeout(timeout);
225  }
226  NPT_Result Cancel(bool shutdown=true) override {
227  return m_SocketDelegate->Cancel(shutdown);
228  }
229 
230 protected:
231  // constructor
232  NPT_Socket() : m_SocketDelegate(NULL) {}
233 
234  // members
235  NPT_SocketInterface* m_SocketDelegate;
236 };
237 
239 
240 /*----------------------------------------------------------------------
241 | NPT_UdpSocket
242 +---------------------------------------------------------------------*/
243 class NPT_UdpSocket : public NPT_Socket,
245 {
246  public:
247  // constructor and destructor
248  NPT_UdpSocket(NPT_Flags flags=0);
249  ~NPT_UdpSocket() override;
250 
251  // delegate NPT_UdpSocketInterface methods
252  NPT_Result Send(const NPT_DataBuffer& packet,
253  const NPT_SocketAddress* address = NULL) override {
254  return m_UdpSocketDelegate->Send(packet, address);
255  }
256  NPT_Result Receive(NPT_DataBuffer& packet,
257  NPT_SocketAddress* address = NULL) override {
258  return m_UdpSocketDelegate->Receive(packet, address);
259  }
260 
261 protected:
262  // constructor
264 
265  // members
266  NPT_UdpSocketInterface* m_UdpSocketDelegate;
267 };
268 
269 /*----------------------------------------------------------------------
270 | NPT_UdpMulticastSocket
271 +---------------------------------------------------------------------*/
274 {
275 public:
276  // constructor and destructor
277  NPT_UdpMulticastSocket(NPT_Flags flags=0);
278  ~NPT_UdpMulticastSocket() override;
279 
280  // delegate NPT_UdpMulticastSocketInterface methods
281  NPT_Result JoinGroup(const NPT_IpAddress& group,
282  const NPT_IpAddress& iface =
283  NPT_IpAddress::Any) override {
284  return m_UdpMulticastSocketDelegate->JoinGroup(group, iface);
285  }
286  NPT_Result LeaveGroup(const NPT_IpAddress& group,
287  const NPT_IpAddress& iface =
288  NPT_IpAddress::Any) override {
289  return m_UdpMulticastSocketDelegate->LeaveGroup(group, iface);
290  }
291  NPT_Result SetTimeToLive(unsigned char ttl) override {
292  return m_UdpMulticastSocketDelegate->SetTimeToLive(ttl);
293  }
294  NPT_Result SetInterface(const NPT_IpAddress& iface) override {
295  return m_UdpMulticastSocketDelegate->SetInterface(iface);
296  }
297 
298 protected:
299  // members
300  NPT_UdpMulticastSocketInterface* m_UdpMulticastSocketDelegate;
301 };
302 
303 /*----------------------------------------------------------------------
304 | NPT_TcpClientSocket
305 +---------------------------------------------------------------------*/
307 {
308 public:
309  // constructors and destructor
310  NPT_TcpClientSocket(NPT_Flags flags=0);
311  ~NPT_TcpClientSocket() override;
312 };
313 
314 /*----------------------------------------------------------------------
315 | NPT_TcpServerSocket
316 +---------------------------------------------------------------------*/
319 {
320 public:
321  // constructors and destructor
322  NPT_TcpServerSocket(NPT_Flags flags=0);
323  ~NPT_TcpServerSocket() override;
324 
325  // delegate NPT_TcpServerSocketInterface methods
326  NPT_Result Listen(unsigned int max_clients) override {
327  return m_TcpServerSocketDelegate->Listen(max_clients);
328  }
329  NPT_Result WaitForNewClient(NPT_Socket*& client,
330  NPT_Timeout timeout = NPT_TIMEOUT_INFINITE,
331  NPT_Flags flags = 0) override {
332  return m_TcpServerSocketDelegate->WaitForNewClient(client, timeout, flags);
333  }
334 
335 protected:
336  // members
337  NPT_TcpServerSocketInterface* m_TcpServerSocketDelegate;
338 };
339 
340 #endif // _NPT_SOCKETS_H_
Definition: NptSockets.h:190
Definition: NptSockets.h:317
Definition: NptNetwork.h:74
Definition: NptSockets.h:272
Definition: NptSockets.h:77
Definition: NptSockets.h:143
Definition: NptSockets.h:158
Definition: NptSockets.h:123
Definition: NptSockets.h:115
Definition: NptSockets.h:175
Definition: SmartPlayList.cpp:137
Definition: NptDataBuffer.h:44
Definition: NptSockets.h:306
Definition: NptSockets.h:243
Definition: NptStrings.h:57