mal-packet-weaver
C++20 packet serialization/deserialization library.
|
Represents a session with packet dispatching functionality. More...
#include "dispatcher-session.hpp"
Public Member Functions | |
DispatcherSession (boost::asio::io_context &io_context, boost::asio::ip::tcp::socket &&socket) | |
Constructor for DispatcherSession. | |
~DispatcherSession () | |
Destructor for DispatcherSession. Cleans up the session and packet dispatcher. | |
template<IsPacket T> requires std::is_base_of_v<Packet, T> | |
bool | send_packet (const T &packet_arg) |
Sends any packet derived from DerivedPacket through the network. | |
std::unique_ptr< Packet > | pop_packet_now () |
Returns the earliest acquired packet. If packet queue is empty, returns nullptr. | |
boost::asio::awaitable< std::unique_ptr< Packet > > | pop_packet_async () |
bool | has_packets () |
Checks if there are packets in the queue. | |
bool | secured () const noexcept |
Checks if the session is secured using encryption. | |
constexpr bool | is_closed () const noexcept |
Checks if the session is closed. | |
constexpr bool | alive () const noexcept |
Checks if the session is alive. | |
void | setup_encryption (std::shared_ptr< mal_packet_weaver::crypto::EncryptionInterface > encryption) |
Sets up encryption for the session using provided encryption interface. | |
void | set_packet_receiver (PacketReceiverFn const receiver) |
Sets the packet receiver for the session. | |
void | enqueue_packet (PacketDispatcher::BasePacketPtr &&packet) |
Enqueues a packet for processing. | |
template<IsPacket DerivedPacket> | |
boost::asio::awaitable< std::unique_ptr< DerivedPacket > > | await_packet (float timeout=-1.0f) |
Wait until the packet is registered in the dispatch system and return as soon as possible. | |
template<IsPacket DerivedPacket> | |
boost::asio::awaitable< std::unique_ptr< DerivedPacket > > | await_packet (PacketFilterFunc< DerivedPacket > filter, float timeout=-1.0f) |
Wait until a packet satisfying the filter condition is registered in the dispatch system and return as soon as possible. | |
template<IsPacket DerivedPacket> | |
void | register_default_handler (PacketHandlerFunc< DerivedPacket > handler, PacketFilterFunc< DerivedPacket > filter={}, float delay=0.0f) |
Registers a default handler for the provided packet type. | |
template<typename Arg1 , IsPacket CustomPacket> | |
void | register_default_handler (PacketHandlerFunc< CustomPacket, Arg1 > &&handler, PacketFilterFunc< CustomPacket, Arg1 > &&filter={}, float delay=0.0f) |
Register a default handler for the provided packet type. | |
template<typename Arg1 , typename Arg2 , typename... Args, IsPacket CustomPacket> | |
void | register_default_handler (PacketHandlerFunc< CustomPacket, Arg1, Arg2, Args... > &&handler, PacketFilterFunc< CustomPacket, Arg1, Arg2, Args... > &&filter={}, float delay=0.0f) |
Register a default handler for the provided packet type. | |
template<IsPacket CustomPacket, typename... FnArgs> | |
void | register_default_handler (std::function< void(FnArgs...)> &&handler, std::function< bool(FnArgs...)> &&filter={}, float delay=0.0f) |
void | enqueue_promise (UniquePacketID packet_id, PacketDispatcher::shared_packet_promise promise) |
Enqueues a promise associated with a packet. | |
void | enqueue_filter_promise (UniquePacketID packet_id, PacketDispatcher::promise_filter filtered_promise) |
Enqueues a promise with a filter associated with a packet. | |
Session & | session () |
Get a reference to the underlying session. | |
PacketDispatcher & | dispatcher () |
Get a reference to the underlying packet dispatcher. | |
void | Destroy () |
Private Attributes | |
boost::asio::io_context & | io_context_ |
Reference to the associated Boost.Asio io_context. | |
std::shared_ptr< Session > | session_ |
The underlying session. | |
std::shared_ptr< PacketDispatcher > | dispatcher_ |
The underlying packet dispatcher. | |
Represents a session with packet dispatching functionality.
Definition at line 10 of file dispatcher-session.hpp.
|
inline |
Constructor for DispatcherSession.
io_context | The IO context to use for the session. |
socket | The TCP socket to use for the session. |
Definition at line 18 of file dispatcher-session.hpp.
|
inline |
Destructor for DispatcherSession. Cleans up the session and packet dispatcher.
Definition at line 31 of file dispatcher-session.hpp.
|
inlinenodiscardconstexprnoexcept |
Checks if the session is alive.
Definition at line 117 of file dispatcher-session.hpp.
|
inline |
Wait until the packet is registered in the dispatch system and return as soon as possible.
This function template waits for a specific type of packet to be registered in the dispatch system. It can optionally wait for a specified timeout duration.
DerivedPacket | The type of packet you want to wait for. |
timeout | If less than or equal to zero, the function will not return until the promise is fulfilled. Otherwise, it will wait for the given timeout (in seconds) before returning. |
Definition at line 164 of file dispatcher-session.hpp.
|
inline |
Wait until a packet satisfying the filter condition is registered in the dispatch system and return as soon as possible.
This function template waits for a packet of a specific type, satisfying a provided filter condition, to be registered in the dispatch system. It can optionally wait for a specified timeout duration.
DerivedPacket | The type of packet you want to wait for. |
filter | A function to filter the packet. If the functor returns true, the packet will fulfill the promise. |
timeout | If less than or equal to zero, the function will not return until the promise is fulfilled. Otherwise, it will wait for the given timeout (in seconds) before returning. |
Definition at line 188 of file dispatcher-session.hpp.
|
inline |
Definition at line 484 of file dispatcher-session.hpp.
|
inlinenodiscard |
Get a reference to the underlying packet dispatcher.
Definition at line 482 of file dispatcher-session.hpp.
|
inline |
Enqueues a promise with a filter associated with a packet.
This function enqueues a promise (associated with a specific packet ID) that includes a filter function. The promise will be fulfilled based on the provided filter's outcome. The enqueued promises with filters will be processed later.
packet_id | The unique packet identifier for which the filtered promise is being enqueued. |
filtered_promise | The promise filter to be enqueued. |
Definition at line 467 of file dispatcher-session.hpp.
|
inline |
Enqueues a packet for processing.
This function enqueues a unique pointer to a packet for processing by pushing it onto the internal queue.
packet | The unique pointer to the packet to be enqueued. |
Definition at line 144 of file dispatcher-session.hpp.
|
inline |
Enqueues a promise associated with a packet.
This function enqueues a promise (associated with a specific packet ID) for future fulfillment. The promise is associated with a unique packet identifier. The enqueued promises will be processed later.
packet_id | The unique packet identifier for which the promise is being enqueued. |
promise | The shared packet promise to be enqueued. |
Definition at line 451 of file dispatcher-session.hpp.
|
inlinenodiscard |
Checks if there are packets in the queue.
Definition at line 93 of file dispatcher-session.hpp.
|
inlinenodiscardconstexprnoexcept |
Checks if the session is closed.
Definition at line 109 of file dispatcher-session.hpp.
|
inline |
Returns nullptr if socket has crashed. If not, it will wait until the packet is available and will return it as soon as possible. This function is threadsafe.
Definition at line 81 of file dispatcher-session.hpp.
|
inline |
Returns the earliest acquired packet. If packet queue is empty, returns nullptr.
Definition at line 73 of file dispatcher-session.hpp.
|
inline |
Register a default handler for the provided packet type.
This function registers a default packet handler for a specific packet type. The handler function can be provided, and if it returns false, the packet is passed to the next handler. An optional filter function can also be provided to determine whether the handler should be applied based on the packet's properties. A delay parameter can be used to postpone the handler's execution for a certain amount of time.
Arg1 | The type of argument 1 for the handler. |
Args | Additional argument types for the handler. |
CustomPacket | The type of packet for which the handler should be registered. |
handler | The packet handler function. |
filter | The packet filter function to determine whether the handler should be applied. |
delay | The delay in seconds before the handler is executed. (Default is 0.0) |
Definition at line 239 of file dispatcher-session.hpp.
|
inline |
Register a default handler for the provided packet type.
This function registers a default packet handler for a specific packet type. The handler function can be provided, and if it returns false, the packet is passed to the next handler. An optional filter function can also be provided to determine whether the handler should be applied based on the packet's properties. A delay parameter can be used to postpone the handler's execution for a certain amount of time.
Arg1 | The type of argument 1 for the handler. |
Arg2 | The type of argument 2 for the handler. |
Args | Additional argument types for the handler. |
CustomPacket | The type of packet for which the handler should be registered. |
handler | The packet handler function. |
filter | The packet filter function to determine whether the handler should be applied. |
delay | The delay in seconds before the handler is executed. (Default is 0.0) |
Definition at line 331 of file dispatcher-session.hpp.
|
inline |
Registers a default handler for the provided packet type.
This function registers a default packet handler for a specific packet type. The handler function can be provided, and if it returns false, the packet is passed to the next handler. An optional filter function can also be provided to determine whether the handler should be applied based on the packet's properties. A delay parameter can be used to postpone the handler's execution for a certain amount of time.
DerivedPacket | The type of packet for which the handler should be registered. |
handler | The packet handler function. If it returns false, the packet will be passed to the next handler. |
filter | The packet filter function to determine whether the handler should be applied. (Optional) |
delay | The delay in seconds before the handler is executed. (Default is 0.0) |
Definition at line 213 of file dispatcher-session.hpp.
|
inlinenodiscardnoexcept |
Checks if the session is secured using encryption.
Definition at line 101 of file dispatcher-session.hpp.
|
inline |
Sends any packet derived from DerivedPacket through the network.
T | Final packet type. (Template functions cannot be overriden, we need to call serialize from the furthest child.) |
packet_arg | Packet value |
Definition at line 58 of file dispatcher-session.hpp.
|
inlinenodiscard |
Get a reference to the underlying session.
Definition at line 476 of file dispatcher-session.hpp.
|
inline |
Sets the packet receiver for the session.
receiver | The function to be called when a packet is received. |
Definition at line 134 of file dispatcher-session.hpp.
|
inline |
Sets up encryption for the session using provided encryption interface.
Definition at line 123 of file dispatcher-session.hpp.
|
private |
The underlying packet dispatcher.
Definition at line 493 of file dispatcher-session.hpp.
|
private |
Reference to the associated Boost.Asio io_context.
Definition at line 491 of file dispatcher-session.hpp.
|
private |
The underlying session.
Definition at line 492 of file dispatcher-session.hpp.