CppServer  1.0.4.0
C++ Server Library
Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
CppServer::Asio::TCPServer Class Reference

TCP server. More...

#include <tcp_server.h>

Inheritance diagram for CppServer::Asio::TCPServer:
CppServer::HTTP::HTTPServer CppServer::WS::WSServer

Public Member Functions

 TCPServer (const std::shared_ptr< Service > &service, int port, InternetProtocol protocol=InternetProtocol::IPv4)
 Initialize TCP server with a given Asio service and port number. More...
 
 TCPServer (const std::shared_ptr< Service > &service, const std::string &address, int port)
 Initialize TCP server with a given Asio service, server address and port number. More...
 
 TCPServer (const std::shared_ptr< Service > &service, const asio::ip::tcp::endpoint &endpoint)
 Initialize TCP server with a given Asio service and endpoint. More...
 
 TCPServer (const TCPServer &)=delete
 
 TCPServer (TCPServer &&)=delete
 
virtual ~TCPServer ()=default
 
TCPServeroperator= (const TCPServer &)=delete
 
TCPServeroperator= (TCPServer &&)=delete
 
const CppCommon::UUID & id () const noexcept
 Get the server Id. More...
 
std::shared_ptr< Service > & service () noexcept
 Get the Asio service. More...
 
std::shared_ptr< asio::io_service > & io_service () noexcept
 Get the Asio IO service. More...
 
asio::io_service::strand & strand () noexcept
 Get the Asio service strand for serialized handler execution. More...
 
asio::ip::tcp::endpoint & endpoint () noexcept
 Get the server endpoint. More...
 
asio::ip::tcp::acceptor & acceptor () noexcept
 Get the server acceptor. More...
 
const std::string & address () const noexcept
 Get the server address. More...
 
int port () const noexcept
 Get the server port number. More...
 
uint64_t connected_sessions () const noexcept
 Get the number of sessions connected to the server. More...
 
uint64_t bytes_pending () const noexcept
 Get the number of bytes pending sent by the server. More...
 
uint64_t bytes_sent () const noexcept
 Get the number of bytes sent by the server. More...
 
uint64_t bytes_received () const noexcept
 Get the number of bytes received by the server. More...
 
bool option_keep_alive () const noexcept
 Get the option: keep alive. More...
 
bool option_no_delay () const noexcept
 Get the option: no delay. More...
 
bool option_reuse_address () const noexcept
 Get the option: reuse address. More...
 
bool option_reuse_port () const noexcept
 Get the option: reuse port. More...
 
bool IsStarted () const noexcept
 Is the server started? More...
 
virtual bool Start ()
 Start the server. More...
 
virtual bool Stop ()
 Stop the server. More...
 
virtual bool Restart ()
 Restart the server. More...
 
virtual bool Multicast (const void *buffer, size_t size)
 Multicast data to all connected sessions. More...
 
virtual bool Multicast (std::string_view text)
 Multicast text to all connected sessions. More...
 
virtual bool DisconnectAll ()
 Disconnect all connected sessions. More...
 
std::shared_ptr< TCPSessionFindSession (const CppCommon::UUID &id)
 Find a session with a given Id. More...
 
void SetupKeepAlive (bool enable) noexcept
 Setup option: keep alive. More...
 
void SetupNoDelay (bool enable) noexcept
 Setup option: no delay. More...
 
void SetupReuseAddress (bool enable) noexcept
 Setup option: reuse address. More...
 
void SetupReusePort (bool enable) noexcept
 Setup option: reuse port. More...
 

Protected Member Functions

virtual std::shared_ptr< TCPSessionCreateSession (const std::shared_ptr< TCPServer > &server)
 Create TCP session factory method. More...
 
virtual void onStarted ()
 Handle server started notification. More...
 
virtual void onStopped ()
 Handle server stopped notification. More...
 
virtual void onConnected (std::shared_ptr< TCPSession > &session)
 Handle session connected notification. More...
 
