CatapultServer  v0.5.0.1 (Elephant)
PacketReader.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "Packet.h"
23 #include "catapult/utils/Logging.h"
24 
25 namespace catapult { namespace ionet {
26 
29  class PacketReader {
30  public:
32  explicit PacketReader(const Packet& packet)
33  : m_pData(packet.Data())
34  , m_numRemainingBytes(packet.Size)
35  , m_hasError(false) {
36  require(sizeof(PacketHeader), "constructor");
38  }
39 
40  public:
42  bool empty() const {
43  return 0 == m_numRemainingBytes || m_hasError;
44  }
45 
47  bool hasError() const {
48  return m_hasError;
49  }
50 
51  public:
53  template<typename TValue>
54  const TValue* readFixed() {
55  require(sizeof(TValue), "readFixed");
56  if (hasError())
57  return nullptr;
58 
59  const auto& value = reinterpret_cast<const TValue&>(*m_pData);
60  advance(sizeof(TValue));
61  return &value;
62  }
63 
65  template<typename TEntity>
66  const TEntity* readVariable() {
67  auto pSize = readFixed<uint32_t>();
68  if (!pSize)
69  return nullptr;
70 
71  // readFixed above (for size) advances the data pointer past the size, but the variable entity should point to the size
72  rewind(sizeof(uint32_t));
73  require(*pSize, "readVariable");
74  if (hasError())
75  return nullptr;
76 
77  const auto& entity = reinterpret_cast<const TEntity&>(*m_pData);
78  advance(*pSize);
79  return &entity;
80  }
81 
82  private:
83  void require(uint32_t numBytes, const char* message) {
84  if (m_numRemainingBytes >= numBytes)
85  return;
86 
87  CATAPULT_LOG(warning) << message << ": requested (" << numBytes << ") bytes with only " << m_numRemainingBytes << " remaining";
88  m_hasError = true;
89  }
90 
91  void advance(uint32_t numBytes) {
92  m_pData += numBytes;
93  m_numRemainingBytes -= numBytes;
94  }
95 
96  void rewind(uint32_t numBytes) {
97  m_pData -= numBytes;
98  m_numRemainingBytes += numBytes;
99  }
100 
101  private:
102  const uint8_t* m_pData;
105  };
106 }}
CATAPULT_LOG
#define CATAPULT_LOG(SEV)
Writes a log entry to the default logger with SEV severity.
Definition: Logging.h:340
catapult::ionet::Packet
A packet header with a data payload.
Definition: Packet.h:32
colorPrint.warning
def warning(*args)
Definition: colorPrint.py:10
catapult::ionet::PacketReader::hasError
bool hasError() const
Returns true if a reading error has been encountered, false otherwise.
Definition: PacketReader.h:47
catapult::ionet::PacketReader::empty
bool empty() const
Returns true if the reader has consumed all data, false otherwise.
Definition: PacketReader.h:42
catapult::ionet::PacketHeader
A packet header that all transferable information is expected to have.
Definition: PacketHeader.h:30
Packet.h
catapult::ionet::PacketReader::PacketReader
PacketReader(const Packet &packet)
Creates a reader around packet.
Definition: PacketReader.h:32
catapult::ionet::PacketReader::readVariable
const TEntity * readVariable()
Reads a variable-sized value from the packet.
Definition: PacketReader.h:66
catapult::ionet::PacketReader::rewind
void rewind(uint32_t numBytes)
Definition: PacketReader.h:96
Data
std::vector< uint8_t > Data
Definition: tools/benchmark/main.cpp:33
catapult::ionet::PacketReader::m_hasError
bool m_hasError
Definition: PacketReader.h:104
catapult::ionet::PacketReader
Definition: PacketReader.h:29
catapult::ionet::PacketReader::require
void require(uint32_t numBytes, const char *message)
Definition: PacketReader.h:83
catapult::ionet::PacketReader::m_numRemainingBytes
uint32_t m_numRemainingBytes
Definition: PacketReader.h:103
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::ionet::PacketReader::readFixed
const TValue * readFixed()
Reads a fixed-sized value from the packet.
Definition: PacketReader.h:54
Logging.h
catapult::ionet::PacketReader::advance
void advance(uint32_t numBytes)
Definition: PacketReader.h:91
catapult::ionet::PacketReader::m_pData
const uint8_t * m_pData
Definition: PacketReader.h:102