mal-packet-weaver
C++20 packet serialization/deserialization library.
Loading...
Searching...
No Matches
mal_packet_weaver::PacketDispatcher Class Referencefinal

The PacketDispatcher class is responsible for managing packet dispatching and handling. More...

#include "packet-dispatcher.hpp"

Inheritance diagram for mal_packet_weaver::PacketDispatcher:
[legend]
Collaboration diagram for mal_packet_weaver::PacketDispatcher:
[legend]

Classes

class  SynchronizationWrapper
 A wrapper class for synchronizing and processing data. More...
 

Public Types

using BasePacketPtr = std::unique_ptr<Packet>
 Alias for a unique pointer to a base packet type.
 
template<typename T >
using shared_promise = std::shared_ptr<std::promise<T>>
 Alias for a shared promise of type T.
 
using shared_packet_promise = shared_promise<BasePacketPtr>
 Alias for a shared promise of a base packet pointer.
 
using promise_filter = std::pair<std::function<bool(BasePacketPtr const &)>, shared_packet_promise>
 Alias for a filter function paired with a shared packet promise.
 
using handler_tuple = std::tuple<float, PacketFilterFunc<Packet>, PacketHandlerFunc<Packet>>
 Alias for a tuple containing information for packet handling.
 

Public Member Functions

 PacketDispatcher (boost::asio::io_context &io_context)
 Constructs a PacketDispatcher instance associated with the given io_context.
 
