1 #include "otsdaq-core/NetworkUtilities/ReceiverSocket.h"
2 #include "otsdaq-core/MessageFacility/MessageFacility.h"
3 #include "otsdaq-core/Macros/CoutMacros.h"
4 #include "otsdaq-core/NetworkUtilities/NetworkConverters.h"
10 #include <arpa/inet.h>
17 ReceiverSocket::ReceiverSocket(std::string IPAddress,
unsigned int port)
19 , addressLength_(sizeof(fromAddress_))
23 __COUT__ <<
"ReceiverSocket constructor " << IPAddress <<
":" << port << __E__;
29 ReceiverSocket::ReceiverSocket(
void)
30 : addressLength_(sizeof(fromAddress_))
34 __COUT__ <<
"ReceiverSocket constructor" << __E__;
38 ReceiverSocket::~ReceiverSocket(
void)
43 int ReceiverSocket::receive(std::string& buffer,
unsigned int timeoutSeconds,
44 unsigned int timeoutUSeconds,
bool verbose)
46 return receive(buffer, dummyIPAddress_, dummyPort_, timeoutSeconds, timeoutUSeconds, verbose);
53 int ReceiverSocket::receive(std::string& buffer,
unsigned long& fromIPAddress,
54 unsigned short& fromPort,
unsigned int timeoutSeconds,
unsigned int timeoutUSeconds,
58 std::lock_guard<std::mutex> lock(receiveMutex_);
61 timeout_.tv_sec = timeoutSeconds;
62 timeout_.tv_usec = timeoutUSeconds;
64 FD_ZERO(&fileDescriptor_);
65 FD_SET(socketNumber_ , &fileDescriptor_);
66 select(socketNumber_+1, &fileDescriptor_, 0, 0, &timeout_);
68 if(FD_ISSET(socketNumber_, &fileDescriptor_))
70 buffer.resize(maxSocketSize_);
71 if ((numberOfBytes_ = recvfrom(socketNumber_, &buffer[0], maxSocketSize_, 0,
72 (
struct sockaddr *)&fromAddress_, &addressLength_)) == -1)
74 __COUT__ <<
"At socket with IPAddress: " << getIPAddress() <<
" port: " << getPort() << std::endl;
75 __SS__ <<
"Error reading buffer from\tIP:\t";
76 std::string fromIP = inet_ntoa(fromAddress_.sin_addr);
77 fromIPAddress = fromAddress_.sin_addr.s_addr;
78 fromPort = fromAddress_.sin_port;
79 for(
int i = 0; i < 4; i++)
81 ss << ((fromIPAddress << (i * 8)) & 0xff);
85 ss <<
"\tPort\t" << ntohs(fromPort) <<
" IP " << fromIP << std::endl;
86 __COUT__ <<
"\n" << ss.str();
91 fromIPAddress = fromAddress_.sin_addr.s_addr;
92 fromPort = fromAddress_.sin_port;
101 buffer.resize(numberOfBytes_);
106 std::string fromIP = inet_ntoa(fromAddress_.sin_addr);
108 __COUT__ <<
"Receiving " <<
109 " at: " << getIPAddress() <<
112 std::cout <<
":" << ntohs(fromPort) <<
113 " size: " << buffer.size() << std::endl;
134 <<
"No new messages for " << timeoutSeconds+timeoutUSeconds/1000.
135 <<
"s (Total " << readCounter_*(timeoutSeconds+timeoutUSeconds/1000.)
136 <<
"s). Read request timed out receiving on " <<
137 " " << getIPAddress() <<
147 int ReceiverSocket::receive(std::vector<uint32_t>& buffer,
unsigned int timeoutSeconds,
unsigned int timeoutUSeconds,
bool verbose)
149 return receive(buffer, dummyIPAddress_, dummyPort_, timeoutSeconds, timeoutUSeconds, verbose);
156 int ReceiverSocket::receive(std::vector<uint32_t>& buffer,
unsigned long& fromIPAddress,
unsigned short& fromPort,
unsigned int timeoutSeconds,
unsigned int timeoutUSeconds,
bool verbose)
159 std::lock_guard<std::mutex> lock(receiveMutex_);
162 timeout_.tv_sec = timeoutSeconds;
163 timeout_.tv_usec = timeoutUSeconds;
165 FD_ZERO(&fileDescriptor_);
166 FD_SET(socketNumber_ , &fileDescriptor_);
167 select(socketNumber_+1, &fileDescriptor_, 0, 0, &timeout_);
168 __COUT__ <<
"Is this a successful reeeaaad???" << std::endl;
170 if(FD_ISSET(socketNumber_, &fileDescriptor_))
172 buffer.resize(maxSocketSize_/
sizeof(uint32_t));
173 if ((numberOfBytes_ = recvfrom(socketNumber_, &buffer[0], maxSocketSize_, 0, (
struct sockaddr *)&fromAddress_, &addressLength_)) == -1)
175 __COUT__ <<
"At socket with IPAddress: " << getIPAddress() <<
" port: " << getPort() << std::endl;
176 __SS__ <<
"Error reading buffer from\tIP:\t";
177 for(
int i = 0; i < 4; i++)
179 ss << ((fromIPAddress << (i * 8)) & 0xff);
183 ss <<
"\tPort\t" << fromPort << std::endl;
184 __COUT__ <<
"\n" << ss.str();
189 fromIPAddress = fromAddress_.sin_addr.s_addr;
190 fromPort = fromAddress_.sin_port;
199 buffer.resize(numberOfBytes_/
sizeof(uint32_t));
205 struct sockaddr_in sin;
206 socklen_t len =
sizeof(sin);
207 getsockname(socketNumber_, (
struct sockaddr *)&sin, &len);
210 __COUT__ << __COUT_HDR_FL__
211 <<
"No new messages for " << timeoutSeconds+timeoutUSeconds/1000.
212 <<
"s (Total " << readCounter_*(timeoutSeconds+timeoutUSeconds/1000.)
213 <<
"s). Read request timed out for port: " << ntohs(sin.sin_port)
217 __COUT__ <<
"This a successful reeeaaad" << std::endl;