otsdaq  v1_01_02
 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 
7 using namespace ots;
8 
9 //========================================================================================================================
10 TransmitterSocket::TransmitterSocket(void)
11 {
12  __MOUT__ << std::endl;
13 }
14 
15 //========================================================================================================================
16 TransmitterSocket::TransmitterSocket(const std::string &IPAddress, unsigned int port)
17 : Socket(IPAddress, port)
18 {
19  __MOUT__ << std::endl;
20 }
21 
22 //========================================================================================================================
23 TransmitterSocket::~TransmitterSocket(void)
24 {
25 }
26 
27 //========================================================================================================================
28 int TransmitterSocket::send(Socket& toSocket, const std::string& buffer)
29 {
30 
31  //lockout other senders for the remainder of the scope
32  std::lock_guard<std::mutex> lock(sendMutex_);
33 
34 // __MOUT__ << "Socket Descriptor #: " << socketNumber_ <<
35 // " from-port: " << ntohs(socketAddress_.sin_port) <<
36 // " to-port: " << ntohs(toSocket.getSocketAddress().sin_port) << std::endl;
37 
38  if(sendto(socketNumber_, buffer.c_str(), buffer.size(), 0,
39  (struct sockaddr *)&(toSocket.getSocketAddress()),
40  sizeof(sockaddr_in)) < (int)(buffer.size()))
41  {
42  __MOUT__ << "Error writing buffer for port " << ntohs(socketAddress_.sin_port) << std::endl;
43  return -1;
44  }
45  return 0;
46 }
47 
48 //========================================================================================================================
49 int TransmitterSocket::send(Socket& toSocket, const std::vector<uint32_t>& buffer)
50 {
51  //lockout other senders for the remainder of the scope
52  std::lock_guard<std::mutex> lock(sendMutex_);
53 
54 // __MOUT__ << "Socket Descriptor #: " << socketNumber_ <<
55 // " from-port: " << ntohs(socketAddress_.sin_port) <<
56 // " to-port: " << ntohs(toSocket.getSocketAddress().sin_port) << std::endl;
57 
58  if(sendto(socketNumber_, &buffer[0], buffer.size()*sizeof(uint32_t), 0, (struct sockaddr *)&(toSocket.getSocketAddress()), sizeof(sockaddr_in)) < (int)(buffer.size()*sizeof(uint32_t)))
59  {
60  __MOUT__ << "Error writing buffer for port " << ntohs(socketAddress_.sin_port) << std::endl;
61  return -1;
62  }
63  return 0;
64 
65 }