reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
reTurn::AsyncSocketBase Class Referenceabstract

#include <AsyncSocketBase.hxx>

Inheritance diagram for reTurn::AsyncSocketBase:
[legend]

Public Types

using BeforeClosedHandler = std::function< void(unsigned int)>
 

Public Member Functions

 AsyncSocketBase (asio::io_context &ioService)
 
virtual ~AsyncSocketBase ()
 
virtual unsigned int getSocketDescriptor ()=0
 
virtual void registerAsyncSocketBaseHandler (AsyncSocketBaseHandler *handler)
 
virtual asio::error_code bind (const asio::ip::address &address, unsigned short port)=0
 Note: The following API's are thread safe and queue the request to be handled by the ioService thread.
 
virtual void connect (const std::string &address, unsigned short port)=0
 
virtual void send (const StunTuple &destination, const std::shared_ptr< DataBuffer > &data)
 Note: destination is ignored for TCP and TLS connections.
 
virtual void send (const StunTuple &destination, unsigned short channel, const std::shared_ptr< DataBuffer > &data)
 
virtual void receive ()
 Overlapped calls to receive functions have no effect.
 
virtual void framedReceive ()
 
virtual void close ()
 
bool isConnected () const noexcept
 
asio::ip::address & getConnectedAddress () noexcept
 
unsigned short getConnectedPort () const noexcept
 
virtual void setOnBeforeSocketClosedFp (BeforeClosedHandler fp)
 
virtual void doSend (const StunTuple &destination, unsigned short channel, const std::shared_ptr< DataBuffer > &data, const std::size_t bufferStartPos=0)
 Use these if you already operating within the ioService thread.
 
virtual void doSend (const StunTuple &destination, const std::shared_ptr< DataBuffer > &data, const std::size_t bufferStartPos=0)
 
virtual void doReceive ()
 
virtual void doFramedReceive ()
 
virtual void onConnectSuccess ()
 Class override callbacks.
 
virtual void onConnectFailure (const asio::error_code &e)
 
virtual void onReceiveSuccess (const asio::ip::address &address, unsigned short port, const std::shared_ptr< DataBuffer > &data)=0
 
virtual void onReceiveFailure (const asio::error_code &e)=0
 
virtual void onSendSuccess ()=0
 
virtual void onSendFailure (const asio::error_code &e)=0
 
virtual void start ()
 
virtual void stop ()
 
virtual void handleReadHeader (const asio::error_code &e)
 
virtual void handleServerHandshake (const asio::error_code &e)
 
virtual void handleTcpResolve (const asio::error_code &ec, const asio::ip::tcp::resolver::results_type &endpoints)
 
virtual void handleUdpResolve (const asio::error_code &ec, const asio::ip::udp::resolver::results_type &endpoints)
 
virtual void handleConnect (const asio::error_code &ec, const asio::ip::tcp::endpoint &endpoint)
 
virtual void handleClientHandshake (const asio::error_code &ec, const asio::ip::tcp::endpoint &endpoint)
 

Static Public Member Functions

static std::shared_ptr< DataBuffer > allocateBuffer (size_t size)
 Utility API.
 
static size_t channelDataPadding (size_t length)
 Over TCP and TLS, a TURN ChannelData message is padded to a multiple of 4 bytes, the padding not counted in its length (RFC 5766 11.5, RFC 8656 12.5): the padding after one of length bytes.
 
static void setExclusiveAddressUse (asio::ip::udp::socket &socket)
 For a UDP socket, between open() and bind().
 

Protected Member Functions

virtual void handleSend (const asio::error_code &e)
 Handle completion of a sendData operation.
 
virtual void handleReceive (const asio::error_code &e, size_t bytesTransferred)
 
asio::error_code openAndBind (asio::ip::tcp::socket &socket)
 For TCP and TLS client sockets.
 
void asyncConnect (asio::ip::tcp::socket &socket, const asio::ip::tcp::resolver::results_type &endpoints)
 
void startIdleTimer (unsigned int timeoutSeconds, std::function< bool()> inUse)
 For server connections (TcpConnectionIdleTimeout): the connection is closed, with close(), once nothing has been received on it for timeoutSeconds while inUse() is false - eg.
 
void noteReceived ()
 
void stopIdleTimer ()
 

Protected Attributes

asio::ip::tcp::endpoint mLocalTcpEndpoint
 
asio::io_context & mIOService
 The io_context used to perform asynchronous operations.
 
std::shared_ptr< DataBuffer > mReceiveBuffer
 Receive Buffer and state.
 
bool mReceiving
 
