1 #include "otsdaq/NetworkUtilities/TransmitterSocket.h"
2 #include "otsdaq/Macros/CoutMacros.h"
3 #include "otsdaq/MessageFacility/MessageFacility.h"
12 TransmitterSocket::TransmitterSocket(
void) { __COUT__ <<
"TransmitterSocket constructor " << __E__; }
15 TransmitterSocket::TransmitterSocket(
const std::string& IPAddress,
unsigned int port) :
Socket(IPAddress, port)
17 __COUT__ <<
"TransmitterSocket constructor " << IPAddress <<
":" << port << __E__;
21 TransmitterSocket::~TransmitterSocket(
void) {}
24 int TransmitterSocket::send(
Socket& toSocket,
const std::string& buffer,
bool verbose)
27 std::lock_guard<std::mutex> lock(sendMutex_);
33 constexpr
size_t MAX_SEND_SIZE = 65500;
37 while(offset < buffer.size() && sts > 0)
39 auto thisSize = buffer.size() - offset > MAX_SEND_SIZE ? MAX_SEND_SIZE : buffer.size() - offset;
43 __COUT__ <<
"Sending "
44 <<
" from: " << getIPAddress() <<
":" << ntohs(socketAddress_.sin_port) <<
" to: " << toSocket.getIPAddress() <<
":"
45 << ntohs(toSocket.getSocketAddress().sin_port) <<
" size: " << thisSize <<
" remaining = " << (buffer.size() - offset - thisSize)
65 sts = sendto(socketNumber_, buffer.c_str() + offset, thisSize, 0, (
struct sockaddr*)&(toSocket.getSocketAddress()),
sizeof(sockaddr_in));
71 __COUT__ <<
"Error writing buffer for port " << ntohs(socketAddress_.sin_port) <<
": " << strerror(errno) << std::endl;
78 int TransmitterSocket::send(
Socket& toSocket,
const std::vector<uint16_t>& buffer,
bool )
81 std::lock_guard<std::mutex> lock(sendMutex_);
87 constexpr
size_t MAX_SEND_SIZE = 1500;
91 while(offset < buffer.size() && sts > 0)
93 auto thisSize = 2 * (buffer.size() - offset) > MAX_SEND_SIZE ? MAX_SEND_SIZE : 2 * (buffer.size() - offset);
94 sts = sendto(socketNumber_, &buffer[0] + offset, thisSize, 0, (
struct sockaddr*)&(toSocket.getSocketAddress()),
sizeof(sockaddr_in));
100 __COUT__ <<
"Error writing buffer for port " << ntohs(socketAddress_.sin_port) <<
": " << strerror(errno) << std::endl;
107 int TransmitterSocket::send(
Socket& toSocket,
const std::vector<uint32_t>& buffer,
bool )
110 std::lock_guard<std::mutex> lock(sendMutex_);
116 if(sendto(socketNumber_, &buffer[0], buffer.size() *
sizeof(uint32_t), 0, (
struct sockaddr*)&(toSocket.getSocketAddress()),
sizeof(sockaddr_in)) <
117 (
int)(buffer.size() *
sizeof(uint32_t)))
119 __COUT__ <<
"Error writing buffer for port " << ntohs(socketAddress_.sin_port) << std::endl;