Impl GetMsgTypes

This commit is contained in:
Changhua
2023-02-16 22:04:31 +08:00
parent 7f934f6ea9
commit 6c42e328ae
9 changed files with 167 additions and 38 deletions
+66
View File
@@ -0,0 +1,66 @@
#include <map>
#include "log.h"
#include "pb_util.h"
#include "wcf.pb.h"
#define BUF_SIZE (1024 * 1024)
static char buf[BUF_SIZE] = { 0 };
typedef std::map<int, std::string> MsgTypes_t;
void log_buffer(uint8_t *buffer, size_t len)
{
size_t j = sprintf_s(buf, BUF_SIZE, "Encoded message[%ld]: ", len);
for (size_t i = 0; i < len; i++) {
j += sprintf_s(buf + j, BUF_SIZE, "%02X ", buffer[i]);
if (j > BUF_SIZE - 3) {
break;
}
}
LOG_INFO(buf);
}
bool encode_string(pb_ostream_t *stream, const pb_field_t *field, void *const *arg)
{
const char *str = (const char *)*arg;
if (!pb_encode_tag_for_field(stream, field)) {
return false;
}
return pb_encode_string(stream, (uint8_t *)str, strlen(str));
}
bool decode_string(pb_istream_t *stream, const pb_field_t *field, void **arg)
{
std::string *str = static_cast<std::string *>(*arg);
size_t len = stream->bytes_left;
str->resize(len);
if (!pb_read(stream, (uint8_t *)str->data(), len)) {
return false;
}
return true;
}
bool encode_types(pb_ostream_t *stream, const pb_field_t *field, void *const *arg)
{
MsgTypes_t *m = (MsgTypes_t *)*arg;
MsgTypes_TypesEntry message = MsgTypes_TypesEntry_init_default;
for (auto it = m->begin(); it != m->end(); it++) {
message.key = it->first;
message.value.funcs.encode = &encode_string;
message.value.arg = (void *)it->second.c_str();
if (!pb_encode_tag_for_field(stream, field)) {
return false;
}
if (!pb_encode_submessage(stream, MsgTypes_TypesEntry_fields, &message)) {
return false;
}
}
return true;
}
+9
View File
@@ -0,0 +1,9 @@
#pragma once
#include <pb_decode.h>
#include <pb_encode.h>
void log_buffer(uint8_t *buffer, size_t len);
bool encode_string(pb_ostream_t *stream, const pb_field_t *field, void *const *arg);
bool decode_string(pb_istream_t *stream, const pb_field_t *field, void **arg);
bool encode_types(pb_ostream_t *stream, const pb_field_t *field, void *const *arg);
+2 -3
View File
@@ -1,6 +1,5 @@
# Generate all fields as pointers.
* mangle_names:M_STRIP_PACKAGE
* fallback_type:FT_POINTER
MsgTypes.types max_count:10
MsgTypes.TypesEntry.value max_size:16
# MsgTypes.* max_size:16
MsgTypes* fallback_type:FT_CALLBACK
RpcContact* fallback_type:FT_CALLBACK
+7 -7
View File
@@ -55,14 +55,14 @@ message Response
Functions func = 1;
oneof msg
{
int32 status = 2;
string str = 3;
WxMsg wxmsg = 4;
MsgTypes types = 5;
int32 status = 2;
string str = 3;
WxMsg wxmsg = 4;
MsgTypes types = 5;
RpcContacts contacts = 6;
DbNames dbs = 7;
DbTables tables = 8;
DbRows rows = 9;
DbNames dbs = 7;
DbTables tables = 8;
DbRows rows = 9;
};
}