TrinityCore
Loading...
Searching...
No Matches
Trinity::Net::Socket< Stream > Class Template Reference

#include <Socket.h>

+ Inheritance diagram for Trinity::Net::Socket< Stream >:

Classes

struct  Endpoint
 

Public Member Functions

template<typename... Args>
 Socket (IoContextTcpSocket &&socket, Args &&... args)
 
template<typename... Args>
 Socket (boost::asio::io_context &context, Args &&... args)
 
 Socket (Socket const &other)=delete
 
 Socket (Socket &&other)=delete
 
Socketoperator= (Socket const &other)=delete
 
Socketoperator= (Socket &&other)=delete
 
virtual ~Socket ()
 
virtual void Start ()
 
template<BOOST_ASIO_COMPLETION_TOKEN_FOR(void(boost::system::error_code, boost::asio::ip::tcp::endpoint)) Callback>
decltype(auto) Connect (boost::asio::ip::tcp::endpoint const &endpoint, Callback &&callback)
 
template<BOOST_ASIO_COMPLETION_TOKEN_FOR(void(boost::system::error_code, boost::asio::ip::tcp::endpoint)) Callback>
decltype(auto) Connect (std::vector< boost::asio::ip::tcp::endpoint > const &endpoints, Callback &&callback)
 
virtual bool Update ()
 
boost::asio::ip::address const & GetRemoteIpAddress () const
 
uint16 GetRemotePort () const
 
void SetRemoteEndpoint (boost::asio::ip::tcp::endpoint const &endpoint)
 
template<invocable_r< SocketReadCallbackResult > Callback>
void AsyncRead (Callback &&callback)
 
void QueuePacket (MessageBuffer &&buffer)
 
bool IsOpen () const
 
void CloseSocket ()
 
void DelayedCloseSocket ()
 Marks the socket for closing after write buffer becomes empty.
 
MessageBufferGetReadBuffer ()
 
Stream & underlying_stream ()
 

Protected Member Functions

virtual void OnClose ()
 
virtual SocketReadCallbackResult ReadHandler ()
 
bool AsyncProcessQueue ()
 
void SetNoDelay (bool enable)
 

Private Member Functions

bool ReadHandlerInternal (boost::system::error_code const &error, size_t transferredBytes)
 
void QueuedBufferWriteDone ()
 
void WriteHandlerWrapper (boost::system::error_code const &)
 
bool HandleQueue ()
 

Private Attributes

Stream _socket
 
struct Trinity::Net::Socket::Endpoint _remoteEndpoint
 
MessageBuffer _readBuffer = MessageBuffer(0x1000)
 
std::queue< MessageBuffer_writeQueue
 
std::atomic< uint8_openState
 
bool _isWritingAsync = false
 

Static Private Attributes

static constexpr uint8 OpenState_Open = 0x0
 
static constexpr uint8 OpenState_Closing = 0x1
 Transition to Closed state after sending all queued data.
 
static constexpr uint8 OpenState_Closed = 0x2
 

Detailed Description

template<class Stream = IoContextTcpSocket>
class Trinity::Net::Socket< Stream >

Base async socket implementation

Template Parameters
Streamstream type used for operations on socket Stream must implement the following methods:

boost::asio::io_context::executor_type get_executor();

bool is_open() const;

void close(boost::system::error_code& error);

void shutdown(boost::asio::socket_base::shutdown_type what, boost::system::error_code& shutdownError);

template<typename ConnectHandlerType> void async_connect(boost::asio::ip::tcp::endpoint const& endpoint, ConnectHandlerType&& handler);

template<typename MutableBufferSequence, typename ReadHandlerType> void async_read_some(MutableBufferSequence const& buffers, ReadHandlerType&& handler);

template<typename ConstBufferSequence, typename WriteHandlerType> void async_write_some(ConstBufferSequence const& buffers, WriteHandlerType&& handler);

template<typename ConstBufferSequence> std::size_t write_some(ConstBufferSequence const& buffers, boost::system::error_code& error);

template<typename WaitHandlerType> void async_wait(boost::asio::socket_base::wait_type type, WaitHandlerType&& handler);

template<typename SettableSocketOption> void set_option(SettableSocketOption const& option, boost::system::error_code& error);

tcp::socket::endpoint_type remote_endpoint() const;

Definition at line 126 of file Socket.h.

Constructor & Destructor Documentation

◆ Socket() [1/4]

template<class Stream = IoContextTcpSocket>
template<typename... Args>
Trinity::Net::Socket< Stream >::Socket ( IoContextTcpSocket< Stream > &&  socket,
Args &&...  args 
)
inlineexplicit

Definition at line 130 of file Socket.h.

◆ Socket() [2/4]

template<class Stream = IoContextTcpSocket>
template<typename... Args>
Trinity::Net::Socket< Stream >::Socket ( boost::asio::io_context &  context,
Args &&...  args 
)
inlineexplicit

