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

declared to avoid recursive inclusion of header files. More...

#include <ZoneSocket.hpp>

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

Public Member Functions

 ZoneSocket (uint64_t uid, std::shared_ptr< tcp::socket > socket)
 
 ~ZoneSocket ()
 
void start () override
 Initial method invoked once from the network thread that handles the ZoneSocket. More...
 
bool update () override
 Asynchronous update method periodically called from network threads. More...
 
std::shared_ptr< ZoneSessionget_session ()
 Session dependency. More...
 
void set_session (std::shared_ptr< ZoneSession > session)
 
- Public Member Functions inherited from Horizon::Networking::Socket< ZoneSocket >
 Socket (uint64_t socket_id)
 
 Socket (uint64_t socket_id, std::shared_ptr< tcp::socket > socket)
 
virtual ~Socket ()
 
virtual void start ()=0
 Initial method invoked once from the network thread that handles the AuthSocket. More...
 
virtual bool update ()
 Socket update loop called from its NetworkThread every n nanoseconds. More...
 
uint64_t get_socket_id ()
 
std::string & remote_ip_address ()
 
uint16_t remote_port () const
 
void async_read ()
 Asynchronous read operation @thread NetworkThread. More...
 
void async_read_with_callback (ByteBuffer &buf, void(Socket< ZoneSocket >::*)(boost::system::error_code, std::size_t))
 Asynchronous read operation with callback handler @thread NetworkThread. More...
 
virtual void queue_buffer (ByteBuffer &&buffer)
 
bool is_open ()
 
void close_socket ()
 Socket close operation that performs cleanups before shutting down the connection. More...
 
void delayed_close_socket ()
 
ByteBufferget_read_buffer ()
 

Protected Member Functions

void read_handler () override
 Incoming buffer read handler. More...
 
void on_close () override
 Socket cleanup method on connection closure. More...
 
void on_error () override
 Socket error handler. More...
 
- Protected Member Functions inherited from Horizon::Networking::Socket< ZoneSocket >
virtual void on_close ()=0
 
virtual void read_handler ()=0
 
virtual void on_error ()=0
 
bool async_process_queue ()
 Socket write operation. More...
 
void set_no_delay (bool enable)
 Disable the Nagle Algorithm on our socket. More...
 
std::size_t write_buffer_and_send (ByteBuffer &to_send, boost::system::error_code &error)
 Write a message to the buffer. More...
 

Protected Attributes

std::shared_ptr< ZoneSession_session
 

Private Types

typedef Socket< ZoneSocketBaseSocket
 

Detailed Description

declared to avoid recursive inclusion of header files.

Member Typedef Documentation

◆ BaseSocket

Constructor & Destructor Documentation

◆ ZoneSocket()

ZoneSocket::ZoneSocket ( uint64_t  uid,
std::shared_ptr< tcp::socket >  socket 
)
38: Socket(uid, socket)
39{
40 //
41}
Socket(uint64_t socket_id)
Definition: Socket.hpp:84

◆ ~ZoneSocket()

Horizon::Zone::ZoneSocket::~ZoneSocket ( )
inline
47{ }

Member Function Documentation

◆ get_session()

std::shared_ptr< ZoneSession > ZoneSocket::get_session ( )

Session dependency.

Note
required atomic setting/loading. @thread set by network thread, retrieved by main thread.
48{ return std::atomic_load(&_session); }
std::shared_ptr< ZoneSession > _session
Definition: ZoneSocket.hpp:60

References _session.

Referenced by on_close(), on_error(), and read_handler().

+ Here is the caller graph for this function:

◆ on_close()

void ZoneSocket::on_close ( )
overrideprotectedvirtual

Socket cleanup method on connection closure.

Note
This method is invoked from the network thread.

Implements Horizon::Networking::Socket< ZoneSocket >.

75{
76 HLog(info) << "Closed connection from '" << remote_ip_address() << "'.";
77
78 if (get_session()->player() != nullptr) {
79 // Remove player from map (if any)
80 get_session()->perform_cleanup();
81 }
82}
#define HLog(type)
Definition: Logger.hpp:122
std::string & remote_ip_address()
Definition: Socket.hpp:133
std::shared_ptr< ZoneSession > get_session()
Session dependency.
Definition: ZoneSocket.cpp:48

References get_session(), HLog, and Horizon::Networking::Socket< ZoneSocket >::remote_ip_address().

+ Here is the call graph for this function:

◆ on_error()

void ZoneSocket::on_error ( )
overrideprotectedvirtual

Socket error handler.

Note
This method is invoked from the network thread.

