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