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

SimpleTcpClient.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 SIMPLETCPCLIENT
29 #define SIMPLETCPCLIENT
30
31 #include "TcpTypedClient.h"
32
34 namespace core_lib
35 {
37 namespace asio
38 {
40 namespace tcp
41 {
42
45 class CORE_LIBRARY_DLL_SHARED_API SimpleTcpClient final
46 {
47 public:
49 SimpleTcpClient() = delete;
71 SimpleTcpClient(boost_iocontext_t& ioContext, const defs::connection_t& server,
72 const defs::default_message_dispatcher_t& messageDispatcher,
73 eSendOption sendOption = eSendOption::nagleOn,
74 size_t maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT,
75 size_t memPoolMsgCount = 0);
96 SimpleTcpClient(const defs::connection_t& server,
97 const defs::default_message_dispatcher_t& messageDispatcher,
98 eSendOption sendOption = eSendOption::nagleOn,
99 size_t maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT,
100 size_t memPoolMsgCount = 0);
102 ~SimpleTcpClient() = default;
104 SimpleTcpClient(const SimpleTcpClient&) = delete;
106 SimpleTcpClient& operator=(const SimpleTcpClient&) = delete;
108 SimpleTcpClient(SimpleTcpClient&&) = delete;
110 SimpleTcpClient& operator=(SimpleTcpClient&&) = delete;
115 defs::connection_t ServerConnection() const;
120 bool Connected() const;
125 defs::connection_t GetClientDetailsForServer();
130 void CloseConnection();
138 bool
139 SendMessageToServerAsync(int32_t messageId,
140 const defs::connection_t& responseAddress = defs::NULL_CONNECTION);
148 bool SendMessageToServerSync(int32_t messageId,
149 const defs::connection_t& responseAddress = defs::NULL_CONNECTION);
158 bool
159 SendMessageToServerAsync(const defs::char_buffer_t& message, int32_t messageId,
160 const defs::connection_t& responseAddress = defs::NULL_CONNECTION);
169 bool SendMessageToServerSync(const defs::char_buffer_t& message, int32_t messageId,
170 const defs::connection_t& responseAddress = defs::NULL_CONNECTION);
180 template <typename T, typename A = serialize::archives::out_port_bin_t>
181 bool SendMessageToServerAsync(const T& message, int32_t messageId,
182 const defs::connection_t& responseAddress = defs::NULL_CONNECTION)
183 {
184 return m_tcpTypedClient.SendMessageToServerAsync<T, A>(message, messageId, responseAddress);
185 }
195 template <typename T, typename A = serialize::archives::out_port_bin_t>
196 bool SendMessageToServerSync(const T& message, int32_t messageId,
197 const defs::connection_t& responseAddress = defs::NULL_CONNECTION)
198 {
199 return m_tcpTypedClient.SendMessageToServerSync<T, A>(message, messageId, responseAddress);
200 }
206 bool SendMessageToServerAsync(const defs::char_buffer_t& message);
212 bool SendMessageToServerSync(const defs::char_buffer_t& message);
217 size_t NumberOfUnsentAsyncMessages() const;
218
219 private:
221 messages::MessageBuilder m_messageBuilder{};
223 messages::MessageHandler m_messageHandler{};
225 TcpTypedClient<messages::MessageBuilder> m_tcpTypedClient;
226 };
227
228 } // namespace tcp
229 } // namespace asio
230 } // namespace core_lib
231
232 #endif // SIMPLETCPCLIENT
core_lib::asio::defs::default_message_dispatcher_t
std::function< void(default_received_message_ptr_t)> default_message_dispatcher_t
Typedef to default message dispatcher function object.
Definition: AsioDefines.h:286
core_lib::asio::messages::MessageHandler
Default message handler class.
Definition: MessageUtils.h:55
core_lib::asio::tcp::SimpleTcpClient::SendMessageToServerAsync
bool SendMessageToServerAsync(const T &message, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION)
Send a full message to the server asynchronously.
Definition: SimpleTcpClient.h:181
core_lib
The core_lib namespace.
Definition: AsioDefines.h:59
core_lib::asio::tcp::TcpTypedClient
A generic bi-directional TCP client.
Definition: TcpTypedClient.h:57
core_lib::asio::tcp::eSendOption
eSendOption
Enumeration to control nagle algorithm.
Definition: AsioDefines.h:82
core_lib::asio::tcp::SimpleTcpClient::m_tcpTypedClient
TcpTypedClient< messages::MessageBuilder > m_tcpTypedClient
Our actual typed TCP client object.
Definition: SimpleTcpClient.h:225
core_lib::asio::tcp::SimpleTcpClient
A simple bi-directional TCP client, which uses the class MessageHeader as the message header type...
Definition: SimpleTcpClient.h:45
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
TcpTypedClient.h
File containing TCP typed client class declaration.
core_lib::asio::tcp::SimpleTcpClient::SendMessageToServerSync
bool SendMessageToServerSync(const T &message, int32_t messageId, const defs::connection_t &responseAddress=defs::NULL_CONNECTION)
Send a full message to the server synchronously.
Definition: SimpleTcpClient.h:196
boost_iocontext_t
boost_asio::io_context boost_iocontext_t
Boost IO context convenience typedef.
Definition: AsioDefines.h:46
core_lib::asio::messages::MessageBuilder
Default message builder class.
Definition: MessageUtils.h:268
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::defs::NULL_CONNECTION
const connection_t NULL_CONNECTION
Constant defining a null network connection as ("0.0.0.0", 0).