-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
125 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#pragma once | ||
|
||
#include "msg_wrapper.hpp" | ||
#include "varint.hpp" | ||
|
||
namespace rpc_core { | ||
namespace detail { | ||
|
||
class coder { | ||
public: | ||
static std::string serialize(const msg_wrapper& msg) { | ||
std::string payload; | ||
std::string v_seq = to_varint(msg.seq); | ||
std::string v_cmd_len = to_varint(msg.cmd.length()); | ||
payload.reserve(v_seq.size() + v_cmd_len.size() + sizeof(msg.type) + msg.cmd.size() + msg.data.size()); | ||
payload.append(v_seq); | ||
payload.append(v_cmd_len); | ||
payload.append(msg.cmd); | ||
payload.append((char*)&msg.type, sizeof(msg.type)); | ||
if (msg.request_payload) { | ||
payload.append(*msg.request_payload); | ||
} else { | ||
payload.append(msg.data); | ||
} | ||
return payload; | ||
} | ||
|
||
static msg_wrapper deserialize(const std::string& payload, bool& ok) { | ||
msg_wrapper msg; | ||
char* p = (char*)payload.data(); | ||
const char* pend = payload.data() + payload.size(); | ||
uint8_t bytes; | ||
msg.seq = from_varint(p, &bytes); | ||
p += bytes; | ||
uint16_t cmd_len = from_varint(p, &bytes); | ||
p += bytes; | ||
if (p + cmd_len + sizeof(msg.type) > pend) { | ||
ok = false; | ||
return msg; | ||
} | ||
msg.cmd.assign(p, cmd_len); | ||
p += cmd_len; | ||
msg.type = *(msg_wrapper::msg_type*)(p); | ||
p += sizeof(msg.type); | ||
msg.data.assign(p, pend - p); | ||
ok = true; | ||
return msg; | ||
} | ||
}; | ||
|
||
} // namespace detail | ||
} // namespace rpc_core |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <cstring> | ||
#include <string> | ||
|
||
namespace rpc_core { | ||
namespace detail { | ||
|
||
static const uint8_t MSB = 0x80; | ||
static const uint8_t MSB_ALL = ~0x7F; | ||
|
||
inline uint8_t* varint_encode(unsigned long long n, uint8_t* buf, uint8_t* bytes) { | ||
uint8_t* ptr = buf; | ||
while (n & MSB_ALL) { | ||
*(ptr++) = (n & 0xFF) | MSB; | ||
n = n >> 7; | ||
} | ||
*ptr = n; | ||
*bytes = ptr - buf + 1; | ||
return buf; | ||
} | ||
|
||
inline unsigned long long varint_decode(uint8_t* buf, uint8_t* bytes) { | ||
unsigned long long result = 0; | ||
int bits = 0; | ||
uint8_t* ptr = buf; | ||
unsigned long long ll; | ||
while (*ptr & MSB) { | ||
ll = *ptr; | ||
result += ((ll & 0x7F) << bits); | ||
ptr++; | ||
bits += 7; | ||
} | ||
ll = *ptr; | ||
result += ((ll & 0x7F) << bits); | ||
*bytes = ptr - buf + 1; | ||
return result; | ||
} | ||
|
||
inline std::string to_varint(uint32_t var) { | ||
uint8_t buf[sizeof(uint32_t) + 1]; // enough | ||
std::string ret; | ||
uint8_t bytes; | ||
varint_encode(var, buf, &bytes); | ||
return {(char*)buf, bytes}; | ||
} | ||
|
||
inline uint32_t from_varint(void* data, uint8_t* bytes) { | ||
return varint_decode((uint8_t*)data, bytes); | ||
; | ||
} | ||
|
||
} // namespace detail | ||
} // namespace rpc_core |