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

AsioDefines.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 ASIODEFINES
28 #define ASIODEFINES
29
30 #include <vector>
31 #include <functional>
32 #include <memory>
33 #include <utility>
34 #include <string>
35 #include <cstdint>
36 #include <boost/asio.hpp>
37 #include "CoreLibraryDllGlobal.h"
38 #include "Platform/PlatformDefines.h"
39
40 namespace boost_sys = boost::system;
41 namespace boost_asio = boost::asio;
42 namespace boost_placeholders = boost::asio::placeholders;
43 namespace boost_mcast = boost::asio::ip::multicast;
44
46 using boost_iocontext_t = boost_asio::io_context;
48 using boost_tcp_t = boost_asio::ip::tcp;
50 using boost_tcp_acceptor_t = boost::asio::ip::tcp::acceptor;
52 using boost_udp_t = boost::asio::ip::udp;
54 using boost_address_t = boost::asio::ip::address;
56 using boost_address_v4_t = boost::asio::ip::address_v4;
57
59 namespace core_lib
60 {
62 namespace asio
63 {
64
66 namespace tcp
67 {
68
70 enum eDefReservedSize : size_t
71 {
72 DEFAULT_RESERVED_SIZE = 512 * 1024
73 };
74
76 enum eDefUnsentAsyncCount : size_t
77 {
78 MAX_UNSENT_ASYNC_MSG_COUNT = 1000
79 };
80
82 enum class eSendOption
83 {
85 nagleOff,
87 nagleOn
88 };
89
90 class TcpConnection;
91 } // namespace tcp
92
94 namespace udp
95 {
96
98 enum class eUdpOption
99 {
101 broadcast,
103 unicast
104 };
105
113 enum eUdpDatagramMaxSize : size_t
114 {
115 UDP_DATAGRAM_MAX_SIZE = 65507
116 };
117
123 enum eDefaultUdpSize : size_t
124 {
125 DEFAULT_UDP_BUF_SIZE = 8192
126 };
127
129 enum class eMulticastTTL
130 {
132 sameHost = 0,
134 sameSubnet = 1,
136 sameSite = 32,
138 sameRegion = 64,
140 sameContinent = 128,
142 unrestricted = 255
143 };
144
145 } // namespace udp
146
148 namespace defs
149 {
150
152 using connection_t = std::pair<std::string, uint16_t>;
154 extern const connection_t NULL_CONNECTION;
156 using tcp_conn_ptr_t = std::shared_ptr<tcp::TcpConnection>;
158 enum eRespAddressLen : uint32_t
159 {
160 RESPONSE_ADDRESS_LEN = 16
161 };
163 enum eMagicStringLen : uint32_t
164 {
165 MAGIC_STRING_LEN = 16
166 };
168 extern const char DEFAULT_MAGIC_STRING[];
169
171 enum class eArchiveType : uint8_t
172 {
173 portableBinary,
175 binary,
177 json,
179 xml,
181 raw,
183 protobuf
184 };
185
186 // Push single byte alignment for the MessageHeader strcuture for maximum portability.
187 #pragma pack(push, 1)
188
194 struct CORE_LIBRARY_DLL_SHARED_API MessageHeader
195 {
197 char magicString[MAGIC_STRING_LEN]{};
199 char responseAddress[RESPONSE_ADDRESS_LEN]{};
201 uint16_t responsePort{0};
203 int32_t messageId{0};
205 eArchiveType archiveType{eArchiveType::portableBinary};
207 uint32_t totalLength{sizeof(*this)};
208
210 MessageHeader();
212 ~MessageHeader() = default;
214 MessageHeader(const MessageHeader&) = default;
216 MessageHeader& operator=(const MessageHeader&) = default;
217 #ifdef USE_EXPLICIT_MOVE_
218
219 MessageHeader(MessageHeader&& header);
221 MessageHeader& operator=(MessageHeader&& header);
222 #else
223
224 MessageHeader(MessageHeader&&) = default;
226 MessageHeader& operator=(MessageHeader&&) = default;
227 #endif
228 };
229 // Pop single byte alignment.
230 #pragma pack(pop)
231
233 enum eMessageHeaderLen : size_t
234 {
235 MESSAGE_HEADER_LEN = sizeof(MessageHeader)
236 };
237
239 using char_buffer_t = std::vector<char>;
240
243 template <typename Header> struct ReceivedMessage
244 {
246 using header_t = Header;
248 header_t header;
251 char_buffer_t body;
253 ReceivedMessage() = default;
255 ~ReceivedMessage() = default;
257 ReceivedMessage(const ReceivedMessage&) = default;
259 ReceivedMessage& operator=(const ReceivedMessage&) = default;
260 #ifdef USE_EXPLICIT_MOVE_
261
262 ReceivedMessage(ReceivedMessage&& message)
263 {
264 *this = std::move(message);
265 }
267 ReceivedMessage& operator=(ReceivedMessage&& message)
268 {
269 std::swap(header, message.header);
270 body.swap(message.body);
271 return *this;
272 }
273 #else
274
275 ReceivedMessage(ReceivedMessage&&) = default;
277 ReceivedMessage& operator=(ReceivedMessage&&) = default;
278 #endif
279 };
280
282 using default_received_message_t = ReceivedMessage<MessageHeader>;
284 using default_received_message_ptr_t = std::shared_ptr<default_received_message_t>;
286 using default_message_dispatcher_t = std::function<void(default_received_message_ptr_t)>;
288 using check_bytes_left_to_read_t = std::function<size_t(const char_buffer_t&)>;
290 using message_received_handler_t = std::function<void(const char_buffer_t&)>;
291
292 } // namespace defs
293 } // namespace asio
294 } // namespace core_lib
295
296 #endif // ASIODEFINES
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::defs::ReceivedMessage::body
char_buffer_t body
Message body as a char buffer as all data received form socket is fundamentally an array pf chars...
Definition: AsioDefines.h:251
core_lib::asio::udp::eUdpOption::unicast
udp unicasts.
core_lib::asio::udp::eMulticastTTL::sameHost
Multicast only to same host.
core_lib::asio::udp::eUdpOption
eUdpOption
The udp options enumeration.
Definition: AsioDefines.h:98
boost_tcp_acceptor_t
boost::asio::ip::tcp::acceptor boost_tcp_acceptor_t
Boost tcp acceptor convenience typedef.
Definition: AsioDefines.h:50
core_lib::asio::udp::eMulticastTTL::unrestricted
Multicasts are unrestricted.
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
core_lib::asio::defs::eArchiveType::portableBinary
Portable binary archive, requires Cereal serialization.
core_lib::asio::defs::eArchiveType::raw
Raw data, only for POD objects.
boost_udp_t
boost::asio::ip::udp boost_udp_t
Boost udp convenience typedef.
Definition: AsioDefines.h:52
core_lib::asio::defs::eArchiveType::xml
XML archive, requires Cereal serialization.
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::defs::eArchiveType::binary
Binary archive, requires Cereal serialization.
core_lib::asio::defs::eArchiveType::protobuf
Google protocol buffer.
core_lib::asio::udp::eMulticastTTL::sameSubnet
Multicast only to same subnet.
core_lib::asio::defs::eArchiveType::json
JSON archive, requires Cereal serialization.
core_lib::asio::defs::eMessageHeaderLen
eMessageHeaderLen
Constant defining message header magic string length in bytes.
Definition: AsioDefines.h:233
core_lib
The core_lib namespace.
Definition: AsioDefines.h:59
core_lib::asio::tcp::eSendOption
eSendOption
Enumeration to control nagle algorithm.
Definition: AsioDefines.h:82
boost_tcp_t
boost_asio::ip::tcp boost_tcp_t
Boost tcp convenience typedef.
Definition: AsioDefines.h:48
core_lib::asio::defs::default_received_message_ptr_t
std::shared_ptr< default_received_message_t > default_received_message_ptr_t
Typedef to default version of received message shared pointer.
Definition: AsioDefines.h:284
core_lib::asio::defs::ReceivedMessage::header_t
Header header_t
Typedef for header template type.
Definition: AsioDefines.h:246
core_lib::asio::tcp::eDefReservedSize
eDefReservedSize
Default internal receive buffer&#39;s initial reserved size in bytes.
Definition: AsioDefines.h:70
core_lib::asio::udp::eMulticastTTL
eMulticastTTL
The multicast TTL enumeration.
Definition: AsioDefines.h:129
core_lib::asio::defs::eRespAddressLen
eRespAddressLen
Constant defining response IP address length in bytes.
Definition: AsioDefines.h:158
core_lib::asio::udp::eMulticastTTL::sameRegion
Multicast only to same region.
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
boost_address_v4_t
boost::asio::ip::address_v4 boost_address_v4_t
Boost IPV4 address convenience typedef.
Definition: AsioDefines.h:56
core_lib::asio::defs::eMagicStringLen
eMagicStringLen
Constant defining message header magic string length in bytes.
Definition: AsioDefines.h:163
core_lib::asio::defs::MessageHeader
Default message header structure that is also POD.
Definition: AsioDefines.h:194
core_lib::asio::udp::eMulticastTTL::sameContinent
Multicast only to same continent.
boost_iocontext_t
boost_asio::io_context boost_iocontext_t
Boost IO context convenience typedef.
Definition: AsioDefines.h:46
core_lib::asio::udp::eDefaultUdpSize
eDefaultUdpSize
UDP default buffer size.
Definition: AsioDefines.h:123
core_lib::asio::defs::ReceivedMessage::header
header_t header
Message header.
Definition: AsioDefines.h:248
CoreLibraryDllGlobal.h
File containing declaration of DLL import/export control defines.
core_lib::asio::udp::eUdpDatagramMaxSize
eUdpDatagramMaxSize
UDP datagram maximum size for user data.
Definition: AsioDefines.h:113
core_lib::asio::udp::eMulticastTTL::sameSite
Multicast only to same site.
core_lib::asio::defs::eArchiveType
eArchiveType
Message serialization archive type enumeration.
Definition: AsioDefines.h:171
core_lib::asio::udp::eUdpOption::broadcast
udp broadcasts.
PlatformDefines.h
File containing platform specific definitions.
core_lib::asio::defs::DEFAULT_MAGIC_STRING
const char DEFAULT_MAGIC_STRING[]
Constant defining default magc string as "_BEGIN_MESSAGE_".
Definition: AsioDefines.cpp:38
core_lib::asio::tcp::eSendOption::nagleOff
nagleOff - Send immediately.
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
boost_address_t
boost::asio::ip::address boost_address_t
Boost general IP address convenience typedef.
Definition: AsioDefines.h:54
core_lib::asio::tcp::eDefUnsentAsyncCount
eDefUnsentAsyncCount
Maximum number of unsent async messages allowed on TCP socket IO Service queue.
Definition: AsioDefines.h:76
core_lib::asio::defs::ReceivedMessage
Template class to act as a generic wrapper around a received message for a given header type...
Definition: AsioDefines.h:243
core_lib::asio::defs::NULL_CONNECTION
const connection_t NULL_CONNECTION
Constant defining a null network connection as ("0.0.0.0", 0).
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