working commit
This commit is contained in:
+20
-20
@@ -13,37 +13,37 @@ extern "C" {
|
||||
#include <header.hpp>
|
||||
|
||||
Header::Header(const uint32_t ipSize) {
|
||||
pSize = ipSize;
|
||||
pSize = ipSize;
|
||||
}
|
||||
|
||||
Header::Header(void) {
|
||||
pSize = 0;
|
||||
pSize = 0;
|
||||
}
|
||||
|
||||
std::string Header::Encode() {
|
||||
std::string buffer, tmp;
|
||||
auto magic = htonl(MAGIC);
|
||||
tmp = std::string(reinterpret_cast<const char*>(&magic), sizeof(magic));
|
||||
buffer.append(tmp);
|
||||
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;
|
||||
auto size = htonl(pSize);
|
||||
tmp = std::string(reinterpret_cast<const char*>(&size), sizeof(size));
|
||||
buffer.append(tmp);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
std::expected<void, std::string> Header::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 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 Header::PacketSize() {
|
||||
return pSize;
|
||||
return pSize;
|
||||
}
|
||||
|
||||
@@ -5,12 +5,12 @@ const uint32_t MAGIC = 0xABBA;
|
||||
|
||||
class Header {
|
||||
private:
|
||||
uint32_t pSize = 0;
|
||||
uint32_t pSize = 0;
|
||||
public:
|
||||
Header(const uint32_t pSize);
|
||||
Header(void);
|
||||
std::string Encode(void);
|
||||
std::expected<void, std::string> Decode(const std::string buffer);
|
||||
uint32_t PacketSize(void);
|
||||
Header(const uint32_t pSize);
|
||||
Header(void);
|
||||
std::string Encode(void);
|
||||
std::expected<void, std::string> Decode(const std::string buffer);
|
||||
uint32_t PacketSize(void);
|
||||
};
|
||||
|
||||
|
||||
@@ -8,13 +8,13 @@
|
||||
#include <header.hpp>
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
uint32_t size =
|
||||
Header primary(1021);
|
||||
auto rawHeader = primary.Encode();
|
||||
uint32_t size =
|
||||
Header primary(1021);
|
||||
auto rawHeader = primary.Encode();
|
||||
|
||||
Header second;
|
||||
second.Decode(rawHeader);
|
||||
Header second;
|
||||
second.Decode(rawHeader);
|
||||
|
||||
std::cout << std::format("{}\n", second.PacketSize());
|
||||
assert
|
||||
std::cout << std::format("{}\n", second.PacketSize());
|
||||
assert
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
|
||||
|
||||
|
||||
class Packet {
|
||||
std::string meta;
|
||||
std::string payload;
|
||||
}
|
||||
Reference in New Issue
Block a user