|
CppServer 1.0.5.0
C++ Server Library
|
TCP server. More...
#include <tcp_server.h>
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. | |
| 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. | |
| TCPServer (const std::shared_ptr< Service > &service, const asio::ip::tcp::endpoint &endpoint) | |
| Initialize TCP server with a given Asio service and endpoint. | |
| TCPServer (const TCPServer &)=delete | |
| TCPServer (TCPServer &&)=delete | |
| virtual | ~TCPServer ()=default |
| TCPServer & | operator= (const TCPServer &)=delete |
| TCPServer & | operator= (TCPServer &&)=delete |
| const CppCommon::UUID & | id () const noexcept |
| Get the server Id. | |
| std::shared_ptr< Service > & | service () noexcept |
| Get the Asio service. | |
| std::shared_ptr< asio::io_service > & | io_service () noexcept |
| Get the Asio IO service. | |
| asio::io_service::strand & | strand () noexcept |
| Get the Asio service strand for serialized handler execution. | |
| asio::ip::tcp::endpoint & | endpoint () noexcept |
| Get the server endpoint. | |
| asio::ip::tcp::acceptor & | acceptor () noexcept |
| Get the server acceptor. | |
| const std::string & | address () const noexcept |
| Get the server address. | |
| int | port () const noexcept |
| Get the server port number. | |
| uint64_t | connected_sessions () const noexcept |
| Get the number of sessions connected to the server. | |
| uint64_t | bytes_pending () const noexcept |
| Get the number of bytes pending sent by the server. | |
| uint64_t | bytes_sent () const noexcept |
| Get the number of bytes sent by the server. | |
| uint64_t | bytes_received () const noexcept |
| Get the number of bytes received by the server. | |
| bool | option_keep_alive () const noexcept |
| Get the option: keep alive. | |
| bool | option_no_delay () const noexcept |
| Get the option: no delay. | |
| bool | option_reuse_address () const noexcept |
| Get the option: reuse address. | |
| bool | option_reuse_port () const noexcept |
| Get the option: reuse port. | |
| bool | IsStarted () const noexcept |
| Is the server started? | |
| virtual bool | Start () |
| Start the server. | |
| virtual bool | Stop () |
| Stop the server. | |
| virtual bool | Restart () |
| Restart the server. | |
| virtual bool | Multicast (const void *buffer, size_t size) |
| Multicast data to all connected sessions. | |
| virtual bool | Multicast (std::string_view text) |
| Multicast text to all connected sessions. | |
| virtual bool | DisconnectAll () |
| Disconnect all connected sessions. | |
| std::shared_ptr< TCPSession > | FindSession (const CppCommon::UUID &id) |
| Find a session with a given Id. | |
| void | SetupKeepAlive (bool enable) noexcept |
| Setup option: keep alive. | |
| void | SetupNoDelay (bool enable) noexcept |
| Setup option: no delay. | |
| void | SetupReuseAddress (bool enable) noexcept |
| Setup option: reuse address. | |
| void | SetupReusePort (bool enable) noexcept |
| Setup option: reuse port. | |
Protected Member Functions | |
| virtual std::shared_ptr< TCPSession > | CreateSession (const std::shared_ptr< TCPServer > &server) |
| Create TCP session factory method. | |
| virtual void | onStarted () |
| Handle server started notification. | |
| virtual void | onStopped () |
| Handle server stopped notification. | |
| virtual void | onConnected (std::shared_ptr< TCPSession > &session) |
| Handle session connected notification. | |
| virtual void | onDisconnected (std::shared_ptr< TCPSession > &session) |
| Handle session disconnected notification. | |
| virtual void | onError (int error, const std::string &category, const std::string &message) |
| Handle error notification. | |
Protected Attributes | |
| std::shared_mutex | _sessions_lock |
| std::map< CppCommon::UUID, std::shared_ptr< TCPSession > > | _sessions |
Friends | |
| class | TCPSession |
TCP server.
TCP server is used to connect, disconnect and manage TCP sessions.
Thread-safe.
Definition at line 30 of file tcp_server.h.
| 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.
| service | - Asio service |
| port | - Port number |
| protocol | - Internet protocol type (default is IPv4) |
Definition at line 14 of file tcp_server.cpp.
| 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.
| service | - Asio service |
| address | - Server address |
| port | - Port number |
Definition at line 47 of file tcp_server.cpp.
| 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.
| service | - Asio service |
| endpoint | - Server TCP endpoint |
Definition at line 73 of file tcp_server.cpp.
|
delete |
|
virtualdefault |
|
inlinenoexcept |
Get the server acceptor.
Definition at line 74 of file tcp_server.h.
|
inlinenoexcept |
Get the server address.
Definition at line 77 of file tcp_server.h.
|
inlinenoexcept |
Get the number of bytes pending sent by the server.
Definition at line 84 of file tcp_server.h.
|
inlinenoexcept |
Get the number of bytes received by the server.
Definition at line 88 of file tcp_server.h.
|
inlinenoexcept |
Get the number of bytes sent by the server.
Definition at line 86 of file tcp_server.h.
|
inlinenoexcept |
Get the number of sessions connected to the server.
Definition at line 82 of file tcp_server.h.
|
inlineprotectedvirtual |
Create TCP session factory method.
| server | - TCP server |
Definition at line 182 of file tcp_server.h.
|
virtual |
Disconnect all connected sessions.
Definition at line 259 of file tcp_server.cpp.
|
inlinenoexcept |
Get the server endpoint.
Definition at line 72 of file tcp_server.h.
| std::shared_ptr< TCPSession > CppServer::Asio::TCPServer::FindSession | ( | const CppCommon::UUID & | id | ) |
Find a session with a given Id.
| id | - Session Id |
Definition at line 285 of file tcp_server.cpp.
|
inlinenoexcept |
Get the server Id.
Definition at line 63 of file tcp_server.h.
|
inlinenoexcept |
Get the Asio IO service.
Definition at line 68 of file tcp_server.h.
|
inlinenoexcept |
Is the server started?
Definition at line 100 of file tcp_server.h.
Multicast data to all connected sessions.
| buffer | - Buffer to multicast |
| size | - Buffer size |
Reimplemented in CppServer::WS::WSServer.
Definition at line 238 of file tcp_server.cpp.
Multicast text to all connected sessions.
| text | - Text to multicast |
Definition at line 130 of file tcp_server.h.
|
inlineprotectedvirtual |
Handle session connected notification.
| session | - Connected session |
Definition at line 194 of file tcp_server.h.
|
inlineprotectedvirtual |
Handle session disconnected notification.
| session | - Disconnected session |
Definition at line 199 of file tcp_server.h.
|
inlineprotectedvirtual |
Handle error notification.
| error | - Error code |
| category | - Error category |
| message | - Error message |
Definition at line 207 of file tcp_server.h.
Handle server started notification.
Definition at line 186 of file tcp_server.h.
Handle server stopped notification.
Definition at line 188 of file tcp_server.h.
|
inlinenoexcept |
Get the option: keep alive.
Definition at line 91 of file tcp_server.h.
|
inlinenoexcept |
Get the option: no delay.
Definition at line 93 of file tcp_server.h.
|
inlinenoexcept |
Get the option: reuse address.
Definition at line 95 of file tcp_server.h.
|
inlinenoexcept |
Get the option: reuse port.
Definition at line 97 of file tcp_server.h.
|
inlinenoexcept |
Get the server port number.
Definition at line 79 of file tcp_server.h.
|
virtual |
Restart the server.
Definition at line 186 of file tcp_server.cpp.
|
inlinenoexcept |
Get the Asio service.
Definition at line 66 of file tcp_server.h.
Setup option: keep alive.
This option will setup SO_KEEPALIVE if the OS support this feature.
| enable | - Enable/disable option |
Definition at line 151 of file tcp_server.h.
Setup option: no delay.
This option will enable/disable Nagle's algorithm for TCP protocol.
https://en.wikipedia.org/wiki/Nagle%27s_algorithm
| enable | - Enable/disable option |
Definition at line 160 of file tcp_server.h.
Setup option: reuse address.
This option will enable/disable SO_REUSEADDR if the OS support this feature.
| enable | - Enable/disable option |
Definition at line 167 of file tcp_server.h.
Setup option: reuse port.
This option will enable/disable SO_REUSEPORT if the OS support this feature.
| enable | - Enable/disable option |
Definition at line 174 of file tcp_server.h.
|
virtual |
Start the server.
Definition at line 97 of file tcp_server.cpp.
|
virtual |
Stop the server.
Definition at line 147 of file tcp_server.cpp.
|
inlinenoexcept |
Get the Asio service strand for serialized handler execution.
Definition at line 70 of file tcp_server.h.
|
friend |
Definition at line 32 of file tcp_server.h.
|
protected |
Definition at line 212 of file tcp_server.h.
|
protected |
Definition at line 211 of file tcp_server.h.