ASIO Client Server Framework
Loading...
Searching...
No Matches
protocol.h
1// include/protocol.h
2#pragma once
3#include <array>
4#include <cstdint>
5#include <string>
6#include <bit>
7#include <vector>
8#include <cstring>
9#include <string_view>
10#include <optional>
11#include <mutex>
12
13namespace acsf {
14
16inline constexpr std::size_t HeaderSize = 4;
18inline constexpr std::uint32_t MaxMessageSize = 1024 * 1024; // 1 MB
19
20// Logger
22inline std::uintmax_t MAX_LOG_FILE_SIZE = 10 * 1024 * 1024;
23inline constexpr std::uintmax_t LOG_BUFFER_SIZE = 1024;
24
25// Timeouts
26inline std::mutex pLock;
27// timeout on any read timeout on read after header timeout on any send timeout on client sending data
37inline std::chrono::milliseconds READ_TIMEOUT_MS{15000}, READ_BODY_TIMEOUT_MS{60000}, SEND_TIMEOUT_MS{60000}, SERVER_HEARTBEAT_TIMEOUT_MS{45000}, HEARTBEAT_RATE{15000};
38
39
41struct Message {
43 uint64_t isFrom = 0;
45 bool isHeartBeat = false;
47 std::string payload;
48};
49
63
64// Logger
69inline void setMaxLogFileSize(std::uintmax_t newSize){
70 MAX_LOG_FILE_SIZE = newSize;
71}
72
73// Timeouts
80inline void setTimeout(std::chrono::milliseconds &targetTimeout, std::chrono::milliseconds timeout){
81 {
82 std::scoped_lock<std::mutex> lock(pLock);
83 targetTimeout = timeout;
84 }
85}
86
87// CPU = little endian : Netowrk = Big endian
89inline std::uint32_t networkToHostLong(uint32_t bytes){
90 // Big endian -> system endian
91 if constexpr (std::endian::native == std::endian::little) {
92 return std::byteswap(bytes);
93 }
94 return bytes;
95}
96
98inline std::uint32_t hostToNetworkLong(uint32_t bytes){
99 // system endian -> big endian
100 if constexpr (std::endian::native == std::endian::little) {
101 return std::byteswap(bytes);
102 }
103 return bytes;
104}
105
106// Builds [4-byte big-endian length][body].
114inline std::optional<std::vector<char>> getMessageFrame(std::string_view body, bool isHeartBeat){
115 if (body.size() > MaxMessageSize || (body.size() <= 0 && !isHeartBeat)) {
116 // No logger here: protocol functions are free functions with no Logger. The caller (Connection) logs the nullopt.
117 return std::nullopt;
118 }
119
120 std::vector<char> messageFrame;
121
122 uint32_t bodySize = body.size();
123 uint32_t header = bodySize | (isHeartBeat ? (1u << 31) : 0u);
124 uint32_t netPacket = hostToNetworkLong(header);
125
126 messageFrame.resize(sizeof(netPacket) + bodySize);
127
128 std::memcpy(messageFrame.data(), &netPacket, sizeof(netPacket));
129 std::memcpy(messageFrame.data() + sizeof(netPacket), body.data(), bodySize);
130
131 return messageFrame;
132}
133
134// Reads the 4 header bytes and returns the payload length and heart beat bool.
140inline std::optional<std::pair<std::uint32_t, bool>> parseHeader(const std::array<char, HeaderSize>& header){
141 std::uint32_t data = 0;
142 std::uint32_t length = 0;
143 bool isHeartbeat;
144
145 std::memcpy(&data, header.data(), HeaderSize);
146 data = networkToHostLong(data);
147
148 length = (data << 1) >> 1;
149 isHeartbeat = data & (1u << 31);
150
151 if (length > MaxMessageSize || (length <= 0 && !isHeartbeat)) {
152 // No logger here: protocol functions are free functions with no Logger. The caller (Connection) logs the nullopt.
153 return std::nullopt;
154 }
155
156 return std::make_pair(length, isHeartbeat);
157}
158
159}
Everything in the framework lives in this namespace.
TimeoutType
Selects a timeout for Server::setTimeout().
Definition protocol.h:51
@ HeartbeatTimeout
Definition protocol.h:59
@ ReadBody
Definition protocol.h:55
@ Read
Definition protocol.h:53
@ HeartbeatRate
Definition protocol.h:61
@ Send
Definition protocol.h:57
std::uint32_t networkToHostLong(uint32_t bytes)
Converts a 32 bit value from network (big endian) to host byte order.
Definition protocol.h:89
std::uint32_t hostToNetworkLong(uint32_t bytes)
Converts a 32 bit value from host to network (big endian) byte order.
Definition protocol.h:98
std::uintmax_t MAX_LOG_FILE_SIZE
Default size in bytes at which a log file is rotated. Change with setMaxLogFileSize().
Definition protocol.h:22
void setMaxLogFileSize(std::uintmax_t newSize)
Sets the size at which log files are rotated.
Definition protocol.h:69
constexpr std::uint32_t MaxMessageSize
Largest allowed message body in bytes (1 MB).
Definition protocol.h:18
std::chrono::milliseconds READ_TIMEOUT_MS
Global timeouts in milliseconds, shared by every server and client. Change them with setTimeout().
Definition protocol.h:37
constexpr std::size_t HeaderSize
Size in bytes of the length header in front of every message.
Definition protocol.h:16
std::optional< std::vector< char > > getMessageFrame(std::string_view body, bool isHeartBeat)
Builds the bytes put on the wire: a 4 byte big endian header followed by the body....
Definition protocol.h:114
void setTimeout(std::chrono::milliseconds &targetTimeout, std::chrono::milliseconds timeout)
Changes one of the global timeouts.
Definition protocol.h:80
std::optional< std::pair< std::uint32_t, bool > > parseHeader(const std::array< char, HeaderSize > &header)
Reads a 4 byte header.
Definition protocol.h:140
A message received from the other side.
Definition protocol.h:41
uint64_t isFrom
Definition protocol.h:43
bool isHeartBeat
Definition protocol.h:45
std::string payload
Definition protocol.h:47