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

Represents a session with packet dispatching functionality. More...

#include "dispatcher-session.hpp"

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

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< Packetpop_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.
 
Sessionsession ()
 Get a reference to the underlying session.
 
PacketDispatcherdispatcher ()
 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< Sessionsession_
 The underlying session.
 
std::shared_ptr< PacketDispatcherdispatcher_
 The underlying packet dispatcher.
 

Detailed Description

Represents a session with packet dispatching functionality.

Definition at line 10 of file dispatcher-session.hpp.

Constructor & Destructor Documentation

◆ DispatcherSession()

mal_packet_weaver::DispatcherSession::DispatcherSession ( boost::asio::io_context & io_context,
boost::asio::ip::tcp::socket && socket )
inline

Constructor for DispatcherSession.

Parameters
io_contextThe IO context to use for the session.
socketThe TCP socket to use for the session.

Definition at line 18 of file dispatcher-session.hpp.

◆ ~DispatcherSession()

mal_packet_weaver::DispatcherSession::~DispatcherSession ( )
inline

Destructor for DispatcherSession. Cleans up the session and packet dispatcher.

Definition at line 31 of file dispatcher-session.hpp.

Member Function Documentation

◆ alive()

bool mal_packet_weaver::DispatcherSession::alive ( ) const
inlinenodiscardconstexprnoexcept

Checks if the session is alive.

Returns
true if the session is alive, false otherwise.
See also
Session::alive

Definition at line 117 of file dispatcher-session.hpp.

◆ await_packet() [1/2]

template<IsPacket DerivedPacket>
boost::asio::awaitable< std::unique_ptr< DerivedPacket > > mal_packet_weaver::DispatcherSession::await_packet ( float timeout = -1.0f)
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.

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 164 of file dispatcher-session.hpp.

◆ await_packet() [2/2]

template<IsPacket DerivedPacket>
boost::asio::awaitable< std::unique_ptr< DerivedPacket > > mal_packet_weaver::DispatcherSession::await_packet ( PacketFilterFunc< DerivedPacket > filter,
float timeout = -1.0f )
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.

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 188 of file dispatcher-session.hpp.

◆ Destroy()

void mal_packet_weaver::DispatcherSession::Destroy ( )
inline

Definition at line 484 of file dispatcher-session.hpp.

◆ dispatcher()

PacketDispatcher & mal_packet_weaver::DispatcherSession::dispatcher ( )
inlinenodiscard

Get a reference to the underlying packet dispatcher.

Returns
A reference to the packet dispatcher.

Definition at line 482 of file dispatcher-session.hpp.

◆ enqueue_filter_promise()

