42 #include <visp3/core/vpConfig.h> 43 #include <visp3/core/vpRequest.h> 50 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 51 #include <arpa/inet.h> 53 #include <netinet/in.h> 54 #include <sys/socket.h> 63 #if defined(__APPLE__) && defined(__MACH__) // Apple OSX and iOS (Darwin) 64 #include <TargetConditionals.h> 87 #ifndef DOXYGEN_SHOULD_SKIP_THIS 89 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 90 int socketFileDescriptorReceptor;
91 socklen_t receptorAddressSize;
93 SOCKET socketFileDescriptorReceptor;
94 int receptorAddressSize;
96 struct sockaddr_in receptorAddress;
97 std::string receptorIP;
99 vpReceptor() : socketFileDescriptorReceptor(0), receptorAddressSize(), receptorAddress(), receptorIP() {}
103 struct sockaddr_in emitterAddress;
104 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 105 int socketFileDescriptorEmitter;
107 SOCKET socketFileDescriptorEmitter;
109 vpEmitter() : emitterAddress(), socketFileDescriptorEmitter(0)
111 emitterAddress.sin_family = AF_INET;
112 emitterAddress.sin_addr.s_addr = INADDR_ANY;
113 emitterAddress.sin_port = 0;
114 socketFileDescriptorEmitter = 0;
126 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 150 std::vector<int> _handleRequests();
151 int _handleFirstRequest();
153 void _receiveRequest();
154 void _receiveRequestFrom(
const unsigned int &receptorEmitting);
155 int _receiveRequestOnce();
156 int _receiveRequestOnceFrom(
const unsigned int &receptorEmitting);
164 int getReceptorIndex(
const char *name);
175 if (ind >= (
int)request_list.size() || ind < 0)
177 return request_list[(unsigned)ind]->getId();
189 void print(
const char *
id =
"");
191 template <
typename T>
int receive(T *
object,
const unsigned int &sizeOfObject =
sizeof(T));
192 template <
typename 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 *);
207 template <
typename T>
int send(T *
object,
const int unsigned &sizeOfObject =
sizeof(T));
208 template <
typename T>
int sendTo(T *
object,
const unsigned int &dest,
const unsigned int &sizeOfObject =
sizeof(T));
211 int sendRequestTo(
vpRequest &req,
const unsigned int &dest);
213 int sendAndEncodeRequest(
vpRequest &req);
214 int sendAndEncodeRequestTo(
vpRequest &req,
const unsigned int &dest);
279 if (receptor_list.size() == 0) {
287 tv.tv_usec = (int)tv_usec;
289 tv.tv_usec = tv_usec;
292 FD_ZERO(&readFileDescriptor);
294 for (
unsigned int i = 0; i < receptor_list.size(); i++) {
295 FD_SET((
unsigned int)receptor_list[i].socketFileDescriptorReceptor, &readFileDescriptor);
298 socketMax = receptor_list[i].socketFileDescriptorReceptor;
300 if (socketMax < receptor_list[i].socketFileDescriptorReceptor)
301 socketMax = receptor_list[i].socketFileDescriptorReceptor;
304 int value = select((
int)socketMax + 1, &readFileDescriptor, NULL, NULL, &tv);
311 }
else if (value == 0) {
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,
321 (
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);
358 template <
typename T>
361 if (receptor_list.size() == 0 || receptorEmitting > (
unsigned int)receptor_list.size() - 1) {
363 vpTRACE(
"No receptor at the specified index");
369 tv.tv_usec = (int)tv_usec;
371 tv.tv_usec = tv_usec;
374 FD_ZERO(&readFileDescriptor);
376 socketMax = receptor_list[receptorEmitting].socketFileDescriptorReceptor;
377 FD_SET((
unsigned int)receptor_list[receptorEmitting].socketFileDescriptorReceptor, &readFileDescriptor);
379 int value = select((
int)socketMax + 1, &readFileDescriptor, NULL, NULL, &tv);
386 }
else if (value == 0) {
390 if (FD_ISSET((
unsigned int)receptor_list[receptorEmitting].socketFileDescriptorReceptor, &readFileDescriptor)) {
391 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 393 recv(receptor_list[receptorEmitting].socketFileDescriptorReceptor, (
char *)(
void *)
object, sizeOfObject, 0);
395 numbytes = recv((
unsigned int)receptor_list[receptorEmitting].socketFileDescriptorReceptor,
396 (
char *)(
void *)
object, (
int)sizeOfObject, 0);
399 std::cout <<
"Disconnected : " << inet_ntoa(receptor_list[receptorEmitting].receptorAddress.sin_addr)
401 receptor_list.erase(receptor_list.begin() + (int)receptorEmitting);
431 template <
typename T>
int vpNetwork::send(T *
object,
const unsigned int &sizeOfObject)
433 if (receptor_list.size() == 0) {
441 #if defined(__linux__) 442 flags = MSG_NOSIGNAL;
445 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 446 return sendto(receptor_list[0].socketFileDescriptorReceptor, (
const char *)(
void *)
object, sizeOfObject, flags,
447 (sockaddr *)&receptor_list[0].receptorAddress, receptor_list[0].receptorAddressSize);
449 return sendto(receptor_list[0].socketFileDescriptorReceptor, (
const char *)(
void *)
object, (
int)sizeOfObject, flags,
450 (sockaddr *)&receptor_list[0].receptorAddress, receptor_list[0].receptorAddressSize);
475 template <
typename T>
int vpNetwork::sendTo(T *
object,
const unsigned int &dest,
const unsigned int &sizeOfObject)
477 if (receptor_list.size() == 0 || dest > (
unsigned int)receptor_list.size() - 1) {
479 vpTRACE(
"No receptor at the specified index.");
485 #if defined(__linux__) 486 flags = MSG_NOSIGNAL;
489 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX 490 return sendto(receptor_list[dest].socketFileDescriptorReceptor, (
const char *)(
void *)
object, sizeOfObject, flags,
491 (sockaddr *)&receptor_list[dest].receptorAddress, receptor_list[dest].receptorAddressSize);
493 return sendto(receptor_list[dest].socketFileDescriptorReceptor, (
const char *)(
void *)
object, (
int)sizeOfObject,
494 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))