9#ifndef CPPSERVER_ASIO_TCP_SERVER_H
10#define CPPSERVER_ASIO_TCP_SERVER_H
14#include "system/uuid.h"
18#include <shared_mutex>
30class TCPServer :
public std::enable_shared_from_this<TCPServer>
106 virtual bool Start();
143 std::shared_ptr<TCPSession>
FindSession(
const CppCommon::UUID&
id);
182 virtual std::shared_ptr<TCPSession>
CreateSession(
const std::shared_ptr<TCPServer>& server) {
return std::make_shared<TCPSession>(server); }
212 std::map<CppCommon::UUID, std::shared_ptr<TCPSession>>
_sessions;
218 std::shared_ptr<Service> _service;
220 std::shared_ptr<asio::io_service> _io_service;
222 asio::io_service::strand _strand;
223 bool _strand_required;
225 std::string _address;
228 std::shared_ptr<TCPSession> _session;
229 asio::ip::tcp::endpoint _endpoint;
230 asio::ip::tcp::acceptor _acceptor;
231 std::atomic<bool> _started;
238 bool _option_keep_alive;
239 bool _option_no_delay;
240 bool _option_reuse_address;
241 bool _option_reuse_port;
247 void RegisterSession();
252 void UnregisterSession(
const CppCommon::UUID&
id);
258 void SendError(std::error_code
ec);
Asio allocate handler wrapper.
bool option_keep_alive() const noexcept
Get the option: keep alive.
virtual bool DisconnectAll()
Disconnect all connected sessions.
virtual bool Multicast(const void *buffer, size_t size)
Multicast data to all connected sessions.
const std::string & address() const noexcept
Get the server address.
TCPServer & operator=(TCPServer &&)=delete
uint64_t bytes_pending() const noexcept
Get the number of bytes pending sent by the server.
virtual bool Start()
Start the server.
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
std::map< CppCommon::UUID, std::shared_ptr< TCPSession > > _sessions
std::shared_mutex _sessions_lock
void SetupKeepAlive(bool enable) noexcept
Setup option: keep alive.
virtual std::shared_ptr< TCPSession > CreateSession(const std::shared_ptr< TCPServer > &server)
Create TCP session factory method.
void SetupReusePort(bool enable) noexcept
Setup option: reuse port.
uint64_t bytes_sent() const noexcept
Get the number of bytes sent by the server.
std::shared_ptr< asio::io_service > & io_service() noexcept
Get the Asio IO service.
std::shared_ptr< TCPSession > FindSession(const CppCommon::UUID &id)
Find a session with a given Id.
virtual bool Stop()
Stop the server.
virtual void onDisconnected(std::shared_ptr< TCPSession > &session)
Handle session disconnected notification.
bool option_reuse_port() const noexcept
Get the option: reuse port.
asio::io_service::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
bool option_no_delay() const noexcept
Get the option: no delay.
asio::ip::tcp::acceptor & acceptor() noexcept
Get the server acceptor.
void SetupNoDelay(bool enable) noexcept
Setup option: no delay.
int port() const noexcept
Get the server port number.
uint64_t bytes_received() const noexcept
Get the number of bytes received by the server.
void SetupReuseAddress(bool enable) noexcept
Setup option: reuse address.
virtual void onConnected(std::shared_ptr< TCPSession > &session)
Handle session connected notification.
virtual bool Restart()
Restart the server.
TCPServer & operator=(const TCPServer &)=delete
asio::ip::tcp::endpoint & endpoint() noexcept
Get the server endpoint.
virtual void onStopped()
Handle server stopped notification.
uint64_t connected_sessions() const noexcept
Get the number of sessions connected to the server.
const CppCommon::UUID & id() const noexcept
Get the server Id.
virtual ~TCPServer()=default
virtual void onStarted()
Handle server started notification.
virtual bool Multicast(std::string_view text)
Multicast text to all connected sessions.
bool IsStarted() const noexcept
Is the server started?
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
bool option_reuse_address() const noexcept
Get the option: reuse address.
TCPServer(TCPServer &&)=delete
TCPServer(const TCPServer &)=delete
InternetProtocol
Internet protocol.
@ IPv4
Internet Protocol version 4.
C++ Server project definitions.