CatapultServer  v0.5.0.1 (Elephant)
Packet.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "PacketHeader.h"
25 #include <memory>
26 
27 namespace catapult { namespace ionet {
28 
29 #pragma pack(push, 1)
30 
32  struct Packet : public PacketHeader, public utils::NonCopyable {
33  public:
35  uint8_t* Data() {
36  return Size <= sizeof(Packet) ? nullptr : reinterpret_cast<uint8_t*>(this) + sizeof(Packet);
37  }
38 
40  constexpr const uint8_t* Data() const {
41  return Size <= sizeof(Packet) ? nullptr : reinterpret_cast<const uint8_t*>(this) + sizeof(Packet);
42  }
43  };
44 
45 #pragma pack(pop)
46 
48  template<typename TPacket>
49  std::shared_ptr<TPacket> CreateSharedPacket(uint32_t payloadSize = 0) {
50  uint32_t packetSize = sizeof(TPacket) + payloadSize;
51  auto pPacket = utils::MakeSharedWithSize<TPacket>(packetSize);
52  pPacket->Size = packetSize;
53  pPacket->Type = TPacket::Packet_Type;
54  return pPacket;
55  }
56 
57  template<>
58  inline std::shared_ptr<Packet> CreateSharedPacket(uint32_t payloadSize) {
59  uint32_t packetSize = sizeof(Packet) + payloadSize;
60  auto pPacket = utils::MakeSharedWithSize<Packet>(packetSize);
61  pPacket->Size = packetSize;
62  pPacket->Type = PacketType::Undefined;
63  return pPacket;
64  }
65 
67  template<typename TPacket>
68  const TPacket* CoercePacket(const Packet* pPacket) {
69  return TPacket::Packet_Type != pPacket->Type || sizeof(TPacket) != pPacket->Size
70  ? nullptr
71  : static_cast<const TPacket*>(pPacket);
72  }
73 
75  constexpr bool IsPacketValid(const Packet& packet, PacketType type) {
76  return sizeof(Packet) == packet.Size && type == packet.Type;
77  }
78 }}
catapult::ionet::Packet::Data
constexpr const uint8_t * Data() const
Returns a const pointer to data contained in this packet.
Definition: Packet.h:40
MemoryUtils.h
catapult::ionet::Packet
A packet header with a data payload.
Definition: Packet.h:32
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
PacketHeader.h
catapult::ionet::PacketHeader
A packet header that all transferable information is expected to have.
Definition: PacketHeader.h:30
catapult::ionet::PacketHeader::Type
PacketType Type
Type of the packet.
Definition: PacketHeader.h:35
catapult::ionet::PacketType
PacketType
An enumeration of known packet types.
Definition: PacketType.h:171
catapult::ionet::CreateSharedPacket
std::shared_ptr< TPacket > CreateSharedPacket(uint32_t payloadSize=0)
Creates a packet of the specified type (TPacket) with the specified payload size.
Definition: Packet.h:49
Packet_Type
static constexpr auto Packet_Type
Definition: RemoteNodeApi.cpp:33
catapult::ionet::CoercePacket
const TPacket * CoercePacket(const Packet *pPacket)
Coerces pPacket to the desired packet type or nullptr if it is incompatible.
Definition: Packet.h:68
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::utils::NonCopyable
A class that can neither be copied nor moved.
Definition: NonCopyable.h:26
catapult::ionet::IsPacketValid
constexpr bool IsPacketValid(const Packet &packet, PacketType type)
Checks if packet is valid with type.
Definition: Packet.h:75
NonCopyable.h