CatapultServer  v0.5.0.1 (Elephant)
PacketEntityUtils.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "Packet.h"
23 #include "PacketPayloadParser.h"
25 
26 namespace catapult { namespace ionet {
27 
29  inline size_t CalculatePacketDataSize(const Packet& packet) {
30  constexpr auto Min_Size = sizeof(PacketHeader);
31  if (packet.Size <= Min_Size) {
32  if (packet.Size < Min_Size)
33  CATAPULT_LOG(warning) << "packet size (" << packet.Size << ") must be at least " << Min_Size;
34 
35  return 0;
36  }
37 
38  return packet.Size - Min_Size;
39  }
40 
42  template<typename TEntity>
43  bool IsSizeValid(const TEntity& entity) {
44  return TEntity::CalculateRealSize(entity) == entity.Size;
45  }
46 
49  template<typename TEntity, typename TIsValidPredicate>
50  model::EntityRange<TEntity> ExtractEntitiesFromPacket(const Packet& packet, TIsValidPredicate isValid) {
51  auto dataSize = CalculatePacketDataSize(packet);
52  auto offsets = ExtractEntityOffsets<TEntity>({ packet.Data(), dataSize }, isValid);
53  return offsets.empty()
55  : model::EntityRange<TEntity>::CopyVariable(packet.Data(), dataSize, offsets);
56  }
57 
60  template<typename TEntity, typename TIsValidPredicate>
61  std::unique_ptr<TEntity> ExtractEntityFromPacket(const Packet& packet, TIsValidPredicate isValid) {
62  auto dataSize = CalculatePacketDataSize(packet);
63  if (!ContainsSingleEntity<TEntity>({ packet.Data(), dataSize }, isValid))
64  return nullptr;
65 
66  auto pEntity = utils::MakeUniqueWithSize<TEntity>(dataSize);
67  std::memcpy(static_cast<void*>(pEntity.get()), packet.Data(), dataSize);
68  return pEntity;
69  }
70 
73  template<typename TStructure>
75  auto dataSize = CalculatePacketDataSize(packet);
76  auto numStructures = CountFixedSizeStructures<TStructure>({ packet.Data(), dataSize });
77  return 0 == numStructures
79  : model::EntityRange<TStructure>::CopyFixed(packet.Data(), numStructures);
80  }
81 }}
CATAPULT_LOG
#define CATAPULT_LOG(SEV)
Writes a log entry to the default logger with SEV severity.
Definition: Logging.h:340
catapult::ionet::IsSizeValid
bool IsSizeValid(const TEntity &entity)
Checks the real size of entity against its reported size and returns true if the sizes match.
Definition: PacketEntityUtils.h:43
PacketPayloadParser.h
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::Packet::Data
uint8_t * Data()
Returns a non-const pointer to data contained in this packet.
Definition: Packet.h:35
catapult::ionet::PacketHeader::Size
uint32_t Size
Size of the packet.
Definition: PacketHeader.h:32
catapult::model::EntityRange
Represents a range of entities.
Definition: EntityRange.h:31
catapult::ionet::ExtractFixedSizeStructuresFromPacket
model::EntityRange< TStructure > ExtractFixedSizeStructuresFromPacket(const Packet &packet)
Definition: PacketEntityUtils.h:74
catapult::ionet::PacketHeader
A packet header that all transferable information is expected to have.
Definition: PacketHeader.h:30
catapult::model::EntityRange::CopyFixed
static EntityRange CopyFixed(const uint8_t *pData, size_t numElements)
Creates an entity range around numElements fixed size elements pointed to by pData.
Definition: EntityRange.h:278
Packet.h
catapult::ionet::CalculatePacketDataSize
size_t CalculatePacketDataSize(const Packet &packet)
Calculates the data size of packet.
Definition: PacketEntityUtils.h:29
catapult::model::EntityRange::CopyVariable
static EntityRange CopyVariable(const uint8_t *pData, size_t dataSize, const std::vector< size_t > &offsets)
Definition: EntityRange.h:287
catapult::ionet::ExtractEntityFromPacket
std::unique_ptr< TEntity > ExtractEntityFromPacket(const Packet &packet, TIsValidPredicate isValid)
Definition: PacketEntityUtils.h:61
EntityRange.h
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::ionet::ExtractEntitiesFromPacket
model::EntityRange< TEntity > ExtractEntitiesFromPacket(const Packet &packet, TIsValidPredicate isValid)
Definition: PacketEntityUtils.h:50