Horizon Official Technical Documentation
Horizon::Char::CharSession Class Reference

#include <CharSession.hpp>

+ Inheritance diagram for Horizon::Char::CharSession:
+ Collaboration diagram for Horizon::Char::CharSession:

Public Member Functions

 CharSession (uint64_t uid)
 
 ~CharSession ()
 
void initialize ()
 
void update (uint32_t diff)
 
void perform_cleanup ()
 
std::unique_ptr< CharClientInterface > & clif ()
 
std::unique_ptr< ClientPacketLengthTable > & pkt_tbl ()
 
s_session_dataget_session_data ()
 
void set_session_data (s_session_data &data)
 
void transmit_buffer (ByteBuffer _buffer, std::size_t size)
 
- Public Member Functions inherited from Horizon::Networking::Session< CharSocket, CharSession >
 Session (uint64_t uid)
 
virtual ~Session ()
 
std::shared_ptr< CharSocket > get_socket ()
 Get the socket. More...
 
void set_socket (std::weak_ptr< CharSocket > socket)
 Set the socket. More...
 
virtual void update (uint32_t diff)=0
 
virtual void initialize ()=0
 
bool is_initialized ()
 Called to verify if the session is initialized. More...
 
void set_initialized (bool initialized)
 Set whether the session is initialized or not. More...
 
uint64_t get_session_id ()
 Get the unique id of the session. More...
 
ThreadSafeQueue< ByteBuffer > & get_recv_queue ()
 Receive queue of the buffer received by the socket. More...
 

Protected Attributes

std::unique_ptr< CharClientInterface_clif
 
std::unique_ptr< ClientPacketLengthTable_pkt_tbl
 
s_session_data _session_data
 
std::mutex _sd_mutex
 
bool _first_packet_sent {false}
 

Constructor & Destructor Documentation

◆ CharSession()

◆ ~CharSession()

CharSession::~CharSession ( )
43{
44 //
45}

Member Function Documentation

◆ clif()

std::unique_ptr< CharClientInterface > & Horizon::Char::CharSession::clif ( )
inline
77{ return _clif; }
std::unique_ptr< CharClientInterface > _clif
Definition: CharSession.hpp:87

References _clif.

◆ get_session_data()

s_session_data & Horizon::Char::CharSession::get_session_data ( )
inline
80{ std::lock_guard<std::mutex> lock(_sd_mutex); return _session_data; }
std::mutex _sd_mutex
Definition: CharSession.hpp:90
s_session_data _session_data
Definition: CharSession.hpp:89

References _sd_mutex, and _session_data.

◆ initialize()

void CharSession::initialize ( )
virtual

Called when the session is started. Virtual method that must be implmented by the derived class. Typically used to initialize the session.

Returns
void

Implements Horizon::Networking::Session< CharSocket, CharSession >.

48{
49 _pkt_tbl = std::make_unique<ClientPacketLengthTable>(shared_from_this());
50 _clif = std::make_unique<CharClientInterface>(shared_from_this());
51 set_initialized(true);
52}
std::unique_ptr< ClientPacketLengthTable > _pkt_tbl
Definition: CharSession.hpp:88
void set_initialized(bool initialized)
Set whether the session is initialized or not.
Definition: Session.hpp:93

References _clif, _pkt_tbl, and Horizon::Networking::Session< CharSocket, CharSession >::set_initialized().

+ Here is the call graph for this function:

◆ perform_cleanup()

void Horizon::Char::CharSession::perform_cleanup ( )

◆ pkt_tbl()

std::unique_ptr< ClientPacketLengthTable > & Horizon::Char::CharSession::pkt_tbl ( )
inline
78{ return _pkt_tbl; }

References _pkt_tbl.

◆ set_session_data()

void Horizon::Char::CharSession::set_session_data ( s_session_data data)
inline
81{ std::lock_guard<std::mutex> lock(_sd_mutex); _session_data = data; }

References _sd_mutex, and _session_data.

◆ transmit_buffer()

