1 #include "otsdaq-core/NetworkUtilities/ReceiverSocket.h"
2 #include "otsdaq-core/Macros/CoutMacros.h"
3 #include "otsdaq-core/MessageFacility/MessageFacility.h"
4 #include "otsdaq-core/NetworkUtilities/NetworkConverters.h"
10 #include <arpa/inet.h>
16 ReceiverSocket::ReceiverSocket(std::string IPAddress,
unsigned int port)
18 , addressLength_(sizeof(fromAddress_))
22 __COUT__ <<
"ReceiverSocket constructor " << IPAddress <<
":" << port << __E__;
27 ReceiverSocket::ReceiverSocket(
void)
28 : addressLength_(sizeof(fromAddress_)), numberOfBytes_(0), readCounter_(0)
30 __COUT__ <<
"ReceiverSocket constructor" << __E__;
34 ReceiverSocket::~ReceiverSocket(
void) {}
37 int ReceiverSocket::receive(std::string& buffer,
38 unsigned int timeoutSeconds,
39 unsigned int timeoutUSeconds,
43 buffer, dummyIPAddress_, dummyPort_, timeoutSeconds, timeoutUSeconds, verbose);
50 int ReceiverSocket::receive(std::string& buffer,
51 unsigned long& fromIPAddress,
52 unsigned short& fromPort,
53 unsigned int timeoutSeconds,
54 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_);
73 if((numberOfBytes_ = recvfrom(socketNumber_,
77 (
struct sockaddr*)&fromAddress_,
78 &addressLength_)) == -1)
80 __COUT__ <<
"At socket with IPAddress: " << getIPAddress()
81 <<
" port: " << getPort() << std::endl;
82 __SS__ <<
"Error reading buffer from\tIP:\t";
83 std::string fromIP = inet_ntoa(fromAddress_.sin_addr);
84 fromIPAddress = fromAddress_.sin_addr.s_addr;
85 fromPort = fromAddress_.sin_port;
86 for(
int i = 0; i < 4; i++)
88 ss << ((fromIPAddress << (i * 8)) & 0xff);
92 ss <<
"\tPort\t" << ntohs(fromPort) <<
" IP " << fromIP << std::endl;
93 __COUT__ <<
"\n" << ss.str();
98 fromIPAddress = fromAddress_.sin_addr.s_addr;
99 fromPort = fromAddress_.sin_port;
111 buffer.resize(numberOfBytes_);
116 std::string fromIP = inet_ntoa(fromAddress_.sin_addr);
118 __COUT__ <<
"Receiving "
119 <<
" at: " << getIPAddress() <<
":" << getPort()
120 <<
" from: " << fromIP;
121 std::cout <<
":" << ntohs(fromPort) <<
" size: " << buffer.size()
143 __COUT__ <<
"No new messages for " << timeoutSeconds + timeoutUSeconds / 1000.
145 << readCounter_ * (timeoutSeconds + timeoutUSeconds / 1000.)
146 <<
"s). Read request timed out receiving on "
147 <<
" " << getIPAddress() <<
":" << getPort() << std::endl;
155 int ReceiverSocket::receive(std::vector<uint32_t>& buffer,
156 unsigned int timeoutSeconds,
157 unsigned int timeoutUSeconds,
161 buffer, dummyIPAddress_, dummyPort_, timeoutSeconds, timeoutUSeconds, verbose);
168 int ReceiverSocket::receive(std::vector<uint32_t>& buffer,
169 unsigned long& fromIPAddress,
170 unsigned short& fromPort,
171 unsigned int timeoutSeconds,
172 unsigned int timeoutUSeconds,
176 std::lock_guard<std::mutex> lock(receiveMutex_);
179 timeout_.tv_sec = timeoutSeconds;
180 timeout_.tv_usec = timeoutUSeconds;
182 FD_ZERO(&fileDescriptor_);
183 FD_SET(socketNumber_, &fileDescriptor_);
184 select(socketNumber_ + 1, &fileDescriptor_, 0, 0, &timeout_);
185 __COUT__ <<
"Is this a successful reeeaaad???" << std::endl;
187 if(FD_ISSET(socketNumber_, &fileDescriptor_))
189 buffer.resize(maxSocketSize_ /
sizeof(uint32_t));
195 if((numberOfBytes_ = recvfrom(socketNumber_,
199 (
struct sockaddr*)&fromAddress_,
200 &addressLength_)) == -1)
202 __COUT__ <<
"At socket with IPAddress: " << getIPAddress()
203 <<
" port: " << getPort() << std::endl;
204 __SS__ <<
"Error reading buffer from\tIP:\t";
205 for(
int i = 0; i < 4; i++)
207 ss << ((fromIPAddress << (i * 8)) & 0xff);
211 ss <<
"\tPort\t" << fromPort << std::endl;
212 __COUT__ <<
"\n" << ss.str();
217 fromIPAddress = fromAddress_.sin_addr.s_addr;
218 fromPort = fromAddress_.sin_port;
230 buffer.resize(numberOfBytes_ /
sizeof(uint32_t));
236 struct sockaddr_in sin;
237 socklen_t len =
sizeof(sin);
238 getsockname(socketNumber_, (
struct sockaddr*)&sin, &len);
241 __COUT__ << __COUT_HDR_FL__ <<
"No new messages for "
242 << timeoutSeconds + timeoutUSeconds / 1000. <<
"s (Total "
243 << readCounter_ * (timeoutSeconds + timeoutUSeconds / 1000.)
244 <<
"s). Read request timed out for port: " << ntohs(sin.sin_port)
248 __COUT__ <<
"This a successful reeeaaad" << std::endl;