otsdaq  v1_01_04
 All Classes Namespaces Functions
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__ << std::endl;
15 }
16 
17 //========================================================================================================================
18 TransmitterSocket::TransmitterSocket(const std::string &IPAddress, unsigned int port)
19  : Socket(IPAddress, port)
20 {
21  __COUT__ << std::endl;
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, (struct sockaddr *)&(toSocket.getSocketAddress()), sizeof(sockaddr_in));
75  offset += sts;
76  }
77 
78  if (sts <= 0)
79  {
80  __COUT__ << "Error writing buffer for port " << ntohs(socketAddress_.sin_port) << ": " << strerror(errno) << std::endl;
81  return -1;
82  }
83  return 0;
84 }
85 
86 //========================================================================================================================
87 int TransmitterSocket::send(Socket& toSocket, const std::vector<uint16_t>& buffer,
88  bool verbose)
89 {
90 
91  //lockout other senders for the remainder of the scope
92  std::lock_guard<std::mutex> lock(sendMutex_);
93 
94  // __COUT__ << "Socket Descriptor #: " << socketNumber_ <<
95  // " from-port: " << ntohs(socketAddress_.sin_port) <<
96  // " to-port: " << ntohs(toSocket.getSocketAddress().sin_port) << std::endl;
97 
98  constexpr size_t MAX_SEND_SIZE = 1500;
99  size_t offset = 0;
100  int sts = 1;
101 
102  while (offset < buffer.size() && sts > 0)
103  {
104  auto thisSize = 2 * (buffer.size() - offset) > MAX_SEND_SIZE ? MAX_SEND_SIZE : 2 * (buffer.size() - offset);
105  sts = sendto(socketNumber_, &buffer[0] + offset, thisSize, 0, (struct sockaddr *)&(toSocket.getSocketAddress()), sizeof(sockaddr_in));
106  offset += sts / 2;
107  }
108 
109  if (sts <= 0)
110  {
111  __COUT__ << "Error writing buffer for port " << ntohs(socketAddress_.sin_port) << ": " << strerror(errno) << std::endl;
112  return -1;
113  }
114  return 0;
115 }
116 
117 //========================================================================================================================
118 int TransmitterSocket::send(Socket& toSocket, const std::vector<uint32_t>& buffer,
119  bool verbose)
120 {
121  //lockout other senders for the remainder of the scope
122  std::lock_guard<std::mutex> lock(sendMutex_);
123 
124  // __COUT__ << "Socket Descriptor #: " << socketNumber_ <<
125  // " from-port: " << ntohs(socketAddress_.sin_port) <<
126  // " to-port: " << ntohs(toSocket.getSocketAddress().sin_port) << std::endl;
127 
128  if (sendto(socketNumber_, &buffer[0], buffer.size() * sizeof(uint32_t), 0, (struct sockaddr *)&(toSocket.getSocketAddress()), sizeof(sockaddr_in)) < (int)(buffer.size() * sizeof(uint32_t)))
129  {
130  __COUT__ << "Error writing buffer for port " << ntohs(socketAddress_.sin_port) << std::endl;
131  return -1;
132  }
133  return 0;
134 
135 }