45 #include <visp/vpConfig.h>
46 #include <visp/vpRequest.h>
53 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
55 # include <sys/socket.h>
56 # include <netinet/in.h>
57 # include <arpa/inet.h>
88 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
89 int socketFileDescriptorReceptor;
90 socklen_t receptorAddressSize;
95 struct sockaddr_in receptorAddress;
98 vpReceptor() : socketFileDescriptorReceptor(0), receptorAddressSize(), receptorAddress(), receptorIP() {}
102 struct sockaddr_in emitterAddress;
103 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
104 int socketFileDescriptorEmitter;
108 vpEmitter() : emitterAddress(), socketFileDescriptorEmitter(0)
110 emitterAddress.sin_family = AF_INET;
111 emitterAddress.sin_addr.s_addr = INADDR_ANY;
112 emitterAddress.sin_port = 0;
113 socketFileDescriptorEmitter = 0;
124 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
149 std::vector<int> _handleRequests();
150 int _handleFirstRequest();
152 void _receiveRequest();
153 void _receiveRequestFrom(
const unsigned int &receptorEmitting);
154 int _receiveRequestOnce();
155 int _receiveRequestOnceFrom(
const unsigned int &receptorEmitting);
164 int getReceptorIndex(
const char *name);
174 if(ind >= (
int)request_list.size() || ind < 0)
176 return request_list[(unsigned)ind]->getId();
188 void print(
const char *
id =
"");
191 int receive(T*
object,
const unsigned int &sizeOfObject =
sizeof(T));
193 int receiveFrom(T*
object,
const unsigned int &receptorEmitting,
const unsigned int &sizeOfObject =
sizeof(T));
195 std::vector<int> receiveRequest();
196 std::vector<int> receiveRequestFrom(
const unsigned int &receptorEmitting);
197 int receiveRequestOnce();
198 int receiveRequestOnceFrom(
const unsigned int &receptorEmitting);
200 std::vector<int> receiveAndDecodeRequest();
201 std::vector<int> receiveAndDecodeRequestFrom(
const unsigned int &receptorEmitting);
202 int receiveAndDecodeRequestOnce();
203 int receiveAndDecodeRequestOnceFrom(
const unsigned int &receptorEmitting);
205 void removeDecodingRequest(
const char *);
208 int send(T*
object,
const int unsigned &sizeOfObject =
sizeof(T));
210 int sendTo(T*
object,
const unsigned int &dest,
const unsigned int &sizeOfObject =
sizeof(T));
213 int sendRequestTo(
vpRequest &req,
const unsigned int &dest);
215 int sendAndEncodeRequest(
vpRequest &req);
216 int sendAndEncodeRequestTo(
vpRequest &req,
const unsigned int &dest);
281 if(receptor_list.size() == 0)
291 FD_ZERO(&readFileDescriptor);
293 for(
unsigned int i=0; i<receptor_list.size(); i++){
294 FD_SET((
unsigned int)receptor_list[i].socketFileDescriptorReceptor,&readFileDescriptor);
297 socketMax = receptor_list[i].socketFileDescriptorReceptor;
299 if(socketMax < receptor_list[i].socketFileDescriptorReceptor) socketMax = receptor_list[i].socketFileDescriptorReceptor;
302 int value = select((
int)socketMax+1,&readFileDescriptor,NULL,NULL,&
tv);
315 for(
unsigned int i=0; i<receptor_list.size(); i++){
316 if(FD_ISSET((
unsigned int)receptor_list[i].socketFileDescriptorReceptor,&readFileDescriptor)){
317 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
318 numbytes = recv(receptor_list[i].socketFileDescriptorReceptor, (
char*)(
void*)
object, sizeOfObject, 0);
320 numbytes = recv((
unsigned int)receptor_list[i].socketFileDescriptorReceptor, (
char*)(
void*)
object, (
int)sizeOfObject, 0);
324 std::cout <<
"Disconnected : " << inet_ntoa(receptor_list[i].receptorAddress.sin_addr) << std::endl;
325 receptor_list.erase(receptor_list.begin()+(int)i);
361 if(receptor_list.size() == 0 || receptorEmitting > (int)receptor_list.size()-1 )
364 vpTRACE(
"No receptor at the specified index" );
371 FD_ZERO(&readFileDescriptor);
373 socketMax = receptor_list[receptorEmitting].socketFileDescriptorReceptor;
374 FD_SET((
unsigned int)receptor_list[receptorEmitting].socketFileDescriptorReceptor,&readFileDescriptor);
376 int value = select((
int)socketMax+1,&readFileDescriptor,NULL,NULL,&
tv);
389 if(FD_ISSET((
unsigned int)receptor_list[receptorEmitting].socketFileDescriptorReceptor,&readFileDescriptor)){
390 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
391 numbytes = recv(receptor_list[receptorEmitting].socketFileDescriptorReceptor, (
char*)(
void*)
object, sizeOfObject, 0);
393 numbytes = recv((
unsigned int)receptor_list[receptorEmitting].socketFileDescriptorReceptor, (
char*)(
void*)
object, (
int)sizeOfObject, 0);
397 std::cout <<
"Disconnected : " << inet_ntoa(receptor_list[receptorEmitting].receptorAddress.sin_addr) << std::endl;
398 receptor_list.erase(receptor_list.begin()+(int)receptorEmitting);
430 if(receptor_list.size() == 0)
439 #if defined(__linux__)
440 flags = MSG_NOSIGNAL;
443 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
444 return sendto(receptor_list[0].socketFileDescriptorReceptor, (
const char*)(
void*)
object, sizeOfObject,
445 flags, (sockaddr*) &receptor_list[0].receptorAddress,receptor_list[0].receptorAddressSize);
447 return sendto(receptor_list[0].socketFileDescriptorReceptor, (
const char*)(
void*)
object, (
int)sizeOfObject,
448 flags, (sockaddr*) &receptor_list[0].receptorAddress,receptor_list[0].receptorAddressSize);
477 if(receptor_list.size() == 0 || dest > (int)receptor_list.size()-1 )
480 vpTRACE(
"No receptor at the specified index." );
486 #if defined(__linux__)
487 flags = MSG_NOSIGNAL;
490 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
491 return sendto(receptor_list[dest].socketFileDescriptorReceptor, (
const char*)(
void*)
object, sizeOfObject,
492 flags, (sockaddr*) &receptor_list[dest].receptorAddress,receptor_list[dest].receptorAddressSize);
494 return sendto(receptor_list[dest].socketFileDescriptorReceptor, (
const char*)(
void*)
object, (
int)sizeOfObject,
495 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
std::string getRequestIdFromIndex(const int &ind)
fd_set readFileDescriptor
This class represents a Transmission Control Protocol (TCP) network.
int send(T *object, const int unsigned &sizeOfObject=sizeof(T))
unsigned int getMaxSizeReceivedMessage()
SOCKET socketFileDescriptorEmitter
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)
SOCKET socketFileDescriptorReceptor
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))