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

TcpConnection.h
Go to the documentation of this file.
1 // This file is part of CoreLibrary containing useful reusable utility
2 // classes.
3 //
4 // Copyright (C) 2014 to present, Duncan Crutchley
5 // Contact <dac1976github@outlook.com>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published
9 // by the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License and GNU Lesser General Public License
16 // for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // and GNU Lesser General Public License along with this program. If
20 // not, see <http://www.gnu.org/licenses/>.
21
27 #ifndef TCPCONNECTION
28 #define TCPCONNECTION
29
30 #include "AsioDefines.h"
31 #include "Threads/SyncEvent.h"
32 #include <mutex>
33
35 namespace core_lib
36 {
38 namespace asio
39 {
41 namespace tcp
42 {
43
45 class TcpConnections;
46
52 class CORE_LIBRARY_DLL_SHARED_API TcpConnection final
53 : public std::enable_shared_from_this<TcpConnection>
54 {
55 public:
57 TcpConnection() = delete;
68 TcpConnection(boost_iocontext_t& ioContext, TcpConnections& connections, size_t minAmountToRead,
69 const defs::check_bytes_left_to_read_t& checkBytesLeftToRead,
70 const defs::message_received_handler_t& messageReceivedHandler,
71 eSendOption sendOption = eSendOption::nagleOn,
72 size_t maxAllowedUnsentAsyncMessages = MAX_UNSENT_ASYNC_MSG_COUNT);
74 TcpConnection(const TcpConnection&) = delete;
76 TcpConnection& operator=(const TcpConnection&) = delete;
78 TcpConnection(TcpConnection&&) = delete;
80 TcpConnection& operator=(TcpConnection&&) = delete;
82 virtual ~TcpConnection() = default;
87 boost_tcp_t::socket& Socket();
92 const boost_tcp_t::socket& Socket() const;
97 void Connect(const defs::connection_t& endPoint);
99 void CloseConnection();
101 void StartAsyncRead();
107 bool SendMessageAsync(const defs::char_buffer_t& message);
113 bool SendMessageSync(const defs::char_buffer_t& message);
118 size_t NumberOfUnsentAsyncMessages() const;
119
120 private:
125 void SetClosing(bool closing);
130 bool IsClosing() const;
132 void ProcessCloseSocket();
134 void DestroySelf();
139 void AsyncReadFromSocket(size_t amountToRead);
146 void ReadComplete(const boost_sys::error_code& error, size_t bytesReceived,
147 size_t bytesExpected);
152 void AsyncWriteToSocket(defs::char_buffer_t const& message);
157 bool IncrementUnsentAsyncCounter();
159 void DecrementUnsentAsyncCounter();
160
161 private:
163 mutable std::mutex m_mutex;
165 threads::SyncEvent m_closedEvent{};
167 bool m_closing{false};
169 boost_iocontext_t::strand m_strand;
171 TcpConnections& m_connections;
173 size_t m_minAmountToRead{0};
175 defs::check_bytes_left_to_read_t m_checkBytesLeftToRead{};
177 defs::message_received_handler_t m_messageReceivedHandler{};
179 eSendOption m_sendOption{eSendOption::nagleOn};
181 defs::char_buffer_t m_receiveBuffer{};
183 defs::char_buffer_t m_messageBuffer{};
185 size_t m_maxAllowedUnsentAsyncMessages{MAX_UNSENT_ASYNC_MSG_COUNT};
187 size_t m_numUnsentAsyncMessages{0};
189 boost_tcp_t::socket m_socket;
190 };
191
192 } // namespace tcp
193 } // namespace asio
194 } // namespace core_lib
195
196 #endif // TCPCONNECTION
core_lib::asio::tcp::TcpConnection::m_connections
TcpConnections & m_connections
Reference to TCP connections object.
Definition: TcpConnection.h:171
core_lib::asio::tcp::TcpConnection::m_socket
boost_tcp_t::socket m_socket
TCP socket.
Definition: TcpConnection.h:189
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
AsioDefines.h
File containing useful definitions.
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::asio::tcp::TcpConnection::m_mutex
std::mutex m_mutex
Access mutex for thread safety.
Definition: TcpConnection.h:163
core_lib::threads::SyncEvent
Class defining a thread synchronisation event.
Definition: SyncEvent.h:70
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::tcp::TcpConnection::m_strand
boost_iocontext_t::strand m_strand
I/O context strand.
Definition: TcpConnection.h:169
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
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::tcp::TcpConnection
TCP connection class.
Definition: TcpConnection.h:52
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