virtual void onDisconnected (std::shared_ptr< TCPSession > &session)
 Handle session disconnected notification. More...
 
virtual void onError (int error, const std::string &category, const std::string &message)
 Handle error notification. More...
 

Protected Attributes

std::shared_mutex _sessions_lock
 
std::map< CppCommon::UUID, std::shared_ptr< TCPSession > > _sessions
 

Friends

class TCPSession
 

Detailed Description

TCP server.

TCP server is used to connect, disconnect and manage TCP sessions.

Thread-safe.

Examples
tcp_chat_server.cpp.

Definition at line 30 of file tcp_server.h.

Constructor & Destructor Documentation

◆ TCPServer() [1/5]

CppServer::Asio::TCPServer::TCPServer ( const std::shared_ptr< Service > &  service,
int  port,
InternetProtocol  protocol = InternetProtocol::IPv4 
)

Initialize TCP server with a given Asio service and port number.

Parameters
service- Asio service
port- Port number
protocol- Internet protocol type (default is IPv4)
Examples
tcp_chat_server.cpp.

Definition at line 14 of file tcp_server.cpp.

◆ TCPServer() [2/5]

CppServer::Asio::TCPServer::TCPServer ( const std::shared_ptr< Service > &  service,
const std::string &  address,
int  port 
)

Initialize TCP server with a given Asio service, server address and port number.

Parameters
service- Asio service
address- Server address
port- Port number

Definition at line 47 of file tcp_server.cpp.

◆ TCPServer() [3/5]

CppServer::Asio::TCPServer::TCPServer ( const std::shared_ptr< Service > &  service,
const asio::ip::tcp::endpoint &  endpoint 
)

Initialize TCP server with a given Asio service and endpoint.

Parameters
service- Asio service
endpoint- Server TCP endpoint

Definition at line 73 of file tcp_server.cpp.

◆ TCPServer() [4/5]

CppServer::Asio::TCPServer::TCPServer ( const TCPServer )
delete

◆ TCPServer() [5/5]

CppServer::Asio::TCPServer::TCPServer ( TCPServer &&  )
delete

◆ ~TCPServer()

virtual CppServer::Asio::TCPServer::~TCPServer ( )
virtualdefault

Member Function Documentation

◆ acceptor()

asio::ip::tcp::acceptor& CppServer::Asio::TCPServer::acceptor ( )
inlinenoexcept

Get the server acceptor.

Definition at line 74 of file tcp_server.h.

◆ address()

const std::string& CppServer::Asio::TCPServer::address ( ) const
inlinenoexcept

Get the server address.

Definition at line 77 of file tcp_server.h.

◆ bytes_pending()

uint64_t CppServer::Asio::TCPServer::bytes_pending ( ) const
inlinenoexcept

Get the number of bytes pending sent by the server.

Definition at line 84 of file tcp_server.h.

◆ bytes_received()

uint64_t CppServer::Asio::TCPServer::bytes_received ( ) const
inlinenoexcept

Get the number of bytes received by the server.

Definition at line 88 of file tcp_server.h.

◆ bytes_sent()

uint64_t CppServer::Asio::TCPServer::bytes_sent ( ) const
inlinenoexcept

Get the number of bytes sent by the server.

Definition at line 86 of file tcp_server.h.

◆ connected_sessions()

uint64_t CppServer::Asio::TCPServer::connected_sessions ( ) const
inlinenoexcept

Get the number of sessions connected to the server.

Definition at line 82 of file tcp_server.h.

◆ CreateSession()

virtual std::shared_ptr<TCPSession> CppServer::Asio::TCPServer::CreateSession ( const std::shared_ptr< TCPServer > &  server)
inlineprotectedvirtual

Create TCP session factory method.

Parameters
server- TCP server
Returns
TCP session
Examples
tcp_chat_server.cpp.

