Horizon Official Technical Documentation
Horizon::Auth::AuthSocket Class Reference

#include <AuthSocket.hpp>

+ Inheritance diagram for Horizon::Auth::AuthSocket:
+ Collaboration diagram for Horizon::Auth::AuthSocket:

Public Member Functions

 AuthSocket (uint64_t uid)
 
 AuthSocket (uint64_t uid, std::shared_ptr< tcp::socket > socket)
 
 ~AuthSocket ()
 
virtual void start () override
 Initial method invoked once from the network thread that handles the AuthSocket. More...
 
bool update () override
 Asynchronous update method periodically called from network threads. More...
 
std::shared_ptr< AuthSessionget_session ()
 @thread created by network thread and called from main thread / client-sockt-mgr for update(). More...
 
void set_session (std::shared_ptr< AuthSession > session)
 
void update_session (uint32_t diff)
 Packets are processed within the session associated with this socket. More...
 
- Public Member Functions inherited from Horizon::Networking::Socket< AuthSocket >
 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< AuthSocket >::*)(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
 Read handler for when a message is read from the network and placed within this socket's message buffer. More...
 
void on_close () override
 Socket cleanup method on connection closure. More...
 
void on_error () override
 Method invoked when an error occured during a read operation on the socket. More...
 
- Protected Member Functions inherited from Horizon::Networking::Socket< AuthSocket >
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< AuthSocketBaseSocket
 

Private Attributes

std::shared_ptr< AuthSession_session
 

Member Typedef Documentation

◆ BaseSocket

Constructor & Destructor Documentation

◆ AuthSocket() [1/2]

AuthSocket::AuthSocket ( uint64_t  uid)
explicit
39: Socket(uid)
40{
41}
Socket(uint64_t socket_id)
Definition: Socket.hpp:84

◆ AuthSocket() [2/2]

AuthSocket::AuthSocket ( uint64_t  uid,
std::shared_ptr< tcp::socket >  socket 
)
explicit
44: Socket(uid, socket)
45{
46}

◆ ~AuthSocket()

AuthSocket::~AuthSocket ( )
49{
50
51}

Member Function Documentation

◆ get_session()

std::shared_ptr< AuthSession > AuthSocket::get_session ( )

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

56{ return std::atomic_load(&_session); }
std::shared_ptr< AuthSession > _session
Definition: AuthSocket.hpp:69

References _session.

Referenced by read_handler(), and update_session().

+ Here is the caller graph for this function:

◆ on_close()

void AuthSocket::on_close ( )
overrideprotectedvirtual

Socket cleanup method on connection closure.

Called from close_socket() in parent class. @thread NetworkThread

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

82{
83 HLog(info) << "Closed connection from " << remote_ip_address();
84
85 /* Perform socket manager cleanup. */
86 sClientSocketMgr->set_socket_for_removal(shared_from_this());
87}
#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< AuthSocket >::remote_ip_address(), and sClientSocketMgr.

+ Here is the call graph for this function:

◆ on_error()

void AuthSocket::on_error ( )
overrideprotectedvirtual

Method invoked when an error occured during a read operation on the socket.

@thread NetworkThread

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

94{
95
96}

◆ read_handler()

void AuthSocket::read_handler ( )
overrideprotectedvirtual

Read handler for when a message is read from the network and placed within this socket's message buffer.

The packet length is checked before the packet is processed and its corresponding handler is called. @thread NetworkThread

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

117{
118 while (get_read_buffer().active_length()) {
119 uint16_t packet_id = 0x0;
120 memcpy(&packet_id, get_read_buffer().get_read_pointer(), sizeof(uint16_t));
121
122 std::pair<uint16_t, std::shared_ptr<Base::NetworkPacket<AuthSession>>> p;
123 p = get_session()->pkt_tbl()->get_hpacket_info(packet_id);
124
125 if (p.first == 0) { //
126 p = get_session()->pkt_tbl()->get_tpacket_info(packet_id);
127 }
128
129 int16_t packet_length = p.first;
130
131 if (packet_length == -1) {
132 memcpy(&packet_length, get_read_buffer().get_read_pointer() + 2, sizeof(int16_t));
133 if (get_read_buffer().active_length() < packet_length) {
134 HLog(debug) << "Received packet 0x" << packet_id << " has expected length " << packet_length << " but buffer only supplied " << get_read_buffer().active_length() << " from client.";
135 break;
136 }
137 } else if (packet_length == 0) {
138 HLog(warning) << "Received non-existent packet id 0x" << std::hex << packet_id << ", disconnecting session..." << std::endl;
140 close_socket();
141 break;
142 }
143
144 ByteBuffer b;
145 b.append(get_read_buffer().get_read_pointer(), packet_length);
146 get_session()->get_recv_queue().push(std::move(b));
147 get_read_buffer().read_completed(packet_length);
148 }
149}
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::shared_ptr< AuthSession > get_session()
@thread created by network thread and called from main thread / client-sockt-mgr for update().
Definition: AuthSocket.cpp:56
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

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

+ Here is the call graph for this function:

◆ set_session()

void AuthSocket::set_session ( std::shared_ptr< AuthSession session)
57{ std::atomic_store(&_session, session); }

References _session.

Referenced by start().

+ Here is the caller graph for this function:

◆ start()

void AuthSocket::start ( )
overridevirtual

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

@thread NetworkThread

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

64{
65 auto session = std::make_shared<AuthSession>(get_socket_id());
66 session->set_socket(shared_from_this());
67 set_session(session);
68
69 session->initialize();
70
71 HLog(info) << "Established connection from " << remote_ip_address();
72
73 // Starts the async_read loop.
74 async_read();
75}
void set_session(std::shared_ptr< AuthSession > session)
Definition: AuthSocket.cpp:57
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< AuthSocket >::async_read(), Horizon::Networking::Socket< AuthSocket >::get_socket_id(), HLog, Horizon::Networking::Socket< AuthSocket >::remote_ip_address(), and set_session().

+ Here is the call graph for this function:

◆ update()

bool AuthSocket::update ( )
overridevirtual

Asynchronous update method periodically called from network threads.

Returns
true on successful update, false on failure. @thread NetworkThread

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

104{
106 sClientSocketMgr->set_socket_for_removal(shared_from_this());
107
108 return BaseSocket::update();
109}
shutdown_stages get_shutdown_stage()
Definition: Server.hpp:74
@ SHUTDOWN_INITIATED
Definition: Server.hpp:65

References get_shutdown_stage(), sClientSocketMgr, and SHUTDOWN_INITIATED.

+ Here is the call graph for this function:

◆ update_session()

void AuthSocket::update_session ( uint32_t  diff)

Packets are processed within the session associated with this socket.

Packets lengths are checked in the NetworkThread before being processed here in the main thread. @thread Main or Other Thread (Not Network Thread)

157{
158 get_session()->update(diff);
159}

References get_session().

+ Here is the call graph for this function:

Member Data Documentation

◆ _session

std::shared_ptr<AuthSession> Horizon::Auth::AuthSocket::_session
private

Referenced by get_session(), and set_session().


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