asio::ip::address mConnectedAddress
 Connected Info and State.
 
unsigned short mConnectedPort
 
bool mConnected
 
AsyncSocketBaseHandler * mAsyncSocketBaseHandler
 Handlers.
 
BeforeClosedHandler mOnBeforeSocketCloseFp
 Provides an opportunity for the app to clean up, e.g., QoS-related data or resources just before the socket is closed.
 

Detailed Description

Definition at line 27 of file AsyncSocketBase.hxx.

Member Typedef Documentation

◆ BeforeClosedHandler

Constructor & Destructor Documentation

◆ AsyncSocketBase()

reTurn::AsyncSocketBase::AsyncSocketBase ( asio::io_context &  ioService)
explicit

Definition at line 18 of file AsyncSocketBase.cxx.

◆ ~AsyncSocketBase()

reTurn::AsyncSocketBase::~AsyncSocketBase ( )
virtual

Definition at line 26 of file AsyncSocketBase.cxx.

Member Function Documentation

◆ allocateBuffer()

std::shared_ptr< DataBuffer > reTurn::AsyncSocketBase::allocateBuffer ( size_t  size)
static

Utility API.

Definition at line 178 of file AsyncSocketBase.cxx.

◆ asyncConnect()

void reTurn::AsyncSocketBase::asyncConnect ( asio::ip::tcp::socket &  socket,
const asio::ip::tcp::resolver::results_type &  endpoints 
)
protected

Definition at line 214 of file AsyncSocketBase.cxx.

◆ bind()

virtual asio::error_code reTurn::AsyncSocketBase::bind ( const asio::ip::address &  address,
unsigned short  port 
)
pure virtual

Note: The following API's are thread safe and queue the request to be handled by the ioService thread.

Implemented in reTurn::AsyncTcpSocketBase, reTurn::AsyncTlsSocketBase, and reTurn::AsyncUdpSocketBase.

◆ channelDataPadding()

static size_t reTurn::AsyncSocketBase::channelDataPadding ( size_t  length)
inlinestatic

Over TCP and TLS, a TURN ChannelData message is padded to a multiple of 4 bytes, the padding not counted in its length (RFC 5766 11.5, RFC 8656 12.5): the padding after one of length bytes.

Definition at line 76 of file AsyncSocketBase.hxx.

◆ close()

void reTurn::AsyncSocketBase::close ( )
virtual

Reimplemented in reTurn::TcpConnection, and reTurn::TlsConnection.

Definition at line 172 of file AsyncSocketBase.cxx.

◆ connect()

virtual void reTurn::AsyncSocketBase::connect ( const std::string &  address,
unsigned short  port 
)
pure virtual

◆ doFramedReceive()

void reTurn::AsyncSocketBase::doFramedReceive ( )
virtual

Definition at line 143 of file AsyncSocketBase.cxx.

◆ doReceive()

void reTurn::AsyncSocketBase::doReceive ( )
virtual

Definition at line 126 of file AsyncSocketBase.cxx.

◆ doSend() [1/2]

void reTurn::AsyncSocketBase::doSend ( const StunTuple &  destination,
const std::shared_ptr< DataBuffer > &  data,
const std::size_t  bufferStartPos = 0 
)
virtual

Definition at line 50 of file AsyncSocketBase.cxx.

◆ doSend() [2/2]

void reTurn::AsyncSocketBase::doSend ( const StunTuple &  destination,
unsigned short  channel,
const std::shared_ptr< DataBuffer > &  data,
const std::size_t  bufferStartPos = 0 
)
virtual

Use these if you already operating within the ioService thread.

Definition at line 56 of file AsyncSocketBase.cxx.

◆ framedReceive()

void reTurn::AsyncSocketBase::framedReceive ( )
virtual

Definition at line 137 of file AsyncSocketBase.cxx.

◆ getConnectedAddress()

asio::ip::address & reTurn::AsyncSocketBase::getConnectedAddress ( )
inlinenoexcept

Definition at line 52 of file AsyncSocketBase.hxx.

◆ getConnectedPort()

unsigned short reTurn::AsyncSocketBase::getConnectedPort ( ) const
inlinenoexcept

Definition at line 53 of file AsyncSocketBase.hxx.

◆ getSocketDescriptor()

◆ handleClientHandshake()

virtual void reTurn::AsyncSocketBase::handleClientHandshake ( const asio::error_code &  ec,
const asio::ip::tcp::endpoint &  endpoint 
)
inlinevirtual

Reimplemented in reTurn::AsyncTlsSocketBase.

Definition at line 94 of file AsyncSocketBase.hxx.

◆ handleConnect()

