42 #include <visp3/core/vpConfig.h> 43 #include <visp3/core/vpRequest.h> 51 #ifdef VISP_HAVE_FUNC_INET_NTOP 53 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 54 # include <arpa/inet.h> 56 # include <netinet/in.h> 57 # include <sys/socket.h> 62 # include <winsock2.h> 66 #if defined(__APPLE__) && defined(__MACH__) // Apple OSX and iOS (Darwin) 67 # include <TargetConditionals.h> 90 #ifndef DOXYGEN_SHOULD_SKIP_THIS 92 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 93 int socketFileDescriptorReceptor;
94 socklen_t receptorAddressSize;
96 SOCKET socketFileDescriptorReceptor;
97 int receptorAddressSize;
99 struct sockaddr_in receptorAddress;
100 std::string receptorIP;
102 vpReceptor() : socketFileDescriptorReceptor(0), receptorAddressSize(), receptorAddress(), receptorIP() {}
106 struct sockaddr_in emitterAddress;
107 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 108 int socketFileDescriptorEmitter;
110 SOCKET socketFileDescriptorEmitter;
112 vpEmitter() : emitterAddress(), socketFileDescriptorEmitter(0)
114 emitterAddress.sin_family = AF_INET;
115 emitterAddress.sin_addr.s_addr = INADDR_ANY;
116 emitterAddress.sin_port = 0;
117 socketFileDescriptorEmitter = 0;
129 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 153 std::vector<int> _handleRequests();
154 int _handleFirstRequest();
156 void _receiveRequest();
157 void _receiveRequestFrom(
const unsigned int &receptorEmitting);
158 int _receiveRequestOnce();
159 int _receiveRequestOnceFrom(
const unsigned int &receptorEmitting);
167 int getReceptorIndex(
const char *name);
178 if (ind >= (
int)request_list.size() || ind < 0)
180 return request_list[(unsigned)ind]->getId();
192 void print(
const char *
id =
"");
194 template <
typename T>
int receive(T *
object,
const unsigned int &sizeOfObject =
sizeof(T));
195 template <
typename T>
196 int receiveFrom(T *
object,
const unsigned int &receptorEmitting,
const unsigned int &sizeOfObject =
sizeof(T));
198 std::vector<int> receiveRequest();
199 std::vector<int> receiveRequestFrom(
const unsigned int &receptorEmitting);
200 int receiveRequestOnce();
201 int receiveRequestOnceFrom(
const unsigned int &receptorEmitting);
203 std::vector<int> receiveAndDecodeRequest();
204 std::vector<int> receiveAndDecodeRequestFrom(
const unsigned int &receptorEmitting);
205 int receiveAndDecodeRequestOnce();
206 int receiveAndDecodeRequestOnceFrom(
const unsigned int &receptorEmitting);
208 void removeDecodingRequest(
const char *);
210 template <
typename T>
int send(T *
object,
const int unsigned &sizeOfObject =
sizeof(T));
211 template <
typename T>
int sendTo(T *
object,
const unsigned int &dest,
const unsigned int &sizeOfObject =
sizeof(T));
214 int sendRequestTo(
vpRequest &req,
const unsigned int &dest);
216 int sendAndEncodeRequest(
vpRequest &req);
217 int sendAndEncodeRequestTo(
vpRequest &req,
const unsigned int &dest);
282 if (receptor_list.size() == 0) {
290 tv.tv_usec = (int)tv_usec;
292 tv.tv_usec = tv_usec;
295 FD_ZERO(&readFileDescriptor);
297 for (
unsigned int i = 0; i < receptor_list.size(); i++) {
298 FD_SET((
unsigned int)receptor_list[i].socketFileDescriptorReceptor, &readFileDescriptor);
301 socketMax = receptor_list[i].socketFileDescriptorReceptor;
303 if (socketMax < receptor_list[i].socketFileDescriptorReceptor)
304 socketMax = receptor_list[i].socketFileDescriptorReceptor;
307 int value = select((
int)socketMax + 1, &readFileDescriptor, NULL, NULL, &tv);
314 }
else if (value == 0) {
318 for (
unsigned int i = 0; i < receptor_list.size(); i++) {
319 if (FD_ISSET((
unsigned int)receptor_list[i].socketFileDescriptorReceptor, &readFileDescriptor)) {
320 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 321 numbytes = recv(receptor_list[i].socketFileDescriptorReceptor, (
char *)(
void *)
object, sizeOfObject, 0);
323 numbytes = recv((
unsigned int)receptor_list[i].socketFileDescriptorReceptor, (
char *)(
void *)
object,
324 (
int)sizeOfObject, 0);
327 std::cout <<
"Disconnected : " << inet_ntoa(receptor_list[i].receptorAddress.sin_addr) << std::endl;
328 receptor_list.erase(receptor_list.begin() + (int)i);
361 template <
typename T>
364 if (receptor_list.size() == 0 || receptorEmitting > (
unsigned int)receptor_list.size() - 1) {
366 vpTRACE(
"No receptor at the specified index");
372 tv.tv_usec = (int)tv_usec;
374 tv.tv_usec = tv_usec;
377 FD_ZERO(&readFileDescriptor);
379 socketMax = receptor_list[receptorEmitting].socketFileDescriptorReceptor;
380 FD_SET((
unsigned int)receptor_list[receptorEmitting].socketFileDescriptorReceptor, &readFileDescriptor);
382 int value = select((
int)socketMax + 1, &readFileDescriptor, NULL, NULL, &tv);
389 }
else if (value == 0) {
393 if (FD_ISSET((
unsigned int)receptor_list[receptorEmitting].socketFileDescriptorReceptor, &readFileDescriptor)) {
394 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 396 recv(receptor_list[receptorEmitting].socketFileDescriptorReceptor, (
char *)(
void *)
object, sizeOfObject, 0);
398 numbytes = recv((
unsigned int)receptor_list[receptorEmitting].socketFileDescriptorReceptor,
399 (
char *)(
void *)
object, (
int)sizeOfObject, 0);
402 std::cout <<
"Disconnected : " << inet_ntoa(receptor_list[receptorEmitting].receptorAddress.sin_addr)
404 receptor_list.erase(receptor_list.begin() + (int)receptorEmitting);
434 template <
typename T>
int vpNetwork::send(T *
object,
const unsigned int &sizeOfObject)
436 if (receptor_list.size() == 0) {
444 #if defined(__linux__) 445 flags = MSG_NOSIGNAL;
448 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 449 return sendto(receptor_list[0].socketFileDescriptorReceptor, (
const char *)(
void *)
object, sizeOfObject, flags,
450 (sockaddr *)&receptor_list[0].receptorAddress, receptor_list[0].receptorAddressSize);
452 return sendto(receptor_list[0].socketFileDescriptorReceptor, (
const char *)(
void *)
object, (
int)sizeOfObject, flags,
453 (sockaddr *)&receptor_list[0].receptorAddress, receptor_list[0].receptorAddressSize);
478 template <
typename T>
int vpNetwork::sendTo(T *
object,
const unsigned int &dest,
const unsigned int &sizeOfObject)
480 if (receptor_list.size() == 0 || dest > (
unsigned int)receptor_list.size() - 1) {
482 vpTRACE(
"No receptor at the specified index.");
488 #if defined(__linux__) 489 flags = MSG_NOSIGNAL;
492 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 493 return sendto(receptor_list[dest].socketFileDescriptorReceptor, (
const char *)(
void *)
object, sizeOfObject, flags,
494 (sockaddr *)&receptor_list[dest].receptorAddress, receptor_list[dest].receptorAddressSize);
496 return sendto(receptor_list[dest].socketFileDescriptorReceptor, (
const char *)(
void *)
object, (
int)sizeOfObject,
497 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))