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

#include <CharSocket.hpp>

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

Public Member Functions

 CharSocket (uint64_t uid, std::shared_ptr< tcp::socket > socket)
 
 ~CharSocket ()
 
void start () override
 Initial method invoked once from the network thread that handles the CharSocket. More...
 
bool update () override
 Asynchronous update method periodically called from network threads. More...
 
std::shared_ptr< CharSessionget_session ()
 @thread created by network thread and called from main thread / client-sockt-mgr for update(). More...
 
void set_session (std::shared_ptr< CharSession > session)
 
void update_session (uint32_t diff)
 
- Public Member Functions inherited from Horizon::Networking::Socket< CharSocket >
 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< CharSocket >::*)(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
 
- Protected Member Functions inherited from Horizon::Networking::Socket< CharSocket >
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...
 

Private Types

typedef Socket< CharSocketBaseSocket
 

Private Attributes

std::shared_ptr< CharSession_session
 

Member Typedef Documentation

◆ BaseSocket

Constructor & Destructor Documentation

◆ CharSocket()

CharSocket::CharSocket ( uint64_t  uid,
std::shared_ptr< tcp::socket >  socket 
)
explicit
41: Socket(uid, socket)
42{
43 //
44}
Socket(uint64_t socket_id)
Definition: Socket.hpp:84

◆ ~CharSocket()

CharSocket::~CharSocket ( )
47{
48 //
49}

Member Function Documentation

◆ get_session()

std::shared_ptr< CharSession > CharSocket::get_session ( )

@thread created by network thread and called from main thread / client-sockt-mgr for update().

54{ return std::atomic_load(&_session); }
std::shared_ptr< CharSession > _session
Definition: CharSocket.hpp:69

References _session.

Referenced by read_handler(), and update_session().

+ Here is the caller graph for this function:

◆ on_close()

void CharSocket::on_close ( )
overrideprotectedvirtual

Socket cleanup method on connection closure.

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

78{
79 HLog(info) << "Closed connection from " << remote_ip_address() << ".";
80
81 /* Perform socket manager cleanup. */
82 sClientSocketMgr->set_socket_for_removal(shared_from_this());
83}
#define sClientSocketMgr
Definition: ClientSocketMgr.hpp:176
#define HLog(type)
Definition: Logger.hpp:122
std::string & remote_ip_address()
Definition: Socket.hpp:133

References HLog, Horizon::Networking::Socket< CharSocket >::remote_ip_address(), and sClientSocketMgr.

+ Here is the call graph for this function:

◆ on_error()

void CharSocket::on_error ( )
overrideprotectedvirtual

◆ read_handler()

void CharSocket::read_handler ( )
overrideprotectedvirtual

Incoming buffer read handler.

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

106{
107 while (get_read_buffer().active_length()) {
108 uint16_t packet_id = 0x0;
109 memcpy(&packet_id, get_read_buffer().get_read_pointer(), sizeof(uint16_t));
110
111 HPacketTablePairType p = get_session()->pkt_tbl()->get_hpacket_info(packet_id);
112
113 int16_t packet_length = p.first;
114
115 HLog(debug) << "Received packet 0x" << packet_id << " of length " << packet_length << " from client.";
116 HLog(debug) << "Data:" << get_read_buffer().to_string();
117
118 if (packet_length == -1) {
119 memcpy(&packet_length, get_read_buffer().get_read_pointer() + 2, sizeof(int16_t));
120 if (get_read_buffer().active_length() < packet_length) {
121 HLog(debug) << "Received packet 0x" << packet_id << " has expected length " << packet_length << " but buffer only supplied " << get_read_buffer().active_length() << " from client.";
122 break;
123 }
124 } else if (packet_length == 0) {
125 HLog(warning) << "Received non-existent packet id 0x" << std::hex << packet_id << ", disconnecting session..." << std::endl;
127 close_socket();
128 break;
129 }
130
131 ByteBuffer b;
132 b.append(get_read_buffer().get_read_pointer(), packet_length);
133 get_session()->get_recv_queue().push(std::move(b));
134 get_read_buffer().read_completed(packet_length);
135 }
136}
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
std::string to_string()
Definition: ByteBuffer.hpp:330
std::shared_ptr< CharSession > get_session()
@thread created by network thread and called from main thread / client-sockt-mgr for update().
Definition: CharSocket.cpp:54
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< CharSocket >::close_socket(), Horizon::Networking::Socket< CharSocket >::get_read_buffer(), get_session(), HLog, ByteBuffer::read_completed(), and ByteBuffer::to_string().

+ Here is the call graph for this function:

◆ set_session()

void CharSocket::set_session ( std::shared_ptr< CharSession session)
55{ std::atomic_store(&_session, session); }

References _session.

Referenced by start().

+ Here is the caller graph for this function:

◆ start()

void CharSocket::start ( )
overridevirtual

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

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

61{
62 auto session = std::make_shared<CharSession>(get_socket_id());
63 session->set_socket(shared_from_this());
64 set_session(session);
65
66 session->initialize();
67
68 HLog(info) << "Established connection from " << remote_ip_address() << ".";
69
70 // Start async_read loop.
71 async_read();
72}
void set_session(std::shared_ptr< CharSession > session)
Definition: CharSocket.cpp:55
uint64_t get_socket_id()
Definition: Socket.hpp:130
void async_read()
Asynchronous read operation @thread NetworkThread.
Definition: Socket.hpp:140

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

+ Here is the call graph for this function:

◆ update()

bool CharSocket::update ( )
overridevirtual

Asynchronous update method periodically called from network threads.

Returns
true on successful update, false on failure.

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

95{
97 sClientSocketMgr->set_socket_for_removal(shared_from_this());
98
99 return BaseSocket::update();
100}
shutdown_stages get_shutdown_stage()
Definition: Server.hpp:74
@ SHUTDOWN_NOT_STARTED
Definition: Server.hpp:64

References get_shutdown_stage(), sClientSocketMgr, and SHUTDOWN_NOT_STARTED.

+ Here is the call graph for this function:

◆ update_session()

void CharSocket::update_session ( uint32_t  diff)
139{
140 get_session()->update(diff);
141}

References get_session().

+ Here is the call graph for this function:

Member Data Documentation

◆ _session

std::shared_ptr<CharSession> Horizon::Char::CharSocket::_session
private

Referenced by get_session(), and set_session().


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