virtual void reTurn::AsyncSocketBase::handleConnect ( const asio::error_code &  ec,
const asio::ip::tcp::endpoint &  endpoint 
)
inlinevirtual

Reimplemented in reTurn::AsyncTcpSocketBase, and reTurn::AsyncTlsSocketBase.

Definition at line 93 of file AsyncSocketBase.hxx.

◆ handleReadHeader()

virtual void reTurn::AsyncSocketBase::handleReadHeader ( const asio::error_code &  e)
inlinevirtual

Reimplemented in reTurn::AsyncTcpSocketBase, and reTurn::AsyncTlsSocketBase.

Definition at line 89 of file AsyncSocketBase.hxx.

◆ handleReceive()

void reTurn::AsyncSocketBase::handleReceive ( const asio::error_code &  e,
size_t  bytesTransferred 
)
protectedvirtual

Definition at line 154 of file AsyncSocketBase.cxx.

◆ handleSend()

void reTurn::AsyncSocketBase::handleSend ( const asio::error_code &  e)
protectedvirtual

Handle completion of a sendData operation.

Definition at line 81 of file AsyncSocketBase.cxx.

◆ handleServerHandshake()

virtual void reTurn::AsyncSocketBase::handleServerHandshake ( const asio::error_code &  e)
inlinevirtual

Reimplemented in reTurn::AsyncTlsSocketBase.

Definition at line 90 of file AsyncSocketBase.hxx.

◆ handleTcpResolve()

virtual void reTurn::AsyncSocketBase::handleTcpResolve ( const asio::error_code &  ec,
const asio::ip::tcp::resolver::results_type &  endpoints 
)
inlinevirtual

Reimplemented in reTurn::AsyncTcpSocketBase, and reTurn::AsyncTlsSocketBase.

Definition at line 91 of file AsyncSocketBase.hxx.

◆ handleUdpResolve()

virtual void reTurn::AsyncSocketBase::handleUdpResolve ( const asio::error_code &  ec,
const asio::ip::udp::resolver::results_type &  endpoints 
)
inlinevirtual

Reimplemented in reTurn::AsyncUdpSocketBase.

Definition at line 92 of file AsyncSocketBase.hxx.

◆ isConnected()

bool reTurn::AsyncSocketBase::isConnected ( ) const
inlinenoexcept

Definition at line 51 of file AsyncSocketBase.hxx.

◆ noteReceived()

void reTurn::AsyncSocketBase::noteReceived ( )
inlineprotected

Definition at line 114 of file AsyncSocketBase.hxx.

◆ onConnectFailure()

virtual void reTurn::AsyncSocketBase::onConnectFailure ( const asio::error_code &  e)
inlinevirtual

Definition at line 65 of file AsyncSocketBase.hxx.

◆ onConnectSuccess()

virtual void reTurn::AsyncSocketBase::onConnectSuccess ( )
inlinevirtual

Class override callbacks.

Definition at line 64 of file AsyncSocketBase.hxx.

◆ onReceiveFailure()

virtual void reTurn::AsyncSocketBase::onReceiveFailure ( const asio::error_code &  e)
pure virtual

◆ onReceiveSuccess()

virtual void reTurn::AsyncSocketBase::onReceiveSuccess ( const asio::ip::address &  address,
unsigned short  port,
const std::shared_ptr< DataBuffer > &  data 
)
pure virtual

◆ onSendFailure()

virtual void reTurn::AsyncSocketBase::onSendFailure ( const asio::error_code &  e)
pure virtual

◆ onSendSuccess()

virtual void reTurn::AsyncSocketBase::onSendSuccess ( )
pure virtual

◆ openAndBind()

asio::error_code reTurn::AsyncSocketBase::openAndBind ( asio::ip::tcp::socket &  socket)
protected

For TCP and TLS client sockets.

openAndBind opens socket and binds it to mLocalTcpEndpoint (set by bind()). asyncConnect connects socket to each of endpoints in turn until one succeeds, then calls handleConnect - like asio::async_connect, but binding the socket before each attempt: asio::async_connect closes it first, losing the binding.

Definition at line 198 of file AsyncSocketBase.cxx.

◆ receive()

void reTurn::AsyncSocketBase::receive ( )
virtual

Overlapped calls to receive functions have no effect.

Definition at line 120 of file AsyncSocketBase.cxx.

◆ registerAsyncSocketBaseHandler()

virtual void reTurn::AsyncSocketBase::registerAsyncSocketBaseHandler ( AsyncSocketBaseHandler *  handler)
inlinevirtual

Definition at line 38 of file AsyncSocketBase.hxx.

