This file is indexed.

/usr/include/visp/vpNetwork.h is in libvisp-dev 2.8.0-4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/****************************************************************************
 *
 * $Id: vpNetwork.h 4135 2013-02-13 16:48:19Z fspindle $
 *
 * This file is part of the ViSP software.
 * Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
 * 
 * This software is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * ("GPL") version 2 as published by the Free Software Foundation.
 * See the file LICENSE.txt at the root directory of this source
 * distribution for additional information about the GNU GPL.
 *
 * For using ViSP with software that can not be combined with the GNU
 * GPL, please contact INRIA about acquiring a ViSP Professional 
 * Edition License.
 *
 * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
 * 
 * This software was developed at:
 * INRIA Rennes - Bretagne Atlantique
 * Campus Universitaire de Beaulieu
 * 35042 Rennes Cedex
 * France
 * http://www.irisa.fr/lagadic
 *
 * If you have questions regarding the use of this file, please contact
 * INRIA at visp@inria.fr
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * Description:
 * TCP Network
 *
 * Authors:
 * Aurelien Yol
 *
 *****************************************************************************/

#ifndef vpNetwork_H
#define vpNetwork_H

#include <visp/vpConfig.h>
#include <visp/vpRequest.h>

#include <vector>
#include <stdio.h>
#include <string.h>
#include <iostream>

#ifdef UNIX
#  include <unistd.h> 
#  include <sys/socket.h>
#  include <netinet/in.h>
#  include <arpa/inet.h>
#  include <netdb.h>
#else
#  include<io.h>
//#  include<winsock.h>
#  include<winsock2.h>
//#  pragma comment(lib, "ws2_32.lib") // Done by CMake in main CMakeLists.txt
#endif


/*! 
  \class vpNetwork
  
  \ingroup Network

  \brief This class represents a Transmission Control Protocol (TCP) network.
  
  TCP provides reliable, ordered delivery of a stream of bytes from a program 
  on one computer to another program on another computer.
  
  \warning This class shouldn't be used directly. You better use vpClient and
  vpServer to simulate your network. Some exemples are provided in these classes.

  \sa vpServer
  \sa vpNetwork
*/
class VISP_EXPORT vpNetwork
{
protected:

  struct vpReceptor{
#ifdef UNIX
    int                   socketFileDescriptorReceptor;
    socklen_t             receptorAddressSize;
#else
    SOCKET                socketFileDescriptorReceptor;
    int                   receptorAddressSize;
#endif
    struct sockaddr_in    receptorAddress;
    std::string           receptorIP;
  };
  
  struct vpEmitter{
    struct sockaddr_in    emitterAdress;
#ifdef UNIX
    int                   socketFileDescriptorEmitter;
#else
    SOCKET                socketFileDescriptorEmitter;
#endif
  };
  
  //######## PARAMETERS ########
  //#                          #
  //############################
  
  vpEmitter               emitter;
  std::vector<vpReceptor> receptor_list;
  fd_set                  readFileDescriptor;
#ifdef UNIX
  int                     socketMax;
#else
  SOCKET                  socketMax;
#endif
  
  //Message Handling 
  std::vector<vpRequest*> request_list;
  
  unsigned int            max_size_message;
  std::string             separator;
  std::string             beginning;
  std::string             end;
  std::string             param_sep;
  
  std::string             currentMessageReceived;
    
  struct timeval          tv;
  long                    tv_sec;
  long                    tv_usec;
  
  bool                    verboseMode;
  
private:
  
  std::vector<int>  _handleRequests();
  int               _handleFirstRequest();
  
  void              _receiveRequest();
  void              _receiveRequestFrom(const unsigned int &receptorEmitting);
  int               _receiveRequestOnce();
  int               _receiveRequestOnceFrom(const unsigned int &receptorEmitting);
  
public:

                    vpNetwork();
  virtual           ~vpNetwork();
  
  void              addDecodingRequest(vpRequest *);
  
  int               getReceptorIndex(const char *name);
  