void CharSession::transmit_buffer ( ByteBuffer  _buffer,
std::size_t  size 
)
55{
56 if (get_socket() == nullptr || !get_socket()->is_open())
57 return;
58
59 if (!_buffer.is_empty()) {
60 // The first packet in the character server has no packet_id
61 // and is the account id so its length is not deduceable as it can't be looked up.
62 if(_first_packet_sent == true) {
63 uint16_t packet_id = 0x0;
64 int16_t packet_len = 0;
65
66 memcpy(&packet_id, _buffer.get_read_pointer(), sizeof(int16_t));
67
68 TPacketTablePairType p = _pkt_tbl->get_tpacket_info(packet_id);
69
70 if (p.first == -1) {
71 memcpy(&packet_len, _buffer.get_read_pointer() + 2, sizeof(int16_t));
72 } else {
73 packet_len = p.first;
74 }
75
76 if (packet_id == 0x0000) {
77 HLog(warning) << "Server tried to send a disabled packet... ignoring.";
78 return;
79 }
80
81 if (packet_len != _buffer.active_length()) {
82 HLog(warning) << "Packet 0x" << std::hex << packet_id << " has length len " << std::dec << packet_len << " but buffer has " << _buffer.active_length() << " bytes... ignoring.";
83 return;
84 }
85 } else {
86 _first_packet_sent = true;
87 }
88
89 get_socket()->queue_buffer(std::move(_buffer));
90 }
91}
#define HLog(type)
Definition: Logger.hpp:122
bool is_empty() const
Definition: ByteBuffer.hpp:336
uint8_t * get_read_pointer()
Definition: ByteBuffer.hpp:327
size_t active_length() const
Definition: ByteBuffer.hpp:333
bool _first_packet_sent
Definition: CharSession.hpp:91
std::shared_ptr< CharSocket > get_socket()
Get the socket.
Definition: Session.hpp:70
std::pair< int16_t, TPacketStructPtrType > TPacketTablePairType
Definition: PacketLengthTable.hpp:42

References _first_packet_sent, _pkt_tbl, ByteBuffer::active_length(), ByteBuffer::get_read_pointer(), Horizon::Networking::Session< CharSocket, CharSession >::get_socket(), HLog, and ByteBuffer::is_empty().

+ Here is the call graph for this function:

◆ update()

void CharSession::update ( uint32_t  diff)
virtual

Called when the session is started. Virtual method that must be implmented by the derived class. Typically used to update the session and process packets.

Returns
void

Implements Horizon::Networking::Session< CharSocket, CharSession >.

94{
95 std::shared_ptr<ByteBuffer> read_buf;
96 while ((read_buf = get_recv_queue().try_pop())) {
97 uint16_t packet_id = 0x0;
98 memcpy(&packet_id, read_buf->get_read_pointer(), sizeof(uint16_t));
99 HPacketTablePairType p = _pkt_tbl->get_hpacket_info(packet_id);
100
101 HLog(debug) << "Handling packet 0x" << std::hex << packet_id << " - 0x" << p.first << std::endl;
102
103 p.second->handle(std::move(*read_buf));
104 }
105}
ThreadSafeQueue< ByteBuffer > & get_recv_queue()
Receive queue of the buffer received by the socket.
Definition: Session.hpp:101
std::pair< int16_t, HPacketStructPtrType > HPacketTablePairType
Definition: PacketLengthTable.hpp:41

References _pkt_tbl, Horizon::Networking::Session< CharSocket, CharSession >::get_recv_queue(), and HLog.

+ Here is the call graph for this function:

Member Data Documentation

◆ _clif

std::unique_ptr<CharClientInterface> Horizon::Char::CharSession::_clif
protected

Referenced by clif(), and initialize().

◆ _first_packet_sent

bool Horizon::Char::CharSession::_first_packet_sent {false}
protected

Referenced by transmit_buffer().

◆ _pkt_tbl

std::unique_ptr<ClientPacketLengthTable> Horizon::Char::CharSession::_pkt_tbl
protected

◆ _sd_mutex

std::mutex Horizon::Char::CharSession::_sd_mutex
protected

◆ _session_data

s_session_data Horizon::Char::CharSession::_session_data
protected

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