Horizon Official Technical Documentation
Horizon::Zone::ZoneSession Class Reference

#include <ZoneSession.hpp>

+ Inheritance diagram for Horizon::Zone::ZoneSession:
+ Collaboration diagram for Horizon::Zone::ZoneSession:

Public Member Functions

 ZoneSession (uint64_t uid)
 
 ~ZoneSession ()
 
void transmit_buffer (ByteBuffer _buffer, std::size_t size)
 Queues a buffer to be sent to the client. More...
 
void update (uint32_t diff)
 Update loop for each Zone Session. More...
 
void perform_cleanup ()
 Performs generic logout of player in cases where the connection was closed abruptly or by instruction. More...
 
void initialize ()
 Initializes the zone session's members. More...
 
std::unique_ptr< ZoneClientInterface > & clif ()
 
std::unique_ptr< ClientPacketLengthTable > & pkt_tbl ()
 
std::shared_ptr< Units::Playerplayer ()
 
void set_player (std::shared_ptr< Units::Player > pl)
 
std::string get_map_name ()
 
void set_map_name (std::string map_name)
 
- Public Member Functions inherited from Horizon::Networking::Session< ZoneSocket, ZoneSession >
 Session (uint64_t uid)
 
virtual ~Session ()
 
std::shared_ptr< ZoneSocket > get_socket ()
 Get the socket. More...
 
