Horizon Official Technical Documentation
|
|
A Socket object that handles a single connection. More...
#include <Socket.hpp>
Public Member Functions | |
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< SocketType >::*)(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 () |
ByteBuffer & | get_read_buffer () |
Protected Member Functions | |
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 Member Functions | |
void | read_handler_internal (boost::system::error_code error, size_t transferredBytes) |
Aysnchronous reading handler method. More... | |
void | write_handler_wrapper (boost::system::error_code, std::size_t) |
Write handler wrapper. More... | |
bool | handle_queue () |
Handle the queue. More... | |
std::shared_ptr< tcp::socket > | get_socket () |
Private Attributes | |
uint64_t | _socket_id |
std::shared_ptr< tcp::socket > | _socket |
After accepting, the reference count of this pointer should be 1. More... | |
std::string | _remote_ip_address |
uint16_t | _remote_port |
ByteBuffer | _read_buffer |
ThreadSafeQueue< ByteBuffer > | _write_queue |
std::atomic< bool > | _closed |
std::atomic< bool > | _closing |
bool | _is_writing_async |
A Socket object that handles a single connection.
Sockets are moved into the thread by SocketMgr, once accepted or connected. Once started, the object blocks to handle I/O events and requires explicit stopping.
SocketType | The type of the Socket object. The Socket object is templated to allow the SocketMgr to handle different types of Socket objects. The Socket object must inherit from the Socket class. The SocketMgr will call the start() method of the Socket object. The Socket object must implement the start() method. The start() method is called once from the NetworkThread. The Socket object must implement the update() method. The update() method is called every n nanoseconds from the NetworkThread. |
socket_id | The unique identifier of the Socket object. |
socket | The boost::asio::ip::tcp::socket object. |
_remote_ip_address | The remote IP address of the Socket object. |
_remote_port | The remote port of the Socket object. |
_read_buffer | The ByteBuffer object that holds the read buffer. |
_closed | The atomic boolean that indicates whether the Socket object is closed. The variables are set only from the NetworkThread. Once set, the Socket object will not accept any new read/write requests. |
_closing | The atomic boolean that indicates whether the Socket object is closing. The variables are set only from the NetworkThread. Once set, the Socket object will not accept any new write requests. The Socket object will continue to read until the read buffer is empty. Once the read buffer is empty, the Socket object will be closed. |
_is_writing_async | The atomic boolean that indicates whether the Socket object is writing asynchronously. |
_write_queue | The ThreadSafeQueue object that holds the write queue. The write queue is processed in the NetworkThread. |
|
inlineexplicit |
References Horizon::Networking::Socket< SocketType >::_read_buffer, READ_BLOCK_SIZE, and ByteBuffer::resize().
|
inlineexplicit |
References Horizon::Networking::Socket< SocketType >::_read_buffer, READ_BLOCK_SIZE, and ByteBuffer::resize().
|
inlinevirtual |
|
inlineprotected |
Socket write operation.
@thread NetworkThread
References Horizon::Networking::Socket< SocketType >::_is_writing_async, and Horizon::Networking::Socket< SocketType >::_socket.
Referenced by Horizon::Networking::Socket< SocketType >::handle_queue().
|
inline |
Asynchronous read operation @thread NetworkThread.
References Horizon::Networking::Socket< SocketType >::_read_buffer, Horizon::Networking::Socket< SocketType >::_socket, ByteBuffer::ensure_free_space(), ByteBuffer::flush(), ByteBuffer::get_write_pointer(), Horizon::Networking::Socket< SocketType >::is_open(), and ByteBuffer::remaining_space().
Referenced by Horizon::Networking::Socket< SocketType >::read_handler_internal().
|
inline |
Asynchronous read operation with callback handler @thread NetworkThread.
References Horizon::Networking::Socket< SocketType >::_read_buffer, Horizon::Networking::Socket< SocketType >::_socket, ByteBuffer::ensure_free_space(), ByteBuffer::flush(), ByteBuffer::get_write_pointer(), Horizon::Networking::Socket< SocketType >::is_open(), and ByteBuffer::remaining_space().
|
inline |
Socket close operation that performs cleanups before shutting down the connection.
@thread NetworkThread
Socket finalisation.
References Horizon::Networking::Socket< SocketType >::_closed, Horizon::Networking::Socket< SocketType >::_socket, HLog, Horizon::Networking::Socket< SocketType >::on_close(), and Horizon::Networking::Socket< SocketType >::remote_ip_address().
Referenced by Horizon::Networking::Socket< SocketType >::handle_queue(), and Horizon::Networking::Socket< SocketType >::read_handler_internal().
|
inline |
|
inline |
|
inlineprivate |
|
inline |
|
inlineprivate |
Handle the queue.
If we have a would block error, we need to re-process the queue.
Re-process queue if we have remaining bytes.
References Horizon::Networking::Socket< SocketType >::_closing, Horizon::Networking::Socket< SocketType >::_write_queue, Horizon::Networking::Socket< SocketType >::async_process_queue(), Horizon::Networking::Socket< SocketType >::close_socket(), ThreadSafeQueue< T >::empty(), ThreadSafeQueue< T >::front(), ThreadSafeQueue< T >::try_pop(), and Horizon::Networking::Socket< SocketType >::write_buffer_and_send().
Referenced by Horizon::Networking::Socket< SocketType >::update(), and Horizon::Networking::Socket< SocketType >::write_handler_wrapper().
|
inline |
References Horizon::Networking::Socket< SocketType >::_closed, and Horizon::Networking::Socket< SocketType >::_closing.
Referenced by Horizon::Networking::Socket< SocketType >::async_read(), and Horizon::Networking::Socket< SocketType >::async_read_with_callback().
|
protectedpure virtual |
Implemented in Horizon::Auth::AuthSocket, Horizon::Char::CharSocket, and Horizon::Zone::ZoneSocket.
Referenced by Horizon::Networking::Socket< SocketType >::close_socket().
|
protectedpure virtual |
Implemented in Horizon::Auth::AuthSocket, Horizon::Char::CharSocket, and Horizon::Zone::ZoneSocket.
Referenced by Horizon::Networking::Socket< SocketType >::read_handler_internal().
|
inlinevirtual |
References Horizon::Networking::Socket< SocketType >::_write_queue, and ThreadSafeQueue< T >::push().
|
protectedpure virtual |
Implemented in Horizon::Auth::AuthSocket, Horizon::Char::CharSocket, and Horizon::Zone::ZoneSocket.
Referenced by Horizon::Networking::Socket< SocketType >::read_handler_internal().
|
inlineprivate |
Aysnchronous reading handler method.
error | |
transferredBytes | @thread NetworkThread |
References Horizon::Networking::Socket< SocketType >::_read_buffer, Horizon::Networking::Socket< SocketType >::async_read(), Horizon::Networking::Socket< SocketType >::close_socket(), HLog, Horizon::Networking::Socket< SocketType >::on_error(), Horizon::Networking::Socket< SocketType >::read_handler(), and ByteBuffer::write_completed().
|
inline |
References Horizon::Networking::Socket< SocketType >::_remote_ip_address.
Referenced by Horizon::Networking::Socket< SocketType >::close_socket(), and Horizon::Networking::Socket< SocketType >::set_no_delay().
|
inline |
|
inlineprotected |
Disable the Nagle Algorithm on our socket.
enable |
References Horizon::Networking::Socket< SocketType >::_socket, HLog, and Horizon::Networking::Socket< SocketType >::remote_ip_address().
|
pure virtual |
Initial method invoked once from the network thread that handles the AuthSocket.
@thread NetworkThread
Implemented in Horizon::Auth::AuthSocket, Horizon::Char::CharSocket, and Horizon::Zone::ZoneSocket.
|
inlinevirtual |
Socket update loop called from its NetworkThread every n nanoseconds.
Processes the message queue. @thread NetworkThread
Reimplemented in Horizon::Auth::AuthSocket, Horizon::Char::CharSocket, and Horizon::Zone::ZoneSocket.
References Horizon::Networking::Socket< SocketType >::_closed, Horizon::Networking::Socket< SocketType >::_closing, Horizon::Networking::Socket< SocketType >::_is_writing_async, Horizon::Networking::Socket< SocketType >::_write_queue, ThreadSafeQueue< T >::empty(), and Horizon::Networking::Socket< SocketType >::handle_queue().
|
inlineprotected |
Write a message to the buffer.
to_send | |
error |
References Horizon::Networking::Socket< SocketType >::_socket, ByteBuffer::active_length(), and ByteBuffer::get_read_pointer().
Referenced by Horizon::Networking::Socket< SocketType >::handle_queue().
|
inlineprivate |
Write handler wrapper.
error | |
transferedBytes |
References Horizon::Networking::Socket< SocketType >::_is_writing_async, and Horizon::Networking::Socket< SocketType >::handle_queue().
|
private |
|
private |
|
private |
|
private |
Referenced by Horizon::Networking::Socket< SocketType >::async_read(), Horizon::Networking::Socket< SocketType >::async_read_with_callback(), Horizon::Networking::Socket< SocketType >::get_read_buffer(), Horizon::Networking::Socket< SocketType >::read_handler_internal(), and Horizon::Networking::Socket< SocketType >::Socket().
|
private |
|
private |
Referenced by Horizon::Networking::Socket< SocketType >::remote_port().
|
private |
After accepting, the reference count of this pointer should be 1.
Referenced by Horizon::Networking::Socket< SocketType >::async_process_queue(), Horizon::Networking::Socket< SocketType >::async_read(), Horizon::Networking::Socket< SocketType >::async_read_with_callback(), Horizon::Networking::Socket< SocketType >::close_socket(), Horizon::Networking::Socket< SocketType >::get_socket(), Horizon::Networking::Socket< SocketType >::set_no_delay(), Horizon::Networking::Socket< SocketType >::write_buffer_and_send(), and Horizon::Networking::Socket< SocketType >::~Socket().
|
private |
Referenced by Horizon::Networking::Socket< SocketType >::get_socket_id().
|
private |