140 lines
3.9 KiB
C++
140 lines
3.9 KiB
C++
#ifndef INCLUDE_PROTOCOL_SESSION_HPP_
|
|
#define INCLUDE_PROTOCOL_SESSION_HPP_
|
|
|
|
#include "disconnection/disconnect_packet.hpp"
|
|
#include <packet_interface.hpp>
|
|
|
|
#include <cstddef>
|
|
#include <spdlog/spdlog.h>
|
|
#include <string>
|
|
#include <sys/socket.h>
|
|
#include <thread>
|
|
#include <unistd.h>
|
|
#include <vector>
|
|
|
|
// Forward declaration to resolve circular dependency
|
|
class ISessionState;
|
|
#include <state_interface.hpp>
|
|
|
|
/**
|
|
* @brief Class representing a session in the MQTT protocol.
|
|
*
|
|
* This class manages the communication session with an MQTT client.
|
|
*/
|
|
class Session {
|
|
public:
|
|
/**
|
|
* @brief Default constructor.
|
|
*/
|
|
Session();
|
|
|
|
/**
|
|
* @brief Constructor initializing the session with a socket file descriptor.
|
|
*
|
|
* @param socket_fd The socket file descriptor.
|
|
*/
|
|
Session(int socket_fd);
|
|
|
|
/**
|
|
* @brief Destructor.
|
|
*/
|
|
~Session();
|
|
|
|
std::string client_id; /**< The client ID associated with the session. */
|
|
|
|
/**
|
|
* @brief Starts listening for incoming data on the session socket.
|
|
*/
|
|
void listen();
|
|
|
|
/**
|
|
* @brief Closes the session.
|
|
*/
|
|
void close();
|
|
|
|
/**
|
|
* @brief Closes the session with a specific reason code.
|
|
*
|
|
* @param reason_code The reason code for disconnection.
|
|
*/
|
|
void close(const DisconnectReasonCode &reason_code);
|
|
|
|
/**
|
|
* @brief Sets the current state of the session.
|
|
*
|
|
* @param state The state to set.
|
|
*/
|
|
void set_state(ISessionState &state);
|
|
|
|
/**
|
|
* @brief Checks if the session is alive.
|
|
*
|
|
* @return True if the session is alive, false otherwise.
|
|
*/
|
|
inline bool is_alive() const { return this->is_session_alive; };
|
|
|
|
/**
|
|
* @brief Checks if the session is connected.
|
|
*
|
|
* @return True if the session is connected, false otherwise.
|
|
*/
|
|
inline bool is_connected() const { return this->is_session_connected; };
|
|
|
|
/**
|
|
* @brief Gets the current packet being processed by the session.
|
|
*
|
|
* @return A pointer to the current packet.
|
|
*/
|
|
inline IPacket *get_current_packet() const { return this->current_packet; };
|
|
|
|
/**
|
|
* @brief Gets the current state of the session.
|
|
*
|
|
* @return A pointer to the current state.
|
|
*/
|
|
inline ISessionState *get_current_state() const { return this->current_state; };
|
|
|
|
/**
|
|
* @brief Sends data over the session socket.
|
|
*
|
|
* @param buffer The data to send.
|
|
* @return The number of bytes sent.
|
|
*/
|
|
std::size_t send(const std::vector<std::byte> &buffer);
|
|
|
|
std::function<void(Session *)> on_connect; /**< Callback function invoked when a client connects. */
|
|
std::function<void(Session *)> on_disconnect; /**< Callback function invoked when a client disconnects. */
|
|
|
|
protected:
|
|
friend class StateConnect;
|
|
friend class StateDisconnect;
|
|
std::atomic<bool> is_session_alive; /**< Flag indicating if the session is alive. */
|
|
std::atomic<bool> is_session_connected; /**< Flag indicating if the session is connected. */
|
|
|
|
private:
|
|
ISessionState *current_state; /**< Pointer to the current session state. */
|
|
IPacket *current_packet; /**< Pointer to the current packet being processed. */
|
|
|
|
int socket; /**< Socket file descriptor for the session. */
|
|
const unsigned int buffer_size = 65535; /**< Max TCP packet bytes accepted in a single receive call. */
|
|
|
|
uint16_t keepalive_sec; /**< Keepalive in seconds to wait for messages in session */
|
|
std::atomic<std::time_t> last_seen; /**< Last UNIX timestamp from when this session has communicated.*/
|
|
std::thread last_seen_thread; /**< Thread for handling unresponsive sessions. */
|
|
void process_last_seen(); /**< Process last seen information, finishing the session */
|
|
|
|
/**
|
|
* @brief Joins the keepalive thread.
|
|
*/
|
|
void join();
|
|
|
|
/**
|
|
* @brief Closes the session if it's not connected within a timeout period.
|
|
*
|
|
* @param timeout_sec Timeout period in seconds.
|
|
*/
|
|
void close_if_not_connected(uint timeout_sec = 10);
|
|
};
|
|
|
|
#endif // INCLUDE_PROTOCOL_SESSION_HPP_
|