Mercurial > projects > hoofbaby
comparison deps/Platinum/ThirdParty/Neptune/Source/Core/NptSockets.h @ 0:3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
author | fraserofthenight |
---|---|
date | Mon, 06 Jul 2009 08:06:28 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3425707ddbf6 |
---|---|
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 | |
45 /*---------------------------------------------------------------------- | |
46 | constants | |
47 +---------------------------------------------------------------------*/ | |
48 const int NPT_ERROR_CONNECTION_RESET = NPT_ERROR_BASE_SOCKET - 0; | |
49 const int NPT_ERROR_CONNECTION_ABORTED = NPT_ERROR_BASE_SOCKET - 1; | |
50 const int NPT_ERROR_CONNECTION_REFUSED = NPT_ERROR_BASE_SOCKET - 2; | |
51 const int NPT_ERROR_CONNECTION_FAILED = NPT_ERROR_BASE_SOCKET - 3; | |
52 const int NPT_ERROR_HOST_UNKNOWN = NPT_ERROR_BASE_SOCKET - 4; | |
53 const int NPT_ERROR_SOCKET_FAILED = NPT_ERROR_BASE_SOCKET - 5; | |
54 const int NPT_ERROR_GETSOCKOPT_FAILED = NPT_ERROR_BASE_SOCKET - 6; | |
55 const int NPT_ERROR_SETSOCKOPT_FAILED = NPT_ERROR_BASE_SOCKET - 7; | |
56 const int NPT_ERROR_SOCKET_CONTROL_FAILED = NPT_ERROR_BASE_SOCKET - 8; | |
57 const int NPT_ERROR_BIND_FAILED = NPT_ERROR_BASE_SOCKET - 9; | |
58 const int NPT_ERROR_LISTEN_FAILED = NPT_ERROR_BASE_SOCKET - 10; | |
59 const int NPT_ERROR_ACCEPT_FAILED = NPT_ERROR_BASE_SOCKET - 11; | |
60 const int NPT_ERROR_ADDRESS_IN_USE = NPT_ERROR_BASE_SOCKET - 12; | |
61 const int NPT_ERROR_NETWORK_DOWN = NPT_ERROR_BASE_SOCKET - 13; | |
62 const int NPT_ERROR_NETWORK_UNREACHABLE = NPT_ERROR_BASE_SOCKET - 14; | |
63 | |
64 /*---------------------------------------------------------------------- | |
65 | forward references | |
66 +---------------------------------------------------------------------*/ | |
67 class NPT_Socket; | |
68 | |
69 /*---------------------------------------------------------------------- | |
70 | NPT_SocketAddress | |
71 +---------------------------------------------------------------------*/ | |
72 class NPT_SocketAddress | |
73 { | |
74 public: | |
75 // constructors and destructor | |
76 NPT_SocketAddress() : m_Port(0) {} | |
77 NPT_SocketAddress(const NPT_IpAddress& address, NPT_IpPort port) : | |
78 m_IpAddress(address), | |
79 m_Port(port) {} | |
80 | |
81 // methods | |
82 NPT_Result SetIpAddress(const NPT_IpAddress& address) { | |
83 m_IpAddress = address; | |
84 return NPT_SUCCESS; | |
85 } | |
86 const NPT_IpAddress& GetIpAddress() const { | |
87 return m_IpAddress; | |
88 } | |
89 NPT_Result SetPort(NPT_IpPort port) { | |
90 m_Port = port; | |
91 return NPT_SUCCESS; | |
92 } | |
93 NPT_IpPort GetPort() const { | |
94 return m_Port; | |
95 } | |
96 NPT_String ToString() const; | |
97 | |
98 // operators | |
99 bool operator==(const NPT_SocketAddress& other) const; | |
100 | |
101 private: | |
102 // members | |
103 NPT_IpAddress m_IpAddress; | |
104 NPT_IpPort m_Port; | |
105 }; | |
106 | |
107 /*---------------------------------------------------------------------- | |
108 | NPT_SocketInfo | |
109 +---------------------------------------------------------------------*/ | |
110 typedef struct { | |
111 NPT_SocketAddress local_address; | |
112 NPT_SocketAddress remote_address; | |
113 } NPT_SocketInfo; | |
114 | |
115 /*---------------------------------------------------------------------- | |
116 | NPT_SocketInterface | |
117 +---------------------------------------------------------------------*/ | |
118 class NPT_SocketInterface | |
119 { | |
120 public: | |
121 virtual ~NPT_SocketInterface() {} | |
122 | |
123 // interface methods | |
124 virtual NPT_Result Bind(const NPT_SocketAddress& address, bool reuse_address = true) = 0; | |
125 virtual NPT_Result Connect(const NPT_SocketAddress& address, NPT_Timeout timeout) = 0; | |
126 virtual NPT_Result Disconnect() = 0; | |
127 virtual NPT_Result WaitForConnection(NPT_Timeout timeout) = 0; | |
128 virtual NPT_Result GetInputStream(NPT_InputStreamReference& stream) = 0; | |
129 virtual NPT_Result GetOutputStream(NPT_OutputStreamReference& stream) = 0; | |
130 virtual NPT_Result GetInfo(NPT_SocketInfo& info) = 0; | |
131 virtual NPT_Result SetBlockingMode(bool blocking) = 0; | |
132 virtual NPT_Result SetReadTimeout(NPT_Timeout timeout) = 0; | |
133 virtual NPT_Result SetWriteTimeout(NPT_Timeout timeout) = 0; | |
134 }; | |
135 | |
136 /*---------------------------------------------------------------------- | |
137 | NPT_UdpSocketInterface | |
138 +---------------------------------------------------------------------*/ | |
139 class NPT_UdpSocketInterface | |
140 { | |
141 public: | |
142 virtual ~NPT_UdpSocketInterface() {} | |
143 | |
144 // methods | |
145 virtual NPT_Result Send(const NPT_DataBuffer& packet, | |
146 const NPT_SocketAddress* address = NULL) = 0; | |
147 virtual NPT_Result Receive(NPT_DataBuffer& packet, | |
148 NPT_SocketAddress* address = NULL) = 0; | |
149 }; | |
150 | |
151 /*---------------------------------------------------------------------- | |
152 | NPT_UdpMulticastSocketInterface | |
153 +---------------------------------------------------------------------*/ | |
154 class NPT_UdpMulticastSocketInterface | |
155 { | |
156 public: | |
157 virtual ~NPT_UdpMulticastSocketInterface() {} | |
158 | |
159 // methods | |
160 virtual NPT_Result JoinGroup(const NPT_IpAddress& group, | |
161 const NPT_IpAddress& iface) = 0; | |
162 virtual NPT_Result LeaveGroup(const NPT_IpAddress& group, | |
163 const NPT_IpAddress& iface) = 0; | |
164 virtual NPT_Result SetTimeToLive(unsigned char ttl) = 0; | |
165 virtual NPT_Result SetInterface(const NPT_IpAddress& iface) = 0; | |
166 }; | |
167 | |
168 /*---------------------------------------------------------------------- | |
169 | NPT_TcpServerSocketInterface | |
170 +---------------------------------------------------------------------*/ | |
171 class NPT_TcpServerSocketInterface | |
172 { | |
173 public: | |
174 virtual ~NPT_TcpServerSocketInterface() {} | |
175 | |
176 // interface methods | |
177 virtual NPT_Result Listen(unsigned int max_clients) = 0; | |
178 virtual NPT_Result WaitForNewClient(NPT_Socket*& client, | |
179 NPT_Timeout timeout) = 0; | |
180 }; | |
181 | |
182 /*---------------------------------------------------------------------- | |
183 | NPT_Socket | |
184 +---------------------------------------------------------------------*/ | |
185 class NPT_Socket : public NPT_SocketInterface | |
186 { | |
187 public: | |
188 // constructor and destructor | |
189 NPT_Socket(NPT_SocketInterface* delegate) : | |
190 m_SocketDelegate(delegate) {} | |
191 virtual ~NPT_Socket(); | |
192 | |
193 // delegate NPT_SocketInterface methods | |
194 NPT_Result Bind(const NPT_SocketAddress& address, bool reuse_address = true) { | |
195 return m_SocketDelegate->Bind(address, reuse_address); | |
196 } | |
197 NPT_Result Connect(const NPT_SocketAddress& address, | |
198 NPT_Timeout timeout = NPT_TIMEOUT_INFINITE) { | |
199 return m_SocketDelegate->Connect(address, timeout); | |
200 } | |
201 NPT_Result Disconnect() { | |
202 return m_SocketDelegate->Disconnect(); | |
203 } | |
204 NPT_Result WaitForConnection(NPT_Timeout timeout = NPT_TIMEOUT_INFINITE) { | |
205 return m_SocketDelegate->WaitForConnection(timeout); | |
206 } | |
207 NPT_Result GetInputStream(NPT_InputStreamReference& stream) { | |
208 return m_SocketDelegate->GetInputStream(stream); | |
209 } | |
210 NPT_Result GetOutputStream(NPT_OutputStreamReference& stream) { | |
211 return m_SocketDelegate->GetOutputStream(stream); | |
212 } | |
213 NPT_Result GetInfo(NPT_SocketInfo& info) { | |
214 return m_SocketDelegate->GetInfo(info); | |
215 } | |
216 NPT_Result SetBlockingMode(bool blocking) { | |
217 return m_SocketDelegate->SetBlockingMode(blocking); | |
218 } | |
219 NPT_Result SetReadTimeout(NPT_Timeout timeout) { | |
220 return m_SocketDelegate->SetReadTimeout(timeout); | |
221 } | |
222 NPT_Result SetWriteTimeout(NPT_Timeout timeout) { | |
223 return m_SocketDelegate->SetWriteTimeout(timeout); | |
224 } | |
225 | |
226 protected: | |
227 // constructor | |
228 NPT_Socket() {} | |
229 | |
230 // members | |
231 NPT_SocketInterface* m_SocketDelegate; | |
232 }; | |
233 | |
234 /*---------------------------------------------------------------------- | |
235 | NPT_UdpSocket | |
236 +---------------------------------------------------------------------*/ | |
237 class NPT_UdpSocket : public NPT_Socket, | |
238 public NPT_UdpSocketInterface | |
239 { | |
240 public: | |
241 // constructor and destructor | |
242 NPT_UdpSocket(); | |
243 virtual ~NPT_UdpSocket(); | |
244 | |
245 // delegate NPT_UdpSocketInterface methods | |
246 NPT_Result Send(const NPT_DataBuffer& packet, | |
247 const NPT_SocketAddress* address = NULL) { | |
248 return m_UdpSocketDelegate->Send(packet, address); | |
249 } | |
250 NPT_Result Receive(NPT_DataBuffer& packet, | |
251 NPT_SocketAddress* address = NULL) { | |
252 return m_UdpSocketDelegate->Receive(packet, address); | |
253 } | |
254 | |
255 protected: | |
256 // constructor | |
257 NPT_UdpSocket(NPT_UdpSocketInterface* delegate); | |
258 | |
259 // members | |
260 NPT_UdpSocketInterface* m_UdpSocketDelegate; | |
261 }; | |
262 | |
263 /*---------------------------------------------------------------------- | |
264 | NPT_UdpMulticastSocket | |
265 +---------------------------------------------------------------------*/ | |
266 class NPT_UdpMulticastSocket : public NPT_UdpSocket, | |
267 public NPT_UdpMulticastSocketInterface | |
268 { | |
269 public: | |
270 // constructor and destructor | |
271 NPT_UdpMulticastSocket(); | |
272 virtual ~NPT_UdpMulticastSocket(); | |
273 | |
274 // delegate NPT_UdpMulticastSocketInterface methods | |
275 NPT_Result JoinGroup(const NPT_IpAddress& group, | |
276 const NPT_IpAddress& iface = | |
277 NPT_IpAddress::Any) { | |
278 return m_UdpMulticastSocketDelegate->JoinGroup(group, iface); | |
279 } | |
280 NPT_Result LeaveGroup(const NPT_IpAddress& group, | |
281 const NPT_IpAddress& iface = | |
282 NPT_IpAddress::Any) { | |
283 return m_UdpMulticastSocketDelegate->LeaveGroup(group, iface); | |
284 } | |
285 NPT_Result SetTimeToLive(unsigned char ttl) { | |
286 return m_UdpMulticastSocketDelegate->SetTimeToLive(ttl); | |
287 } | |
288 NPT_Result SetInterface(const NPT_IpAddress& iface) { | |
289 return m_UdpMulticastSocketDelegate->SetInterface(iface); | |
290 } | |
291 | |
292 protected: | |
293 // members | |
294 NPT_UdpMulticastSocketInterface* m_UdpMulticastSocketDelegate; | |
295 }; | |
296 | |
297 /*---------------------------------------------------------------------- | |
298 | NPT_TcpClientSocket | |
299 +---------------------------------------------------------------------*/ | |
300 class NPT_TcpClientSocket : public NPT_Socket | |
301 { | |
302 public: | |
303 // constructors and destructor | |
304 NPT_TcpClientSocket(); | |
305 virtual ~NPT_TcpClientSocket(); | |
306 }; | |
307 | |
308 /*---------------------------------------------------------------------- | |
309 | NPT_TcpServerSocket | |
310 +---------------------------------------------------------------------*/ | |
311 class NPT_TcpServerSocket : public NPT_Socket, | |
312 public NPT_TcpServerSocketInterface | |
313 { | |
314 public: | |
315 // constructors and destructor | |
316 NPT_TcpServerSocket(); | |
317 virtual ~NPT_TcpServerSocket(); | |
318 | |
319 // delegate NPT_TcpServerSocketInterface methods | |
320 NPT_Result Listen(unsigned int max_clients) { | |
321 return m_TcpServerSocketDelegate->Listen(max_clients); | |
322 } | |
323 NPT_Result WaitForNewClient(NPT_Socket*& client, | |
324 NPT_Timeout timeout = NPT_TIMEOUT_INFINITE) { | |
325 return m_TcpServerSocketDelegate->WaitForNewClient(client, timeout); | |
326 } | |
327 | |
328 protected: | |
329 // members | |
330 NPT_TcpServerSocketInterface* m_TcpServerSocketDelegate; | |
331 }; | |
332 | |
333 #endif // _NPT_SOCKETS_H_ |