void mal_packet_weaver::DispatcherSession::enqueue_filter_promise ( UniquePacketID packet_id,
PacketDispatcher::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 467 of file dispatcher-session.hpp.

◆ enqueue_packet()

void mal_packet_weaver::DispatcherSession::enqueue_packet ( PacketDispatcher::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 144 of file dispatcher-session.hpp.

◆ enqueue_promise()

void mal_packet_weaver::DispatcherSession::enqueue_promise ( UniquePacketID packet_id,
PacketDispatcher::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 451 of file dispatcher-session.hpp.

◆ has_packets()

bool mal_packet_weaver::DispatcherSession::has_packets ( )
inlinenodiscard

Checks if there are packets in the queue.

Returns
true if there are packets in the queue, false otherwise.
See also
Session::has_packets

Definition at line 93 of file dispatcher-session.hpp.

◆ is_closed()

bool mal_packet_weaver::DispatcherSession::is_closed ( ) const
inlinenodiscardconstexprnoexcept

Checks if the session is closed.

Returns
true if the session is closed, false otherwise.
See also
Session::is_closed

Definition at line 109 of file dispatcher-session.hpp.

◆ pop_packet_async()

boost::asio::awaitable< std::unique_ptr< Packet > > mal_packet_weaver::DispatcherSession::pop_packet_async ( )
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.

See also
Session::pop_packet_async

Definition at line 81 of file dispatcher-session.hpp.

◆ pop_packet_now()

std::unique_ptr< Packet > mal_packet_weaver::DispatcherSession::pop_packet_now ( )
inline

Returns the earliest acquired packet. If packet queue is empty, returns nullptr.

Warning
If packet receiver is set through SetPacketReceiver there's no reason to call this function. Generally packets will just go through the receiver. There's no ordering neither option to configure which packet you will receive.
Returns
std::unique_ptr<Packet>
See also
Session::pop_packet_now

Definition at line 73 of file dispatcher-session.hpp.

◆ register_default_handler() [1/4]

template<typename Arg1 , IsPacket CustomPacket>
void mal_packet_weaver::DispatcherSession::register_default_handler ( PacketHandlerFunc< CustomPacket, Arg1 > && handler,
PacketFilterFunc< CustomPacket, Arg1 > && filter = {},
float delay = 0.0f )
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.

Template Parameters
Arg1The type of argument 1 for the handler.
ArgsAdditional argument types for the handler.
CustomPacketThe 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.
delayThe delay in seconds before the handler is executed. (Default is 0.0)
Note
Supported types for Args are: Session&, std::shared_ptr<Session>, io_context &, std::shared_ptr<PacketDispatcher>, and PacketDispatcher&. The std::unique_ptr<CustomPacket> type should be last.

Definition at line 239 of file dispatcher-session.hpp.

◆ register_default_handler() [2/4]

template<typename Arg1 , typename Arg2 , typename... Args, IsPacket CustomPacket>
void mal_packet_weaver::DispatcherSession::register_default_handler ( PacketHandlerFunc< CustomPacket, Arg1, Arg2, Args... > && handler,
PacketFilterFunc< CustomPacket, Arg1, Arg2, Args... > && filter = {},
float delay = 0.0f )
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.

Template Parameters
Arg1The type of argument 1 for the handler.
Arg2The type of argument 2 for the handler.
ArgsAdditional argument types for the handler.
CustomPacketThe 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.
delayThe delay in seconds before the handler is executed. (Default is 0.0)
Note
Supported types for Args are: Session&, std::shared_ptr<Session>, io_context &, std::shared_ptr<PacketDispatcher>, and PacketDispatcher&. The std::unique_ptr<CustomPacket> type should be last.

Definition at line 331 of file dispatcher-session.hpp.

◆ register_default_handler() [3/4]

template<IsPacket DerivedPacket>
void mal_packet_weaver::DispatcherSession::register_default_handler ( PacketHandlerFunc< DerivedPacket > handler,
PacketFilterFunc< DerivedPacket > filter = {},
float delay = 0.0f )
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.

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. If it returns false, the packet will be passed to the next handler.
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 213 of file dispatcher-session.hpp.

Here is the caller graph for this function:

◆ register_default_handler() [4/4]

template<IsPacket CustomPacket, typename... FnArgs>
void mal_packet_weaver::DispatcherSession::register_default_handler ( std::function< void(FnArgs...)> && handler,
std::function< bool(FnArgs...)> && filter = {},
float delay = 0.0f )
inline

Definition at line 436 of file dispatcher-session.hpp.

Here is the call graph for this function:

◆ secured()

bool mal_packet_weaver::DispatcherSession::secured ( ) const
inlinenodiscardnoexcept

Checks if the session is secured using encryption.

Returns
true if the session is secured, false otherwise.
See also
Session::secured

Definition at line 101 of file dispatcher-session.hpp.

◆ send_packet()

template<IsPacket T>
requires std::is_base_of_v<Packet, T>
bool mal_packet_weaver::DispatcherSession::send_packet ( const T & packet_arg)
inline

Sends any packet derived from DerivedPacket through the network.

Template Parameters
TFinal packet type. (Template functions cannot be overriden, we need to call serialize from the furthest child.)
Note
Blockable until packets_to_send_ can retrieve the value.
Parameters
packet_argPacket value
Returns
true if session got the packet.
false if session was closed.
See also
Session::send_packet

Definition at line 58 of file dispatcher-session.hpp.

◆ session()

Session & mal_packet_weaver::DispatcherSession::session ( )
inlinenodiscard

Get a reference to the underlying session.

Returns
A reference to the session.

Definition at line 476 of file dispatcher-session.hpp.

◆ set_packet_receiver()

void mal_packet_weaver::DispatcherSession::set_packet_receiver ( PacketReceiverFn const receiver)
inline

Sets the packet receiver for the session.

Parameters
receiverThe function to be called when a packet is received.
See also
Session::set_packet_receiver

Definition at line 134 of file dispatcher-session.hpp.

◆ setup_encryption()

void mal_packet_weaver::DispatcherSession::setup_encryption ( std::shared_ptr< mal_packet_weaver::crypto::EncryptionInterface > encryption)
inline

Sets up encryption for the session using provided encryption interface.

See also
Session::setup_encryption

Definition at line 123 of file dispatcher-session.hpp.

Member Data Documentation

◆ dispatcher_

std::shared_ptr<PacketDispatcher> mal_packet_weaver::DispatcherSession::dispatcher_
private

The underlying packet dispatcher.

Definition at line 493 of file dispatcher-session.hpp.

◆ io_context_

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

Reference to the associated Boost.Asio io_context.

Definition at line 491 of file dispatcher-session.hpp.

◆ session_

std::shared_ptr<Session> mal_packet_weaver::DispatcherSession::session_
private

The underlying session.

Definition at line 492 of file dispatcher-session.hpp.


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