Definition at line 182 of file tcp_server.h.

◆ DisconnectAll()

bool CppServer::Asio::TCPServer::DisconnectAll ( )
virtual

Disconnect all connected sessions.

Returns
'true' if all sessions were successfully disconnected, 'false' if the server is not started

Definition at line 259 of file tcp_server.cpp.

◆ endpoint()

asio::ip::tcp::endpoint& CppServer::Asio::TCPServer::endpoint ( )
inlinenoexcept

Get the server endpoint.

Definition at line 72 of file tcp_server.h.

◆ FindSession()

std::shared_ptr< TCPSession > CppServer::Asio::TCPServer::FindSession ( const CppCommon::UUID &  id)

Find a session with a given Id.

Parameters
id- Session Id
Returns
Session with a given Id or null if the session it not connected

Definition at line 285 of file tcp_server.cpp.

◆ id()

const CppCommon::UUID& CppServer::Asio::TCPServer::id ( ) const
inlinenoexcept

Get the server Id.

Definition at line 63 of file tcp_server.h.

◆ io_service()

std::shared_ptr<asio::io_service>& CppServer::Asio::TCPServer::io_service ( )
inlinenoexcept

Get the Asio IO service.

Definition at line 68 of file tcp_server.h.

◆ IsStarted()

bool CppServer::Asio::TCPServer::IsStarted ( ) const
inlinenoexcept

Is the server started?

Definition at line 100 of file tcp_server.h.

◆ Multicast() [1/2]

bool CppServer::Asio::TCPServer::Multicast ( const void *  buffer,
size_t  size 
)
virtual

Multicast data to all connected sessions.

Parameters
buffer- Buffer to multicast
size- Buffer size
Returns
'true' if the data was successfully multicast, 'false' if the server is not started

Reimplemented in CppServer::WS::WSServer.

Definition at line 238 of file tcp_server.cpp.

◆ Multicast() [2/2]

virtual bool CppServer::Asio::TCPServer::Multicast ( std::string_view  text)
inlinevirtual

Multicast text to all connected sessions.

Parameters
text- Text to multicast
Returns
'true' if the text was successfully multicast, 'false' if the server is not started

Definition at line 130 of file tcp_server.h.

◆ onConnected()

virtual void CppServer::Asio::TCPServer::onConnected ( std::shared_ptr< TCPSession > &  session)
inlineprotectedvirtual

Handle session connected notification.

Parameters
session- Connected session

Definition at line 194 of file tcp_server.h.

◆ onDisconnected()

virtual void CppServer::Asio::TCPServer::onDisconnected ( std::shared_ptr< TCPSession > &  session)
inlineprotectedvirtual

Handle session disconnected notification.

Parameters
session- Disconnected session

Definition at line 199 of file tcp_server.h.

◆ onError()

virtual void CppServer::Asio::TCPServer::onError ( int  error,
const std::string &  category,
const std::string &  message 
)
inlineprotectedvirtual

Handle error notification.

Parameters
error- Error code
category- Error category
message- Error message
Examples
http_server.cpp, tcp_chat_server.cpp, and ws_chat_server.cpp.

Definition at line 207 of file tcp_server.h.

◆ onStarted()

virtual void CppServer::Asio::TCPServer::onStarted ( )
inlineprotectedvirtual

Handle server started notification.

Definition at line 186 of file tcp_server.h.

◆ onStopped()

virtual void CppServer::Asio::TCPServer::onStopped ( )
inlineprotectedvirtual

Handle server stopped notification.

Definition at line 188 of file tcp_server.h.

◆ operator=() [1/2]

TCPServer& CppServer::Asio::TCPServer::operator= ( const TCPServer )
delete

◆ operator=() [2/2]

TCPServer& CppServer::Asio::TCPServer::operator= ( TCPServer &&  )
delete

◆ option_keep_alive()

bool CppServer::Asio::TCPServer::option_keep_alive ( ) const
inlinenoexcept

