25 explicit Session(boost::asio::io_context &io, boost::asio::ip::tcp::socket &&socket);
46 bool send_packet(
const T &packet_arg)
requires std::is_base_of_v<Packet, T>;
64 boost::asio::awaitable<std::unique_ptr<Packet>>
pop_packet_async(boost::asio::io_context &io);
92 [[nodiscard]]
constexpr bool alive() const noexcept {
return alive_; }
97 void setup_encryption(std::shared_ptr<mal_packet_weaver::crypto::EncryptionInterface> encryption)
144 boost::asio::awaitable<
void>
send_all(boost::asio::io_context &io);
213#include "session.inl"
Represents a network session for sending and receiving packets.
boost::lockfree::queue< ByteArray *, boost::lockfree::fixed_sized< true > > packets_to_send_
Lock-free queue to store packets that are waiting to be sent.
std::shared_ptr< mal_packet_weaver::crypto::EncryptionInterface > encryption_
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.
std::unique_ptr< Packet > pop_packet_now()
Returns the earliest acquired packet. If packet queue is empty, returns nullptr.
constexpr bool alive() const noexcept
Checks if the session is alive.
boost::asio::awaitable< std::shared_ptr< Session > > get_shared_ptr(boost::asio::io_context &io)
Retrieves a shared pointer to the current session.
boost::lockfree::queue< ByteArray *, boost::lockfree::fixed_sized< true > > received_packets_
Lock-free queue to store received packets that are waiting to be processed.
void Destroy()
Coroutines use the shared pointer from this, so you need to explicitly call Destroy so alive_ is fals...
Session(boost::asio::io_context &io, boost::asio::ip::tcp::socket &&socket)
Constructor for the Session class.
boost::asio::ip::tcp::tcp::socket socket_
The TCP socket for network communication.
bool send_packet(const T &packet_arg)
Sends any packet derived from DerivedPacket through the network.
boost::asio::awaitable< void > send_all(boost::asio::io_context &io)
Asynchronously sends all packets in the queue through the network.
constexpr bool is_closed() const noexcept
Checks if the session is closed.
boost::asio::awaitable< void > async_packet_sender(boost::asio::io_context &io)
Asynchronously receives and processes incoming packets from the network.
boost::asio::awaitable< std::unique_ptr< Packet > > pop_packet_async(boost::asio::io_context &io)
std::unique_ptr< ByteArray > pop_packet_data() noexcept
Pops the packet data from the received packets queue.
boost::asio::awaitable< void > async_packet_forger(boost::asio::io_context &io)
Asynchronously forges new packets from the buffer.
bool has_packets()
Checks if there are packets in the queue.
virtual ~Session()
Destructor for the Session class.
void setup_encryption(std::shared_ptr< mal_packet_weaver::crypto::EncryptionInterface > encryption)
Sets up encryption for the session using provided encryption interface.
bool secured() const noexcept
Checks if the session is secured using encryption.
bool alive_
Indicates whether the session is alive and operational.
void set_packet_receiver(PacketReceiverFn const receiver)
Sets the packet receiver for the session.
This is the main namespace for the Mal Packet Weaver library.
std::function< void(std::unique_ptr< Packet > &&)> PacketReceiverFn