  /*!
    Get the Id of the request at the index ind.

    \param ind : Index of the request.
    
    \return Id of the request.
  */
  std::string       getRequestIdFromIndex(const int &ind){ 
                        if(ind >= (int)request_list.size() || ind < 0)
                          return "";
                        return request_list[(unsigned)ind]->getId(); 
                    }
  
  /*!
    Get the maximum size that the emitter can receive (in request mode).
    
    \sa vpNetwork::setMaxSizeReceivedMessage()

    \return Acutal max size value.
  */
  unsigned int      getMaxSizeReceivedMessage(){ return max_size_message; }
  
  void      print(const char *id = "");
  
  template<typename T>
  int               receive(T* object, const unsigned int &sizeOfObject = sizeof(T));
  template<typename T>
  int               receiveFrom(T* object, const unsigned int &receptorEmitting, const unsigned int &sizeOfObject = sizeof(T));
  
  std::vector<int>  receiveRequest();
  std::vector<int>  receiveRequestFrom(const unsigned int &receptorEmitting);
  int               receiveRequestOnce();
  int               receiveRequestOnceFrom(const unsigned int &receptorEmitting);
  
  std::vector<int>  receiveAndDecodeRequest();
  std::vector<int>  receiveAndDecodeRequestFrom(const unsigned int &receptorEmitting);
  int               receiveAndDecodeRequestOnce();
  int               receiveAndDecodeRequestOnceFrom(const unsigned int &receptorEmitting);
  
  void              removeDecodingRequest(const char *);
  
  template<typename T>
  int               send(T* object, const int unsigned &sizeOfObject = sizeof(T));
  template<typename T>
  int               sendTo(T* object, const unsigned int &dest, const unsigned int &sizeOfObject = sizeof(T));
  
  int               sendRequest(vpRequest &req);
  int               sendRequestTo(vpRequest &req, const unsigned int &dest);
  
  int               sendAndEncodeRequest(vpRequest &req);
  int               sendAndEncodeRequestTo(vpRequest &req, const unsigned int &dest);
  
  /*!
    Change the maximum size that the emitter can receive (in request mode).
    
    \sa vpNetwork::getMaxSizeReceivedMessage()

    \param s : new maximum size value.
  */
  void              setMaxSizeReceivedMessage(const unsigned int &s){ max_size_message = s;}
  
  /*!
    Change the time the emitter spend to check if he receives a message from a receptor.
    Initially this value is set to 10usec.
    
    \sa vpNetwork::setTimeoutUSec()

    \param sec : new value in second.
  */
  void              setTimeoutSec(const long &sec){ tv_sec = sec; }
  
  /*!
    Change the time the emitter spend to check if he receives a message from a receptor.
    Initially this value is set to 10usec.
    
    \sa vpNetwork::setTimeoutSec()

    \param usec : new value in micro second.
  */
  void              setTimeoutUSec(const long &usec){ tv_usec = usec; }
  
  /*!
    Set the verbose mode.
    
    \param mode : Change the verbose mode. True to turn on, False to turn off.
  */
  void              setVerbose(const bool &mode){ verboseMode = mode; }
};

//######## Definition of Template Functions ########
//#                                                #
//##################################################

