Woman, Life, Freedom


Async BSD Sockets Library
Library protected and public interface header documentation
All Classes Functions Variables
AsyncBsdSocketLib::TcpListener Class Reference

TCP listener (server) to accept a TcpClient. More...

#include <tcp_listener.h>

Inheritance diagram for AsyncBsdSocketLib::TcpListener:
AsyncBsdSocketLib::NetworkSocket AsyncBsdSocketLib::Communicator

Public Member Functions

 TcpListener (uint16_t port)
 Constructor to listen on any IP address. More...
 
 TcpListener (std::string ipAddress, uint16_t port)
 Constructor. More...
 
int Connection () const noexcept override
 Connection descriptor for sending and receiving. More...
 
bool TrySetup () noexcept override
 Try to setup the communicator. More...
 
bool TryAccept () noexcept
 Try to accept a client to form a connection. More...
 
template<std::size_t N>
ssize_t Send (const std::array< uint8_t, N > &buffer) const noexcept
 Send a byte array to the connected client. More...
 
template<std::size_t N>
ssize_t Receive (std::array< uint8_t, N > &buffer) const noexcept
 Receive a byte array from the connected client. More...
 
bool TryMakeConnectionNonblock () noexcept
 Try to make the current connection (if exists) non-block. More...
 
- Public Member Functions inherited from AsyncBsdSocketLib::Communicator
int Descriptor () const noexcept
 File descriptor. More...
 
virtual bool TryClose () noexcept
 Try to close the communicator. More...
 

Additional Inherited Members

- Protected Member Functions inherited from AsyncBsdSocketLib::NetworkSocket
 NetworkSocket (std::string ipAddress, uint16_t port)
 Constructor. More...
 
- Protected Attributes inherited from AsyncBsdSocketLib::NetworkSocket
const std::string IpAddress
 Listening/Connecting IP address for server/client.
 
const uint16_t Port
 Listening/Connecting port number for server/client.
 
- Protected Attributes inherited from AsyncBsdSocketLib::Communicator
int FileDescriptor
 File descriptor.
 

Detailed Description

TCP listener (server) to accept a TcpClient.

See also
TcpClient

Constructor & Destructor Documentation

◆ TcpListener() [1/2]

AsyncBsdSocketLib::TcpListener::TcpListener ( uint16_t  port)

Constructor to listen on any IP address.

Parameters
portListen port number

◆ TcpListener() [2/2]

AsyncBsdSocketLib::TcpListener::TcpListener ( std::string  ipAddress,
uint16_t  port 
)

Constructor.

Parameters
ipAddressListen IP address
portListen port number

Member Function Documentation

◆ Connection()

int AsyncBsdSocketLib::TcpListener::Connection ( ) const
overridevirtualnoexcept

Connection descriptor for sending and receiving.

Returns
A non-negative value if there is a valid connection; otherwise -1

Implements AsyncBsdSocketLib::Communicator.

◆ Receive()

template<std::size_t N>
ssize_t AsyncBsdSocketLib::TcpListener::Receive ( std::array< uint8_t, N > &  buffer) const
inlinenoexcept

Receive a byte array from the connected client.

Template Parameters
NReceive buffer size
Parameters
bufferReceive buffer byte array
Returns
Size of received bytes (-1 in case of receiving failed)
Warning
Due to edge-triggered polling, starvation can occur

◆ Send()

template<std::size_t N>
ssize_t AsyncBsdSocketLib::TcpListener::Send ( const std::array< uint8_t, N > &  buffer) const
inlinenoexcept

Send a byte array to the connected client.

Template Parameters
NSend buffer size
Parameters
bufferSend buffer byte array
Returns
Size of sent bytes (-1 in case of sending failed)

◆ TryAccept()

bool AsyncBsdSocketLib::TcpListener::TryAccept ( )
noexcept

Try to accept a client to form a connection.

Returns
True if the client is successfully accepted; otherwise false

◆ TryMakeConnectionNonblock()

bool AsyncBsdSocketLib::TcpListener::TryMakeConnectionNonblock ( )
noexcept

Try to make the current connection (if exists) non-block.

Returns
True if the non-blocking flag is set successfully; otherwise false

◆ TrySetup()

bool AsyncBsdSocketLib::TcpListener::TrySetup ( )
overridevirtualnoexcept

Try to setup the communicator.

Returns
True if the communicator is successfully set up; otherwise false

Implements AsyncBsdSocketLib::Communicator.