otsdaq  v2_00_00
TransceiverSocket.cc
1 #include "otsdaq-core/NetworkUtilities/TransceiverSocket.h"
2 #include "otsdaq-core/MessageFacility/MessageFacility.h"
3 #include "otsdaq-core/Macros/CoutHeaderMacros.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 
27 //========================================================================================================================
28 //returns 0 on success
29 int TransceiverSocket::acknowledge(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 
37  if(verbose)
38  __COUT__ << "Acknowledging on Socket Descriptor #: " << socketNumber_ <<
39  " from-port: " << ntohs(socketAddress_.sin_port) <<
40  " to-port: " << ntohs(ReceiverSocket::fromAddress_.sin_port) << std::endl;
41 
42  constexpr size_t MAX_SEND_SIZE = 65500;
43  if(buffer.size() > MAX_SEND_SIZE)
44  {
45  __COUT__ << "Too large! Error writing buffer from port " <<
46  ntohs(TransmitterSocket::socketAddress_.sin_port) << ": " << std::endl;
47  return -1;
48  }
49 
50  size_t offset = 0;
51  size_t thisSize = buffer.size();
52  int sendToSize = sendto(socketNumber_,
53  buffer.c_str() + offset,
54  thisSize,
55  0,
56  (struct sockaddr *)&(ReceiverSocket::fromAddress_), sizeof(sockaddr_in));
57 
58 
59  if (sendToSize <= 0)
60  {
61  __COUT__ << "Error writing buffer from port " <<
62  ntohs(TransmitterSocket::socketAddress_.sin_port) << ": " <<
63  strerror(errno) << std::endl;
64  return -1;
65  }
66 
67  return 0;
68 }