Definition at line 136 of file Socket.h.

◆ Socket() [3/4]

template<class Stream = IoContextTcpSocket>
Trinity::Net::Socket< Stream >::Socket ( Socket< Stream > const &  other)
delete

◆ Socket() [4/4]

template<class Stream = IoContextTcpSocket>
Trinity::Net::Socket< Stream >::Socket ( Socket< Stream > &&  other)
delete

◆ ~Socket()

template<class Stream = IoContextTcpSocket>
virtual Trinity::Net::Socket< Stream >::~Socket ( )
inlinevirtual

Reimplemented in Trinity::Net::Http::Socket.

Definition at line 145 of file Socket.h.

Member Function Documentation

◆ AsyncProcessQueue()

template<class Stream = IoContextTcpSocket>
bool Trinity::Net::Socket< Stream >::AsyncProcessQueue ( )
inlineprotected

Definition at line 264 of file Socket.h.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ AsyncRead()

template<class Stream = IoContextTcpSocket>
template<invocable_r< SocketReadCallbackResult > Callback>
void Trinity::Net::Socket< Stream >::AsyncRead ( Callback &&  callback)
inline

Definition at line 202 of file Socket.h.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ CloseSocket()

template<class Stream = IoContextTcpSocket>
void Trinity::Net::Socket< Stream >::CloseSocket ( )
inline

Definition at line 227 of file Socket.h.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Connect() [1/2]

template<class Stream = IoContextTcpSocket>
template<BOOST_ASIO_COMPLETION_TOKEN_FOR(void(boost::system::error_code, boost::asio::ip::tcp::endpoint)) Callback>
decltype(auto) Trinity::Net::Socket< Stream >::Connect ( boost::asio::ip::tcp::endpoint const &  endpoint,
Callback &&  callback 
)
inline

Definition at line 155 of file Socket.h.

+ Here is the call graph for this function:

◆ Connect() [2/2]

template<class Stream = IoContextTcpSocket>
template<BOOST_ASIO_COMPLETION_TOKEN_FOR(void(boost::system::error_code, boost::asio::ip::tcp::endpoint)) Callback>
decltype(auto) Trinity::Net::Socket< Stream >::Connect ( std::vector< boost::asio::ip::tcp::endpoint > const &  endpoints,
Callback &&  callback 
)
inline

Definition at line 163 of file Socket.h.

+ Here is the call graph for this function:

◆ DelayedCloseSocket()

template<class Stream = IoContextTcpSocket>
void Trinity::Net::Socket< Stream >::DelayedCloseSocket ( )
inline

Marks the socket for closing after write buffer becomes empty.

Definition at line 242 of file Socket.h.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ GetReadBuffer()

template<class Stream = IoContextTcpSocket>
MessageBuffer & Trinity::Net::Socket< Stream >::GetReadBuffer ( )
inline

Definition at line 252 of file Socket.h.

+ Here is the caller graph for this function:

◆ GetRemoteIpAddress()

template<class Stream = IoContextTcpSocket>
boost::asio::ip::address const & Trinity::Net::Socket< Stream >::GetRemoteIpAddress ( ) const
inline

Definition at line 186 of file Socket.h.

+ Here is the caller graph for this function:

◆ GetRemotePort()

template<class Stream = IoContextTcpSocket>
uint16 Trinity::Net::Socket< Stream >::GetRemotePort ( ) const
inline

Definition at line 191 of file Socket.h.

+ Here is the caller graph for this function:

◆ HandleQueue()

template<class Stream = IoContextTcpSocket>
bool Trinity::Net::Socket< Stream >::HandleQueue ( )
inlineprivate

Definition at line 344 of file Socket.h.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ IsOpen()

template<class Stream = IoContextTcpSocket>
bool Trinity::Net::Socket< Stream >::IsOpen ( ) const
inline

Definition at line 225 of file Socket.h.

+ Here is the caller graph for this function:

◆ OnClose()

template<class Stream = IoContextTcpSocket>
virtual void Trinity::Net::Socket< Stream >::OnClose ( )
inlineprotectedvirtual

Reimplemented in WorldSocket.

Definition at line 260 of file Socket.h.

+ Here is the caller graph for this function:

◆ operator=() [1/2]

template<class Stream = IoContextTcpSocket>
Socket & Trinity::Net::Socket< Stream >::operator= ( Socket< Stream > &&  other)
delete

◆ operator=() [2/2]

template<class Stream = IoContextTcpSocket>
Socket & Trinity::Net::Socket< Stream >::operator= ( Socket< Stream > const &  other)
delete

◆ QueuedBufferWriteDone()

template<class Stream = IoContextTcpSocket>
void Trinity::Net::Socket< Stream >::QueuedBufferWriteDone ( )
inlineprivate

