Debian Codegraph @

3rdparty/xspublic/xstypes/xssocket.h

raw
1
2 // Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // 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 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32
33 #ifndef XSSOCKET_H
34 #define XSSOCKET_H
35
36 #include "xsstring.h"
37 #include "xsbytearray.h"
38 #include "xsresultvalue.h"
39 #include "xstypedefs.h"
40
41 struct XsSocket;
42 struct XsSocketInterface;
43 typedef struct XsSocket XsSocket;
44
45 /*! \brief the protocol on top of IP */
46 enum IpProtocol {
47 IP_UDP, /*!< \brief The UDP/IP protocol */
48 IP_TCP /*!< \brief The TCP/IP protocol */
49 };
50
51 /*! \brief The network layer protocol, or IP address family */
52 enum NetworkLayerProtocol {
53 NLP_IPV4, /*!< \brief IPv4 address family */
54 NLP_IPV6, /*!< \brief IPv6 address family */
55 NLP_IPVX /*!< \brief any IP address family */
56 };
57
58 enum XsSocketOption {
59 XSO_ReuseAddress, /*!< \brief Allow bind to reuse a local port (using different local addresses) */
60 XSO_ReusePort /*!< \brief Allow bind to reuse the exact local address */
61 };
62
63 #ifdef WIN32
64 # if defined(_WIN64)
65 typedef unsigned __int64 XSOCKET;
66 # else
67 typedef unsigned int XSOCKET;
68 # endif
69 #else
70 typedef int XSOCKET;
71 #endif
72
73 #ifdef __cplusplus
74 extern "C" {
75 #endif
76 #ifndef __cplusplus
77 #define XSSOCKET_INITIALIZER { NULL }
78 #endif
79
80 #define XSSOCKET_INFINITE_TIMEOUT -1
81
82 XSTYPES_DLL_API void XsSocket_create(XsSocket* thisPtr, enum NetworkLayerProtocol ip, enum IpProtocol protocol);
83 XSTYPES_DLL_API void XsSocket_createFromFileDescriptor(XsSocket* thisPtr, int sockfd, XsDataFlags flags);
84 XSTYPES_DLL_API void XsSocket_destroy(XsSocket* thisPtr);
85 XSTYPES_DLL_API XsResultValue XsSocket_close(XsSocket* thisPtr);
86
87 XSTYPES_DLL_API XSOCKET XsSocket_nativeDescriptor(XsSocket const* thisPtr);
88
89 XSTYPES_DLL_API int XsSocket_select(XsSocket* thisPtr, int mstimeout, int *canRead, int *canWrite);
90 XSTYPES_DLL_API int XsSocket_read(XsSocket* thisPtr, void* dest, XsSize size, int timeout);
91 XSTYPES_DLL_API int XsSocket_readFrom(XsSocket* thisPtr, void* dest, XsSize size, XsString* hostname, uint16_t* port, int timeout);
92 XSTYPES_DLL_API int XsSocket_read2ByteArray(XsSocket* thisPtr, XsByteArray* dest, int timeout);
93 XSTYPES_DLL_API int XsSocket_readFrom2ByteArray(XsSocket* thisPtr, XsByteArray* dest, XsString* hostname, uint16_t* port, int timeout);
94
95 XSTYPES_DLL_API int XsSocket_write(XsSocket* thisPtr, const void* buffer, XsSize size);
96 XSTYPES_DLL_API int XsSocket_writeTo(XsSocket* thisPtr, const void* buffer, XsSize size, const XsString* hostname, uint16_t port);
97
98 XSTYPES_DLL_API void XsSocket_flush(XsSocket* thisPtr);
99
100 XSTYPES_DLL_API XsSocket* XsSocket_accept(XsSocket* thisPtr, int mstimeout);
101
102 XSTYPES_DLL_API XsSocket* XsSocket_allocate();
103 XSTYPES_DLL_API void XsSocket_free(XsSocket* thisPtr);
104
105 XSTYPES_DLL_API XsResultValue XsSocket_setSocketOption(XsSocket* thisPtr, enum XsSocketOption option, void* valuePtr, int valueSize);
106 XSTYPES_DLL_API XsResultValue XsSocket_bind(XsSocket* thisPtr, const XsString* hostname, uint16_t port);
107 XSTYPES_DLL_API XsResultValue XsSocket_listen(XsSocket* thisPtr, int maxPending);
108 XSTYPES_DLL_API XsResultValue XsSocket_connect(XsSocket* thisPtr, const XsString* host, uint16_t port);
109
110 XSTYPES_DLL_API int XsSocket_isUsable(const XsSocket* thisPtr);
111 XSTYPES_DLL_API void XsSocket_getRemoteAddress(const XsSocket* thisPtr, XsString *address);
112 XSTYPES_DLL_API int XsSocket_getLastSystemError(const XsSocket* thisPtr);
113 XSTYPES_DLL_API int XsSocket_enableBroadcasts(XsSocket* thisPtr, int enable);
114 XSTYPES_DLL_API int XsSocket_broadcast(XsSocket* thisPtr, const void* buffer, XsSize size, uint16_t port);
115
116 #ifdef __cplusplus
117 }
118 #endif
119
120 /*! \brief A platform independent socket implementation
121
122 The internals are hidden to remove any inconvenience that
123 may occur because of the order of inclusion of system headers.
124 */
125 struct XsSocket
126 {
127 #ifdef __cplusplus
128 /*! \brief \copybrief XsSocket_create
129
130 \param[in] protocol the protocol on top of IP to use
131 \param[in] ipVersion the ip address family to use
132 */
133 inline explicit XsSocket(IpProtocol protocol, NetworkLayerProtocol ipVersion = NLP_IPVX)
134 : d(NULL)
135 {
136 (void)d;
137 XsSocket_create(this, ipVersion, protocol);
138 }
139
140 /*! \brief \copybrief XsSocket_createFromFileDescriptor
141
142 \param[in] sockfd the file descriptor of the socket to wrap
143 \param[in] flags if set to XSDF_Managed the wrapped socket will be closed when object is destroyed
144 */
145 inline explicit XsSocket(int sockfd, XsDataFlags flags = XSDF_None)
146 : d(NULL)
147 {
148 (void)d;
149 XsSocket_createFromFileDescriptor(this, sockfd, flags);
150 }
151
152 /*! \brief \copybrief XsSocket_destroy
153 */
154 inline ~XsSocket()
155 {
156 XsSocket_destroy(this);
157 }
158
159 /*! \brief \copybrief XsSocket_close
160 */
161 inline XsResultValue close()
162 {
163 return XsSocket_close(this);
164 }
165
166 /*! \brief Return the native file descriptor */
167 inline XSOCKET nativeDescriptor() const
168 {
169 return XsSocket_nativeDescriptor(this);
170 }
171
172 /*! \brief read data from the socket into \a buffer
173
174 \param[in,out] buffer the buffer to fill, contents are automatically adjusted to the data size available
175 \param[in] timeout the maximum time to wait before data is in in milliseconds
176
177 \returns the number of bytes read from the socket
178 \see read(void*,XsSize,int)
179 \see XsSocket_read
180 */
181 inline int read(XsByteArray& buffer, int timeout = 0)
182 {
183 return XsSocket_read2ByteArray(this, &buffer, timeout);
184 }
185
186 /*! \brief \copybrief XsSocket_read
187
188 \param[in,out] dest the destination buffer for the incoming data
189 \param[in] size the size of the destination buffer
190 \param[in] timeout the maximum time to wait before data is in in milliseconds
191
192 \returns the number of bytes read from the socket
193 \see read(XsByteArray&,int)
194 \see XsSocket_read
195 */
196 inline int read(void* dest, XsSize size, int timeout = 0)
197 {
198 return XsSocket_read(this, dest, size, timeout);
199 }
200
201 /*! \brief read data from the socket into \a buffer
202
203 \param[in,out] buffer the buffer to write into
204 \param[in,out] hostname a pointer to a string that contains the hostname of the sender after returning from this function. May be NULL
205 \param[in,out] port a pointer that will contain the port of the sender after returning from this function. May be NULL
206 \param[in] timeout the read timeout in milliseconds
207
208 \returns the size of the data read from the socket, -1 on error
209 \see readFrom(void*,XsSize,XsString*,uint16_t*,int)
210 \see XsSocket_readFrom
211 */
212 inline int readFrom(XsByteArray& buffer, XsString* hostname = NULL, uint16_t* port = NULL, int timeout = 0)
213 {
214 return XsSocket_readFrom2ByteArray(this, &buffer, hostname, port, timeout);
215 }
216
217 /*! \brief \copybrief XsSocket_readFrom
218
219 \param[in,out] dest the buffer to write into
220 \param[in] size the size of the destination buffer
221 \param[in,out] hostname a pointer to a string that contains the hostname of the sender after returning from this function. May be NULL
222 \param[in,out] port a pointer that will contain the port of the sender after returning from this function. May be NULL
223 \param[in] timeout the read timeout in milliseconds
224
225 \returns the size of the data read from the socket, -1 on error
226 \see readFrom(XsByteArray,XsString*,uint16_t,int)
227 \see XsSocket_readFrom
228 */
229 inline int readFrom(void* dest, XsSize size, XsString* hostname = NULL, uint16_t* port = NULL, int timeout = 0)
230 {
231 return XsSocket_readFrom(this, dest, size, hostname, port, timeout);
232 }
233
234 /*! \brief Write the data in \a buffer to the socket
235
236 \param[in] buffer the data to write
237
238 \returns the number of bytes written
239 \see write(const void*,XsSize)
240 \see XsSocket_write
241 */
242 int write(const XsByteArray& buffer)
243 {
244 return write(buffer.data(), buffer.size());
245 }
246
247 /*! \brief \copybrief XsSocket_write
248
249 \param[in] data the data to write
250 \param[in] size the size of the data to write
251
252 \returns the number of bytes written to the socket
253 \see write(const XsByteArray&)
254 \see XsSocket_write
255 */
256 int write(const void* data, XsSize size)
257 {
258 return XsSocket_write(this, data, size);
259 }
260
261 /*! \brief Write the data in \a buffer to the socket
262
263 \param[in] buffer the data to write
264 \param[in] hostname the host to write data to
265 \param[in] port the port to write data to
266
267 \returns the number of bytes written
268 \see writeTo(const void*,XsSize,XsString,uint16_t)
269 \see XsSocket_writeTo
270 */
271 int writeTo(const XsByteArray& buffer, const XsString& hostname, uint16_t port)
272 {
273 return writeTo(buffer.data(), buffer.size(), hostname, port);
274 }
275
276 /*! \brief \copybrief XsSocket_writeTo
277
278 \param[in] data the data to write
279 \param[in] size the size of the data to write
280 \param[in] hostname the host to write data to
281 \param[in] port the port to write data to
282
283 \returns the number of bytes written to the socket
284 \see writeTo(XsByteArray,XsString,uint16_t)
285 \see XsSocket_writeTo
286 */
287 int writeTo(const void* data, XsSize size, const XsString& hostname, uint16_t port)
288 {
289 return XsSocket_writeTo(this, data, size, &hostname, port);
290 }
291
292 /*! \brief \copybrief XsSocket_flush
293 */
294 void flush()
295 {
296 XsSocket_flush(this);
297 }
298
299 /*! \brief \copybrief XsSocket_select
300 \param[in] mstimeout timeout to wait for socket to be readable/writeable
301 \param[in,out] canRead if not null then will be set to non-zero to indicate that the socket has data to read.
302 \param[in,out] canWrite if not null then will be set to non-zero to indicate that the socket can be written to.
303 \returns -1 on error, 0 on timeout, or a positive number otherwise
304 */
305 int select(int mstimeout, int *canRead, int *canWrite)
306 {
307 return XsSocket_select(this, mstimeout, canRead, canWrite);
308 }
309
310 /*! \brief \copybrief XsSocket_accept
311
312 \param[in] mstimeout the timeout in milliseconds. Negative timeouts count as infinite; XSSOCKET_INFINITE_TIMEOUT (-1) by default
313 \returns a pointer to a newly created XsSocket, NULL on error. The pointer can be deleted.
314
315 \see listen
316 \see XsSocket_accept
317 */
318 XsSocket* accept(int mstimeout = XSSOCKET_INFINITE_TIMEOUT)
319 {
320 return XsSocket_accept(this, mstimeout);
321 }
322
323 /*! \brief Allocate memory for a socket */
324 inline void* operator new(size_t)
325 {
326 XsSocket *s = XsSocket_allocate();
327 if (!s)
328 throw std::bad_alloc();
329 return s;
330 }
331
332 /*! \brief De-allocate the socket */
333 inline void operator delete(void* p) noexcept
334 {
335 XsSocket_free(reinterpret_cast<XsSocket*>(p));
336 }
337
338 /*! \brief \copybrief XsSocket_setSocketOption
339 \param[in] option The socket option to change
340 \param[in] valuePtr Points to the new value of the socket option
341 \param[in] valueSize The size of the value \a valuePtr points to
342 \returns XRV_OK on success, another XsResultValue otherwise
343 \see XsSocket_setSocketOption
344 */
345 XsResultValue setSocketOption(XsSocketOption option, void* valuePtr, int valueSize)
346 {
347 return XsSocket_setSocketOption(this, option, valuePtr, valueSize);
348 }
349
350 /*! \brief Sets an socket option of int type
351 \param[in] option The socket option to change
352 \param[in] value The new value of the socket option
353 \returns XRV_OK on success, another XsResultValue otherwise
354 \see XsSocket_setSocketOption
355 */
356 XsResultValue setSocketOption(XsSocketOption option, int value)
357 {
358 return XsSocket_setSocketOption(this, option, (void*)&value, sizeof(int));
359 }
360
361 /*! \brief Bind to any interface on \a port
362
363 \param[in] port the port to bind to
364
365 \returns XRV_OK on success, another XsResultValue otherwise
366 \see bind(XsString, port)
367 \see XsSocket_bind
368 */
369 XsResultValue bind(uint16_t port)
370 {
371 return XsSocket_bind(this, NULL, port);
372 }
373
374 /*! \brief \copybrief XsSocket_bind
375
376 \param[in] hostname the hostname to bind to
377 \param[in] port the port to bind to
378
379 \returns XRV_OK on success, another XsResultValue otherwise
380 \see bind(uint16_t)
381 \see XsSocket_bind
382 */
383 XsResultValue bind(const XsString& hostname, uint16_t port)
384 {
385 return XsSocket_bind(this, &hostname, port);
386 }
387
388 /*! \brief \copybrief XsSocket_listen
389
390 \param[in] maxPending the maximum pending connections
391
392 \returns XRV_OK on success, another XsResultValue when an error occurs
393 \see XsSocket_listen
394 \see XsSocket_accept
395 */
396 XsResultValue listen(int maxPending = 20)
397 {
398 return XsSocket_listen(this, maxPending);
399 }
400
401 /*! \brief \copybrief XsSocket_connect
402
403 \param[in] host the host to connect to
404 \param[in] port the port to connect to
405
406 \returns XRV_OK on success, another XsResultValue when an error occurs
407 \see XsSocket_connect
408 */
409 XsResultValue connect(const XsString& host, uint16_t port)
410 {
411 return XsSocket_connect(this, &host, port);
412 }
413
414
415 /*! \brief Return true if the socket is usable
416
417 \returns true if the socket can be used for communication, false otherwise
418 \see XsSocket_isUsable
419 */
420 bool isUsable() const
421 {
422 return XsSocket_isUsable(this) != 0;
423 }
424
425 /*! \brief Get the IP address of the remote host this socket is connected to.
426
427 Only works for connected TCP sockets
428 \returns the remote host IP address
429 */
430 XsString getRemoteAddress() const
431 {
432 XsString address;
433 XsSocket_getRemoteAddress(this, &address);
434 return address;
435 }
436
437 /*! \brief Return the system error reported by the last operation
438 */
439 int getLastSystemError() const
440 {
441 return XsSocket_getLastSystemError(this);
442 }
443
444 /*! \brief Broadcast data over the socket to the port indicated by \a port
445
446 \param[in] data the data to write
447 \param[in] size the size of the data
448 \param[in] port the port to send data to
449
450 \returns the number of bytes written, -1 on error, depending on whether the majority of sub broadcasts succeeded or failed.
451 */
452 int broadcast(const void* data, XsSize size, uint16_t port)
453 {
454 return XsSocket_broadcast(this, data, size, port);
455 }
456
457 /*! \brief Enable sending and receiving broadcasts on this socket
458 \details By default sockets do not receive broadcasts and can't send them. This function can enable this option.
459 \param enable Set to 1 to enable broadcasts, 0 to disable them again
460 \return 1 if successful, 0 if a failure occurred
461 */
462 bool enableBroadcasts(bool enable)
463 {
464 return 0 != XsSocket_enableBroadcasts(this, enable ? 1 : 0);
465 }
466 private:
467 #endif
468 struct XsSocketPrivate *d;
469 };
470
471 #endif