1 /*****************************************************************
3 | Neptune - Network Sockets
5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC.
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.
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.
30 ****************************************************************/
32 #ifndef _NPT_SOCKETS_H_
33 #define _NPT_SOCKETS_H_
35 /*----------------------------------------------------------------------
37 +---------------------------------------------------------------------*/
39 #include "NptConstants.h"
40 #include "NptStreams.h"
41 #include "NptStrings.h"
42 #include "NptDataBuffer.h"
43 #include "NptNetwork.h"
44 #include "NptThreads.h"
46 /*----------------------------------------------------------------------
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;
67 const unsigned int NPT_SOCKET_FLAG_CANCELLABLE
= 1; // make the socket cancellable
69 /*----------------------------------------------------------------------
71 +---------------------------------------------------------------------*/
74 /*----------------------------------------------------------------------
76 +---------------------------------------------------------------------*/
77 class NPT_SocketAddress
80 // constructors and destructor
81 NPT_SocketAddress() : m_Port(0) {}
82 NPT_SocketAddress(const NPT_IpAddress
& address
, NPT_IpPort port
) :
87 NPT_Result
SetIpAddress(const NPT_IpAddress
& address
) {
88 m_IpAddress
= address
;
91 const NPT_IpAddress
& GetIpAddress() const {
94 NPT_Result
SetPort(NPT_IpPort port
) {
98 NPT_IpPort
GetPort() const {
101 NPT_String
ToString() const;
104 bool operator==(const NPT_SocketAddress
& other
) const;
108 NPT_IpAddress m_IpAddress
;
112 /*----------------------------------------------------------------------
114 +---------------------------------------------------------------------*/
116 NPT_SocketAddress local_address
;
117 NPT_SocketAddress remote_address
;
120 /*----------------------------------------------------------------------
121 | NPT_SocketInterface
122 +---------------------------------------------------------------------*/
123 class NPT_SocketInterface
126 virtual ~NPT_SocketInterface() {}
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;
140 /*----------------------------------------------------------------------
141 | NPT_UdpSocketInterface
142 +---------------------------------------------------------------------*/
143 class NPT_UdpSocketInterface
146 virtual ~NPT_UdpSocketInterface() {}
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;
155 /*----------------------------------------------------------------------
156 | NPT_UdpMulticastSocketInterface
157 +---------------------------------------------------------------------*/
158 class NPT_UdpMulticastSocketInterface
161 virtual ~NPT_UdpMulticastSocketInterface() {}
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;
172 /*----------------------------------------------------------------------
173 | NPT_TcpServerSocketInterface
174 +---------------------------------------------------------------------*/
175 class NPT_TcpServerSocketInterface
178 virtual ~NPT_TcpServerSocketInterface() {}
181 virtual NPT_Result
Listen(unsigned int max_clients
) = 0;
182 virtual NPT_Result
WaitForNewClient(NPT_Socket
*& client
,
184 NPT_Flags flags
) = 0;
187 /*----------------------------------------------------------------------
189 +---------------------------------------------------------------------*/
190 class NPT_Socket
: public NPT_SocketInterface
194 static NPT_Result
CancelBlockerSocket(NPT_Thread::ThreadId thread_id
);
196 // constructor and destructor
197 explicit NPT_Socket(NPT_SocketInterface
* delegate
) : m_SocketDelegate(delegate
) {}
198 ~NPT_Socket() override
;
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
);
204 NPT_Result
Connect(const NPT_SocketAddress
& address
,
205 NPT_Timeout timeout
= NPT_TIMEOUT_INFINITE
) override
{
206 return m_SocketDelegate
->Connect(address
, timeout
);
208 NPT_Result
WaitForConnection(NPT_Timeout timeout
= NPT_TIMEOUT_INFINITE
) override
{
209 return m_SocketDelegate
->WaitForConnection(timeout
);
211 NPT_Result
GetInputStream(NPT_InputStreamReference
& stream
) override
{
212 return m_SocketDelegate
->GetInputStream(stream
);
214 NPT_Result
GetOutputStream(NPT_OutputStreamReference
& stream
) override
{
215 return m_SocketDelegate
->GetOutputStream(stream
);
217 NPT_Result
GetInfo(NPT_SocketInfo
& info
) override
{
218 return m_SocketDelegate
->GetInfo(info
);
220 NPT_Result
SetReadTimeout(NPT_Timeout timeout
) override
{
221 return m_SocketDelegate
->SetReadTimeout(timeout
);
223 NPT_Result
SetWriteTimeout(NPT_Timeout timeout
) override
{
224 return m_SocketDelegate
->SetWriteTimeout(timeout
);
226 NPT_Result
Cancel(bool shutdown
=true) override
{
227 return m_SocketDelegate
->Cancel(shutdown
);
232 NPT_Socket() : m_SocketDelegate(NULL
) {}
235 NPT_SocketInterface
* m_SocketDelegate
;
238 typedef NPT_Reference
<NPT_Socket
> NPT_SocketReference
;
240 /*----------------------------------------------------------------------
242 +---------------------------------------------------------------------*/
243 class NPT_UdpSocket
: public NPT_Socket
,
244 public NPT_UdpSocketInterface
247 // constructor and destructor
248 NPT_UdpSocket(NPT_Flags flags
=0);
249 ~NPT_UdpSocket() override
;
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
);
256 NPT_Result
Receive(NPT_DataBuffer
& packet
,
257 NPT_SocketAddress
* address
= NULL
) override
{
258 return m_UdpSocketDelegate
->Receive(packet
, address
);
263 NPT_UdpSocket(NPT_UdpSocketInterface
* delegate
);
266 NPT_UdpSocketInterface
* m_UdpSocketDelegate
;
269 /*----------------------------------------------------------------------
270 | NPT_UdpMulticastSocket
271 +---------------------------------------------------------------------*/
272 class NPT_UdpMulticastSocket
: public NPT_UdpSocket
,
273 public NPT_UdpMulticastSocketInterface
276 // constructor and destructor
277 NPT_UdpMulticastSocket(NPT_Flags flags
=0);
278 ~NPT_UdpMulticastSocket() override
;
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
);
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
);
291 NPT_Result
SetTimeToLive(unsigned char ttl
) override
{
292 return m_UdpMulticastSocketDelegate
->SetTimeToLive(ttl
);
294 NPT_Result
SetInterface(const NPT_IpAddress
& iface
) override
{
295 return m_UdpMulticastSocketDelegate
->SetInterface(iface
);
300 NPT_UdpMulticastSocketInterface
* m_UdpMulticastSocketDelegate
;
303 /*----------------------------------------------------------------------
304 | NPT_TcpClientSocket
305 +---------------------------------------------------------------------*/
306 class NPT_TcpClientSocket
: public NPT_Socket
309 // constructors and destructor
310 NPT_TcpClientSocket(NPT_Flags flags
=0);
311 ~NPT_TcpClientSocket() override
;
314 /*----------------------------------------------------------------------
315 | NPT_TcpServerSocket
316 +---------------------------------------------------------------------*/
317 class NPT_TcpServerSocket
: public NPT_Socket
,
318 public NPT_TcpServerSocketInterface
321 // constructors and destructor
322 NPT_TcpServerSocket(NPT_Flags flags
=0);
323 ~NPT_TcpServerSocket() override
;
325 // delegate NPT_TcpServerSocketInterface methods
326 NPT_Result
Listen(unsigned int max_clients
) override
{
327 return m_TcpServerSocketDelegate
->Listen(max_clients
);
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
);
337 NPT_TcpServerSocketInterface
* m_TcpServerSocketDelegate
;
340 #endif // _NPT_SOCKETS_H_