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"
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 {
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;
253  ReceivedMessage() = default;
255  ~ReceivedMessage() = default;
257  ReceivedMessage(const ReceivedMessage&) = default;
259  ReceivedMessage& operator=(const ReceivedMessage&) = default;
260 #ifdef USE_EXPLICIT_MOVE_
261 
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 
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
std::function< void(default_received_message_ptr_t)> default_message_dispatcher_t
Typedef to default message dispatcher function object.
Definition: AsioDefines.h:286
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
eUdpOption
The udp options enumeration.
Definition: AsioDefines.h:98
boost::asio::ip::tcp::acceptor boost_tcp_acceptor_t
Boost tcp acceptor convenience typedef.
Definition: AsioDefines.h:50
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
Portable binary archive, requires Cereal serialization.
Raw data, only for POD objects.
boost::asio::ip::udp boost_udp_t
Boost udp convenience typedef.
Definition: AsioDefines.h:52
XML archive, requires Cereal serialization.
std::function< void(const char_buffer_t &)> message_received_handler_t
Typedef to message received handler function object.
Definition: AsioDefines.h:290
Binary archive, requires Cereal serialization.
Multicast only to same subnet.
JSON archive, requires Cereal serialization.
eMessageHeaderLen
Constant defining message header magic string length in bytes.
Definition: AsioDefines.h:233
The core_lib namespace.
Definition: AsioDefines.h:59
eSendOption
Enumeration to control nagle algorithm.
Definition: AsioDefines.h:82
boost_asio::ip::tcp boost_tcp_t
Boost tcp convenience typedef.
Definition: AsioDefines.h:48
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
Header header_t
Typedef for header template type.
Definition: AsioDefines.h:246
eDefReservedSize
Default internal receive buffer&#39;s initial reserved size in bytes.
Definition: AsioDefines.h:70
eMulticastTTL
The multicast TTL enumeration.
Definition: AsioDefines.h:129
eRespAddressLen
Constant defining response IP address length in bytes.
Definition: AsioDefines.h:158
Multicast only to same region.
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::ip::address_v4 boost_address_v4_t
Boost IPV4 address convenience typedef.
Definition: AsioDefines.h:56
eMagicStringLen
Constant defining message header magic string length in bytes.
Definition: AsioDefines.h:163
Default message header structure that is also POD.
Definition: AsioDefines.h:194
Multicast only to same continent.
boost_asio::io_context boost_iocontext_t
Boost IO context convenience typedef.
Definition: AsioDefines.h:46
eDefaultUdpSize
UDP default buffer size.
Definition: AsioDefines.h:123
header_t header
Message header.
Definition: AsioDefines.h:248
File containing declaration of DLL import/export control defines.
eUdpDatagramMaxSize
UDP datagram maximum size for user data.
Definition: AsioDefines.h:113
eArchiveType
Message serialization archive type enumeration.
Definition: AsioDefines.h:171
File containing platform specific definitions.
const char DEFAULT_MAGIC_STRING[]
Constant defining default magc string as "_BEGIN_MESSAGE_".
Definition: AsioDefines.cpp:38
nagleOff - Send immediately.
TCP connection class.
Definition: TcpConnection.h:52
std::vector< char > char_buffer_t
Typedef to generic char buffer based on s std::vector<char>.
Definition: AsioDefines.h:239
boost::asio::ip::address boost_address_t
Boost general IP address convenience typedef.
Definition: AsioDefines.h:54
eDefUnsentAsyncCount
Maximum number of unsent async messages allowed on TCP socket IO Service queue.
Definition: AsioDefines.h:76
Template class to act as a generic wrapper around a received message for a given header type...
Definition: AsioDefines.h:243
const connection_t NULL_CONNECTION
Constant defining a null network connection as ("0.0.0.0", 0).
std::shared_ptr< tcp::TcpConnection > tcp_conn_ptr_t
Typedef describing shared_ptr to a TcpConnection object.
Definition: AsioDefines.h:156