xssocket.h
Go to the documentation of this file.
1 
2 // Copyright (c) 2003-2021 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 
34 // Copyright (c) 2003-2021 Xsens Technologies B.V. or subsidiaries worldwide.
35 // All rights reserved.
36 //
37 // Redistribution and use in source and binary forms, with or without modification,
38 // are permitted provided that the following conditions are met:
39 //
40 // 1. Redistributions of source code must retain the above copyright notice,
41 // this list of conditions, and the following disclaimer.
42 //
43 // 2. Redistributions in binary form must reproduce the above copyright notice,
44 // this list of conditions, and the following disclaimer in the documentation
45 // and/or other materials provided with the distribution.
46 //
47 // 3. Neither the names of the copyright holders nor the names of their contributors
48 // may be used to endorse or promote products derived from this software without
49 // specific prior written permission.
50 //
51 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
52 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
53 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
54 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
56 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
58 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
60 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
61 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
62 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
63 //
64 
65 #ifndef XSSOCKET_H
66 #define XSSOCKET_H
67 
68 #include "xsstring.h"
69 #include "xsbytearray.h"
70 #include "xsresultvalue.h"
71 #include "xstypedefs.h"
72 
73 struct XsSocket;
74 struct XsSocketInterface;
75 typedef struct XsSocket XsSocket;
76 
79 {
82 };
83 
86 {
90 };
91 
93 {
96 };
97 
98 #ifdef WIN32
99  #if defined(_WIN64)
100  typedef unsigned __int64 XSOCKET;
101  #else
102  typedef unsigned int XSOCKET;
103  #endif
104 #else
105  typedef int XSOCKET;
106 #endif
107 
108 #ifdef __cplusplus
109 extern "C" {
110 #endif
111 #ifndef __cplusplus
112 #define XSSOCKET_INITIALIZER { NULL }
113 #endif
114 
115 #define XSSOCKET_INFINITE_TIMEOUT -1
116 
117 XSTYPES_DLL_API void XsSocket_create(XsSocket* thisPtr, enum NetworkLayerProtocol ip, enum IpProtocol protocol);
121 
123 
124 XSTYPES_DLL_API int XsSocket_select(XsSocket* thisPtr, int mstimeout, int* canRead, int* canWrite);
125 XSTYPES_DLL_API int XsSocket_read(XsSocket* thisPtr, void* dest, XsSize size, int timeout);
126 XSTYPES_DLL_API int XsSocket_readFrom(XsSocket* thisPtr, void* dest, XsSize size, XsString* hostname, uint16_t* port, int timeout);
127 XSTYPES_DLL_API int XsSocket_read2ByteArray(XsSocket* thisPtr, XsByteArray* dest, int timeout);
128 XSTYPES_DLL_API int XsSocket_readFrom2ByteArray(XsSocket* thisPtr, XsByteArray* dest, XsString* hostname, uint16_t* port, int timeout);
129 
130 XSTYPES_DLL_API int XsSocket_write(XsSocket* thisPtr, const void* buffer, XsSize size);
131 XSTYPES_DLL_API int XsSocket_writeTo(XsSocket* thisPtr, const void* buffer, XsSize size, const XsString* hostname, uint16_t port);
132 
134 
135 XSTYPES_DLL_API XsSocket* XsSocket_accept(XsSocket* thisPtr, int mstimeout);
136 
138 XSTYPES_DLL_API void XsSocket_free(XsSocket* thisPtr);
139 
140 XSTYPES_DLL_API XsResultValue XsSocket_setSocketOption(XsSocket* thisPtr, enum XsSocketOption option, void* valuePtr, int valueSize);
141 XSTYPES_DLL_API XsResultValue XsSocket_bind(XsSocket* thisPtr, const XsString* hostname, uint16_t port);
142 XSTYPES_DLL_API XsResultValue XsSocket_listen(XsSocket* thisPtr, int maxPending);
143 XSTYPES_DLL_API XsResultValue XsSocket_connect(XsSocket* thisPtr, const XsString* host, uint16_t port);
144 
145 XSTYPES_DLL_API int XsSocket_isUsable(const XsSocket* thisPtr);
146 XSTYPES_DLL_API void XsSocket_getRemoteAddress(const XsSocket* thisPtr, XsString* address);
148 XSTYPES_DLL_API int XsSocket_enableBroadcasts(XsSocket* thisPtr, int enable);
149 XSTYPES_DLL_API int XsSocket_broadcast(XsSocket* thisPtr, const void* buffer, XsSize size, uint16_t port);
150 
151 #ifdef __cplusplus
152 }
153 #endif
154 
160 struct XsSocket
161 {
162 #ifdef __cplusplus
163 
168  inline explicit XsSocket(IpProtocol protocol, NetworkLayerProtocol ipVersion = NLP_IPVX)
169  : d(NULL)
170  {
171  (void)d;
172  XsSocket_create(this, ipVersion, protocol);
173  }
174 
180  inline explicit XsSocket(int sockfd, XsDataFlags flags = XSDF_None)
181  : d(NULL)
182  {
183  (void)d;
184  XsSocket_createFromFileDescriptor(this, sockfd, flags);
185  }
186 
189  inline ~XsSocket()
190  {
191  XsSocket_destroy(this);
192  }
193 
196  inline XsResultValue close()
197  {
198  return XsSocket_close(this);
199  }
200 
202  inline XSOCKET nativeDescriptor() const
203  {
204  return XsSocket_nativeDescriptor(this);
205  }
206 
216  inline int read(XsByteArray& buffer, int timeout = 0)
217  {
218  return XsSocket_read2ByteArray(this, &buffer, timeout);
219  }
220 
231  inline int read(void* dest, XsSize size, int timeout = 0)
232  {
233  return XsSocket_read(this, dest, size, timeout);
234  }
235 
247  inline int readFrom(XsByteArray& buffer, XsString* hostname = NULL, uint16_t* port = NULL, int timeout = 0)
248  {
249  return XsSocket_readFrom2ByteArray(this, &buffer, hostname, port, timeout);
250  }
251 
264  inline int readFrom(void* dest, XsSize size, XsString* hostname = NULL, uint16_t* port = NULL, int timeout = 0)
265  {
266  return XsSocket_readFrom(this, dest, size, hostname, port, timeout);
267  }
268 
277  int write(const XsByteArray& buffer)
278  {
279  return write(buffer.data(), buffer.size());
280  }
281 
291  int write(const void* data, XsSize size)
292  {
293  return XsSocket_write(this, data, size);
294  }
295 
306  int writeTo(const XsByteArray& buffer, const XsString& hostname, uint16_t port)
307  {
308  return writeTo(buffer.data(), buffer.size(), hostname, port);
309  }
310 
322  int writeTo(const void* data, XsSize size, const XsString& hostname, uint16_t port)
323  {
324  return XsSocket_writeTo(this, data, size, &hostname, port);
325  }
326 
329  void flush()
330  {
331  XsSocket_flush(this);
332  }
333 
340  int select(int mstimeout, int* canRead, int* canWrite)
341  {
342  return XsSocket_select(this, mstimeout, canRead, canWrite);
343  }
344 
353  XsSocket* accept(int mstimeout = XSSOCKET_INFINITE_TIMEOUT)
354  {
355  return XsSocket_accept(this, mstimeout);
356  }
357 
359  inline void* operator new (size_t)
360  {
362  if (!s)
363  throw std::bad_alloc();
364  return s;
365  }
366 
368  inline void operator delete (void* p) noexcept
369  {
370  XsSocket_free(reinterpret_cast<XsSocket*>(p));
371  }
372 
380  XsResultValue setSocketOption(XsSocketOption option, void* valuePtr, int valueSize)
381  {
382  return XsSocket_setSocketOption(this, option, valuePtr, valueSize);
383  }
384 
391  XsResultValue setSocketOption(XsSocketOption option, int value)
392  {
393  return XsSocket_setSocketOption(this, option, (void*)&value, sizeof(int));
394  }
395 
404  XsResultValue bind(uint16_t port)
405  {
406  return XsSocket_bind(this, NULL, port);
407  }
408 
418  XsResultValue bind(const XsString& hostname, uint16_t port)
419  {
420  return XsSocket_bind(this, &hostname, port);
421  }
422 
431  XsResultValue listen(int maxPending = 20)
432  {
433  return XsSocket_listen(this, maxPending);
434  }
435 
444  XsResultValue connect(const XsString& host, uint16_t port)
445  {
446  return XsSocket_connect(this, &host, port);
447  }
448 
449 
455  bool isUsable() const
456  {
457  return XsSocket_isUsable(this) != 0;
458  }
459 
465  XsString getRemoteAddress() const
466  {
467  XsString address;
468  XsSocket_getRemoteAddress(this, &address);
469  return address;
470  }
471 
474  int getLastSystemError() const
475  {
476  return XsSocket_getLastSystemError(this);
477  }
478 
487  int broadcast(const void* data, XsSize size, uint16_t port)
488  {
489  return XsSocket_broadcast(this, data, size, port);
490  }
491 
497  bool enableBroadcasts(bool enable)
498  {
499  return 0 != XsSocket_enableBroadcasts(this, enable ? 1 : 0);
500  }
501 private:
502 #endif
504 };
505 
506 #endif
XsSocket_write
XSTYPES_DLL_API int XsSocket_write(XsSocket *thisPtr, const void *buffer, XsSize size)
Write data to the socket.
Definition: xssocket.c:665
XsSocket_writeTo
XSTYPES_DLL_API int XsSocket_writeTo(XsSocket *thisPtr, const void *buffer, XsSize size, const XsString *hostname, uint16_t port)
Write data to the socket to the host indicated by hostname, port.
Definition: xssocket.c:861
XsSocket_connect
XSTYPES_DLL_API XsResultValue XsSocket_connect(XsSocket *thisPtr, const XsString *host, uint16_t port)
Connect to hostame on port.
Definition: xssocket.c:1296
NLP_IPVX
@ NLP_IPVX
any IP address family
Definition: xssocket.h:89
IpProtocol
IpProtocol
the protocol on top of IP
Definition: xssocket.h:78
xsstring.h
XsByteArray
A list of uint8_t values.
IP_TCP
@ IP_TCP
The TCP/IP protocol.
Definition: xssocket.h:81
XSOCKET
int XSOCKET
Definition: xssocket.h:105
s
XmlRpcServer s
XsSocketPrivate
Definition: xssocket.c:112
XsSocket_accept
XSTYPES_DLL_API XsSocket * XsSocket_accept(XsSocket *thisPtr, int mstimeout)
Accept an incoming connection.
Definition: xssocket.c:1084
XSO_ReuseAddress
@ XSO_ReuseAddress
Allow bind to reuse a local port (using different local addresses)
Definition: xssocket.h:94
XsSocket_destroy
XSTYPES_DLL_API void XsSocket_destroy(XsSocket *thisPtr)
Destroy the given socket.
Definition: xssocket.c:457
xstypedefs.h
XSSOCKET_INFINITE_TIMEOUT
#define XSSOCKET_INFINITE_TIMEOUT
Definition: xssocket.h:115
XsSocket_flush
XSTYPES_DLL_API void XsSocket_flush(XsSocket *thisPtr)
Flush the incoming data.
Definition: xssocket.c:1063
XsSocket::d
struct XsSocketPrivate * d
Definition: xssocket.h:503
NetworkLayerProtocol
NetworkLayerProtocol
The network layer protocol, or IP address family.
Definition: xssocket.h:85
XsSocketOption
XsSocketOption
Definition: xssocket.h:92
data
data
XsResultValue
XsResultValue
Xsens result values.
Definition: xsresultvalue.h:82
XsSocket_allocate
XSTYPES_DLL_API XsSocket * XsSocket_allocate()
Dynamically allocate an XsSocket.
Definition: xssocket.c:1126
XsSocket
A platform independent socket implementation.
Definition: xssocket.h:160
XsSocket::XsSocket_select
int XsSocket_select(XsSocket *thisPtr, int mstimeout, int *canRead, int *canWrite)
Wait for read or write for mstimeout milliseconds on socket thisPtr.
Definition: xssocket.c:485
XsSocket_broadcast
XSTYPES_DLL_API int XsSocket_broadcast(XsSocket *thisPtr, const void *buffer, XsSize size, uint16_t port)
Broadcast data over the socket to the port indicated by port.
Definition: xssocket.c:924
XsSocket_getLastSystemError
XSTYPES_DLL_API int XsSocket_getLastSystemError(const XsSocket *thisPtr)
Return the system error code of the last socket operation.
Definition: xssocket.c:1324
XsSize
size_t XsSize
XsSize must be unsigned number!
Definition: xstypedefs.h:74
XsSocket::XsSocket_close
XsResultValue XsSocket_close(XsSocket *thisPtr)
Close a socket.
Definition: xssocket.c:433
XsSocket_getRemoteAddress
XSTYPES_DLL_API void XsSocket_getRemoteAddress(const XsSocket *thisPtr, XsString *address)
Get the numeric IP address of remote host of this socket.
Definition: xssocket.c:1315
XsSocket
struct XsSocket XsSocket
Definition: xssocket.h:75
IP_UDP
@ IP_UDP
The UDP/IP protocol.
Definition: xssocket.h:80
XsSocket_create
XSTYPES_DLL_API void XsSocket_create(XsSocket *thisPtr, enum NetworkLayerProtocol ip, enum IpProtocol protocol)
Create socket thisPtr for IP version ip with protocol protocol.
Definition: xssocket.c:350
XsSocket_readFrom
XSTYPES_DLL_API int XsSocket_readFrom(XsSocket *thisPtr, void *dest, XsSize size, XsString *hostname, uint16_t *port, int timeout)
Read size data into dest.
Definition: xssocket.c:574
XsSocket_nativeDescriptor
XSTYPES_DLL_API XSOCKET XsSocket_nativeDescriptor(XsSocket const *thisPtr)
Return the native file descriptor.
Definition: xssocket.c:422
NLP_IPV4
@ NLP_IPV4
IPv4 address family.
Definition: xssocket.h:87
XsSocket::XsSocket_createFromFileDescriptor
void XsSocket_createFromFileDescriptor(XsSocket *thisPtr, int sockfd, XsDataFlags flags)
Create a socket from a native file descriptor.
Definition: xssocket.c:410
XsSocket_isUsable
XSTYPES_DLL_API int XsSocket_isUsable(const XsSocket *thisPtr)
Return non-zero if the socket is usable for reading and writing data.
Definition: xssocket.c:1306
XsSocket_listen
XSTYPES_DLL_API XsResultValue XsSocket_listen(XsSocket *thisPtr, int maxPending)
Start listening for incoming connections on this socket.
Definition: xssocket.c:1259
XsSocket_free
XSTYPES_DLL_API void XsSocket_free(XsSocket *thisPtr)
Free a socket returned from XsSocket_accept() or XsSocket_allocate()
Definition: xssocket.c:1140
XsSocket_bind
XSTYPES_DLL_API XsResultValue XsSocket_bind(XsSocket *thisPtr, const XsString *hostname, uint16_t port)
Bind to the hostname and port combination.
Definition: xssocket.c:1220
XSDF_None
@ XSDF_None
No flag set.
Definition: xstypedefs.h:109
XSTYPES_DLL_API
#define XSTYPES_DLL_API
Definition: xstypesconfig.h:65
NLP_IPV6
@ NLP_IPV6
IPv6 address family.
Definition: xssocket.h:88
XsDataFlags
XsDataFlags
These flags define the behaviour of data contained by Xsens data structures.
Definition: xstypedefs.h:107
XsSocket_read2ByteArray
XSTYPES_DLL_API int XsSocket_read2ByteArray(XsSocket *thisPtr, XsByteArray *dest, int timeout)
Read size data into dest.
Definition: xssocket.c:609
xsbytearray.h
XSO_ReusePort
@ XSO_ReusePort
Allow bind to reuse the exact local address.
Definition: xssocket.h:95
xsresultvalue.h
XsSocket::XsSocket_enableBroadcasts
int XsSocket_enableBroadcasts(XsSocket *thisPtr, int enable)
Enable sending and receiving broadcasts on this socket.
Definition: xssocket.c:895
XsString
A 0-terminated managed string of characters.
XsSocket_read
XSTYPES_DLL_API int XsSocket_read(XsSocket *thisPtr, void *dest, XsSize size, int timeout)
Read size data into dest.
Definition: xssocket.c:545
XsSocket_readFrom2ByteArray
XSTYPES_DLL_API int XsSocket_readFrom2ByteArray(XsSocket *thisPtr, XsByteArray *dest, XsString *hostname, uint16_t *port, int timeout)
Read size data into dest.
Definition: xssocket.c:624
XsSocket::XsSocket_setSocketOption
XsResultValue XsSocket_setSocketOption(XsSocket *thisPtr, enum XsSocketOption option, void *valuePtr, int valueSize)
Changes the value of a socket option.
Definition: xssocket.c:1153


xsens_mti_driver
Author(s):
autogenerated on Sun Sep 3 2023 02:43:20