mal-packet-weaver
C++20 packet serialization/deserialization library.
Loading...
Searching...
No Matches
session.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "crypto.hpp"
4#include "packet.hpp"
5namespace mal_packet_weaver
6{
7 using PacketReceiverFn = std::function<void(std::unique_ptr<Packet> &&)>;
16 class Session final : public non_copyable_non_movable, public std::enable_shared_from_this<Session>
17 {
18 public:
25 explicit Session(boost::asio::io_context &io, boost::asio::ip::tcp::socket &&socket);
26
30 virtual ~Session();
31
45 template <IsPacket T>
46 bool send_packet(const T &packet_arg) requires std::is_base_of_v<Packet, T>;
47
57 std::unique_ptr<Packet> pop_packet_now();
58
64 boost::asio::awaitable<std::unique_ptr<Packet>> pop_packet_async(boost::asio::io_context &io);
65
71 [[nodiscard]] inline bool has_packets() { return !received_packets_.empty(); }
72
78 [[nodiscard]] inline bool secured() const noexcept { return encryption_ != nullptr; }
79
85 [[nodiscard]] constexpr bool is_closed() const noexcept { return !alive_; }
86
92 [[nodiscard]] constexpr bool alive() const noexcept { return alive_; }
93
97 void setup_encryption(std::shared_ptr<mal_packet_weaver::crypto::EncryptionInterface> encryption)
98 {
99 encryption_ = encryption;
100 }
101
108 {
109 std::lock_guard guard{ packet_receiver_mutex_ };
110 packet_receiver_ = receiver;
111 }
112
118 void Destroy() { alive_ = false; }
119
120 private:
129 std::unique_ptr<ByteArray> pop_packet_data() noexcept;
130
137 boost::asio::awaitable<std::shared_ptr<Session>> get_shared_ptr(boost::asio::io_context &io);
138
144 boost::asio::awaitable<void> send_all(boost::asio::io_context &io);
145
151 boost::asio::awaitable<void> async_packet_forger(boost::asio::io_context &io);
162 boost::asio::awaitable<void> async_packet_sender(boost::asio::io_context &io);
163
181 boost::lockfree::queue<ByteArray *, boost::lockfree::fixed_sized<true>> received_packets_;
185 boost::lockfree::queue<ByteArray *, boost::lockfree::fixed_sized<true>> packets_to_send_;
186
190 bool alive_ = true;
191
195 boost::asio::ip::tcp::tcp::socket socket_;
196
200 std::shared_ptr<mal_packet_weaver::crypto::EncryptionInterface> encryption_ = nullptr;
201
206
211 };
212} // namespace mal_packet_weaver
213#include "session.inl"
Represents a network session for sending and receiving packets.
Definition session.hpp:17
boost::lockfree::queue< ByteArray *, boost::lockfree::fixed_sized< true > > packets_to_send_
Lock-free queue to store packets that are waiting to be sent.
Definition session.hpp:185
std::shared_ptr< mal_packet_weaver::crypto::EncryptionInterface > encryption_
Holder for encryption using EncryptionInterface.
Definition session.hpp:200
std::mutex packet_receiver_mutex_
Mutex to ensure thread-safe access to the packet receiver function.
Definition session.hpp:205
PacketReceiverFn packet_receiver_
Callback function for processing received packets.
Definition session.hpp:210
std::unique_ptr< Packet > pop_packet_now()
Returns the earliest acquired packet. If packet queue is empty, returns nullptr.
Definition session.cpp:49
constexpr bool alive() const noexcept
Checks if the session is alive.
Definition session.hpp:92
boost::asio::awaitable< std::shared_ptr< Session > > get_shared_ptr(boost::asio::io_context &io)
Retrieves a shared pointer to the current session.
Definition session.cpp:112
boost::lockfree::queue< ByteArray *, boost::lockfree::fixed_sized< true > > received_packets_
Lock-free queue to store received packets that are waiting to be processed.
Definition session.hpp:181
void Destroy()
Coroutines use the shared pointer from this, so you need to explicitly call Destroy so alive_ is fals...
Definition session.hpp:118
Session(boost::asio::io_context &io, boost::asio::ip::tcp::socket &&socket)
Constructor for the Session class.
Definition session.cpp:5
boost::asio::ip::tcp::tcp::socket socket_
The TCP socket for network communication.
Definition session.hpp:195
bool send_packet(const T &packet_arg)
Sends any packet derived from DerivedPacket through the network.
Definition session.inl:8
boost::asio::awaitable< void > send_all(boost::asio::io_context &io)
Asynchronously sends all packets in the queue through the network.
Definition session.cpp:139
constexpr bool is_closed() const noexcept
Checks if the session is closed.
Definition session.hpp:85
boost::asio::awaitable< void > async_packet_sender(boost::asio::io_context &io)
Asynchronously receives and processes incoming packets from the network.
Definition session.cpp:332
boost::asio::awaitable< std::unique_ptr< Packet > > pop_packet_async(boost::asio::io_context &io)
Definition session.cpp:78
std::unique_ptr< ByteArray > pop_packet_data() noexcept
Pops the packet data from the received packets queue.
Definition session.cpp:98
boost::asio::awaitable< void > async_packet_forger(boost::asio::io_context &io)
Asynchronously forges new packets from the buffer.
Definition session.cpp:236
bool has_packets()
Checks if there are packets in the queue.
Definition session.hpp:71
virtual ~Session()
Destructor for the Session class.
Definition session.cpp:33
void setup_encryption(std::shared_ptr< mal_packet_weaver::crypto::EncryptionInterface > encryption)
Sets up encryption for the session using provided encryption interface.
Definition session.hpp:97
bool secured() const noexcept
Checks if the session is secured using encryption.
Definition session.hpp:78
bool alive_
Indicates whether the session is alive and operational.
Definition session.hpp:190
void set_packet_receiver(PacketReceiverFn const receiver)
Sets the packet receiver for the session.
Definition session.hpp:107
A class that can't be copied neither moved.
This is the main namespace for the Mal Packet Weaver library.
Definition common.hpp:42
std::function< void(std::unique_ptr< Packet > &&)> PacketReceiverFn
Definition session.hpp:7
STL namespace.
A dynamically resizable array of bytes.