ISTcpServer.cpp
Go to the documentation of this file.
1 /*
2 MIT LICENSE
3 
4 Copyright (c) 2014-2021 Inertial Sense, Inc. - http://inertialsense.com
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
7 
8 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9 
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
13 #include "ISConstants.h"
14 
15 #if PLATFORM_IS_LINUX || PLATFORM_IS_APPLE
16 
17 /* Assume that any non-Windows platform uses POSIX-style sockets instead. */
18 #include <sys/socket.h>
19 #include <sys/ioctl.h>
20 #include <arpa/inet.h>
21 #include <netdb.h> /* Needed for getaddrinfo() and freeaddrinfo() */
22 #include <unistd.h> /* Needed for close() */
23 #include <fcntl.h>
24 #include <errno.h>
25 
26 #endif
27 
28 #include "ISTcpServer.h"
29 #include "ISUtilities.h"
30 
32 {
34  m_delegate = delegate;
35  m_socket = 0;
36  m_port = 0;
37 }
38 
40 {
41  Close();
43 }
44 
45 int cISTcpServer::Open(const string& ipAddress, int port)
46 {
47  m_ipAddress = ipAddress;
48  m_port = port;
49  int status;
50  char portString[64];
51  snprintf(portString, sizeof(portString), "%ld", (long)m_port);
52  addrinfo* result = NULL;
53  addrinfo hints = addrinfo();
54  hints.ai_family = AF_INET;
55  hints.ai_socktype = SOCK_STREAM;
56  hints.ai_protocol = IPPROTO_TCP;
57  hints.ai_flags = AI_PASSIVE;
58  status = getaddrinfo(m_ipAddress.length() == 0 ? NULL : m_ipAddress.c_str(), portString, &hints, &result);
59  if (status != 0)
60  {
61  Close();
62  return status;
63  }
64 
65  // setup socket
66  m_socket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
67  if (m_socket == 0)
68  {
69  freeaddrinfo(result);
70  Close();
71  return -1;
72  }
73 
74  int enable = 1;
75  if (setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&enable, sizeof(enable)) < 0)
76  {
77  freeaddrinfo(result);
78  Close();
79  return -1;
80  }
81 
82  // setup listener socket
83  status = ::bind(m_socket, result->ai_addr, (int)result->ai_addrlen);
84  if (status != 0)
85  {
86  freeaddrinfo(result);
87  Close();
88  return -1;
89  }
90 
91  freeaddrinfo(result);
92 
93  status = listen(m_socket, SOMAXCONN);
94  if (status != 0)
95  {
96  Close();
97  return -1;
98  }
99 
100  return status;
101 }
102 
104 {
105  int status = ISSocketClose(m_socket);
106  for (size_t i = 0; i < m_clients.size(); i++)
107  {
108  status |= ISSocketClose(m_clients[i]);
109  }
110  m_clients.clear();
111  return status;
112 }
113 
115 {
116  uint8_t readBuff[8192];
117 
118  // accept new sockets
119  while (ISSocketCanRead(m_socket, 1))
120  {
121  if (m_delegate != NULLPTR)
122  {
124  }
125  socket_t socket = accept(m_socket, NULLPTR, NULLPTR);
126  if (socket != 0)
127  {
128  ISSocketSetBlocking(socket, false);
129  m_clients.push_back(socket);
130  if (m_delegate != NULLPTR)
131  {
132  m_delegate->OnClientConnected(this, socket);
133  }
134  }
135  else if (m_delegate != NULLPTR)
136  {
138  }
139  }
140 
141  for (size_t i = 0; i < m_clients.size(); i++)
142  {
143  if (ISSocketCanRead(m_clients[i], 1))
144  {
145  int count;
146  if ((count = ISSocketRead(m_clients[i], readBuff, sizeof(readBuff))) < 0)
147  {
148  // remove the client
149  if (m_delegate != NULLPTR)
150  {
152  }
154  m_clients.erase(m_clients.begin() + i--);
155  }
156  else if (count > 0 && m_delegate != NULLPTR)
157  {
158  m_delegate->OnClientDataReceived(this, m_clients[i], readBuff, count);
159  }
160  }
161  }
162 }
163 
164 int cISTcpServer::Write(const uint8_t* data, int dataLength)
165 {
166  for (size_t i = 0; i < m_clients.size(); i++)
167  {
168  int written = 0;
169  int count;
170  while (written < dataLength)
171  {
172  count = ISSocketWrite(m_clients[i], data + written, dataLength - written);
173  if (count < 1)
174  {
175  // remove the client
176  if (m_delegate != NULLPTR)
177  {
179  }
181  m_clients.erase(m_clients.begin() + i--);
182  break;
183  }
184  else
185  {
186  written += count;
187  if (written == dataLength)
188  {
189  break;
190  }
191  }
192  }
193  }
194  return dataLength; // TODO: Maybe be smarter about detecting difference in bytes written for each client
195 }
virtual void OnClientConnecting(cISTcpServer *server)
Definition: ISTcpServer.h:48
NMI_API SOCKET socket(uint16 u16Domain, uint8 u8Type, uint8 u8Flags)
Definition: socket.c:477
NMI_API sint8 listen(SOCKET sock, uint8 backlog)
Definition: socket.c:607
int ISSocketClose(socket_t &socket)
int ISSocketRead(socket_t socket, uint8_t *data, int dataLength)
#define AF_INET
Definition: socket.h:72
size_t count(InputIterator first, InputIterator last, T const &item)
Definition: catch.hpp:3206
NMI_API sint8 accept(SOCKET sock, struct sockaddr *addr, uint8 *addrlen)
Definition: socket.c:645
#define NULL
Definition: nm_bsp.h:52
virtual void OnClientConnected(cISTcpServer *server, socket_t socket)
Definition: ISTcpServer.h:58
int Open(const string &ipAddress, int port)
Definition: ISTcpServer.cpp:45
int ISSocketCanRead(socket_t socket, int timeoutMilliseconds)
Definition: ISTcpClient.cpp:86
virtual void OnClientConnectFailed(cISTcpServer *server)
Definition: ISTcpServer.h:68
void ISSocketFrameworkShutdown()
Definition: ISTcpClient.cpp:57
#define NULLPTR
Definition: ISConstants.h:426
NMI_API sint8 bind(SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen)
Definition: socket.c:563
NMI_API sint8 setsockopt(SOCKET socket, uint8 u8Level, uint8 option_name, const void *option_value, uint16 u16OptionLen)
Definition: socket.c:1175
int Write(const uint8_t *data, int dataLength)
cISTcpServer(iISTcpServerDelegate *delegate=NULL)
Definition: ISTcpServer.cpp:31
int ISSocketSetBlocking(socket_t socket, bool blocking)
USBInterfaceDescriptor data
int32_t m_port
Definition: ISTcpServer.h:139
socket_t m_socket
Definition: ISTcpServer.h:136
void ISSocketFrameworkInitialize()
Definition: ISTcpClient.cpp:40
#define SOL_SOCKET
Definition: socket.h:112
#define snprintf
Definition: test_suite.cpp:86
int ISSocketWrite(socket_t socket, const uint8_t *data, int dataLength)
Definition: ISTcpClient.cpp:96
virtual void OnClientDataReceived(cISTcpServer *server, socket_t socket, uint8_t *data, int dataLength)
Definition: ISTcpServer.h:36
virtual ~cISTcpServer()
Definition: ISTcpServer.cpp:39
iISTcpServerDelegate * m_delegate
Definition: ISTcpServer.h:140
vector< socket_t > m_clients
Definition: ISTcpServer.h:137
virtual void OnClientDisconnected(cISTcpServer *server, socket_t socket)
Definition: ISTcpServer.h:78
#define SOCK_STREAM
Definition: socket.h:79
string m_ipAddress
Definition: ISTcpServer.h:138


inertial_sense_ros
Author(s):
autogenerated on Sun Feb 28 2021 03:17:57