working commit
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
|
||||
extern "C" {
|
||||
#include <arpa/inet.h>
|
||||
}
|
||||
|
||||
#include <expected>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <format>
|
||||
#include <cstdint>
|
||||
|
||||
#include <msgheader.hpp>
|
||||
|
||||
MessageHeader::MessageHeader(const uint32_t ipSize) {
|
||||
pSize = ipSize;
|
||||
}
|
||||
|
||||
MessageHeader::MessageHeader(void) {
|
||||
pSize = 0;
|
||||
}
|
||||
|
||||
std::string MessageHeader::Encode() {
|
||||
std::string buffer, tmp;
|
||||
auto magic = htonl(MAGIC);
|
||||
tmp = std::string(reinterpret_cast<const char*>(&magic), sizeof(magic));
|
||||
buffer.append(tmp);
|
||||
|
||||
auto size = htonl(pSize);
|
||||
tmp = std::string(reinterpret_cast<const char*>(&size), sizeof(size));
|
||||
buffer.append(tmp);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
std::expected<void, std::string> MessageHeader::Decode(const std::string rawHeader) {
|
||||
uint32_t tmp;
|
||||
std::memcpy(&tmp, rawHeader.data(), sizeof(uint32_t));
|
||||
auto magic = ntohl(tmp);
|
||||
if (magic != MAGIC) {
|
||||
return std::unexpected("Wrong magic code");
|
||||
}
|
||||
std::memcpy(&tmp, rawHeader.data() + sizeof(uint32_t), sizeof(uint32_t));
|
||||
pSize = ntohl(tmp);
|
||||
return {};
|
||||
}
|
||||
|
||||
uint32_t MessageHeader::PacketSize() {
|
||||
return pSize;
|
||||
}
|
||||
Reference in New Issue
Block a user