Core Library 1.7.0.0 Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.

TcpServer.h
Go to the documentation of this file.
1
2 // This file is part of CoreLibrary containing useful reusable utility
3 // classes.
4 //
5 // Copyright (C) 2014 to present, Duncan Crutchley
6 // Contact <dac1976github@outlook.com>
7 //
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Lesser General Public License as published
10 // by the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License and GNU Lesser General Public License
17 // for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // and GNU Lesser General Public License along with this program. If
21 // not, see <http://www.gnu.org/licenses/>.
22
28 #ifndef TCPSERVER
29 #define TCPSERVER
30
31 #include "IoContextThreadGroup.h"
32 #include "TcpConnections.h"
33 #include "Threads/SyncEvent.h"
34
36 namespace core_lib
37 {
39 namespace asio
40 {
42 namespace tcp
43 {
44
53 class CORE_LIBRARY_DLL_SHARED_API TcpServer final
54 {
55 public:
57 TcpServer() = delete;
76 TcpServer(boost_iocontext_t& ioContext, uint16_t listenPort, size_t minAmountToRead,
77 const defs::check_bytes_left_to_read_t& checkBytesLeftToRead,
78 const defs::message_received_handler_t& messageReceivedHandler,
79 eSendOption sendOption = eSendOption::nagleOn,
80 size_t maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT);
97 TcpServer(uint16_t listenPort, size_t minAmountToRead,
98 const defs::check_bytes_left_to_read_t& checkBytesLeftToRead,
99 const defs::message_received_handler_t& messageReceivedHandler,
100 eSendOption sendOption = eSendOption::nagleOn,
101 size_t maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT);
103 ~TcpServer();
105 TcpServer(const TcpServer&) = delete;
107 TcpServer& operator=(const TcpServer&) = delete;
109 TcpServer(TcpServer&&) = delete;
111 TcpServer& operator=(TcpServer&&) = delete;
121 defs::connection_t GetServerDetailsForClient(const defs::connection_t& client) const;
126 uint16_t ListenPort() const;
131 size_t NumberOfClients() const;
136 void CloseAcceptor();
138 void OpenAcceptor();
145 bool SendMessageToClientAsync(const defs::connection_t& client,
146 const defs::char_buffer_t& message) const;
153 bool SendMessageToClientSync(const defs::connection_t& client,
154 const defs::char_buffer_t& message) const;
160 bool SendMessageToAllClients(const defs::char_buffer_t& message) const;
166 size_t NumberOfUnsentAsyncMessages(const defs::connection_t& client) const;
167
173 bool IsConnected(const defs::connection_t& client) const;
174
175 private:
177 void AcceptConnection();
183 void AcceptHandler(defs::tcp_conn_ptr_t connection, const boost_sys::error_code& error);
185 void ProcessCloseAcceptor();
186
187 private:
189 std::unique_ptr<IoContextThreadGroup> m_ioThreadGroup{};
191 boost_iocontext_t& m_ioContext;
193 boost_iocontext_t::strand m_strand;
195 std::unique_ptr<boost_tcp_acceptor_t> m_acceptor{};
197 uint16_t m_listenPort{0};
199 size_t m_minAmountToRead{0};
201 defs::check_bytes_left_to_read_t m_checkBytesLeftToRead{};
203 defs::message_received_handler_t m_messageReceivedHandler{};
205 eSendOption m_sendOption{eSendOption::nagleOn};
207 size_t m_maxAllowedUnsentAsyncMessages{MAX_UNSENT_ASYNC_MSG_COUNT};
209 TcpConnections m_clientConnections{};
211 threads::SyncEvent m_closedEvent{};
212 };
213
214 } // namespace tcp
215 } // namespace asio
216 } // namespace core_lib
217
218 #endif // TCPSERVER
core_lib::asio::tcp::TcpServer::m_ioContext
boost_iocontext_t & m_ioContext
I/O context reference.
Definition: TcpServer.h:191
core_lib::asio::defs::check_bytes_left_to_read_t
std::function< size_t(const char_buffer_t &)> check_bytes_left_to_read_t
Typedef to bytes left to reading checking utility function object.
Definition: AsioDefines.h:288
IoContextThreadGroup.h
File containing declarations relating the IoContextThreadGroup class.
core_lib::asio::defs::message_received_handler_t
std::function< void(const char_buffer_t &)> message_received_handler_t
Typedef to message received handler function object.
Definition: AsioDefines.h:290
core_lib::threads::SyncEvent
Class defining a thread synchronisation event.
Definition: SyncEvent.h:70
TcpConnections.h
File containing TCP connections class declaration.
core_lib
The core_lib namespace.
Definition: AsioDefines.h:59
core_lib::asio::tcp::TcpConnections
TCP connections class to manage the TcpConnection objects.
Definition: TcpConnections.h:52
core_lib::asio::tcp::eSendOption
eSendOption
Enumeration to control nagle algorithm.
Definition: AsioDefines.h:82
core_lib::asio::tcp::eSendOption::nagleOn
nagleOn - Send when possible.
core_lib::asio::defs::connection_t
std::pair< std::string, uint16_t > connection_t
Typedef describing a network connection as (address, port).
Definition: AsioDefines.h:152
core_lib::asio::tcp::TcpServer
A bi-directional TCP server.
Definition: TcpServer.h:53
boost_iocontext_t
boost_asio::io_context boost_iocontext_t
Boost IO context convenience typedef.
Definition: AsioDefines.h:46
SyncEvent.h
File containing declaration of SyncEvent class.
core_lib::asio::defs::char_buffer_t
std::vector< char > char_buffer_t
Typedef to generic char buffer based on s std::vector<char>.
Definition: AsioDefines.h:239
core_lib::asio::tcp::TcpServer::m_strand
boost_iocontext_t::strand m_strand
I/O context strand.
Definition: TcpServer.h:193
core_lib::asio::defs::tcp_conn_ptr_t
std::shared_ptr< tcp::TcpConnection > tcp_conn_ptr_t
Typedef describing shared_ptr to a TcpConnection object.
Definition: AsioDefines.h:156