otsdaq  v2_03_00
TransmitterSocket.cc
1 #include "otsdaq-core/NetworkUtilities/TransmitterSocket.h"
2 #include "otsdaq-core/Macros/CoutMacros.h"
3 #include "otsdaq-core/MessageFacility/MessageFacility.h"
4 
5 #include <iomanip> /* for setfill */
6 #include <iostream>
7 #include <sstream>
8 
9 using namespace ots;
10 
11 //========================================================================================================================
12 TransmitterSocket::TransmitterSocket(void)
13 {
14  __COUT__ << "TransmitterSocket constructor " << __E__;
15 }
16 
17 //========================================================================================================================
18 TransmitterSocket::TransmitterSocket(const std::string& IPAddress, unsigned int port)
19  : Socket(IPAddress, port)
20 {
21  __COUT__ << "TransmitterSocket constructor " << IPAddress << ":" << port << __E__;
22 }
23 
24 //========================================================================================================================
25 TransmitterSocket::~TransmitterSocket(void) {}
26 
27 //========================================================================================================================
28 int TransmitterSocket::send(Socket& toSocket, const std::string& buffer, bool verbose)
29 {
30  // lockout other senders for the remainder of the scope
31  std::lock_guard<std::mutex> lock(sendMutex_);
32 
33  // __COUT__ << "Socket Descriptor #: " << socketNumber_ <<
34  // " from-port: " << ntohs(socketAddress_.sin_port) <<
35  // " to-port: " << ntohs(toSocket.getSocketAddress().sin_port) << std::endl;
36 
37  constexpr size_t MAX_SEND_SIZE = 65500;
38  size_t offset = 0;
39  int sts = 1;
40  bool delay = false;
41  while(offset < buffer.size() && sts > 0)
42  {
43  auto thisSize = buffer.size() - offset > MAX_SEND_SIZE ? MAX_SEND_SIZE
44  : buffer.size() - offset;
45 
46  if(verbose) // debug
47  {
48  __COUT__ << "Sending "
49  << " from: " << getIPAddress() << ":"
50  << ntohs(socketAddress_.sin_port)
51  << " to: " << toSocket.getIPAddress() << ":"
52  << ntohs(toSocket.getSocketAddress().sin_port)
53  << " size: " << thisSize
54  << " remaining = " << (buffer.size() - offset - thisSize)
55  << std::endl;
56  // std::stringstream ss;
57  // ss << "\t";
58  // uint32_t begin = 0;
59  // for(uint32_t i=begin; i<buffer.size(); i++)
60  // {
61  // if(i==begin+2) ss << ":::";
62  // else if(i==begin+10) ss << ":::";
63  // ss << std::setfill('0') << std::setw(2) << std::hex <<
64  //(((int16_t) buffer[i]) &0xFF) << "-" << std::dec;
65  // }
66  // ss << std::endl;
67  // std::cout << ss.str();
68  }
69 
70  if(delay)
71  usleep(10000);
72  else
73  delay = true;
74  sts = sendto(socketNumber_,
75  buffer.c_str() + offset,
76  thisSize,
77  0,
78  (struct sockaddr*)&(toSocket.getSocketAddress()),
79  sizeof(sockaddr_in));
80  offset += sts;
81  }
82 
83  if(sts <= 0)
84  {
85  __COUT__ << "Error writing buffer for port " << ntohs(socketAddress_.sin_port)
86  << ": " << strerror(errno) << std::endl;
87  return -1;
88  }
89  return 0;
90 }
91 
92 //========================================================================================================================
93 int TransmitterSocket::send(Socket& toSocket,
94  const std::vector<uint16_t>& buffer,
95  bool verbose)
96 {
97  // lockout other senders for the remainder of the scope
98  std::lock_guard<std::mutex> lock(sendMutex_);
99 
100  // __COUT__ << "Socket Descriptor #: " << socketNumber_ <<
101  // " from-port: " << ntohs(socketAddress_.sin_port) <<
102  // " to-port: " << ntohs(toSocket.getSocketAddress().sin_port) << std::endl;
103 
104  constexpr size_t MAX_SEND_SIZE = 1500;
105  size_t offset = 0;
106  int sts = 1;
107 
108  while(offset < buffer.size() && sts > 0)
109  {
110  auto thisSize = 2 * (buffer.size() - offset) > MAX_SEND_SIZE
111  ? MAX_SEND_SIZE
112  : 2 * (buffer.size() - offset);
113  sts = sendto(socketNumber_,
114  &buffer[0] + offset,
115  thisSize,
116  0,
117  (struct sockaddr*)&(toSocket.getSocketAddress()),
118  sizeof(sockaddr_in));
119  offset += sts / 2;
120  }
121 
122  if(sts <= 0)
123  {
124  __COUT__ << "Error writing buffer for port " << ntohs(socketAddress_.sin_port)
125  << ": " << strerror(errno) << std::endl;
126  return -1;
127  }
128  return 0;
129 }
130 
131 //========================================================================================================================
132 int TransmitterSocket::send(Socket& toSocket,
133  const std::vector<uint32_t>& buffer,
134  bool verbose)
135 {
136  // lockout other senders for the remainder of the scope
137  std::lock_guard<std::mutex> lock(sendMutex_);
138 
139  // __COUT__ << "Socket Descriptor #: " << socketNumber_ <<
140  // " from-port: " << ntohs(socketAddress_.sin_port) <<
141  // " to-port: " << ntohs(toSocket.getSocketAddress().sin_port) << std::endl;
142 
143  if(sendto(socketNumber_,
144  &buffer[0],
145  buffer.size() * sizeof(uint32_t),
146  0,
147  (struct sockaddr*)&(toSocket.getSocketAddress()),
148  sizeof(sockaddr_in)) < (int)(buffer.size() * sizeof(uint32_t)))
149  {
150  __COUT__ << "Error writing buffer for port " << ntohs(socketAddress_.sin_port)
151  << std::endl;
152  return -1;
153  }
154  return 0;
155 }