Core Library  1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
TcpClient.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 TCPCLIENT
29 #define TCPCLIENT
30 
31 #include "IoContextThreadGroup.h"
32 #include "TcpConnections.h"
33 
35 namespace core_lib
36 {
38 namespace asio
39 {
41 namespace tcp
42 {
43 
52 class CORE_LIBRARY_DLL_SHARED_API TcpClient final
53 {
54 public:
56  TcpClient() = delete;
75  TcpClient(boost_iocontext_t& ioContext, const defs::connection_t& server,
76  size_t minAmountToRead, const defs::check_bytes_left_to_read_t& checkBytesLeftToRead,
77  const defs::message_received_handler_t& messageReceivedHandler,
78  eSendOption sendOption = eSendOption::nagleOn,
79  size_t maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT);
97  TcpClient(const defs::connection_t& server, 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  ~TcpClient();
105  TcpClient(const TcpClient&) = delete;
107  TcpClient& operator=(const TcpClient&) = delete;
109  TcpClient(TcpClient&&) = delete;
111  TcpClient& operator=(TcpClient&&) = delete;
116  defs::connection_t ServerConnection() const;
121  bool Connected() const;
128  defs::connection_t GetClientDetailsForServer() const;
133  bool CheckAndCreateConnection();
138  void CloseConnection();
144  bool SendMessageToServerAsync(const defs::char_buffer_t& message);
150  bool SendMessageToServerSync(const defs::char_buffer_t& message);
155  size_t NumberOfUnsentAsyncMessages() const;
156 
157 private:
159  void CreateConnection();
160 
161 private:
163  std::unique_ptr<IoContextThreadGroup> m_ioThreadGroup{};
167  defs::connection_t m_server{};
169  size_t m_minAmountToRead{0};
171  defs::check_bytes_left_to_read_t m_checkBytesLeftToRead{};
173  defs::message_received_handler_t m_messageReceivedHandler{};
177  size_t m_maxAllowedUnsentAsyncMessages{MAX_UNSENT_ASYNC_MSG_COUNT};
179  TcpConnections m_serverConnection{};
180 };
181 
182 } // namespace tcp
183 } // namespace asio
184 } // namespace core_lib
185 
186 #endif // TCPCLIENT
A bi-directional TCP client.
Definition: TcpClient.h:52
boost_iocontext_t & m_ioContext
I/O context reference.
Definition: TcpClient.h:165
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
File containing declarations relating the IoContextThreadGroup class.
std::function< void(const char_buffer_t &)> message_received_handler_t
Typedef to message received handler function object.
Definition: AsioDefines.h:290
File containing TCP connections class declaration.
The core_lib namespace.
Definition: AsioDefines.h:59
TCP connections class to manage the TcpConnection objects.
Definition: TcpConnections.h:52
eSendOption
Enumeration to control nagle algorithm.
Definition: AsioDefines.h:82
nagleOn - Send when possible.
std::pair< std::string, uint16_t > connection_t
Typedef describing a network connection as (address, port).
Definition: AsioDefines.h:152
boost_asio::io_context boost_iocontext_t
Boost IO context convenience typedef.
Definition: AsioDefines.h:46
std::vector< char > char_buffer_t
Typedef to generic char buffer based on s std::vector<char>.
Definition: AsioDefines.h:239