Get the option: keep alive.

Definition at line 91 of file tcp_server.h.

◆ option_no_delay()

bool CppServer::Asio::TCPServer::option_no_delay ( ) const
inlinenoexcept

Get the option: no delay.

Definition at line 93 of file tcp_server.h.

◆ option_reuse_address()

bool CppServer::Asio::TCPServer::option_reuse_address ( ) const
inlinenoexcept

Get the option: reuse address.

Definition at line 95 of file tcp_server.h.

◆ option_reuse_port()

bool CppServer::Asio::TCPServer::option_reuse_port ( ) const
inlinenoexcept

Get the option: reuse port.

Definition at line 97 of file tcp_server.h.

◆ port()

int CppServer::Asio::TCPServer::port ( ) const
inlinenoexcept

Get the server port number.

Definition at line 79 of file tcp_server.h.

◆ Restart()

bool CppServer::Asio::TCPServer::Restart ( )
virtual

Restart the server.

Returns
'true' if the server was successfully restarted, 'false' if the server failed to restart

Definition at line 186 of file tcp_server.cpp.

◆ service()

std::shared_ptr<Service>& CppServer::Asio::TCPServer::service ( )
inlinenoexcept

Get the Asio service.

Definition at line 66 of file tcp_server.h.

◆ SetupKeepAlive()

void CppServer::Asio::TCPServer::SetupKeepAlive ( bool  enable)
inlinenoexcept

Setup option: keep alive.

This option will setup SO_KEEPALIVE if the OS support this feature.

Parameters
enable- Enable/disable option

Definition at line 151 of file tcp_server.h.

◆ SetupNoDelay()

void CppServer::Asio::TCPServer::SetupNoDelay ( bool  enable)
inlinenoexcept

Setup option: no delay.

This option will enable/disable Nagle's algorithm for TCP protocol.

https://en.wikipedia.org/wiki/Nagle%27s_algorithm

Parameters
enable- Enable/disable option

Definition at line 160 of file tcp_server.h.

◆ SetupReuseAddress()

void CppServer::Asio::TCPServer::SetupReuseAddress ( bool  enable)
inlinenoexcept

Setup option: reuse address.

This option will enable/disable SO_REUSEADDR if the OS support this feature.

Parameters
enable- Enable/disable option

Definition at line 167 of file tcp_server.h.

◆ SetupReusePort()

void CppServer::Asio::TCPServer::SetupReusePort ( bool  enable)
inlinenoexcept

Setup option: reuse port.

This option will enable/disable SO_REUSEPORT if the OS support this feature.

Parameters
enable- Enable/disable option

Definition at line 174 of file tcp_server.h.

◆ Start()

bool CppServer::Asio::TCPServer::Start ( )
virtual

Start the server.

Returns
'true' if the server was successfully started, 'false' if the server failed to start

Definition at line 97 of file tcp_server.cpp.

◆ Stop()

bool CppServer::Asio::TCPServer::Stop ( )
virtual

Stop the server.

Returns
'true' if the server was successfully stopped, 'false' if the server is already stopped

Definition at line 147 of file tcp_server.cpp.

◆ strand()

asio::io_service::strand& CppServer::Asio::TCPServer::strand ( )
inlinenoexcept

Get the Asio service strand for serialized handler execution.

Definition at line 70 of file tcp_server.h.

Friends And Related Function Documentation

◆ TCPSession

friend class TCPSession
friend

Definition at line 32 of file tcp_server.h.

Member Data Documentation

◆ _sessions

std::map<CppCommon::UUID, std::shared_ptr<TCPSession> > CppServer::Asio::TCPServer::_sessions
protected

Definition at line 212 of file tcp_server.h.

◆ _sessions_lock

std::shared_mutex CppServer::Asio::TCPServer::_sessions_lock
protected

Definition at line 211 of file tcp_server.h.


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