otsdaq  v2_05_02_indev
TCPPacket.cc
1 #include "otsdaq/NetworkUtilities/TCPPacket.h"
2 #include <arpa/inet.h>
3 #include <iostream>
4 
5 using namespace ots;
6 
7 //==============================================================================
8 TCPPacket::TCPPacket() : fBuffer("") {}
9 
10 //==============================================================================
11 TCPPacket::~TCPPacket(void) {}
12 
13 //==============================================================================
14 std::string TCPPacket::encode(char const* message, std::size_t length)
15 {
16  return encode(std::string(message, length));
17 }
18 
19 //==============================================================================
20 std::string TCPPacket::encode(const std::string& message)
21 {
22  uint32_t size = htonl(TCPPacket::headerLength + message.length());
23  std::string buffer = std::string(TCPPacket::headerLength, ' ') + message; // THE HEADER LENGTH IS SET TO 4 = sizeof(uint32_t)
24  buffer[0] = (size)&0xff;
25  buffer[1] = (size >> 8) & 0xff;
26  buffer[2] = (size >> 16) & 0xff;
27  buffer[3] = (size >> 24) & 0xff;
28 
29  // std::cout << __PRETTY_FUNCTION__
30  // << std::hex
31  // << "Message length: " << message.length()
32  // << ", Buffer length: " << buffer.length()
33  // << ", htonl length: " << size
34  // << std::dec
35 
36  // << std::endl;
37  // std::cout << __PRETTY_FUNCTION__ << "Sending-";
38  // for (auto l = 0; l < buffer.length(); l++)
39  // std::cout << buffer[l] << "-";
40  // std::cout << std::endl;
41  return buffer;
42 }
43 
44 //==============================================================================
45 void TCPPacket::reset(void) { fBuffer.clear(); }
46 
47 //==============================================================================
48 bool TCPPacket::isEmpty(void) {return !fBuffer.size(); }
49 
50 //==============================================================================
51 bool TCPPacket::decode(std::string& message)
52 {
53  if(fBuffer.length() < headerLength)
54  return false;
55  uint32_t length = ntohl(reinterpret_cast<uint32_t&>(fBuffer.at(0))); // THE HEADER IS FIXED TO SIZE 4 = SIZEOF(uint32_t)
56  // std::cout << __PRETTY_FUNCTION__ << "Receiving-";
57  // or (auto l = 0; l < fBuffer.length(); l++)
58  // for (auto l = 0; l < 4; l++)
59  // std::cout << std::hex << (unsigned int)fBuffer[l] << std::dec << "-";
60  // std::cout << std::endl;
61  std::cout << __PRETTY_FUNCTION__
62  //<< std::hex
63  << "Buffer length: "
64  << fBuffer.length()
65  // << ", htonl length: " << length
66  << ", ntohl length: "
67  << length
68  //<< std::dec
69  << std::endl;
70  if(fBuffer.length() == length)
71  {
72  message = fBuffer.substr(headerLength);
73  fBuffer.clear();
74  return true;
75  }
76  else if(fBuffer.length() > length)
77  {
78  message = fBuffer.substr(headerLength, length - headerLength);
79  std::cout << __PRETTY_FUNCTION__ << "Erasing: " << length
80  << " characters!"
81  //<< " Msg:-" << message << "-"
82  << std::endl;
83  fBuffer.erase(0, length);
84  return true;
85  }
86  else
87  {
88  std::cout << __PRETTY_FUNCTION__ << "Can't decode an incomplete message! Length is only: " << fBuffer.length() << std::endl;
89  return false;
90  }
91 }