9 #ifndef CPPSERVER_ASIO_TCP_SERVER_H
10 #define CPPSERVER_ASIO_TCP_SERVER_H
14 #include "system/uuid.h"
18 #include <shared_mutex>
30 class TCPServer :
public std::enable_shared_from_this<TCPServer>
63 const CppCommon::UUID&
id() const noexcept {
return _id; }
66 std::shared_ptr<Service>&
service() noexcept {
return _service; }
68 std::shared_ptr<asio::io_service>&
io_service() noexcept {
return _io_service; }
70 asio::io_service::strand&
strand() noexcept {
return _strand; }
72 asio::ip::tcp::endpoint&
endpoint() noexcept {
return _endpoint; }
74 asio::ip::tcp::acceptor&
acceptor() noexcept {
return _acceptor; }
77 const std::string&
address() const noexcept {
return _address; }
79 int port() const noexcept {
return _port; }
86 uint64_t
bytes_sent() const noexcept {
return _bytes_sent; }
106 virtual bool Start();
124 virtual bool Multicast(
const void* buffer,
size_t size);
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); }
207 virtual void onError(
int error,
const std::string& category,
const std::string& message) {}
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;
234 uint64_t _bytes_pending;
235 uint64_t _bytes_sent;
236 uint64_t _bytes_received;
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);
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 CppCommon::UUID & id() const noexcept
Get the server Id.
uint64_t bytes_pending() const noexcept
Get the number of bytes pending sent by the server.
virtual bool Start()
Start the server.
TCPServer & operator=(const TCPServer &)=delete
std::map< CppCommon::UUID, std::shared_ptr< TCPSession > > _sessions
std::shared_mutex _sessions_lock
void SetupKeepAlive(bool enable) noexcept
Setup option: keep alive.
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< TCPSession > FindSession(const CppCommon::UUID &id)
Find a session with a given Id.
TCPServer & operator=(TCPServer &&)=delete
const std::string & address() const noexcept
Get the server address.
TCPServer(const std::shared_ptr< Service > &service, int port, InternetProtocol protocol=InternetProtocol::IPv4)
Initialize TCP server with a given Asio service and port number.
virtual bool Stop()
Stop the server.
asio::io_service::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
virtual void onDisconnected(std::shared_ptr< TCPSession > &session)
Handle session disconnected notification.
bool option_reuse_port() const noexcept
Get the option: reuse port.
asio::ip::tcp::endpoint & endpoint() noexcept
Get the server endpoint.
std::shared_ptr< asio::io_service > & io_service() noexcept
Get the Asio IO service.
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
bool option_no_delay() const noexcept
Get the option: no delay.
virtual std::shared_ptr< TCPSession > CreateSession(const std::shared_ptr< TCPServer > &server)
Create TCP session factory method.
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.
asio::ip::tcp::acceptor & acceptor() noexcept
Get the server acceptor.
virtual void onStopped()
Handle server stopped notification.
uint64_t connected_sessions() const noexcept
Get the number of sessions connected to the server.
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.