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

Represents a network session for sending and receiving packets. More...

#include "session.hpp"

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

Public Member Functions

 Session (boost::asio::io_context &io, boost::asio::ip::tcp::socket &&socket)
 Constructor for the Session class.
 
virtual ~Session ()
 Destructor for the Session class.
 
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 (boost::asio::io_context &io)
 
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 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

std::unique_ptr< ByteArraypop_packet_data () noexcept
 Pops the packet data from the received packets queue.
 
boost::asio::awaitable< std::shared_ptr< Session > > get_shared_ptr (boost::asio::io_context &io)
 Retrieves a shared pointer to the current session.
 
boost::asio::awaitable< void > send_all (boost::asio::io_context &io)
 Asynchronously sends all packets in the queue through the network.
 
boost::asio::awaitable< void > async_packet_forger (boost::asio::io_context &io)
 Asynchronously forges new packets from the buffer.
 
boost::asio::awaitable< void > async_packet_sender (boost::asio::io_context &io)
 Asynchronously receives and processes incoming packets from the network.
 

Private Attributes

boost::lockfree::queue< ByteArray *, boost::lockfree::fixed_sized< true > > received_packets_
 Lock-free queue to store received packets that are waiting to be processed.
 
boost::lockfree::queue< ByteArray *, boost::lockfree::fixed_sized< true > > packets_to_send_
 Lock-free queue to store packets that are waiting to be sent.
 
bool alive_ = true
 Indicates whether the session is alive and operational.
 
boost::asio::ip::tcp::tcp::socket socket_
 The TCP socket for network communication.
 
std::shared_ptr< mal_packet_weaver::crypto::EncryptionInterfaceencryption_ = nullptr
 Holder for encryption using EncryptionInterface.
 
std::mutex packet_receiver_mutex_
 Mutex to ensure thread-safe access to the packet receiver function.
 
PacketReceiverFn packet_receiver_
 Callback function for processing received packets.
 

Detailed Description

Represents a network session for sending and receiving packets.

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

Constructor & Destructor Documentation

◆ Session()

mal_packet_weaver::Session::Session ( boost::asio::io_context & io,
boost::asio::ip::tcp::socket && socket )
explicit

Constructor for the Session class.

Parameters
ioThe boost::asio::io_context used for I/O operations.
socketThe boost::asio::ip::tcp::socket associated with the session.

Definition at line 5 of file session.cpp.

Here is the call graph for this function:

◆ ~Session()

mal_packet_weaver::Session::~Session ( )
virtual

Destructor for the Session class.

Definition at line 33 of file session.cpp.

Member Function Documentation

◆ alive()

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

Checks if the session is alive.

Returns
true if the session is alive, false otherwise.

Definition at line 92 of file session.hpp.

◆ async_packet_forger()

boost::asio::awaitable< void > mal_packet_weaver::Session::async_packet_forger ( boost::asio::io_context & io)
private

Asynchronously forges new packets from the buffer.

Parameters
ioThe boost::asio::io_context used for asynchronous operations.

Definition at line 236 of file session.cpp.

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

◆ async_packet_sender()

boost::asio::awaitable< void > mal_packet_weaver::Session::async_packet_sender ( boost::asio::io_context & io)
private

Asynchronously receives and processes incoming packets from the network.

This function continuously waits for incoming packets from the network and processes them. It decrypts and deserializes encrypted packets if encryption is enabled. If a valid packet receiver is set, it invokes the receiver's callback function to handle the received packet.

Parameters
ioThe boost::asio::io_context used for asynchronous operations.

Definition at line 332 of file session.cpp.

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

◆ Destroy()

void mal_packet_weaver::Session::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 118 of file session.hpp.

◆ get_shared_ptr()

boost::asio::awaitable< std::shared_ptr< Session > > mal_packet_weaver::Session::get_shared_ptr ( boost::asio::io_context & io)
private

Retrieves a shared pointer to the current session.

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

Definition at line 112 of file session.cpp.

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

◆ has_packets()

bool mal_packet_weaver::Session::has_packets ( )
inlinenodiscard

Checks if there are packets in the queue.

Returns
true if there are packets in the queue, false otherwise.

