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