23inline constexpr std::uintmax_t LOG_BUFFER_SIZE = 1024;
26inline std::mutex pLock;
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};
80inline void setTimeout(std::chrono::milliseconds &targetTimeout, std::chrono::milliseconds timeout){
82 std::scoped_lock<std::mutex> lock(pLock);
83 targetTimeout = timeout;
91 if constexpr (std::endian::native == std::endian::little) {
92 return std::byteswap(bytes);
100 if constexpr (std::endian::native == std::endian::little) {
101 return std::byteswap(bytes);
114inline std::optional<std::vector<char>>
getMessageFrame(std::string_view body,
bool isHeartBeat){
115 if (body.size() >
MaxMessageSize || (body.size() <= 0 && !isHeartBeat)) {
120 std::vector<char> messageFrame;
122 uint32_t bodySize = body.size();
123 uint32_t header = bodySize | (isHeartBeat ? (1u << 31) : 0u);
126 messageFrame.resize(
sizeof(netPacket) + bodySize);
128 std::memcpy(messageFrame.data(), &netPacket,
sizeof(netPacket));
129 std::memcpy(messageFrame.data() +
sizeof(netPacket), body.data(), bodySize);
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;
145 std::memcpy(&data, header.data(),
HeaderSize);
148 length = (data << 1) >> 1;
149 isHeartbeat = data & (1u << 31);
156 return std::make_pair(length, isHeartbeat);
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