void set_socket (std::weak_ptr< ZoneSocket > 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< ZoneClientInterface_clif
 
std::unique_ptr< ClientPacketLengthTable_pkt_tbl
 
std::shared_ptr< Units::Player_player
 
std::string _map_name {""}
 

Detailed Description

ZoneSession is responsible for handling the session of a client.

Constructor & Destructor Documentation

◆ ZoneSession()

ZoneSession::ZoneSession ( uint64_t  uid)

This is the constructor of the class ZoneSession.

Parameters
uidis a int64_t variable which is used to handle the unique id of the session.
socketis a shared pointer of type ZoneSocket which is used to handle the socket.
40: Session(uid)
41{
42}
Session(uint64_t uid)
Definition: Session.hpp:57

◆ ~ZoneSession()

ZoneSession::~ZoneSession ( )

This is the destructor of the class ZoneSession.

Returns
void
45{
46 //
47}

Member Function Documentation

◆ clif()

std::unique_ptr< ZoneClientInterface > & Horizon::Zone::ZoneSession::clif ( )
inline

This function is a getter function which is used to get the client interface.

Returns
std::unique_ptr<ZoneClientInterface> &
101{ return _clif; }
std::unique_ptr< ZoneClientInterface > _clif
Definition: ZoneSession.hpp:120

References _clif.

◆ get_map_name()

std::string Horizon::Zone::ZoneSession::get_map_name ( )
inline

This function is a getter function which is used to get the map name.

117{ return _map_name; }
std::string _map_name
Definition: ZoneSession.hpp:123

References _map_name.

◆ initialize()

void ZoneSession::initialize ( )
virtual

Initializes the zone session's members.

This function is used to handle the initialization of the session.

Returns
void

@thread Initialized from the Network Thread after socket and session creation.

Implements Horizon::Networking::Session< ZoneSocket, ZoneSession >.

54{
55 try {
56 // Initialize the packet length table. This is used to determine the length of the packet from the packet id.
57 _pkt_tbl = std::make_unique<ClientPacketLengthTable>(shared_from_this());
58 // Initialize the packet handler. This is used to handle the packets received from the client.
59 _clif = std::make_unique<ZoneClientInterface>(shared_from_this());
60 // It is the responsibility of this method to initialize the player, but
61 // since player is the map owning thread's responsibility, we wait for the
62 // session to start by letting the client send a CZ_ENTER packet.
63 // When the packet is sent, the player will be created by the Networking Thread.
64 // Initialized by the GameLogicProcess.
65 set_initialized(true);
66 }
67 catch (std::exception& error) {
68 HLog(error) << "ZoneSession::initialize: " << error.what();
69 }
70}
#define HLog(type)
Definition: Logger.hpp:122
void set_initialized(bool initialized)
Set whether the session is initialized or not.
Definition: Session.hpp:93
std::unique_ptr< ClientPacketLengthTable > _pkt_tbl
Definition: ZoneSession.hpp:121

References _clif, _pkt_tbl, HLog, and Horizon::Networking::Session< ZoneSocket, ZoneSession >::set_initialized().

+ Here is the call graph for this function:

◆ perform_cleanup()

void ZoneSession::perform_cleanup ( )

Performs generic logout of player in cases where the connection was closed abruptly or by instruction.

This function is used to handle the cleanup of the session.

Returns
void

Also marks the player for removal from the MapContainerThread. @thread Called from the NetworkThread.

143{
144 //sZone->get_component_of_type<GameLogicProcess>(Horizon::System::RUNTIME_GAMELOGIC)->manage_session_in_map(SESSION_ACTION_LOGOUT_AND_REMOVE, get_map_name(), shared_from_this());
145}

◆ pkt_tbl()

std::unique_ptr< ClientPacketLengthTable > & Horizon::Zone::ZoneSession::pkt_tbl ( )
inline

This function is a getter function which is used to get the client packet length table.

Returns
std::unique_ptr<ClientPacketLengthTable> &
105{ return _pkt_tbl; }

References _pkt_tbl.

◆ player()

std::shared_ptr< Units::Player > Horizon::Zone::ZoneSession::player ( )
inline

This function is a getter function which is used to get the player.

Returns
std::shared_ptr<Units::Player>
109{ return _player; }
std::shared_ptr< Units::Player > _player
Definition: ZoneSession.hpp:122

References _player.

◆ set_map_name()

void Horizon::Zone::ZoneSession::set_map_name ( std::string  map_name)
inline
118{ _map_name = map_name; }

References _map_name.

◆ set_player()

void Horizon::Zone::ZoneSession::set_player ( std::shared_ptr< Units::Player pl)
inline

This function is a setter function which is used to set the player.

Parameters
plis a shared pointer of type Player which is used to set the _player variable.
Returns
void
114{ _player = pl; }

References _player.

◆ transmit_buffer()

void ZoneSession::transmit_buffer ( ByteBuffer  _buffer,
std::size_t  size 
)

Queues a buffer to be sent to the client.

This function is used to queue the buffer to be sent to the client.

Parameters
_bufferis a reference to a ByteBuffer which is used to handle the buffer.
sizeis a size_t variable which is used to handle the size of the buffer.
Returns
void
Return values
None
Side Effects: None
Thread Safety: None
Sync/Async: None
Reentrancy: None
Parameters
_bufferThe buffer to be sent.
sizeThe size of the buffer.
Returns
void
77{
78 if (get_socket() == nullptr || !get_socket()->is_open())
79 return;
80
81 if (!_buffer.is_empty()) {
82 uint16_t packet_id = 0x0;
83 int16_t packet_len = 0;
84
85 memcpy(&packet_id, _buffer.get_read_pointer(), sizeof(int16_t));
86
87 TPacketTablePairType p = _pkt_tbl->get_tpacket_info(packet_id);
88
89 if (p.first == -1) {
90 memcpy(&packet_len, _buffer.get_read_pointer() + 2, sizeof(int16_t));
91 } else {
92 packet_len = p.first;
93 }
94
95 // Warns when the packet length is not the same as the buffer length.
96 if (packet_len != _buffer.active_length()) {
97 HLog(warning) << "Packet 0x" << std::hex << packet_id << " has length " << std::dec << packet_len << " but buffer has " << _buffer.active_length() << " bytes... ignoring.";
98 return;
99 }
100
101 // Queue the buffer for the Network Thread to send.
102 get_socket()->queue_buffer(std::move(_buffer));
103 }
104}
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
std::shared_ptr< ZoneSocket > get_socket()
Get the socket.
Definition: Session.hpp:70
std::pair< int16_t, TPacketStructPtrType > TPacketTablePairType
Definition: PacketLengthTable.hpp:42

References _pkt_tbl, ByteBuffer::active_length(), ByteBuffer::get_read_pointer(), Horizon::Networking::Session< ZoneSocket, ZoneSession >::get_socket(), HLog, and ByteBuffer::is_empty().

+ Here is the call graph for this function:

◆ update()

void ZoneSession::update ( uint32_t  diff)
virtual

Update loop for each Zone Session.

This function is used to handle the incoming packets from the client and categorize the packets to be processed by the server.

Parameters
diffis a uint32_t variable which is used to handle the time difference.
Returns
void

@thread called from Runtime.

See also
Horizon::System::RUNTIME_RUNTIME

Implements Horizon::Networking::Session< ZoneSocket, ZoneSession >.

111{
112 std::shared_ptr<ByteBuffer> read_buf;
113
114 if (get_socket() == nullptr || !get_socket()->is_open())
115 return;
116
117 while ((read_buf = get_recv_queue().try_pop())) {
118 while (read_buf->active_length()) {
119 uint16_t packet_id = 0x0;
120 memcpy(&packet_id, read_buf->get_read_pointer(), sizeof(int16_t));
121 HPacketTablePairType p = _pkt_tbl->get_hpacket_info(packet_id);
122
123 int16_t packet_len = p.first;
124
125 if (packet_len == -1) {
126 memcpy(&packet_len, read_buf->get_read_pointer() + 2, sizeof(int16_t));
127 }
128
129 ByteBuffer buf(*read_buf, packet_len);
130 HLog(debug) << "Handling packet 0x" << std::hex << packet_id << " - len:" << std::dec << p.first << std::endl;
131 p.second->handle(std::move(buf));
132 }
133 }
134}
Definition: ByteBuffer.hpp:78
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< ZoneSocket, ZoneSession >::get_recv_queue(), Horizon::Networking::Session< ZoneSocket, ZoneSession >::get_socket(), and HLog.

+ Here is the call graph for this function:

Member Data Documentation

◆ _clif

std::unique_ptr<ZoneClientInterface> Horizon::Zone::ZoneSession::_clif
protected

Referenced by clif(), and initialize().

◆ _map_name

std::string Horizon::Zone::ZoneSession::_map_name {""}
protected

Referenced by get_map_name(), and set_map_name().

◆ _pkt_tbl

std::unique_ptr<ClientPacketLengthTable> Horizon::Zone::ZoneSession::_pkt_tbl
protected

◆ _player

std::shared_ptr<Units::Player> Horizon::Zone::ZoneSession::_player
protected

Referenced by player(), and set_player().


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