Implements Horizon::Networking::Socket< ZoneSocket >.

89{
90 if (get_session()->player() != nullptr) {
91 // Remove player from map (if any)
92 get_session()->perform_cleanup();
93 }
94}

References get_session().

+ Here is the call graph for this function:

◆ read_handler()

void ZoneSocket::read_handler ( )
overrideprotectedvirtual

Incoming buffer read handler.

Implements Horizon::Networking::Socket< ZoneSocket >.

110{
111 while (get_read_buffer().active_length()) {
112 uint16_t packet_id = 0x0;
113 memcpy(&packet_id, get_read_buffer().get_read_pointer(), sizeof(uint16_t));
114
115 HPacketTablePairType p = get_session()->pkt_tbl()->get_hpacket_info(packet_id);
116
117 int16_t packet_length = p.first;
118
119 HLog(debug) << "Received packet 0x" << std::hex << packet_id << " of length " << std::dec << packet_length << " from client.";
120
121 if (packet_length == -1) {
122 memcpy(&packet_length, get_read_buffer().get_read_pointer() + 2, sizeof(int16_t));
123 if (get_read_buffer().active_length() < packet_length) {
124 HLog(debug) << "Received packet 0x" << std::hex << packet_id << " has expected length " << std::dec << packet_length << " but buffer only supplied " << get_read_buffer().active_length() << " from client.";
125 break;
126 }
127 } else if (packet_length == 0) {
128 HLog(warning) << "Received non-existent packet id 0x" << std::hex << packet_id << ", disconnecting session..." << std::endl;
130 close_socket();
131 break;
132 }
133
134 ByteBuffer b;
135 b.append(get_read_buffer().get_read_pointer(), packet_length);
136 get_session()->get_recv_queue().push(std::move(b));
137 get_read_buffer().read_completed(packet_length);
138 }
139}
Definition: ByteBuffer.hpp:78
void read_completed(size_t bytes)
Definition: ByteBuffer.hpp:321
void append(T value)
Definition: ByteBuffer.hpp:140
size_t active_length() const
Definition: ByteBuffer.hpp:333
ByteBuffer & get_read_buffer()
Definition: Socket.hpp:202
void close_socket()
Socket close operation that performs cleanups before shutting down the connection.
Definition: Socket.hpp:176
std::pair< int16_t, HPacketStructPtrType > HPacketTablePairType
Definition: PacketLengthTable.hpp:41

References ByteBuffer::active_length(), ByteBuffer::append(), Horizon::Networking::Socket< ZoneSocket >::close_socket(), Horizon::Networking::Socket< ZoneSocket >::get_read_buffer(), get_session(), HLog, and ByteBuffer::read_completed().

+ Here is the call graph for this function:

◆ set_session()

void ZoneSocket::set_session ( std::shared_ptr< ZoneSession session)
49{ std::atomic_store(&_session, session); }

References _session.

Referenced by start().

+ Here is the caller graph for this function:

◆ start()

void ZoneSocket::start ( )
overridevirtual

Initial method invoked once from the network thread that handles the ZoneSocket.

Implements Horizon::Networking::Socket< ZoneSocket >.

55{
56 auto session = std::make_shared<ZoneSession>(get_socket_id());
57 session->set_socket(shared_from_this());
58
59 set_session(session);
60
61 // Session initialized in network thread to initiate the packet length table and packet handling procedure.
62 session->initialize();
63
64 HLog(info) << "Established connection from '" << remote_ip_address() << "'.";
65
66 // Start async_read loop.
67 async_read();
68}
uint64_t get_socket_id()
Definition: Socket.hpp:130
void async_read()
Asynchronous read operation @thread NetworkThread.
Definition: Socket.hpp:140
void set_session(std::shared_ptr< ZoneSession > session)
Definition: ZoneSocket.cpp:49

References Horizon::Networking::Socket< ZoneSocket >::async_read(), Horizon::Networking::Socket< ZoneSocket >::get_socket_id(), HLog, Horizon::Networking::Socket< ZoneSocket >::remote_ip_address(), and set_session().

+ Here is the call graph for this function:

◆ update()

bool ZoneSocket::update ( )
overridevirtual

Asynchronous update method periodically called from network threads.

@thread NetworkThread

Returns
true on successful update, false on failure.

Reimplemented from Horizon::Networking::Socket< ZoneSocket >.

102{
103 return BaseSocket::update();
104}

Member Data Documentation

◆ _session

std::shared_ptr<ZoneSession> Horizon::Zone::ZoneSocket::_session
protected

Referenced by get_session(), and set_session().


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