42 #include <visp3/core/vpConfig.h> 43 #include <visp3/core/vpRequest.h> 49 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 50 #include <sys/select.h> 54 #ifdef VISP_HAVE_FUNC_INET_NTOP 56 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 57 # include <arpa/inet.h> 59 # include <netinet/in.h> 60 # include <sys/socket.h> 65 # include <winsock2.h> 69 #if defined(__APPLE__) && defined(__MACH__) // Apple OSX and iOS (Darwin) 70 # include <TargetConditionals.h> 93 #ifndef DOXYGEN_SHOULD_SKIP_THIS 95 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 96 int socketFileDescriptorReceptor;
97 socklen_t receptorAddressSize;
99 SOCKET socketFileDescriptorReceptor;
100 int receptorAddressSize;
102 struct sockaddr_in receptorAddress;
103 std::string receptorIP;
105 vpReceptor() : socketFileDescriptorReceptor(0), receptorAddressSize(), receptorAddress(), receptorIP() {}
109 struct sockaddr_in emitterAddress;
110 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 111 int socketFileDescriptorEmitter;
113 SOCKET socketFileDescriptorEmitter;
115 vpEmitter() : emitterAddress(), socketFileDescriptorEmitter(0)
117 emitterAddress.sin_family = AF_INET;
118 emitterAddress.sin_addr.s_addr = INADDR_ANY;
119 emitterAddress.sin_port = 0;
120 socketFileDescriptorEmitter = 0;
132 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 156 std::vector<int> _handleRequests();
157 int _handleFirstRequest();
159 void _receiveRequest();
160 void _receiveRequestFrom(
const unsigned int &receptorEmitting);
161 int _receiveRequestOnce();
162 int _receiveRequestOnceFrom(
const unsigned int &receptorEmitting);
170 int getReceptorIndex(
const char *name);
181 if (ind >= (
int)request_list.size() || ind < 0)
183 return request_list[(unsigned)ind]->getId();
195 void print(
const char *
id =
"");
197 template <
typename T>
int receive(T *
object,
const unsigned int &sizeOfObject =
sizeof(T));
198 template <
typename T>
199 int receiveFrom(T *
object,
const unsigned int &receptorEmitting,
const unsigned int &sizeOfObject =
sizeof(T));
201 std::vector<int> receiveRequest();
202 std::vector<int> receiveRequestFrom(
const unsigned int &receptorEmitting);
203 int receiveRequestOnce();
204 int receiveRequestOnceFrom(
const unsigned int &receptorEmitting);
206 std::vector<int> receiveAndDecodeRequest();
207 std::vector<int> receiveAndDecodeRequestFrom(
const unsigned int &receptorEmitting);
208 int receiveAndDecodeRequestOnce();
209 int receiveAndDecodeRequestOnceFrom(
const unsigned int &receptorEmitting);
211 void removeDecodingRequest(
const char *);
213 template <
typename T>
int send(T *
object,
const int unsigned &sizeOfObject =
sizeof(T));
214 template <
typename T>
int sendTo(T *
object,
const unsigned int &dest,
const unsigned int &sizeOfObject =
sizeof(T));
217 int sendRequestTo(
vpRequest &req,
const unsigned int &dest);
219 int sendAndEncodeRequest(
vpRequest &req);
220 int sendAndEncodeRequestTo(
vpRequest &req,
const unsigned int &dest);
285 if (receptor_list.size() == 0) {
293 tv.tv_usec = (int)tv_usec;
295 tv.tv_usec = tv_usec;
298 FD_ZERO(&readFileDescriptor);
300 for (
unsigned int i = 0; i < receptor_list.size(); i++) {
301 FD_SET((
unsigned int)receptor_list[i].socketFileDescriptorReceptor, &readFileDescriptor);
304 socketMax = receptor_list[i].socketFileDescriptorReceptor;
306 if (socketMax < receptor_list[i].socketFileDescriptorReceptor)
307 socketMax = receptor_list[i].socketFileDescriptorReceptor;
310 int value = select((
int)socketMax + 1, &readFileDescriptor, NULL, NULL, &tv);
317 }
else if (value == 0) {
321 for (
unsigned int i = 0; i < receptor_list.size(); i++) {
322 if (FD_ISSET((
unsigned int)receptor_list[i].socketFileDescriptorReceptor, &readFileDescriptor)) {
323 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 324 numbytes = recv(receptor_list[i].socketFileDescriptorReceptor, (
char *)(
void *)
object, sizeOfObject, 0);
326 numbytes = recv((
unsigned int)receptor_list[i].socketFileDescriptorReceptor, (
char *)(
void *)
object,
327 (
int)sizeOfObject, 0);
330 std::cout <<
"Disconnected : " << inet_ntoa(receptor_list[i].receptorAddress.sin_addr) << std::endl;
331 receptor_list.erase(receptor_list.begin() + (int)i);
364 template <
typename T>
367 if (receptor_list.size() == 0 || receptorEmitting > (
unsigned int)receptor_list.size() - 1) {
369 vpTRACE(
"No receptor at the specified index");
375 tv.tv_usec = (int)tv_usec;
377 tv.tv_usec = tv_usec;
380 FD_ZERO(&readFileDescriptor);
382 socketMax = receptor_list[receptorEmitting].socketFileDescriptorReceptor;
383 FD_SET((
unsigned int)receptor_list[receptorEmitting].socketFileDescriptorReceptor, &readFileDescriptor);
385 int value = select((
int)socketMax + 1, &readFileDescriptor, NULL, NULL, &tv);
392 }
else if (value == 0) {
396 if (FD_ISSET((
unsigned int)receptor_list[receptorEmitting].socketFileDescriptorReceptor, &readFileDescriptor)) {
397 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 399 recv(receptor_list[receptorEmitting].socketFileDescriptorReceptor, (
char *)(
void *)
object, sizeOfObject, 0);
401 numbytes = recv((
unsigned int)receptor_list[receptorEmitting].socketFileDescriptorReceptor,
402 (
char *)(
void *)
object, (
int)sizeOfObject, 0);
405 std::cout <<
"Disconnected : " << inet_ntoa(receptor_list[receptorEmitting].receptorAddress.sin_addr)
407 receptor_list.erase(receptor_list.begin() + (int)receptorEmitting);
437 template <
typename T>
int vpNetwork::send(T *
object,
const unsigned int &sizeOfObject)
439 if (receptor_list.size() == 0) {
447 #if defined(__linux__) 448 flags = MSG_NOSIGNAL;
451 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 452 return sendto(receptor_list[0].socketFileDescriptorReceptor, (
const char *)(
void *)
object, sizeOfObject, flags,
453 (sockaddr *)&receptor_list[0].receptorAddress, receptor_list[0].receptorAddressSize);
455 return sendto(receptor_list[0].socketFileDescriptorReceptor, (
const char *)(
void *)
object, (
int)sizeOfObject, flags,
456 (sockaddr *)&receptor_list[0].receptorAddress, receptor_list[0].receptorAddressSize);
481 template <
typename T>
int vpNetwork::sendTo(T *
object,
const unsigned int &dest,
const unsigned int &sizeOfObject)
483 if (receptor_list.size() == 0 || dest > (
unsigned int)receptor_list.size() - 1) {
485 vpTRACE(
"No receptor at the specified index.");
491 #if defined(__linux__) 492 flags = MSG_NOSIGNAL;
495 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 496 return sendto(receptor_list[dest].socketFileDescriptorReceptor, (
const char *)(
void *)
object, sizeOfObject, flags,
497 (sockaddr *)&receptor_list[dest].receptorAddress, receptor_list[dest].receptorAddressSize);
499 return sendto(receptor_list[dest].socketFileDescriptorReceptor, (
const char *)(
void *)
object, (
int)sizeOfObject,
500 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::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()
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))
std::vector< vpRequest * > request_list
int receiveFrom(T *object, const unsigned int &receptorEmitting, const unsigned int &sizeOfObject=sizeof(T))