Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpServer.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * TCP Server
32  *
33  * Authors:
34  * Aurelien Yol
35  *
36  *****************************************************************************/
37 
38 #include <visp3/core/vpServer.h>
39 
40 #if defined(__APPLE__) && defined(__MACH__) // Apple OSX and iOS (Darwin)
41 # include <TargetConditionals.h> // To detect OSX or IOS using TARGET_OS_IPHONE or TARGET_OS_IOS macro
42 #endif
43 
47 vpServer::vpServer( ) : adress(), port(0), started(false), max_clients(10)
48 {
49  int protocol = 0;
50  emitter.socketFileDescriptorEmitter = socket(AF_INET, SOCK_STREAM, protocol);
51 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
52  if (emitter.socketFileDescriptorEmitter < 0)
53 #else
54  if (emitter.socketFileDescriptorEmitter == INVALID_SOCKET)
55 #endif
56  {
57  vpERROR_TRACE( "vpServer::vpServer(), cannot open socket." );
58  }
59  emitter.emitterAddress.sin_family = AF_INET;
60  emitter.emitterAddress.sin_addr.s_addr = INADDR_ANY;
61  emitter.emitterAddress.sin_port = 0;
62 
63  adress = inet_ntoa(emitter.emitterAddress.sin_addr);
64  port = emitter.emitterAddress.sin_port;
65 }
66 
72 vpServer::vpServer( const int &port_serv ) : adress(), port(0), started(false), max_clients(10)
73 {
74  int protocol = 0;
75  emitter.socketFileDescriptorEmitter = socket(AF_INET, SOCK_STREAM, protocol);
76 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
77  if (emitter.socketFileDescriptorEmitter < 0)
78 #else
79  if (emitter.socketFileDescriptorEmitter == INVALID_SOCKET)
80 #endif
81  {
82  vpERROR_TRACE( "vpServer::vpServer(const int &port_serv), cannot open socket." );
83  }
84  emitter.emitterAddress.sin_family = AF_INET;
85  emitter.emitterAddress.sin_addr.s_addr = INADDR_ANY; //inet_addr("127.0.0.1");;
86  emitter.emitterAddress.sin_port = htons( (unsigned short)port_serv );
87 
88  adress = inet_ntoa(emitter.emitterAddress.sin_addr);
89  port = port_serv;
90 }
91 
98 vpServer::vpServer( const std::string &adress_serv,const int &port_serv )
99  : adress(), port(0), started(false), max_clients(10)
100 {
101  int protocol = 0;
102  emitter.socketFileDescriptorEmitter = socket(AF_INET, SOCK_STREAM, protocol);
103 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
104  if (emitter.socketFileDescriptorEmitter < 0)
105 #else
106  if (emitter.socketFileDescriptorEmitter == INVALID_SOCKET)
107 #endif
108  {
109  vpERROR_TRACE( "vpServer::vpServer(const std::string &adress_serv,const int &port_serv), cannot open socket." );
110  }
111  emitter.emitterAddress.sin_family = AF_INET;
112  emitter.emitterAddress.sin_addr.s_addr = inet_addr(adress_serv.c_str());
113  emitter.emitterAddress.sin_port = htons( (unsigned short)port_serv );
114 
115  adress = adress_serv;
116  port = port_serv;
117 }
118 
123 {
124 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
125  close( emitter.socketFileDescriptorEmitter );
126 #else //Win32
127  closesocket( (unsigned)emitter.socketFileDescriptorEmitter );
128 #endif
129 
130  for(unsigned int i = 0 ; i < receptor_list.size() ; i++)
131 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
132  close( receptor_list[i].socketFileDescriptorReceptor );
133 #else //Win32
134  closesocket( (unsigned)receptor_list[i].socketFileDescriptorReceptor );
135 #endif
136 
137 }
138 
145 {
146  int serverStructLength = sizeof(emitter.emitterAddress);
147 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
148  int bindResult = bind( emitter.socketFileDescriptorEmitter, (struct sockaddr *) &emitter.emitterAddress, (unsigned)serverStructLength );
149 #else //Win32
150  int bindResult = bind( (unsigned)emitter.socketFileDescriptorEmitter, (struct sockaddr *) &emitter.emitterAddress, serverStructLength );
151 #endif
152 
153 
154  if( bindResult < 0 )
155  {
156  std::cout << "Error id : " << bindResult << std::endl;
157  std::string errorMessage( "vpServer::vpServer(), cannot bind to port" );
158  char posrtNumberString[32];
159  sprintf( posrtNumberString, "%d", port );
160  errorMessage += " ";
161  errorMessage += posrtNumberString;
162  errorMessage += " The port may be already used.";
163  vpERROR_TRACE( errorMessage.c_str() );
164  return false;
165  }
166 
167 #ifdef SO_NOSIGPIPE
168  // Mac OS X does not have the MSG_NOSIGNAL flag. It does have this
169  // connections based version, however.
170  if (emitter.socketFileDescriptorEmitter > 0) {
171  int set_option = 1;
172  if (0 == setsockopt(emitter.socketFileDescriptorEmitter, SOL_SOCKET, SO_NOSIGPIPE, &set_option, sizeof(set_option))) {
173  } else {
174  std::cout << "Failed to set socket signal option" << std::endl;
175  }
176  }
177 #endif // SO_NOSIGPIPE
178 
179 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
180  listen( emitter.socketFileDescriptorEmitter, (int)max_clients );
181 #else //Win32
182  listen( (unsigned)emitter.socketFileDescriptorEmitter, (int)max_clients );
183 #endif
184 
185  std::cout << "Server ready" << std::endl;
186 
187  started = true;
188 
189  return true;
190 }
191 
198 {
199  if(!started)
200  if(!start()){
201  return false;
202  }
203 
204  tv.tv_sec = tv_sec;
205 #if TARGET_OS_IPHONE
206  tv.tv_usec = (int)tv_usec;
207 #else
208  tv.tv_usec = tv_usec;
209 #endif
210 
211  FD_ZERO(&readFileDescriptor);
212 
213  socketMax = emitter.socketFileDescriptorEmitter;
214  FD_SET((unsigned)emitter.socketFileDescriptorEmitter,&readFileDescriptor);
215 
216  for(unsigned int i=0; i<receptor_list.size(); i++){
217  FD_SET((unsigned)receptor_list[i].socketFileDescriptorReceptor,&readFileDescriptor);
218 
219  if(i == 0)
220  socketMax = receptor_list[i].socketFileDescriptorReceptor;
221 
222  if(socketMax < receptor_list[i].socketFileDescriptorReceptor) socketMax = receptor_list[i].socketFileDescriptorReceptor;
223  }
224 
225  int value = select((int)socketMax+1,&readFileDescriptor,NULL,NULL,&tv);
226  if(value == -1){
227  //vpERROR_TRACE( "vpServer::run(), select()" );
228  return false;
229  }
230  else if(value == 0){
231  return false;
232  }
233  else{
234  if(FD_ISSET((unsigned int)emitter.socketFileDescriptorEmitter,&readFileDescriptor)){
235  vpNetwork::vpReceptor client;
236  client.receptorAddressSize = sizeof(client.receptorAddress);
237 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
238  client.socketFileDescriptorReceptor = accept(emitter.socketFileDescriptorEmitter,(struct sockaddr*) &client.receptorAddress, &client.receptorAddressSize);
239 #else //Win32
240  client.socketFileDescriptorReceptor = accept((unsigned int)emitter.socketFileDescriptorEmitter,(struct sockaddr*) &client.receptorAddress, &client.receptorAddressSize);
241 #endif
242 
243 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
244  if((client.socketFileDescriptorReceptor) == -1)
245 #else
246  if((client.socketFileDescriptorReceptor) == INVALID_SOCKET)
247 #endif
248  vpERROR_TRACE( "vpServer::run(), accept()" );
249 
250  client.receptorIP = inet_ntoa(client.receptorAddress.sin_addr);
251  printf("New client connected : %s\n", inet_ntoa(client.receptorAddress.sin_addr));
252  receptor_list.push_back(client);
253 
254  return true;
255  }
256  else{
257  for(unsigned int i=0; i<receptor_list.size(); i++){
258  if(FD_ISSET((unsigned int)receptor_list[i].socketFileDescriptorReceptor,&readFileDescriptor)){
259  char deco;
260 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
261  ssize_t numbytes = recv(receptor_list[i].socketFileDescriptorReceptor, &deco, 1, MSG_PEEK);
262 #else //Win32
263  int numbytes = recv((unsigned int)receptor_list[i].socketFileDescriptorReceptor, &deco, 1, MSG_PEEK);
264 #endif
265 
266  if(numbytes == 0)
267  {
268  std::cout << "Disconnected : " << inet_ntoa(receptor_list[i].receptorAddress.sin_addr) << std::endl;
269  receptor_list.erase(receptor_list.begin()+(int)i);
270  return 0;
271  }
272  }
273  }
274  }
275  }
276 
277  return false;
278 }
279 
284 {
285  vpNetwork::print("Client");
286 }
287 
fd_set readFileDescriptor
Definition: vpNetwork.h:123
void print(const char *id="")
Definition: vpNetwork.cpp:121
#define vpERROR_TRACE
Definition: vpDebug.h:391
bool checkForConnections()
Definition: vpServer.cpp:197
bool start()
Definition: vpServer.cpp:144
vpServer()
Definition: vpServer.cpp:47
int socketMax
Definition: vpNetwork.h:125
vpEmitter emitter
Definition: vpNetwork.h:121
long tv_sec
Definition: vpNetwork.h:142
virtual ~vpServer()
Definition: vpServer.cpp:122
struct timeval tv
Definition: vpNetwork.h:141
std::vector< vpReceptor > receptor_list
Definition: vpNetwork.h:122
long tv_usec
Definition: vpNetwork.h:143
void print()
Definition: vpServer.cpp:283