CppServer  1.0.4.0
C++ Server Library
tcp_server.h
Go to the documentation of this file.
1 
9 #ifndef CPPSERVER_ASIO_TCP_SERVER_H
10 #define CPPSERVER_ASIO_TCP_SERVER_H
11 
12 #include "tcp_session.h"
13 
14 #include "system/uuid.h"
15 
16 #include <map>
17 #include <mutex>
18 #include <shared_mutex>
19 #include <vector>
20 
21 namespace CppServer {
22 namespace Asio {
23 
25 
30 class TCPServer : public std::enable_shared_from_this<TCPServer>
31 {
32  friend class TCPSession;
33 
34 public:
36 
41  TCPServer(const std::shared_ptr<Service>& service, int port, InternetProtocol protocol = InternetProtocol::IPv4);
43 
48  TCPServer(const std::shared_ptr<Service>& service, const std::string& address, int port);
50 
54  TCPServer(const std::shared_ptr<Service>& service, const asio::ip::tcp::endpoint& endpoint);
55  TCPServer(const TCPServer&) = delete;
56  TCPServer(TCPServer&&) = delete;
57  virtual ~TCPServer() = default;
58 
59  TCPServer& operator=(const TCPServer&) = delete;
61 
63  const CppCommon::UUID& id() const noexcept { return _id; }
64 
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; }
75 
77  const std::string& address() const noexcept { return _address; }
79  int port() const noexcept { return _port; }
80 
82  uint64_t connected_sessions() const noexcept { return _sessions.size(); }
84  uint64_t bytes_pending() const noexcept { return _bytes_pending; }
86  uint64_t bytes_sent() const noexcept { return _bytes_sent; }
88  uint64_t bytes_received() const noexcept { return _bytes_received; }
89 
91  bool option_keep_alive() const noexcept { return _option_keep_alive; }
93  bool option_no_delay() const noexcept { return _option_no_delay; }
95  bool option_reuse_address() const noexcept { return _option_reuse_address; }
97  bool option_reuse_port() const noexcept { return _option_reuse_port; }
98 
100  bool IsStarted() const noexcept { return _started; }
101 
103 
106  virtual bool Start();
108 
111  virtual bool Stop();
113 
116  virtual bool Restart();
117 
119 
124  virtual bool Multicast(const void* buffer, size_t size);
126 
130  virtual bool Multicast(std::string_view text) { return Multicast(text.data(), text.size()); }
131 
133 
136  virtual bool DisconnectAll();
137 
139 
143  std::shared_ptr<TCPSession> FindSession(const CppCommon::UUID& id);
144 
146 
151  void SetupKeepAlive(bool enable) noexcept { _option_keep_alive = enable; }
153 
160  void SetupNoDelay(bool enable) noexcept { _option_no_delay = enable; }
162 
167  void SetupReuseAddress(bool enable) noexcept { _option_reuse_address = enable; }
169 
174  void SetupReusePort(bool enable) noexcept { _option_reuse_port = enable; }
175 
176 protected:
178 
182  virtual std::shared_ptr<TCPSession> CreateSession(const std::shared_ptr<TCPServer>& server) { return std::make_shared<TCPSession>(server); }
183 
184 protected:
186  virtual void onStarted() {}
188  virtual void onStopped() {}
189 
191 
194  virtual void onConnected(std::shared_ptr<TCPSession>& session) {}
196 
199  virtual void onDisconnected(std::shared_ptr<TCPSession>& session) {}
200 
202 
207  virtual void onError(int error, const std::string& category, const std::string& message) {}
208 
209 protected:
210  // Server sessions
211  std::shared_mutex _sessions_lock;
212  std::map<CppCommon::UUID, std::shared_ptr<TCPSession>> _sessions;
213 
214 private:
215  // Server Id
216  CppCommon::UUID _id;
217  // Asio service
218  std::shared_ptr<Service> _service;
219  // Asio IO service
220  std::shared_ptr<asio::io_service> _io_service;
221  // Asio service strand for serialized handler execution
222  asio::io_service::strand _strand;
223  bool _strand_required;
224  // Server address, scheme & port
225  std::string _address;
226  int _port;
227  // Server endpoint, acceptor & socket
228  std::shared_ptr<TCPSession> _session;
229  asio::ip::tcp::endpoint _endpoint;
230  asio::ip::tcp::acceptor _acceptor;
231  std::atomic<bool> _started;
232  HandlerStorage _acceptor_storage;
233  // Server statistic
234  uint64_t _bytes_pending;
235  uint64_t _bytes_sent;
236  uint64_t _bytes_received;
237  // Options
238  bool _option_keep_alive;
239  bool _option_no_delay;
240  bool _option_reuse_address;
241  bool _option_reuse_port;
242 
244  void Accept();
245 
247  void RegisterSession();
249 
252  void UnregisterSession(const CppCommon::UUID& id);
253 
255  void ClearBuffers();
256 
258  void SendError(std::error_code ec);
259 };
260 
263 } // namespace Asio
264 } // namespace CppServer
265 
266 #endif // CPPSERVER_ASIO_TCP_SERVER_H
Asio handler storage.
Definition: memory.h:27
bool option_keep_alive() const noexcept
Get the option: keep alive.
Definition: tcp_server.h:91
virtual bool DisconnectAll()
Disconnect all connected sessions.
Definition: tcp_server.cpp:259
virtual bool Multicast(const void *buffer, size_t size)
Multicast data to all connected sessions.
Definition: tcp_server.cpp:238
const CppCommon::UUID & id() const noexcept
Get the server Id.
Definition: tcp_server.h:63
uint64_t bytes_pending() const noexcept
Get the number of bytes pending sent by the server.
Definition: tcp_server.h:84
virtual bool Start()
Start the server.
Definition: tcp_server.cpp:97
TCPServer & operator=(const TCPServer &)=delete
std::map< CppCommon::UUID, std::shared_ptr< TCPSession > > _sessions
Definition: tcp_server.h:212
std::shared_mutex _sessions_lock
Definition: tcp_server.h:211
void SetupKeepAlive(bool enable) noexcept
Setup option: keep alive.
Definition: tcp_server.h:151
void SetupReusePort(bool enable) noexcept
Setup option: reuse port.
Definition: tcp_server.h:174
uint64_t bytes_sent() const noexcept
Get the number of bytes sent by the server.
Definition: tcp_server.h:86
std::shared_ptr< TCPSession > FindSession(const CppCommon::UUID &id)
Find a session with a given Id.
Definition: tcp_server.cpp:285
TCPServer & operator=(TCPServer &&)=delete
const std::string & address() const noexcept
Get the server address.
Definition: tcp_server.h:77
TCPServer(const std::shared_ptr< Service > &service, int port, InternetProtocol protocol=InternetProtocol::IPv4)
Initialize TCP server with a given Asio service and port number.
Definition: tcp_server.cpp:14
virtual bool Stop()
Stop the server.
Definition: tcp_server.cpp:147
asio::io_service::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
Definition: tcp_server.h:70
virtual void onDisconnected(std::shared_ptr< TCPSession > &session)
Handle session disconnected notification.
Definition: tcp_server.h:199
bool option_reuse_port() const noexcept
Get the option: reuse port.
Definition: tcp_server.h:97
asio::ip::tcp::endpoint & endpoint() noexcept
Get the server endpoint.
Definition: tcp_server.h:72
std::shared_ptr< asio::io_service > & io_service() noexcept
Get the Asio IO service.
Definition: tcp_server.h:68
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
Definition: tcp_server.h:66
bool option_no_delay() const noexcept
Get the option: no delay.
Definition: tcp_server.h:93
virtual std::shared_ptr< TCPSession > CreateSession(const std::shared_ptr< TCPServer > &server)
Create TCP session factory method.
Definition: tcp_server.h:182
void SetupNoDelay(bool enable) noexcept
Setup option: no delay.
Definition: tcp_server.h:160
int port() const noexcept
Get the server port number.
Definition: tcp_server.h:79
uint64_t bytes_received() const noexcept
Get the number of bytes received by the server.
Definition: tcp_server.h:88
void SetupReuseAddress(bool enable) noexcept
Setup option: reuse address.
Definition: tcp_server.h:167
virtual void onConnected(std::shared_ptr< TCPSession > &session)
Handle session connected notification.
Definition: tcp_server.h:194
virtual bool Restart()
Restart the server.
Definition: tcp_server.cpp:186
asio::ip::tcp::acceptor & acceptor() noexcept
Get the server acceptor.
Definition: tcp_server.h:74
virtual void onStopped()
Handle server stopped notification.
Definition: tcp_server.h:188
uint64_t connected_sessions() const noexcept
Get the number of sessions connected to the server.
Definition: tcp_server.h:82
virtual ~TCPServer()=default
virtual void onStarted()
Handle server started notification.
Definition: tcp_server.h:186
virtual bool Multicast(std::string_view text)
Multicast text to all connected sessions.
Definition: tcp_server.h:130
bool IsStarted() const noexcept
Is the server started?
Definition: tcp_server.h:100
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
Definition: tcp_server.h:207
bool option_reuse_address() const noexcept
Get the option: reuse address.
Definition: tcp_server.h:95
TCPServer(TCPServer &&)=delete
TCPServer(const TCPServer &)=delete
InternetProtocol
Internet protocol.
Definition: asio.h:66
@ IPv4
Internet Protocol version 4.
C++ Server project definitions.
Definition: asio.h:56
TCP session definition.