◆ send() [1/2]

void reTurn::AsyncSocketBase::send ( const StunTuple &  destination,
const std::shared_ptr< DataBuffer > &  data 
)
virtual

Note: destination is ignored for TCP and TLS connections.

Definition at line 32 of file AsyncSocketBase.cxx.

◆ send() [2/2]

void reTurn::AsyncSocketBase::send ( const StunTuple &  destination,
unsigned short  channel,
const std::shared_ptr< DataBuffer > &  data 
)
virtual

Definition at line 41 of file AsyncSocketBase.cxx.

◆ setExclusiveAddressUse()

void reTurn::AsyncSocketBase::setExclusiveAddressUse ( asio::ip::udp::socket &  socket)
static

For a UDP socket, between open() and bind().

UDP sockets don't set SO_REUSEADDR: there's no TIME_WAIT for it to help with, and it would let the socket share a port another socket has (or, on Windows, bind over it), splitting its traffic. On Windows this sets SO_EXCLUSIVEADDRUSE, so neither can another socket - one that sets SO_REUSEADDR - bind over this one.

Definition at line 184 of file AsyncSocketBase.cxx.

◆ setOnBeforeSocketClosedFp()

virtual void reTurn::AsyncSocketBase::setOnBeforeSocketClosedFp ( BeforeClosedHandler  fp)
inlinevirtual

Definition at line 55 of file AsyncSocketBase.hxx.

◆ start()

virtual void reTurn::AsyncSocketBase::start ( )
inlinevirtual

Reimplemented in reTurn::TcpConnection, reTurn::TlsConnection, and reTurn::UdpServer.

Definition at line 87 of file AsyncSocketBase.hxx.

◆ startIdleTimer()

void reTurn::AsyncSocketBase::startIdleTimer ( unsigned int  timeoutSeconds,
std::function< bool()>  inUse 
)
protected

For server connections (TcpConnectionIdleTimeout): the connection is closed, with close(), once nothing has been received on it for timeoutSeconds while inUse() is false - eg.

while it carries no allocation. 0 for no timeout. Call noteReceived() for each message received, and stopIdleTimer() when the connection is stopped.

Definition at line 254 of file AsyncSocketBase.cxx.

◆ stop()

virtual void reTurn::AsyncSocketBase::stop ( )
inlinevirtual

Reimplemented in reTurn::TcpConnection, reTurn::TlsConnection, and reTurn::UdpRelayServer.

Definition at line 88 of file AsyncSocketBase.hxx.

◆ stopIdleTimer()

void reTurn::AsyncSocketBase::stopIdleTimer ( )
protected

Definition at line 268 of file AsyncSocketBase.cxx.

Member Data Documentation

◆ mAsyncSocketBaseHandler

AsyncSocketBaseHandler* reTurn::AsyncSocketBase::mAsyncSocketBaseHandler
protected

Handlers.

Definition at line 130 of file AsyncSocketBase.hxx.

◆ mConnected

bool reTurn::AsyncSocketBase::mConnected
protected

Definition at line 127 of file AsyncSocketBase.hxx.

◆ mConnectedAddress

asio::ip::address reTurn::AsyncSocketBase::mConnectedAddress
protected

Connected Info and State.

Definition at line 125 of file AsyncSocketBase.hxx.

◆ mConnectedPort

unsigned short reTurn::AsyncSocketBase::mConnectedPort
protected

Definition at line 126 of file AsyncSocketBase.hxx.

◆ mIOService

asio::io_context& reTurn::AsyncSocketBase::mIOService
protected

The io_context used to perform asynchronous operations.

Definition at line 118 of file AsyncSocketBase.hxx.

◆ mLocalTcpEndpoint

asio::ip::tcp::endpoint reTurn::AsyncSocketBase::mLocalTcpEndpoint
protected

Definition at line 107 of file AsyncSocketBase.hxx.

◆ mOnBeforeSocketCloseFp

BeforeClosedHandler reTurn::AsyncSocketBase::mOnBeforeSocketCloseFp
protected

Provides an opportunity for the app to clean up, e.g., QoS-related data or resources just before the socket is closed.

Definition at line 134 of file AsyncSocketBase.hxx.

◆ mReceiveBuffer

std::shared_ptr<DataBuffer> reTurn::AsyncSocketBase::mReceiveBuffer
protected

Receive Buffer and state.

Definition at line 121 of file AsyncSocketBase.hxx.

◆ mReceiving

bool reTurn::AsyncSocketBase::mReceiving
protected

Definition at line 122 of file AsyncSocketBase.hxx.


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