Definition at line 311 of file Socket.h.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ QueuePacket()

template<class Stream = IoContextTcpSocket>
void Trinity::Net::Socket< Stream >::QueuePacket ( MessageBuffer &&  buffer)
inline

Definition at line 216 of file Socket.h.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ReadHandler()

template<class Stream = IoContextTcpSocket>
virtual SocketReadCallbackResult Trinity::Net::Socket< Stream >::ReadHandler ( )
inlineprotectedvirtual

Reimplemented in Trinity::Net::Http::BaseSocket< Stream >, Trinity::Net::Http::BaseSocket< Impl::BoostBeastSocketWrapper >, Trinity::Net::Http::BaseSocket< SslStream< Impl::BoostBeastSocketWrapper > >, and WorldSocket.

Definition at line 262 of file Socket.h.

+ Here is the caller graph for this function:

◆ ReadHandlerInternal()

template<class Stream = IoContextTcpSocket>
bool Trinity::Net::Socket< Stream >::ReadHandlerInternal ( boost::system::error_code const &  error,
size_t  transferredBytes 
)
inlineprivate

Definition at line 299 of file Socket.h.

+ Here is the call graph for this function:

◆ SetNoDelay()

template<class Stream = IoContextTcpSocket>
void Trinity::Net::Socket< Stream >::SetNoDelay ( bool  enable)
inlineprotected

Definition at line 289 of file Socket.h.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ SetRemoteEndpoint()

template<class Stream = IoContextTcpSocket>
void Trinity::Net::Socket< Stream >::SetRemoteEndpoint ( boost::asio::ip::tcp::endpoint const &  endpoint)
inline

Definition at line 196 of file Socket.h.

◆ Start()

◆ underlying_stream()

template<class Stream = IoContextTcpSocket>
Stream & Trinity::Net::Socket< Stream >::underlying_stream ( )
inline

Definition at line 254 of file Socket.h.

+ Here is the caller graph for this function:

◆ Update()

template<class Stream = IoContextTcpSocket>
virtual bool Trinity::Net::Socket< Stream >::Update ( )
inlinevirtual

Reimplemented in Trinity::Net::Http::BaseSocket< Stream >, Trinity::Net::Http::BaseSocket< Impl::BoostBeastSocketWrapper >, Trinity::Net::Http::BaseSocket< SslStream< Impl::BoostBeastSocketWrapper > >, and WorldSocket.

Definition at line 170 of file Socket.h.

+ Here is the call graph for this function:

◆ WriteHandlerWrapper()

template<class Stream = IoContextTcpSocket>
void Trinity::Net::Socket< Stream >::WriteHandlerWrapper ( boost::system::error_code const &  )
inlineprivate

Definition at line 338 of file Socket.h.

+ Here is the call graph for this function:

Member Data Documentation

◆ _isWritingAsync

template<class Stream = IoContextTcpSocket>
bool Trinity::Net::Socket< Stream >::_isWritingAsync = false
private

Definition at line 402 of file Socket.h.

◆ _openState

template<class Stream = IoContextTcpSocket>
std::atomic<uint8> Trinity::Net::Socket< Stream >::_openState
private

Definition at line 400 of file Socket.h.

◆ _readBuffer

template<class Stream = IoContextTcpSocket>
MessageBuffer Trinity::Net::Socket< Stream >::_readBuffer = MessageBuffer(0x1000)
private

Definition at line 392 of file Socket.h.

◆ _remoteEndpoint

template<class Stream = IoContextTcpSocket>
struct Trinity::Net::Socket::Endpoint Trinity::Net::Socket< Stream >::_remoteEndpoint
private

◆ _socket

template<class Stream = IoContextTcpSocket>
Stream Trinity::Net::Socket< Stream >::_socket
private

Definition at line 381 of file Socket.h.

◆ _writeQueue

template<class Stream = IoContextTcpSocket>
std::queue<MessageBuffer> Trinity::Net::Socket< Stream >::_writeQueue
private

Definition at line 393 of file Socket.h.

◆ OpenState_Closed

template<class Stream = IoContextTcpSocket>
constexpr uint8 Trinity::Net::Socket< Stream >::OpenState_Closed = 0x2
staticconstexprprivate

Definition at line 398 of file Socket.h.

◆ OpenState_Closing

template<class Stream = IoContextTcpSocket>
constexpr uint8 Trinity::Net::Socket< Stream >::OpenState_Closing = 0x1
staticconstexprprivate

Transition to Closed state after sending all queued data.

Definition at line 397 of file Socket.h.

◆ OpenState_Open

template<class Stream = IoContextTcpSocket>
constexpr uint8 Trinity::Net::Socket< Stream >::OpenState_Open = 0x0
staticconstexprprivate

Definition at line 396 of file Socket.h.


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