Skip to content

Commit

Permalink
raft version use macro
Browse files Browse the repository at this point in the history
  • Loading branch information
BestUO committed Jun 6, 2024
1 parent e76ccc3 commit d815a46
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
4 changes: 2 additions & 2 deletions tools/raft/Raft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ std::string Raft::handleIncomingData(const char* buf,
}
else
{
if (strcmp(buf + 8, RaftCommandType::CommonInfo::version))
if (strcmp(buf + 8, RAFT_VERSION))
{
logOut((void*)this,
": handleIncomingData: serializename ",
std::string(buf + 8, 15),
" != ",
RaftCommandType::CommonInfo::version);
RAFT_VERSION);
return "";
}
else
Expand Down
7 changes: 2 additions & 5 deletions tools/raft/RaftStructs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "../template.hpp"
#include "../simple_serialize.hpp"

constexpr char RaftCommandType::CommonInfo::version[15];
RaftCommandType::CommonInfo::CommonInfo(MessageType m)
: messageType(m)
{ }
Expand All @@ -17,8 +16,7 @@ std::string RaftCommandType::CommonInfo::serialize()
{
std::string context;
writeBuffer(messageType, sizeof(messageType), context);
std::string ver(version, sizeof(version));
writeBuffer(ver, ver.size(), context);
writeBuffer(version, sizeof(version), context);
writeBuffer(term, sizeof(term), context);
writeBuffer(uuid, sizeof(uuid), context);
writeBuffer(timestamp, sizeof(timestamp), context);
Expand All @@ -31,8 +29,7 @@ void RaftCommandType::CommonInfo::deserialize(const char* buf, uint16_t size)
(void)size;
uint16_t offset = 0;
readBuffer(buf, offset, messageType);
std::string ver;
readBuffer(buf, offset, ver);
readBuffer(buf, offset, version);
readBuffer(buf, offset, term);
readBuffer(buf, offset, uuid);
readBuffer(buf, offset, timestamp);
Expand Down
4 changes: 3 additions & 1 deletion tools/raft/RaftStructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <string>
#include "../uuid.hpp"

#define RAFT_VERSION "RAFT-V2.0\0"

struct RaftInfos
{
struct RaftBaseInfo
Expand Down Expand Up @@ -51,7 +53,7 @@ struct RaftCommandType
MessageType messageType = MessageType::VOTE;
uint32_t term;
UUID uuid;
static constexpr char version[15] = "ACCM-Raft\0";
char version[15] = RAFT_VERSION;
uint64_t timestamp
= std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now().time_since_epoch())
Expand Down
20 changes: 16 additions & 4 deletions tools/simple_serialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ inline void writeBuffer(T& data, uint16_t len, std::string& buf)
buf.append((char*)&lenBigEndian, sizeof(uint16_t)).append(s);
}

template <>
inline void readBuffer(const char* buf, uint16_t& offset, UUID& data)
{
uint16_t len = 0;
Expand All @@ -80,7 +79,6 @@ inline void readBuffer(const char* buf, uint16_t& offset, UUID& data)
EndianSwap<>::swap(tmpData.hi64()), EndianSwap<>::swap(tmpData.lo64())};
}

template <>
inline void writeBuffer(const UUID& data, uint16_t len, std::string& buf)
{
auto lenBigEndian = EndianSwap<>::swap(len);
Expand All @@ -90,7 +88,6 @@ inline void writeBuffer(const UUID& data, uint16_t len, std::string& buf)
.append((char*)&tmpData, len);
}

template <>
inline void readBuffer(const char* buf, uint16_t& offset, std::string& data)
{
uint16_t len = 0;
Expand All @@ -101,13 +98,28 @@ inline void readBuffer(const char* buf, uint16_t& offset, std::string& data)
offset += lenBigEndian;
}

template <>
inline void writeBuffer(const std::string& data, uint16_t len, std::string& buf)
{
auto lenBigEndian = EndianSwap<>::swap(len);
buf.append((char*)&lenBigEndian, sizeof(uint16_t)).append(data);
}

inline void readBuffer(const char* buf, uint16_t& offset, char* data)
{
uint16_t len = 0;
memcpy(&len, buf + offset, sizeof(uint16_t));
auto lenBigEndian = EndianSwap<>::swap(len);
offset += sizeof(uint16_t);
memcpy(data, buf + offset, lenBigEndian);
offset += lenBigEndian;
}

inline void writeBuffer(char* data, uint16_t len, std::string& buf)
{
auto lenBigEndian = EndianSwap<>::swap(len);
buf.append((char*)&lenBigEndian, sizeof(uint16_t)).append(data, len);
}

template <typename T, std::enable_if_t<has_serialize<T>::value, int> = 0>
inline void readBuffer(const char* buf, uint16_t& offset, std::set<T>& data)
{
Expand Down

0 comments on commit d815a46

Please sign in to comment.