CatapultServer  v0.5.0.1 (Elephant)
PacketHandlers.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "IoTypes.h"
23 #include "PacketPayload.h"
25 #include "catapult/functions.h"
26 #include "catapult/types.h"
27 #include <vector>
28 
29 namespace catapult { namespace ionet {
30 
33  public:
35  explicit ServerPacketHandlerContext(const Key& key, const std::string& host);
36 
37  public:
39  const Key& key() const;
40 
42  const std::string& host() const;
43 
45  bool hasResponse() const;
46 
48  const PacketPayload& response() const;
49 
50  public:
52  void response(PacketPayload&& payload);
53 
54  private:
55  const Key& m_key;
56  const std::string& m_host;
59  };
60 
63  public:
66 
69 
70  public:
72  explicit ServerPacketHandlers(uint32_t maxPacketDataSize = std::numeric_limits<uint32_t>::max());
73 
74  public:
76  size_t size() const;
77 
79  uint32_t maxPacketDataSize() const;
80 
82  bool canProcess(PacketType type) const;
83 
85  bool canProcess(const Packet& packet) const;
86 
89  bool process(const Packet& packet, ContextType& context) const;
90 
91  public:
93  void registerHandler(PacketType type, const PacketHandler& handler);
94 
95  private:
96  const PacketHandler* findHandler(const Packet& packet) const;
97 
98  private:
100  std::vector<PacketHandler> m_handlers;
101  };
102 }}
catapult::ionet::ServerPacketHandlers::m_maxPacketDataSize
uint32_t m_maxPacketDataSize
Definition: PacketHandlers.h:99
m_host
std::string m_host
Definition: PacketSocket.cpp:477
CATAPULT_LOG
#define CATAPULT_LOG(SEV)
Writes a log entry to the default logger with SEV severity.
Definition: Logging.h:340
catapult::ionet::ServerPacketHandlerContext::host
const std::string & host() const
Gets the host associated with the client.
Definition: PacketHandlers.cpp:38
catapult::ionet::Packet
A packet header with a data payload.
Definition: Packet.h:32
m_maxPacketDataSize
size_t m_maxPacketDataSize
Definition: PacketSocket.cpp:243
colorPrint.warning
def warning(*args)
Definition: colorPrint.py:10
catapult::ionet::ServerPacketHandlerContext::m_key
const Key & m_key
Definition: PacketHandlers.h:55
catapult::ionet::ServerPacketHandlerContext::hasResponse
bool hasResponse() const
Returns true if a response has been associated with this context.
Definition: PacketHandlers.cpp:42
functions.h
catapult::utils::to_underlying_type
constexpr std::underlying_type_t< TEnum > to_underlying_type(TEnum value)
Converts a strongly typed enumeration value to its underlying integral value.
Definition: Casting.h:37
catapult::ionet::ServerPacketHandlerContext::key
const Key & key() const
Gets the public key associated with the client.
Definition: PacketHandlers.cpp:34
catapult::ionet::PacketHeader::Type
PacketType Type
Type of the packet.
Definition: PacketHeader.h:35
catapult::ionet::ServerPacketHandlers::process
bool process(const Packet &packet, ContextType &context) const
Definition: PacketHandlers.cpp:90
catapult::ionet::ServerPacketHandlers::ServerPacketHandlers
ServerPacketHandlers(uint32_t maxPacketDataSize=std::numeric_limits< uint32_t >::max())
Creates packet handlers with a max packet data size (maxPacketDataSize).
Definition: PacketHandlers.cpp:65
catapult::ionet::ServerPacketHandlers::registerHandler
void registerHandler(PacketType type, const PacketHandler &handler)
Registers a handler for the specified packet type.
Definition: PacketHandlers.cpp:100
catapult::ionet::PacketType
PacketType
An enumeration of known packet types.
Definition: PacketType.h:171
PacketHandlers.h
CATAPULT_THROW_RUNTIME_ERROR_1
#define CATAPULT_THROW_RUNTIME_ERROR_1(MESSAGE, PARAM1)
Macro used to throw a catapult runtime error with a single parameter.
Definition: exceptions.h:171
catapult::ionet::ServerPacketHandlerContext::response
const PacketPayload & response() const
Gets the response associated with this context.
Definition: PacketHandlers.cpp:46
catapult::ionet::ServerPacketHandlerContext::m_host
const std::string & m_host
Definition: PacketHandlers.h:56
catapult::ionet::ServerPacketHandlers::PacketHandler
consumer< const Packet &, ContextType & > PacketHandler
Packet handler function.
Definition: PacketHandlers.h:68
catapult::ionet::ServerPacketHandlerContext::ServerPacketHandlerContext
ServerPacketHandlerContext(const Key &key, const std::string &host)
Creates a context around key and \ host.
Definition: PacketHandlers.cpp:28
PacketPayload.h
catapult::ionet::ServerPacketHandlerContext::m_hasResponse
bool m_hasResponse
Definition: PacketHandlers.h:57
catapult::utils::MoveOnly
A class that can be moved but not copied.
Definition: NonCopyable.h:43
CATAPULT_THROW_RUNTIME_ERROR
#define CATAPULT_THROW_RUNTIME_ERROR(MESSAGE)
Macro used to throw a catapult runtime error.
Definition: exceptions.h:167
types.h
Casting.h
catapult::ionet::PacketPayload
A packet payload that can be written.
Definition: PacketPayload.h:29
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::ionet::ServerPacketHandlerContext::m_payload
PacketPayload m_payload
Definition: PacketHandlers.h:58
catapult::ionet::ServerPacketHandlers::m_handlers
std::vector< PacketHandler > m_handlers
Definition: PacketHandlers.h:100
catapult::ionet::ServerPacketHandlers::findHandler
const PacketHandler * findHandler(const Packet &packet) const
Definition: PacketHandlers.cpp:111
catapult::utils::ByteArray< Key_Size, Key_tag >
catapult::ionet::ServerPacketHandlers::maxPacketDataSize
uint32_t maxPacketDataSize() const
Gets the max packet data size.
Definition: PacketHandlers.cpp:76
catapult::consumer
std::function< void(TArgs...)> consumer
A consumer function.
Definition: functions.h:35
catapult::ionet::ServerPacketHandlers::size
size_t size() const
Gets the number of registered handlers.
Definition: PacketHandlers.cpp:68
catapult::ionet::ServerPacketHandlers
A collection of packet handlers where there is at most one handler per packet type.
Definition: PacketHandlers.h:62
catapult::ionet::ServerPacketHandlers::canProcess
bool canProcess(PacketType type) const
Determines if type can be processed by a registered handler.
Definition: PacketHandlers.cpp:80
IoTypes.h
NonCopyable.h
catapult::ionet::ServerPacketHandlerContext
Context passed to a server packet handler function.
Definition: PacketHandlers.h:32