/*!
  Receives a object. The size of the received object is suppose to be the size of the type of the object.
  Note that a received message can correspond to a deconnection signal.
  
  \warning Using this function means that you know what kind of object you are suppose to receive, 
  and when you are suppose to receive.
  If the emitter has several receptors. It might be a problem, and in that case you better use the 
  "request" option.
  
  \sa vpNetwork::receiveRequest()
  \sa vpNetwork::receiveRequestOnce()  
  \sa vpNetwork::receiveAndDecodeRequest()
  \sa vpNetwork::receiveAndDecodeRequestOnce()

  \param object : Received object.
  \param sizeOfObject : Size of the received object.
  
  \return the number of bytes received, or -1 if an error occured.
*/
template<typename T>
int vpNetwork::receive(T* object, const unsigned int &sizeOfObject)
{
  if(receptor_list.size() == 0)
  {
    if(verboseMode)
      vpTRACE( "No receptor" );
    return -1;
  }
  
  tv.tv_sec = tv_sec;
  tv.tv_usec = tv_usec;
  
  FD_ZERO(&readFileDescriptor);        
  
  for(unsigned int i=0; i<receptor_list.size(); i++){
    FD_SET((unsigned int)receptor_list[i].socketFileDescriptorReceptor,&readFileDescriptor);

    if(i == 0)
      socketMax = receptor_list[i].socketFileDescriptorReceptor;
   
    if(socketMax < receptor_list[i].socketFileDescriptorReceptor) socketMax = receptor_list[i].socketFileDescriptorReceptor; 
  }

  int value = select((int)socketMax+1,&readFileDescriptor,NULL,NULL,&tv);
  int numbytes = 0;
  
  if(value == -1){
    if(verboseMode)
      vpERROR_TRACE( "Select error" );
    return -1;
  }
  else if(value == 0){
    //Timeout
    return 0;
  }
  else{
    for(unsigned int i=0; i<receptor_list.size(); i++){
      if(FD_ISSET((unsigned int)receptor_list[i].socketFileDescriptorReceptor,&readFileDescriptor)){
#ifdef UNIX
        numbytes = recv(receptor_list[i].socketFileDescriptorReceptor, (char*)(void*)object, sizeOfObject, 0);
#else
        numbytes = recv((unsigned int)receptor_list[i].socketFileDescriptorReceptor, (char*)(void*)object, (int)sizeOfObject, 0);
#endif
        if(numbytes <= 0)
        {
          std::cout << "Disconnected : " << inet_ntoa(receptor_list[i].receptorAddress.sin_addr) << std::endl;
          receptor_list.erase(receptor_list.begin()+(int)i);
          return numbytes;
        }
        
        break;
      }
    }
  }
  
  return numbytes;
}

/*!
  Receives a object from a receptor, by specifying its size or not.
  Note that a received message can correspond to a deconnection signal.
  
  \warning Using this function means that you know what kind of object you are suppose to receive, 
  and when you are suppose to receive.
  If the emitter has several receptors. It might be a problem, and in that case you better use the 
  "request" mode.
  
  \sa vpNetwork::getReceptorIndex()
  \sa vpNetwork::receiveRequestFrom()
  \sa vpNetwork::receiveRequestOnceFrom() 
  \sa vpNetwork::receiveAndDecodeRequestFrom()
  \sa vpNetwork::receiveAndDecodeRequestOnceFrom()

  \param object : Received object.
  \param receptorEmitting : Index of the receptor emitting the message.
  \param sizeOfObject : Size of the received object.
  
  \return the number of bytes received, or -1 if an error occured.
*/
template<typename T>
int vpNetwork::receiveFrom(T* object, const unsigned int &receptorEmitting, const unsigned int &sizeOfObject)
{
  if(receptor_list.size() == 0 || receptorEmitting > (int)receptor_list.size()-1 )
  {
    if(verboseMode)
      vpTRACE( "No receptor at the specified index" );
    return -1;
  }
  
  tv.tv_sec = tv_sec;
  tv.tv_usec = tv_usec;
  
  FD_ZERO(&readFileDescriptor);
  
  socketMax = receptor_list[receptorEmitting].socketFileDescriptorReceptor;
  FD_SET((unsigned int)receptor_list[receptorEmitting].socketFileDescriptorReceptor,&readFileDescriptor);
    
  int value = select((int)socketMax+1,&readFileDescriptor,NULL,NULL,&tv);
  int numbytes = 0;
  
  if(value == -1){
    if(verboseMode)
      vpERROR_TRACE( "Select error" );
    return -1;
  }
  else if(value == 0){
    //timeout
    return 0;
  }
  else{
    if(FD_ISSET((unsigned int)receptor_list[receptorEmitting].socketFileDescriptorReceptor,&readFileDescriptor)){
#ifdef UNIX
      numbytes = recv(receptor_list[receptorEmitting].socketFileDescriptorReceptor, (char*)(void*)object, sizeOfObject, 0);
#else
      numbytes = recv((unsigned int)receptor_list[receptorEmitting].socketFileDescriptorReceptor, (char*)(void*)object, (int)sizeOfObject, 0);
#endif
      if(numbytes <= 0)
      {
        std::cout << "Disconnected : " << inet_ntoa(receptor_list[receptorEmitting].receptorAddress.sin_addr) << std::endl;
        receptor_list.erase(receptor_list.begin()+(int)receptorEmitting);
        return numbytes;
      }
    }
  }
  
  return numbytes;
}

/*!
  Send an object. The size of the received object is suppose to be the size of its type.
  Note that sending object containing pointers, virtual methods, etc, won't probably work.
  
  \warning Using this function means that, in the other side of the network, it knows what kind of object it is suppose to receive, 
  and when it is suppose to receive.
  If the emitter has several receptors. It might be a problem, and in that case you better use the 
  "request" option.
  
  \sa vpNetwork::sendTo()
  \sa vpNetwork::sendRequest()
  \sa vpNetwork::sendRequestTo()
  \sa vpNetwork::sendAndEncodeRequest()
  \sa vpNetwork::sendAndEncodeRequestTo()

  \param object : Received object.
  \param sizeOfObject : Size of the object
  
  \return The number of bytes sent, or -1 if an error happened.
*/
template<typename T>
int vpNetwork::send(T* object, const unsigned int &sizeOfObject)
{
  if(receptor_list.size() == 0)
  {
    if(verboseMode)
      vpTRACE( "No receptor !" );
    return 0;
  }
  
  int flags = 0;
#if ! defined(APPLE) && ! defined(WIN32)
  flags = MSG_NOSIGNAL; // Only for Linux
#endif

#ifdef UNIX
  return sendto(receptor_list[0].socketFileDescriptorReceptor, (const char*)(void*)object, sizeOfObject, 
                flags, (sockaddr*) &receptor_list[0].receptorAddress,receptor_list[0].receptorAddressSize);
#else
  return sendto(receptor_list[0].socketFileDescriptorReceptor, (const char*)(void*)object, (int)sizeOfObject, 
                flags, (sockaddr*) &receptor_list[0].receptorAddress,receptor_list[0].receptorAddressSize);
#endif
  
}

/*!
  Send an object. The size has to be specified.
  
  \warning Using this function means that, in the other side of the network, it knows what kind of object it is suppose to receive, 
  and when it is suppose to receive.
  If the emitter has several receptors. It might be a problem, and in that case you better use the 
  "request" option.
  
  \sa vpNetwork::getReceptorIndex()
  \sa vpNetwork::send()
  \sa vpNetwork::sendRequest()
  \sa vpNetwork::sendRequestTo()
  \sa vpNetwork::sendAndEncodeRequest()
  \sa vpNetwork::sendAndEncodeRequestTo()

  \param object : Object to send.
  \param dest : Index of the receptor that you are sending the object.
  \param sizeOfObject : Size of the object.
  
  \return The number of bytes sent, or -1 if an error happened.
*/
template<typename T>
int vpNetwork::sendTo(T* object, const unsigned int &dest, const unsigned int &sizeOfObject)
{
  if(receptor_list.size() == 0 || dest > (int)receptor_list.size()-1 )
  {
    if(verboseMode)
      vpTRACE( "No receptor at the specified index." );
    return 0;
  }
  
  int flags = 0;
#if ! defined(APPLE) && ! defined(WIN32)
  flags = MSG_NOSIGNAL; // Only for Linux
#endif

#ifdef UNIX
  return sendto(receptor_list[dest].socketFileDescriptorReceptor, (const char*)(void*)object, sizeOfObject, 
                flags, (sockaddr*) &receptor_list[dest].receptorAddress,receptor_list[dest].receptorAddressSize);
#else
  return sendto(receptor_list[dest].socketFileDescriptorReceptor, (const char*)(void*)object, (int)sizeOfObject, 
                flags, (sockaddr*) &receptor_list[dest].receptorAddress,receptor_list[dest].receptorAddressSize);
#endif
}

#endif