Visual Servoing Platform  version 3.4.0
vpUDPClient.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * UDP Client
33  *
34  *****************************************************************************/
35 
36 #include <cstring>
37 #include <sstream>
38 
39 #include <visp3/core/vpConfig.h>
40 
41 // inet_ntop() not supported on win XP
42 #ifdef VISP_HAVE_FUNC_INET_NTOP
43 
44 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
45 #include <arpa/inet.h>
46 #include <errno.h>
47 #include <netdb.h>
48 #include <unistd.h>
49 #define DWORD int
50 #else
51 #if defined(__MINGW32__)
52 # ifndef _WIN32_WINNT
53 # define _WIN32_WINNT _WIN32_WINNT_VISTA // 0x0600
54 # endif
55 #endif
56 #include <Ws2tcpip.h>
57 #endif
58 
59 #include <visp3/core/vpUDPClient.h>
60 
67  : m_is_init(false), m_serverAddress(), m_serverLength(0), m_socketFileDescriptor()
68 #if defined(_WIN32)
69  , m_wsa()
70 #endif
71 {
72 }
73 
80 vpUDPClient::vpUDPClient(const std::string &hostname, int port)
81  : m_is_init(false), m_serverAddress(), m_serverLength(0), m_socketFileDescriptor()
82 #if defined(_WIN32)
83  , m_wsa()
84 #endif
85 {
86  init(hostname, port);
87 }
88 
93 
97 void vpUDPClient::close()
98 {
99  if (m_is_init) {
100 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
101  ::close(m_socketFileDescriptor);
102 #else
103  closesocket(m_socketFileDescriptor);
104  WSACleanup();
105 #endif
106  m_is_init = false;
107  }
108 }
109 
116 void vpUDPClient::init(const std::string &hostname, int port)
117 {
118  // Close connexion if already initialized
119  close();
120 
121 #if defined(_WIN32)
122  if (WSAStartup(MAKEWORD(2, 2), &m_wsa) != 0) {
123  std::stringstream ss;
124  ss << "Failed WSAStartup for the server, error code: " << WSAGetLastError();
125  throw vpException(vpException::fatalError, ss.str());
126  }
127 #endif
128 
129  /* socket: create the socket */
130  m_socketFileDescriptor = socket(AF_INET, SOCK_DGRAM, 0);
131 #if defined(_WIN32)
132  if (m_socketFileDescriptor == INVALID_SOCKET)
133 #else
134  if (m_socketFileDescriptor < 0)
135 #endif
136  throw vpException(vpException::fatalError, "Error opening UDP socket for the client!");
137 
138  /* build the server's Internet address */
139  memset(&m_serverAddress, 0, sizeof(m_serverAddress));
140  std::stringstream ss;
141  ss << port;
142  struct addrinfo hints;
143  struct addrinfo *result = NULL;
144  struct addrinfo *ptr = NULL;
145 
146  memset(&hints, 0, sizeof(hints));
147  hints.ai_family = AF_INET;
148  hints.ai_socktype = SOCK_DGRAM;
149  hints.ai_protocol = IPPROTO_UDP;
150 
151  DWORD dwRetval = getaddrinfo(hostname.c_str(), ss.str().c_str(), &hints, &result);
152  if (dwRetval != 0) {
153  ss.str("");
154  ss << "getaddrinfo failed with error: " << dwRetval;
155  throw vpException(vpException::fatalError, ss.str());
156  }
157 
158  for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
159  if (ptr->ai_family == AF_INET && ptr->ai_socktype == SOCK_DGRAM) {
160  m_serverAddress = *(struct sockaddr_in *)ptr->ai_addr;
161  break;
162  }
163  }
164 
165  freeaddrinfo(result);
166 
167  m_serverLength = sizeof(m_serverAddress);
168  m_is_init = true;
169 }
170 
193 int vpUDPClient::receive(std::string &msg, int timeoutMs)
194 {
195  if (!m_is_init) {
196  throw(vpException(vpException::notInitialized, "UDP client is not initialized"));
197  }
198  fd_set s;
199  FD_ZERO(&s);
200  FD_SET(m_socketFileDescriptor, &s);
201  struct timeval timeout;
202  if (timeoutMs > 0) {
203  timeout.tv_sec = timeoutMs / 1000;
204  timeout.tv_usec = (timeoutMs % 1000) * 1000;
205  }
206  int retval = select((int)m_socketFileDescriptor + 1, &s, NULL, NULL, timeoutMs > 0 ? &timeout : NULL);
207 
208  if (retval == -1) {
209  std::cerr << "Error select!" << std::endl;
210  return -1;
211  }
212 
213  if (retval > 0) {
214  /* recvfrom: receive a UDP datagram from the server */
215  int length = static_cast<int>(recvfrom(m_socketFileDescriptor, m_buf, sizeof(m_buf), 0, (struct sockaddr *)&m_serverAddress,
216  (socklen_t *)&m_serverLength));
217  if (length <= 0) {
218  return length < 0 ? -1 : 0;
219  }
220 
221  msg = std::string(m_buf, length);
222  return length;
223  }
224 
225  // Timeout
226  return 0;
227 }
228 
239 int vpUDPClient::receive(void *msg, size_t len, int timeoutMs)
240 {
241  if (!m_is_init) {
242  throw(vpException(vpException::notInitialized, "UDP client is not initialized"));
243  }
244  fd_set s;
245  FD_ZERO(&s);
246  FD_SET(m_socketFileDescriptor, &s);
247  struct timeval timeout;
248  if (timeoutMs > 0) {
249  timeout.tv_sec = timeoutMs / 1000;
250  timeout.tv_usec = (timeoutMs % 1000) * 1000;
251  }
252  int retval = select((int)m_socketFileDescriptor + 1, &s, NULL, NULL, timeoutMs > 0 ? &timeout : NULL);
253 
254  if (retval == -1) {
255  std::cerr << "Error select!" << std::endl;
256  return -1;
257  }
258 
259  if (retval > 0) {
260  /* recvfrom: receive a UDP datagram from the server */
261  int length = static_cast<int>(recvfrom(m_socketFileDescriptor, (char *)msg, (int)len, 0, (struct sockaddr *)&m_serverAddress,
262  (socklen_t *)&m_serverLength));
263  if (length <= 0) {
264  return length < 0 ? -1 : 0;
265  }
266 
267  return length;
268  }
269 
270  // Timeout
271  return 0;
272 }
273 
274 
300 int vpUDPClient::send(const std::string &msg)
301 {
302  if (!m_is_init) {
303  throw(vpException(vpException::notInitialized, "UDP client is not initialized"));
304  }
305  if (msg.size() > VP_MAX_UDP_PAYLOAD) {
306  std::cerr << "Message is too long!" << std::endl;
307  return 0;
308  }
309 
310 /* send the message to the server */
311 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
312  return static_cast<int>(sendto(m_socketFileDescriptor, msg.c_str(), msg.size(), 0, (struct sockaddr *)&m_serverAddress,
313  m_serverLength));
314 #else
315  return sendto(m_socketFileDescriptor, msg.c_str(), (int)msg.size(), 0, (struct sockaddr *)&m_serverAddress,
316  m_serverLength);
317 #endif
318 }
319 
329 int vpUDPClient::send(const void *msg, size_t len)
330 {
331  if (!m_is_init) {
332  throw(vpException(vpException::notInitialized, "UDP client is not initialized"));
333  }
334  if (len > VP_MAX_UDP_PAYLOAD) {
335  std::cerr << "Message is too long!" << std::endl;
336  return 0;
337  }
338 
339 /* send the message to the server */
340 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
341  return static_cast<int>(sendto(m_socketFileDescriptor, msg, len, 0, (struct sockaddr *)&m_serverAddress,
342  m_serverLength));
343 #else
344  return sendto(m_socketFileDescriptor, (char *)msg, (int)len, 0, (struct sockaddr *)&m_serverAddress,
345  m_serverLength);
346 #endif
347 }
348 
349 #elif !defined(VISP_BUILD_SHARED_LIBS)
350 // Work arround to avoid warning: libvisp_core.a(vpUDPClient.cpp.o) has no symbols
351 void dummy_vpUDPClient(){};
352 #endif
void init(const std::string &hostname, int port)
error that can be emited by ViSP classes.
Definition: vpException.h:71
bool m_is_init
Definition: vpUDPClient.h:186
virtual ~vpUDPClient()
Definition: vpUDPClient.cpp:92
int receive(std::string &msg, int timeoutMs=0)
Used to indicate that a parameter is not initialized.
Definition: vpException.h:98
int send(const std::string &msg)