Definition at line 71 of file session.hpp.

◆ is_closed()

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

Checks if the session is closed.

Returns
true if the session is closed, false otherwise.

Definition at line 85 of file session.hpp.

◆ pop_packet_async()

boost::asio::awaitable< std::unique_ptr< Packet > > mal_packet_weaver::Session::pop_packet_async ( boost::asio::io_context & io)

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 78 of file session.cpp.

Here is the call graph for this function:

◆ pop_packet_data()

std::unique_ptr< ByteArray > mal_packet_weaver::Session::pop_packet_data ( )
privatenoexcept

Pops the packet data from the received packets queue.

This function retrieves the data of the earliest acquired packet from the queue.

Returns
A unique_ptr<ByteArray> containing the packet data, or nullptr if the queue is empty.

Definition at line 98 of file session.cpp.

Here is the caller graph for this function:

◆ pop_packet_now()

std::unique_ptr< Packet > mal_packet_weaver::Session::pop_packet_now ( )

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

Warning
If packet receiver is set through set_packet_receiver 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>

Definition at line 49 of file session.cpp.

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

◆ secured()

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

Checks if the session is secured using encryption.

Returns
true if the session is secured, false otherwise.

Definition at line 78 of file session.hpp.

◆ send_all()

boost::asio::awaitable< void > mal_packet_weaver::Session::send_all ( boost::asio::io_context & io)
private

Asynchronously sends all packets in the queue through the network.

Parameters
ioThe boost::asio::io_context used for asynchronous operations.

Definition at line 139 of file session.cpp.

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

◆ send_packet()

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

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.

Definition at line 8 of file session.inl.

Here is the call graph for this function:

◆ set_packet_receiver()

void mal_packet_weaver::Session::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.

Definition at line 107 of file session.hpp.

◆ setup_encryption()

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

Sets up encryption for the session using provided encryption interface.

Definition at line 97 of file session.hpp.

Member Data Documentation

◆ alive_

bool mal_packet_weaver::Session::alive_ = true
private

Indicates whether the session is alive and operational.

Definition at line 190 of file session.hpp.

◆ encryption_

std::shared_ptr<mal_packet_weaver::crypto::EncryptionInterface> mal_packet_weaver::Session::encryption_ = nullptr
private

Holder for encryption using EncryptionInterface.

Definition at line 200 of file session.hpp.

◆ packet_receiver_

PacketReceiverFn mal_packet_weaver::Session::packet_receiver_
private

Callback function for processing received packets.

Definition at line 210 of file session.hpp.

◆ packet_receiver_mutex_

std::mutex mal_packet_weaver::Session::packet_receiver_mutex_
private

Mutex to ensure thread-safe access to the packet receiver function.

Definition at line 205 of file session.hpp.

◆ packets_to_send_

boost::lockfree::queue<ByteArray *, boost::lockfree::fixed_sized<true> > mal_packet_weaver::Session::packets_to_send_
private

Lock-free queue to store packets that are waiting to be sent.

Definition at line 185 of file session.hpp.

◆ received_packets_

boost::lockfree::queue<ByteArray *, boost::lockfree::fixed_sized<true> > mal_packet_weaver::Session::received_packets_
private

Lock-free queue to store received packets that are waiting to be processed.

Packets stored in this queue should be created using 'new'. After popping the pointer, you can either delete it manually or wrap it in smart pointers. Be sure to release the smart pointer before pushing it again, as failing to do so could lead to undefined behavior.

Todo
Implement a circular buffer and ByteView handler for the lock-free queue. The ByteView handler should hold a simple pointer to a circular buffer and a ByteView. The circular buffer should automatically free memory allocated by the ByteViewHandler::free() method. This approach optimizes memory usage by avoiding repeated allocation and deallocation from the OS. Another option is to use a default queue of shared pointers, which automatically handles deallocation. The final choice may affect performance and memory usage, and further testing is needed.
Warning
The current implementation is a proof-of-concept and has important TODOs.

Definition at line 181 of file session.hpp.

◆ socket_

boost::asio::ip::tcp::tcp::socket mal_packet_weaver::Session::socket_
private

The TCP socket for network communication.

Definition at line 195 of file session.hpp.


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