Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
|
TCP connection port. More...
#include <tcp_connection_port.h>
Public Member Functions | |
TcpConnectionPort (TcpConnectionType type, uv_loop_t &loop, core::IArena &arena) | |
Initialize. More... | |
virtual | ~TcpConnectionPort () |
Destroy. More... | |
virtual bool | open () |
Open TCP connection. More... | |
virtual AsyncOperationStatus | async_close (ICloseHandler &handler, void *handler_arg) |
Asynchronously close TCP connection. More... | |
bool | accept (const TcpConnectionConfig &config, const address::SocketAddr &server_address, SocketHandle server_socket) |
Establish conection by accepting it from listening socket. More... | |
bool | connect (const TcpClientConfig &config) |
Establish connection to remote peer (asynchronously). More... | |
void | attach_terminate_handler (ITerminateHandler &handler, void *handler_arg) |
Set termination handler and start using it. More... | |
void | attach_connection_handler (IConnHandler &handler) |
Set connection handler and start reporting events to it. More... | |
virtual const address::SocketAddr & | local_address () const |
Return address of the local peer. More... | |
virtual const address::SocketAddr & | remote_address () const |
Return address of the remote peer. More... | |
virtual bool | is_failed () const |
Return true if there was a failure. More... | |
virtual bool | is_writable () const |
Return true if the connection is writable. More... | |
virtual bool | is_readable () const |
Return true if the connection is readable. More... | |
virtual ssize_t | try_write (const void *buf, size_t len) |
Write buf of size len to the connection. More... | |
virtual ssize_t | try_read (void *buf, size_t len) |
Read len bytes from the the connection to buf . More... | |
virtual void | async_terminate (TerminationMode mode) |
Initiate asynchronous graceful shutdown. More... | |
Public Member Functions inherited from roc::netio::BasicPort | |
BasicPort (core::IArena &) | |
Initialize. More... | |
virtual | ~BasicPort () |
Destroy. More... | |
const char * | descriptor () const |
Get a human-readable port description. More... | |
Public Member Functions inherited from roc::core::RefCounted< BasicPort, core::ArenaAllocation > | |
RefCounted () | |
Initialize. More... | |
RefCounted (const core::ArenaAllocation &policy) | |
Initialize. More... | |
int | getref () const |
Get reference counter. More... | |
void | incref () const |
Increment reference counter. More... | |
void | decref () const |
Decrement reference counter. More... | |
Public Member Functions inherited from roc::core::ListNode< Tag > | |
ListData * | list_data () const |
Get pointer to internal data. More... | |
Protected Member Functions | |
virtual void | format_descriptor (core::StringBuilder &b) |
Format descriptor. More... | |
Protected Member Functions inherited from roc::netio::BasicPort | |
void | update_descriptor () |
Format descriptor and store into internal buffer. More... | |
Protected Member Functions inherited from roc::core::ArenaAllocation | |
IArena & | arena () const |
Get arena. More... | |
ArenaAllocation (IArena &arena) | |
Initialize. More... | |
template<class T > | |
void | destroy (T &object) |
Destroy object and return memory to arena. More... | |
Additional Inherited Members | |
Static Public Member Functions inherited from roc::core::ListNode< Tag > | |
static ListNode * | list_node (ListData *data) |
Get pointer to parent node from pointer to internal data. More... | |
TCP connection port.
There are two important interfaces related to TCP connection:
IConn is implemented by TcpConnectionPort. The interface allows to retrieve connection parameters and perform non-blocking I/O.
IConnHandler is implemented by users of netio module. This interface is notified about connection state changes (e.g. connection is established) and availability of I/O (e.g. connection becomes readable).
Methods that are not part of IConn interface are called from within other netio classes, e.g. TcpServerPort, on the network loop thread.
Methods from the IConn interface are called by users of netio module from any thread. They are thread-safe and lock-free.
Connection can be client-side (connect call) or server-side (accept call).
Client-side connection is created using AddTcpClientPort task of the network loop, and is closed using RemovePort task. Before removing the port, the user must call async_terminate() and wait until termination is completed.
Server-side connection is created by TcpServerPort when it receives a new incoming connection. To remove it, the user should call async_terminate(). When termination is completed, TcpServerPort automatically closes and destroys connection.
The following rules must be followed:
TcpConnectionPort maintains an FSM and sees each operation or event handler as a transition between states. Each operation is allowed only in certain states and will panic when not used properly.
State switch mostly happens on the network thread, however some limited set of transitions is allowed from other threads. For this reason, state switching is done using atomic operations.
Definition at line 123 of file tcp_connection_port.h.
roc::netio::TcpConnectionPort::TcpConnectionPort | ( | TcpConnectionType | type, |
uv_loop_t & | loop, | ||
core::IArena & | arena | ||
) |
Initialize.
|
virtual |
Destroy.
bool roc::netio::TcpConnectionPort::accept | ( | const TcpConnectionConfig & | config, |
const address::SocketAddr & | server_address, | ||
SocketHandle | server_socket | ||
) |
Establish conection by accepting it from listening socket.
|
virtual |
Asynchronously close TCP connection.
Implements roc::netio::BasicPort.
|
virtual |
Initiate asynchronous graceful shutdown.
Implements roc::netio::IConn.
void roc::netio::TcpConnectionPort::attach_connection_handler | ( | IConnHandler & | handler | ) |
Set connection handler and start reporting events to it.
void roc::netio::TcpConnectionPort::attach_terminate_handler | ( | ITerminateHandler & | handler, |
void * | handler_arg | ||
) |
Set termination handler and start using it.
bool roc::netio::TcpConnectionPort::connect | ( | const TcpClientConfig & | config | ) |
Establish connection to remote peer (asynchronously).
|
protectedvirtual |
Format descriptor.
Implements roc::netio::BasicPort.
|
virtual |
Return true if there was a failure.
Implements roc::netio::IConn.
|
virtual |
Return true if the connection is readable.
Implements roc::netio::IConn.
|
virtual |
Return true if the connection is writable.
Implements roc::netio::IConn.
|
virtual |
Return address of the local peer.
Implements roc::netio::IConn.
|
virtual |
Open TCP connection.
Implements roc::netio::BasicPort.
|
virtual |
Return address of the remote peer.
Implements roc::netio::IConn.
|
virtual |
Read len
bytes from the the connection to buf
.
Implements roc::netio::IConn.
|
virtual |
Write buf
of size len
to the connection.
Implements roc::netio::IConn.