otsdaq  v2_05_02_indev
TransceiverSocket.cc
1 #include "otsdaq/NetworkUtilities/TransceiverSocket.h"
2 #include "otsdaq/Macros/CoutMacros.h"
3 #include "otsdaq/MessageFacility/MessageFacility.h"
4 
5 #include <iostream>
6 
7 using namespace ots;
8 
9 //==============================================================================
10 TransceiverSocket::TransceiverSocket(void) { __COUT__ << "TransceiverSocket constructor " << __E__; }
11 
12 //==============================================================================
13 TransceiverSocket::TransceiverSocket(std::string IPAddress, unsigned int port) : Socket(IPAddress, port)
14 {
15  __COUT__ << "TransceiverSocket constructor " << IPAddress << ":" << port << __E__;
16 }
17 
18 //==============================================================================
19 TransceiverSocket::~TransceiverSocket(void) {}
20 
21 //==============================================================================
22 // returns 0 on success
23 int TransceiverSocket::acknowledge(const std::string& buffer, bool verbose)
24 {
25  // lockout other senders for the remainder of the scope
26  std::lock_guard<std::mutex> lock(sendMutex_);
27 
28  if(verbose)
29  __COUT__ << "Acknowledging on Socket Descriptor #: " << socketNumber_ << " from-port: " << ntohs(socketAddress_.sin_port)
30  << " to-port: " << ntohs(ReceiverSocket::fromAddress_.sin_port) << std::endl;
31 
32  constexpr size_t MAX_SEND_SIZE = 65500;
33  if(buffer.size() > MAX_SEND_SIZE)
34  {
35  __COUT__ << "Too large! Error writing buffer from port " << ntohs(TransmitterSocket::socketAddress_.sin_port) << ": " << std::endl;
36  return -1;
37  }
38 
39  size_t offset = 0;
40  size_t thisSize = buffer.size();
41  int sendToSize = sendto(socketNumber_, buffer.c_str() + offset, thisSize, 0, (struct sockaddr*)&(ReceiverSocket::fromAddress_), sizeof(sockaddr_in));
42 
43  if(sendToSize <= 0)
44  {
45  __COUT__ << "Error writing buffer from port " << ntohs(TransmitterSocket::socketAddress_.sin_port) << ": " << strerror(errno) << std::endl;
46  return -1;
47  }
48 
49  return 0;
50 }