mal-packet-weaver
C++20 packet serialization/deserialization library.
|
Represents a network session for sending and receiving packets. More...
#include "session.hpp"
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< 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 (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< ByteArray > | pop_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::EncryptionInterface > | encryption_ = 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. | |
Represents a network session for sending and receiving packets.
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.
|
explicit |
Constructor for the Session class.
io | The boost::asio::io_context used for I/O operations. |
socket | The boost::asio::ip::tcp::socket associated with the session. |
Definition at line 5 of file session.cpp.
|
virtual |
Destructor for the Session class.
Definition at line 33 of file session.cpp.
|
inlinenodiscardconstexprnoexcept |
Checks if the session is alive.
Definition at line 92 of file session.hpp.
|
private |
Asynchronously forges new packets from the buffer.
io | The boost::asio::io_context used for asynchronous operations. |
Definition at line 236 of file session.cpp.
|
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.
io | The boost::asio::io_context used for asynchronous operations. |
Definition at line 332 of file session.cpp.
|
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.
|
private |
Retrieves a shared pointer to the current session.
io | The boost::asio::io_context used for asynchronous operations. |
Definition at line 112 of file session.cpp.
|
inlinenodiscard |
Checks if there are packets in the queue.
Definition at line 71 of file session.hpp.
|
inlinenodiscardconstexprnoexcept |
Checks if the session is closed.
Definition at line 85 of file session.hpp.
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.
|
privatenoexcept |
Pops the packet data from the received packets queue.
This function retrieves the data of the earliest acquired packet from the queue.
Definition at line 98 of file session.cpp.
std::unique_ptr< Packet > mal_packet_weaver::Session::pop_packet_now | ( | ) |
Returns the earliest acquired packet. If packet queue is empty, returns nullptr.
Definition at line 49 of file session.cpp.
|
inlinenodiscardnoexcept |
Checks if the session is secured using encryption.
Definition at line 78 of file session.hpp.
|
private |
Asynchronously sends all packets in the queue through the network.
io | The boost::asio::io_context used for asynchronous operations. |
Definition at line 139 of file session.cpp.
bool mal_packet_weaver::Session::send_packet | ( | const T & | packet_arg | ) |
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 8 of file session.inl.
|
inline |
Sets the packet receiver for the session.
receiver | The function to be called when a packet is received. |
Definition at line 107 of file session.hpp.
|
inline |
Sets up encryption for the session using provided encryption interface.
Definition at line 97 of file session.hpp.
|
private |
Indicates whether the session is alive and operational.
Definition at line 190 of file session.hpp.
|
private |
Holder for encryption using EncryptionInterface.
Definition at line 200 of file session.hpp.
|
private |
Callback function for processing received packets.
Definition at line 210 of file session.hpp.
|
private |
Mutex to ensure thread-safe access to the packet receiver function.
Definition at line 205 of file session.hpp.
|
private |
Lock-free queue to store packets that are waiting to be sent.
Definition at line 185 of file session.hpp.
|
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.
Definition at line 181 of file session.hpp.
|
private |
The TCP socket for network communication.
Definition at line 195 of file session.hpp.