void enqueue_packet (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.
 
void register_subsystem_handler (PacketSubsystemID subsystem_id, PacketHandlerFunc< Packet > handler, PacketFilterFunc< Packet > filter={}, float delay=0.0f)
 Registers a subsystem handler for the provided packet type.
 
void enqueue_promise (UniquePacketID packet_id, shared_packet_promise promise)
 Enqueues a promise associated with a packet.
 
void enqueue_filter_promise (UniquePacketID packet_id, promise_filter filtered_promise)
 Enqueues a promise with a filter associated with a packet.
 
void Destroy ()
 Coroutines use the shared pointer from this, so you need to explicitly call Destroy so alive_ is false. This way coroutines can end and unlock the remaining instances of shared_ptr.
 

Private Member Functions

boost::asio::awaitable< std::shared_ptr< PacketDispatcher > > get_shared_ptr ()
 Retrieves a shared pointer to the current dispatcher.
 
boost::asio::awaitable< void > Run ()
 This function represents the main loop for running a task with exponential backoff and asynchronous I/O. It processes input packets and handles them while managing delays and timers.
 
bool fulfill_promises (UniquePacketID packet_id, BasePacketPtr &packet)
 Fulfills promises associated with a packet ID.
 
bool fulfill_handlers (UniquePacketID packet_id, BasePacketPtr &packet, float &min_handler_timestamp, SteadyTimer &timer)
 Fulfills handlers associated with a packet ID and packet data.
 
void push_packet (BasePacketPtr &&packet)
 Pushes an input packet to the unprocessed_packets_input_ queue.
 
boost::asio::awaitable< boolpop_inputs ()
 Pops input packets from input queues to local maps for processing.
 

Private Attributes

boost::asio::io_context & io_context_
 
SignalHandler signal_handler_
 
SynchronizationWrapper< BasePacketPtrunprocessed_packets_input_
 
SynchronizationWrapper< std::pair< UniquePacketID, shared_packet_promise > > promise_map_input_
 
SynchronizationWrapper< std::pair< UniquePacketID, promise_filter > > promise_filter_map_input_
 
SynchronizationWrapper< std::pair< UniquePacketID, handler_tuple > > default_handlers_input_
 
SynchronizationWrapper< std::pair< PacketSubsystemID, handler_tuple > > subsystem_handlers_input_
 
std::unordered_map< UniquePacketID, std::vector< BasePacketPtr > > unprocessed_packets_
 
std::unordered_map< UniquePacketID, std::deque< shared_packet_promise > > promise_map_
 
std::unordered_map< UniquePacketID, std::vector< promise_filter > > promise_filter_map_
 
std::unordered_map< UniquePacketID, std::vector< handler_tuple > > default_handlers_
 
std::unordered_map< PacketSubsystemID, std::vector< handler_tuple > > subsystem_handlers_
 
std::atomic_bool alive_ { true }
 

Detailed Description

The PacketDispatcher class is responsible for managing packet dispatching and handling.

This class associates with a specific io_context and provides functionality for enqueuing packets and managing packet handlers and filters.

Note
Session should be initialized using make_shared.

To correctly destroy this object, you need to call Destroy function, because coroutines share the object from this.

Definition at line 44 of file packet-dispatcher.hpp.

Member Typedef Documentation

◆ BasePacketPtr

Alias for a unique pointer to a base packet type.

Definition at line 148 of file packet-dispatcher.hpp.

◆ handler_tuple

Alias for a tuple containing information for packet handling.

This type alias defines a tuple that holds information related to packet handling. The first element is a float representing a priority, the second element is a packet filter function, and the third element is a packet handler function.

Definition at line 182 of file packet-dispatcher.hpp.

◆ promise_filter

Alias for a filter function paired with a shared packet promise.

This type alias defines a pair where the first element is a filter function that accepts a const reference to a base packet pointer and returns a boolean. The second element is a shared promise that holds a base packet pointer.

Definition at line 172 of file packet-dispatcher.hpp.

◆ shared_packet_promise

Alias for a shared promise of a base packet pointer.

Definition at line 163 of file packet-dispatcher.hpp.

◆ shared_promise

template<typename T >
using mal_packet_weaver::PacketDispatcher::shared_promise = std::shared_ptr<std::promise<T>>

Alias for a shared promise of type T.

This type alias defines a shared pointer to a promise that holds a value of type T.

Template Parameters
TThe type of the value held by the promise.

Definition at line 158 of file packet-dispatcher.hpp.

Constructor & Destructor Documentation

◆ PacketDispatcher()

mal_packet_weaver::PacketDispatcher::PacketDispatcher ( boost::asio::io_context & io_context)

Constructs a PacketDispatcher instance associated with the given io_context.

Parameters
io_contextThe io_context to associate with the dispatcher.

Definition at line 9 of file packet-dispatcher.cpp.

Member Function Documentation

◆ await_packet() [1/2]

template<IsPacket DerivedPacket>
boost::asio::awaitable< std::unique_ptr< DerivedPacket > > mal_packet_weaver::PacketDispatcher::await_packet ( float timeout = -1.0f)

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.

Template Parameters
DerivedPacketThe type of packet you want to wait for.
Parameters
timeoutIf 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.
Returns
boost::asio::awaitable<std::unique_ptr<DerivedPacket>> A unique pointer to the received packet, or nullptr if the timeout was reached.

Definition at line 11 of file packet-dispatcher.inl.

Here is the call graph for this function:

◆ await_packet() [2/2]

template<IsPacket DerivedPacket>
boost::asio::awaitable< std::unique_ptr< DerivedPacket > > mal_packet_weaver::PacketDispatcher::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.

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.

Template Parameters
DerivedPacketThe type of packet you want to wait for.
Parameters
filterA function to filter the packet. If the functor returns true, the packet will fulfill the promise.
timeoutIf 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.
Returns
boost::asio::awaitable<std::unique_ptr<DerivedPacket>> A unique pointer to the received packet, or nullptr if the timeout was reached or the filter condition was not satisfied.

Definition at line 51 of file packet-dispatcher.inl.

Here is the call graph for this function:

◆ Destroy()

void mal_packet_weaver::PacketDispatcher::Destroy ( )
inline

Coroutines use the shared pointer from this, so you need to explicitly call Destroy so alive_ is false. This way coroutines can end and unlock the remaining instances of shared_ptr.

Definition at line 306 of file packet-dispatcher.hpp.

◆ enqueue_filter_promise()

void mal_packet_weaver::PacketDispatcher::enqueue_filter_promise ( UniquePacketID packet_id,
promise_filter filtered_promise )
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.

Parameters
packet_idThe unique packet identifier for which the filtered promise is being enqueued.
filtered_promiseThe promise filter to be enqueued.

Definition at line 123 of file packet-dispatcher.inl.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ enqueue_packet()

void mal_packet_weaver::PacketDispatcher::enqueue_packet ( BasePacketPtr && packet)
inline

Enqueues a packet for processing.

This function enqueues a unique pointer to a packet for processing by pushing it onto the internal queue.

Parameters
packetThe unique pointer to the packet to be enqueued.

Definition at line 5 of file packet-dispatcher.inl.

Here is the call graph for this function:

◆ enqueue_promise()

void mal_packet_weaver::PacketDispatcher::enqueue_promise ( UniquePacketID packet_id,
shared_packet_promise promise )
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.

Parameters
packet_idThe unique packet identifier for which the promise is being enqueued.
promiseThe shared packet promise to be enqueued.

Definition at line 116 of file packet-dispatcher.inl.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fulfill_handlers()

bool mal_packet_weaver::PacketDispatcher::fulfill_handlers ( UniquePacketID packet_id,
BasePacketPtr & packet,
float & min_handler_timestamp,
SteadyTimer & timer )
inlineprivate

Fulfills handlers associated with a packet ID and packet data.

This function fulfills handlers for a given packet ID by searching for associated handlers in the default_handlers_ map. For each handler, it checks if the specified delay is greater than the packet's time alive. If so, it updates the minimum handler timestamp. Then, it checks if the associated filter condition (if any) is satisfied before executing the handler.

Parameters
packet_idThe unique ID of the packet.
packetA reference to the packet for which handlers should be fulfilled.
min_handler_timestampThe minimum handler timestamp to update.
timerThe timer used for timestamp calculations.
Returns
true if at least one handler was fulfilled, otherwise false.

Definition at line 165 of file packet-dispatcher.inl.

Here is the call graph for this function:

◆ fulfill_promises()

bool mal_packet_weaver::PacketDispatcher::fulfill_promises ( UniquePacketID packet_id,
BasePacketPtr & packet )
inlineprivate

Fulfills promises associated with a packet ID.

This function fulfills promises from two different maps: promise_filter_map_ and promise_map_. It searches for promises in promise_filter_map_ first, and if a matching promise is found, it checks if the associated filter condition (if any) is satisfied before fulfilling the promise. If no matching promise is found in promise_filter_map_, it then searches for a promise in promise_map_ and fulfills the first one if available.

Parameters
packet_idThe unique ID of the packet.
packetA reference to the packet to be fulfilled.
Returns
true if at least one promise was fulfilled, otherwise false.

Definition at line 130 of file packet-dispatcher.inl.

◆ get_shared_ptr()

boost::asio::awaitable< std::shared_ptr< PacketDispatcher > > mal_packet_weaver::PacketDispatcher::get_shared_ptr ( )
private

Retrieves a shared pointer to the current dispatcher.

Parameters
ioThe boost::asio::io_context used for asynchronous operations.
Returns
A boost::asio::awaitable that resolves to a shared_ptr<PacketDispatcher>.

Definition at line 109 of file packet-dispatcher.cpp.

Here is the call graph for this function:

◆ pop_inputs()

boost::asio::awaitable< bool > mal_packet_weaver::PacketDispatcher::pop_inputs ( )
private

Pops input packets from input queues to local maps for processing.

Returns
An awaitable indicating whether the task was successful.

Definition at line 215 of file packet-dispatcher.cpp.

◆ push_packet()

void mal_packet_weaver::PacketDispatcher::push_packet ( BasePacketPtr && packet)
inlineprivate

Pushes an input packet to the unprocessed_packets_input_ queue.

This function posts a task to the unprocessed_packets_input_strand_ to push an input packet into the unprocessed_packets_input_ queue. The packet is moved into a unique pointer, and the unprocessed_packets_input_updated_ atomic flag is set to indicate that the queue has been updated.

Parameters
packetThe packet to push (as an rvalue reference).

Definition at line 229 of file packet-dispatcher.inl.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ register_default_handler()

template<IsPacket DerivedPacket>
void mal_packet_weaver::PacketDispatcher::register_default_handler ( PacketHandlerFunc< DerivedPacket > handler,
PacketFilterFunc< DerivedPacket > filter = {},
float delay = 0.0f )

Registers a default handler for the provided packet type.

This function registers a default packet handler for a specific packet type. The filter 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.

Todo
Add an ability to delete handlers
Template Parameters
DerivedPacketThe type of packet for which the handler should be registered.
Parameters
handlerThe packet handler function.
filterThe packet filter function to determine whether the handler should be applied. (Optional)
delayThe delay in seconds before the handler is executed. (Default is 0.0)

Definition at line 94 of file packet-dispatcher.inl.

Here is the call graph for this function:

◆ register_subsystem_handler()

void mal_packet_weaver::PacketDispatcher::register_subsystem_handler ( PacketSubsystemID subsystem_id,
PacketHandlerFunc< Packet > handler,
PacketFilterFunc< Packet > filter = {},
float delay = 0.0f )

Registers a subsystem handler for the provided packet type.

This function registers a subsystem packet handler for a section of packets. The filter 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.

Todo
Add an ability to delete handlers
Parameters
handlerThe packet handler function.
filterThe packet filter function to determine whether the handler should be applied. (Optional)
delayThe delay in seconds before the handler is executed. (Default is 0.0)

Definition at line 100 of file packet-dispatcher.cpp.

◆ Run()

boost::asio::awaitable< void > mal_packet_weaver::PacketDispatcher::Run ( )
private

This function represents the main loop for running a task with exponential backoff and asynchronous I/O. It processes input packets and handles them while managing delays and timers.

Returns
A boost::asio::awaitable<void> representing the asynchronous task.

Definition at line 135 of file packet-dispatcher.cpp.

Here is the call graph for this function:

Member Data Documentation

◆ alive_

std::atomic_bool mal_packet_weaver::PacketDispatcher::alive_ { true }
private

Definition at line 405 of file packet-dispatcher.hpp.

◆ default_handlers_

std::unordered_map<UniquePacketID, std::vector<handler_tuple> > mal_packet_weaver::PacketDispatcher::default_handlers_
private

Map storing default packet handlers for each packet ID.

Definition at line 400 of file packet-dispatcher.hpp.

◆ default_handlers_input_

SynchronizationWrapper<std::pair<UniquePacketID, handler_tuple> > mal_packet_weaver::PacketDispatcher::default_handlers_input_
private

Queue for storing default handlers inputs.

Definition at line 389 of file packet-dispatcher.hpp.

◆ io_context_

boost::asio::io_context& mal_packet_weaver::PacketDispatcher::io_context_
private

Reference to the associated Boost.Asio io_context.

Definition at line 378 of file packet-dispatcher.hpp.

◆ promise_filter_map_

std::unordered_map<UniquePacketID, std::vector<promise_filter> > mal_packet_weaver::PacketDispatcher::promise_filter_map_
private

Map storing promise filters for each packet ID.

Definition at line 398 of file packet-dispatcher.hpp.

◆ promise_filter_map_input_

SynchronizationWrapper<std::pair<UniquePacketID, promise_filter> > mal_packet_weaver::PacketDispatcher::promise_filter_map_input_
private

Queue for storing promise filter map inputs.

Definition at line 387 of file packet-dispatcher.hpp.

◆ promise_map_

std::unordered_map<UniquePacketID, std::deque<shared_packet_promise> > mal_packet_weaver::PacketDispatcher::promise_map_
private

Map storing promises for each packet ID.

Definition at line 396 of file packet-dispatcher.hpp.

◆ promise_map_input_

SynchronizationWrapper<std::pair<UniquePacketID, shared_packet_promise> > mal_packet_weaver::PacketDispatcher::promise_map_input_
private

Queue for storing promise map inputs.

Definition at line 385 of file packet-dispatcher.hpp.

◆ signal_handler_

SignalHandler mal_packet_weaver::PacketDispatcher::signal_handler_
private

Signal handler for the dispatcher. Allows to wait until the actual data has been passed.

Definition at line 380 of file packet-dispatcher.hpp.

◆ subsystem_handlers_

std::unordered_map<PacketSubsystemID, std::vector<handler_tuple> > mal_packet_weaver::PacketDispatcher::subsystem_handlers_
private

Map storing default packet handlers for entire subsystems, if no default_handler_ was declared

Definition at line 402 of file packet-dispatcher.hpp.

◆ subsystem_handlers_input_

SynchronizationWrapper<std::pair<PacketSubsystemID, handler_tuple> > mal_packet_weaver::PacketDispatcher::subsystem_handlers_input_
private

Queue for storing subsystem handlers inputs.

Definition at line 391 of file packet-dispatcher.hpp.

◆ unprocessed_packets_

std::unordered_map<UniquePacketID, std::vector<BasePacketPtr> > mal_packet_weaver::PacketDispatcher::unprocessed_packets_
private

Map storing unprocessed packets for each packet ID.

Definition at line 394 of file packet-dispatcher.hpp.

◆ unprocessed_packets_input_

SynchronizationWrapper<BasePacketPtr> mal_packet_weaver::PacketDispatcher::unprocessed_packets_input_
private

Queue for storing unprocessed input packets.

Definition at line 383 of file packet-dispatcher.hpp.


The documentation for this class was generated from the following files: