41 #include <visp3/core/vpConfig.h>
42 #include <visp3/core/vpRequest.h>
49 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
51 # include <sys/socket.h>
52 # include <netinet/in.h>
53 # include <arpa/inet.h>
84 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
88 SOCKET socketFileDescriptorReceptor;
89 int receptorAddressSize;
91 struct sockaddr_in receptorAddress;
94 vpReceptor() : socketFileDescriptorReceptor(0), receptorAddressSize(), receptorAddress(), receptorIP() {}
98 struct sockaddr_in emitterAddress;
99 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
102 SOCKET socketFileDescriptorEmitter;
104 vpEmitter() : emitterAddress(), socketFileDescriptorEmitter(0)
106 emitterAddress.sin_family = AF_INET;
107 emitterAddress.sin_addr.s_addr = INADDR_ANY;
108 emitterAddress.sin_port = 0;
109 socketFileDescriptorEmitter = 0;
120 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
145 std::vector<int> _handleRequests();
146 int _handleFirstRequest();
148 void _receiveRequest();
149 void _receiveRequestFrom(
const unsigned int &receptorEmitting);
150 int _receiveRequestOnce();
151 int _receiveRequestOnceFrom(
const unsigned int &receptorEmitting);
160 int getReceptorIndex(
const char *name);
170 if(ind >= (
int)request_list.size() || ind < 0)
172 return request_list[(unsigned)ind]->getId();
184 void print(
const char *
id =
"");
187 int receive(T*
object,
const unsigned int &sizeOfObject =
sizeof(T));
189 int receiveFrom(T*
object,
const unsigned int &receptorEmitting,
const unsigned int &sizeOfObject =
sizeof(T));
191 std::vector<int> receiveRequest();
192 std::vector<int> receiveRequestFrom(
const unsigned int &receptorEmitting);
193 int receiveRequestOnce();
194 int receiveRequestOnceFrom(
const unsigned int &receptorEmitting);
196 std::vector<int> receiveAndDecodeRequest();
197 std::vector<int> receiveAndDecodeRequestFrom(
const unsigned int &receptorEmitting);
198 int receiveAndDecodeRequestOnce();
199 int receiveAndDecodeRequestOnceFrom(
const unsigned int &receptorEmitting);
201 void removeDecodingRequest(
const char *);
204 int send(T*
object,
const int unsigned &sizeOfObject =
sizeof(T));
206 int sendTo(T*
object,
const unsigned int &dest,
const unsigned int &sizeOfObject =
sizeof(T));
209 int sendRequestTo(
vpRequest &req,
const unsigned int &dest);
211 int sendAndEncodeRequest(
vpRequest &req);
212 int sendAndEncodeRequestTo(
vpRequest &req,
const unsigned int &dest);
277 if(receptor_list.size() == 0)
287 FD_ZERO(&readFileDescriptor);
289 for(
unsigned int i=0; i<receptor_list.size(); i++){
290 FD_SET((
unsigned int)receptor_list[i].socketFileDescriptorReceptor,&readFileDescriptor);
293 socketMax = receptor_list[i].socketFileDescriptorReceptor;
295 if(socketMax < receptor_list[i].socketFileDescriptorReceptor) socketMax = receptor_list[i].socketFileDescriptorReceptor;
298 int value = select((
int)socketMax+1,&readFileDescriptor,NULL,NULL,&
tv);
311 for(
unsigned int i=0; i<receptor_list.size(); i++){
312 if(FD_ISSET((
unsigned int)receptor_list[i].socketFileDescriptorReceptor,&readFileDescriptor)){
313 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
314 numbytes = recv(receptor_list[i].socketFileDescriptorReceptor, (
char*)(
void*)
object, sizeOfObject, 0);
316 numbytes = recv((
unsigned int)receptor_list[i].socketFileDescriptorReceptor, (
char*)(
void*)
object, (
int)sizeOfObject, 0);
320 std::cout <<
"Disconnected : " << inet_ntoa(receptor_list[i].receptorAddress.sin_addr) << std::endl;
321 receptor_list.erase(receptor_list.begin()+(int)i);
357 if(receptor_list.size() == 0 || receptorEmitting > (
unsigned int)receptor_list.size()-1 )
360 vpTRACE(
"No receptor at the specified index" );
367 FD_ZERO(&readFileDescriptor);
369 socketMax = receptor_list[receptorEmitting].socketFileDescriptorReceptor;
370 FD_SET((
unsigned int)receptor_list[receptorEmitting].socketFileDescriptorReceptor,&readFileDescriptor);
372 int value = select((
int)socketMax+1,&readFileDescriptor,NULL,NULL,&
tv);
385 if(FD_ISSET((
unsigned int)receptor_list[receptorEmitting].socketFileDescriptorReceptor,&readFileDescriptor)){
386 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
387 numbytes = recv(receptor_list[receptorEmitting].socketFileDescriptorReceptor, (
char*)(
void*)
object, sizeOfObject, 0);
389 numbytes = recv((
unsigned int)receptor_list[receptorEmitting].socketFileDescriptorReceptor, (
char*)(
void*)
object, (
int)sizeOfObject, 0);
393 std::cout <<
"Disconnected : " << inet_ntoa(receptor_list[receptorEmitting].receptorAddress.sin_addr) << std::endl;
394 receptor_list.erase(receptor_list.begin()+(int)receptorEmitting);
426 if(receptor_list.size() == 0)
435 #if defined(__linux__)
436 flags = MSG_NOSIGNAL;
439 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
440 return sendto(receptor_list[0].socketFileDescriptorReceptor, (
const char*)(
void*)
object, sizeOfObject,
441 flags, (sockaddr*) &receptor_list[0].receptorAddress,receptor_list[0].receptorAddressSize);
443 return sendto(receptor_list[0].socketFileDescriptorReceptor, (
const char*)(
void*)
object, (
int)sizeOfObject,
444 flags, (sockaddr*) &receptor_list[0].receptorAddress,receptor_list[0].receptorAddressSize);
473 if(receptor_list.size() == 0 || dest > (
unsigned int)receptor_list.size()-1 )
476 vpTRACE(
"No receptor at the specified index." );
482 #if defined(__linux__)
483 flags = MSG_NOSIGNAL;
486 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
487 return sendto(receptor_list[dest].socketFileDescriptorReceptor, (
const char*)(
void*)
object, sizeOfObject,
488 flags, (sockaddr*) &receptor_list[dest].receptorAddress,receptor_list[dest].receptorAddressSize);
490 return sendto(receptor_list[dest].socketFileDescriptorReceptor, (
const char*)(
void*)
object, (
int)sizeOfObject,
491 flags, (sockaddr*) &receptor_list[dest].receptorAddress,receptor_list[dest].receptorAddressSize);
unsigned int max_size_message
void setMaxSizeReceivedMessage(const unsigned int &s)
This the request that will transit on the network.
std::vector< vpRequest * > request_list
socklen_t receptorAddressSize
std::string getRequestIdFromIndex(const int &ind)
fd_set readFileDescriptor
int socketFileDescriptorEmitter
This class represents a Transmission Control Protocol (TCP) network.
int socketFileDescriptorReceptor
int send(T *object, const int unsigned &sizeOfObject=sizeof(T))
unsigned int getMaxSizeReceivedMessage()
int receive(T *object, const unsigned int &sizeOfObject=sizeof(T))
void setTimeoutUSec(const long &usec)
std::vector< vpReceptor > receptor_list
void setVerbose(const bool &mode)
void setTimeoutSec(const long &sec)
std::string currentMessageReceived
int sendTo(T *object, const unsigned int &dest, const unsigned int &sizeOfObject=sizeof(T))
int receiveFrom(T *object, const unsigned int &receptorEmitting, const unsigned int &sizeOfObject=sizeof(T))