00001 #include "otsdaq-core/NetworkUtilities/TransmitterSocket.h"
00002 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00003 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
00004
00005 #include <iostream>
00006 #include <sstream>
00007 #include <iomanip>
00008
00009 using namespace ots;
00010
00011
00012 TransmitterSocket::TransmitterSocket(void)
00013 {
00014 __COUT__ << std::endl;
00015 }
00016
00017
00018 TransmitterSocket::TransmitterSocket(const std::string &IPAddress, unsigned int port)
00019 : Socket(IPAddress, port)
00020 {
00021 __COUT__ << std::endl;
00022 }
00023
00024
00025 TransmitterSocket::~TransmitterSocket(void)
00026 {}
00027
00028
00029 int TransmitterSocket::send(Socket& toSocket, const std::string& buffer,
00030 bool verbose)
00031 {
00032
00033
00034 std::lock_guard<std::mutex> lock(sendMutex_);
00035
00036
00037
00038
00039
00040 constexpr size_t MAX_SEND_SIZE = 65500;
00041 size_t offset = 0;
00042 int sts = 1;
00043 bool delay = false;
00044 while (offset < buffer.size() && sts > 0)
00045 {
00046 auto thisSize = buffer.size() - offset > MAX_SEND_SIZE ? MAX_SEND_SIZE : buffer.size() - offset;
00047
00048 if(verbose)
00049 {
00050 __COUT__ << "Sending " <<
00051 " from: " << getIPAddress() <<
00052 ":" << ntohs(socketAddress_.sin_port) <<
00053 " to: " << toSocket.getIPAddress() <<
00054 ":" << ntohs(toSocket.getSocketAddress().sin_port) <<
00055 " size: " << thisSize << " remaining = " << (buffer.size() - offset - thisSize) << std::endl;
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 }
00068
00069
00070 if(delay)
00071 usleep(10000);
00072 else
00073 delay = true;
00074 sts = sendto(socketNumber_, buffer.c_str() + offset, thisSize, 0, (struct sockaddr *)&(toSocket.getSocketAddress()), sizeof(sockaddr_in));
00075 offset += sts;
00076 }
00077
00078 if (sts <= 0)
00079 {
00080 __COUT__ << "Error writing buffer for port " << ntohs(socketAddress_.sin_port) << ": " << strerror(errno) << std::endl;
00081 return -1;
00082 }
00083 return 0;
00084 }
00085
00086
00087 int TransmitterSocket::send(Socket& toSocket, const std::vector<uint16_t>& buffer,
00088 bool verbose)
00089 {
00090
00091
00092 std::lock_guard<std::mutex> lock(sendMutex_);
00093
00094
00095
00096
00097
00098 constexpr size_t MAX_SEND_SIZE = 1500;
00099 size_t offset = 0;
00100 int sts = 1;
00101
00102 while (offset < buffer.size() && sts > 0)
00103 {
00104 auto thisSize = 2 * (buffer.size() - offset) > MAX_SEND_SIZE ? MAX_SEND_SIZE : 2 * (buffer.size() - offset);
00105 sts = sendto(socketNumber_, &buffer[0] + offset, thisSize, 0, (struct sockaddr *)&(toSocket.getSocketAddress()), sizeof(sockaddr_in));
00106 offset += sts / 2;
00107 }
00108
00109 if (sts <= 0)
00110 {
00111 __COUT__ << "Error writing buffer for port " << ntohs(socketAddress_.sin_port) << ": " << strerror(errno) << std::endl;
00112 return -1;
00113 }
00114 return 0;
00115 }
00116
00117
00118 int TransmitterSocket::send(Socket& toSocket, const std::vector<uint32_t>& buffer,
00119 bool verbose)
00120 {
00121
00122 std::lock_guard<std::mutex> lock(sendMutex_);
00123
00124
00125
00126
00127
00128 if (sendto(socketNumber_, &buffer[0], buffer.size() * sizeof(uint32_t), 0, (struct sockaddr *)&(toSocket.getSocketAddress()), sizeof(sockaddr_in)) < (int)(buffer.size() * sizeof(uint32_t)))
00129 {
00130 __COUT__ << "Error writing buffer for port " << ntohs(socketAddress_.sin_port) << std::endl;
00131 return -1;
00132 }
00133 return 0;
00134
00135 }