1 #include "otsdaq/NetworkUtilities/TCPPacket.h"
8 TCPPacket::TCPPacket() : fBuffer(
"") {}
11 TCPPacket::~TCPPacket(
void) {}
14 std::string TCPPacket::encode(
char const* message, std::size_t length)
16 return encode(std::string(message, length));
20 std::string TCPPacket::encode(
const std::string& message)
22 uint32_t size = htonl(TCPPacket::headerLength + message.length());
23 std::string buffer = std::string(TCPPacket::headerLength,
' ') + message;
24 buffer[0] = (size)&0xff;
25 buffer[1] = (size >> 8) & 0xff;
26 buffer[2] = (size >> 16) & 0xff;
27 buffer[3] = (size >> 24) & 0xff;
45 void TCPPacket::reset(
void) { fBuffer.clear(); }
48 bool TCPPacket::isEmpty(
void) {
return !fBuffer.size(); }
51 bool TCPPacket::decode(std::string& message)
53 if(fBuffer.length() < headerLength)
55 uint32_t length = ntohl(reinterpret_cast<uint32_t&>(fBuffer.at(0)));
61 std::cout << __PRETTY_FUNCTION__
70 if(fBuffer.length() == length)
72 message = fBuffer.substr(headerLength);
76 else if(fBuffer.length() > length)
78 message = fBuffer.substr(headerLength, length - headerLength);
79 std::cout << __PRETTY_FUNCTION__ <<
"Erasing: " << length
83 fBuffer.erase(0, length);
88 std::cout << __PRETTY_FUNCTION__ <<
"Can't decode an incomplete message! Length is only: " << fBuffer.length() << std::endl;