otsdaq  v2_00_00
TransmitterSocket.cc
1 #include "otsdaq-core/NetworkUtilities/TransmitterSocket.h"
2 #include "otsdaq-core/MessageFacility/MessageFacility.h"
3 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
4 
5 #include <iostream>
6 #include <sstream>
7 #include <iomanip> /* for setfill */
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 //========================================================================================================================
29 int TransmitterSocket::send(Socket& toSocket, const std::string& buffer,
30  bool verbose)
31 {
32 
33  //lockout other senders for the remainder of the scope
34  std::lock_guard<std::mutex> lock(sendMutex_);
35 
36  // __COUT__ << "Socket Descriptor #: " << socketNumber_ <<
37  // " from-port: " << ntohs(socketAddress_.sin_port) <<
38  // " to-port: " << ntohs(toSocket.getSocketAddress().sin_port) << std::endl;
39 
40  constexpr size_t MAX_SEND_SIZE = 65500;
41  size_t offset = 0;
42  int sts = 1;
43  bool delay = false;
44  while (offset < buffer.size() && sts > 0)
45  {
46  auto thisSize = buffer.size() - offset > MAX_SEND_SIZE ? MAX_SEND_SIZE : buffer.size() - offset;
47 
48  if(verbose) //debug
49  {
50  __COUT__ << "Sending " <<
51  " from: " << getIPAddress() <<
52  ":" << ntohs(socketAddress_.sin_port) <<
53  " to: " << toSocket.getIPAddress() <<
54  ":" << ntohs(toSocket.getSocketAddress().sin_port) <<
55  " size: " << thisSize << " remaining = " << (buffer.size() - offset - thisSize) << 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 << (((int16_t) buffer[i]) &0xFF) << "-" << std::dec;
64 // }
65 // ss << std::endl;
66 // std::cout << ss.str();
67  }
68 
69 
70  if(delay)
71  usleep(10000);
72  else
73  delay = true;
74  sts = sendto(socketNumber_, buffer.c_str() + offset, thisSize, 0,
75  (struct sockaddr *)&(toSocket.getSocketAddress()), sizeof(sockaddr_in));
76  offset += sts;
77  }
78 
79  if (sts <= 0)
80  {
81  __COUT__ << "Error writing buffer for port " << ntohs(socketAddress_.sin_port) << ": " << strerror(errno) << std::endl;
82  return -1;
83  }
84  return 0;
85 }
86 
87 //========================================================================================================================
88 int TransmitterSocket::send(Socket& toSocket, const std::vector<uint16_t>& buffer,
89  bool verbose)
90 {
91 
92  //lockout other senders for the remainder of the scope
93  std::lock_guard<std::mutex> lock(sendMutex_);
94 
95  // __COUT__ << "Socket Descriptor #: " << socketNumber_ <<
96  // " from-port: " << ntohs(socketAddress_.sin_port) <<
97  // " to-port: " << ntohs(toSocket.getSocketAddress().sin_port) << std::endl;
98 
99  constexpr size_t MAX_SEND_SIZE = 1500;
100  size_t offset = 0;
101  int sts = 1;
102 
103  while (offset < buffer.size() && sts > 0)
104  {
105  auto thisSize = 2 * (buffer.size() - offset) > MAX_SEND_SIZE ? MAX_SEND_SIZE : 2 * (buffer.size() - offset);
106  sts = sendto(socketNumber_, &buffer[0] + offset, thisSize, 0,
107  (struct sockaddr *)&(toSocket.getSocketAddress()), sizeof(sockaddr_in));
108  offset += sts / 2;
109  }
110 
111  if (sts <= 0)
112  {
113  __COUT__ << "Error writing buffer for port " << ntohs(socketAddress_.sin_port) << ": " << strerror(errno) << std::endl;
114  return -1;
115  }
116  return 0;
117 }
118 
119 //========================================================================================================================
120 int TransmitterSocket::send(Socket& toSocket, const std::vector<uint32_t>& buffer,
121  bool verbose)
122 {
123  //lockout other senders for the remainder of the scope
124  std::lock_guard<std::mutex> lock(sendMutex_);
125 
126  // __COUT__ << "Socket Descriptor #: " << socketNumber_ <<
127  // " from-port: " << ntohs(socketAddress_.sin_port) <<
128  // " to-port: " << ntohs(toSocket.getSocketAddress().sin_port) << std::endl;
129 
130  if (sendto(socketNumber_, &buffer[0], buffer.size() * sizeof(uint32_t), 0,
131  (struct sockaddr *)&(toSocket.getSocketAddress()), sizeof(sockaddr_in)) < (int)(buffer.size() * sizeof(uint32_t)))
132  {
133  __COUT__ << "Error writing buffer for port " << ntohs(socketAddress_.sin_port) << std::endl;
134  return -1;
135  }
136  return 0;
137 
138 }