feat: TCP协议支持protobuf序列化,保留JSON兼容
- 新增 tcp_messages_pb2.py protobuf Python绑定 - msg_handler.cpp: 新增 make_packet_pb/raw bytes打包, parse_packet_raw/raw解析 - archery_netcore.cpp: 注册 make_packet_pb/parse_packet_raw - network.py: 新增 _use_proto 标志,_make_send_packet/_parse_recv 自动选择proto/JSON - 登录version加+proto后缀标识proto模式 - protobuf序列化/反序列化失败时自动回退JSON
This commit is contained in:
@@ -51,6 +51,43 @@ namespace netcore {
|
||||
return py::bytes(reinterpret_cast<const char*>(packet.data()), packet.size());
|
||||
}
|
||||
|
||||
// 打包 TCP 数据包 (raw bytes body, 用于 protobuf)
|
||||
py::bytes make_packet_pb(int msg_type, py::bytes body_bytes) {
|
||||
netcore::log_debug(std::string("make_packet_pb msg_type=") + std::to_string(msg_type));
|
||||
|
||||
py::buffer_info buf = py::buffer(body_bytes).request();
|
||||
uint32_t body_len = buf.size;
|
||||
uint32_t checksum = body_len + msg_type;
|
||||
|
||||
std::vector<uint8_t> packet;
|
||||
packet.reserve(12 + body_len);
|
||||
|
||||
// body_len (big-endian, 4 bytes)
|
||||
packet.push_back((body_len >> 24) & 0xFF);
|
||||
packet.push_back((body_len >> 16) & 0xFF);
|
||||
packet.push_back((body_len >> 8) & 0xFF);
|
||||
packet.push_back(body_len & 0xFF);
|
||||
|
||||
// msg_type (big-endian, 4 bytes)
|
||||
packet.push_back((msg_type >> 24) & 0xFF);
|
||||
packet.push_back((msg_type >> 16) & 0xFF);
|
||||
packet.push_back((msg_type >> 8) & 0xFF);
|
||||
packet.push_back(msg_type & 0xFF);
|
||||
|
||||
// checksum (big-endian, 4 bytes)
|
||||
packet.push_back((checksum >> 24) & 0xFF);
|
||||
packet.push_back((checksum >> 16) & 0xFF);
|
||||
packet.push_back((checksum >> 8) & 0xFF);
|
||||
packet.push_back(checksum & 0xFF);
|
||||
|
||||
// 追加 body bytes
|
||||
const uint8_t* body_ptr = static_cast<const uint8_t*>(buf.ptr);
|
||||
packet.insert(packet.end(), body_ptr, body_ptr + body_len);
|
||||
|
||||
netcore::log_debug(std::string("make_packet_pb done bytes=") + std::to_string(packet.size()));
|
||||
return py::bytes(reinterpret_cast<const char*>(packet.data()), packet.size());
|
||||
}
|
||||
|
||||
// 解析 TCP 数据包
|
||||
py::tuple parse_packet(py::bytes data) {
|
||||
// 1) 转换为 bytes view
|
||||
@@ -110,4 +147,26 @@ namespace netcore {
|
||||
return py::make_tuple(py::int_(msg_type), raw_dict);
|
||||
}
|
||||
}
|
||||
|
||||
// 解析 TCP 数据包 -> (msg_type, body_bytes) 不做 JSON 解析
|
||||
py::tuple parse_packet_raw(py::bytes data) {
|
||||
py::buffer_info buf = py::buffer(data).request();
|
||||
if (buf.size < 12) {
|
||||
return py::make_tuple(py::none(), py::none());
|
||||
}
|
||||
|
||||
const uint8_t* ptr = static_cast<const uint8_t*>(buf.ptr);
|
||||
|
||||
uint32_t body_len = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
|
||||
uint32_t msg_type = (ptr[4] << 24) | (ptr[5] << 16) | (ptr[6] << 8) | ptr[7];
|
||||
|
||||
uint32_t expected_len = 12 + body_len;
|
||||
if (buf.size < expected_len) {
|
||||
return py::make_tuple(py::none(), py::none());
|
||||
}
|
||||
|
||||
// 返回原始 body bytes(不做 JSON 解析)
|
||||
py::bytes body_bytes(reinterpret_cast<const char*>(ptr + 12), body_len);
|
||||
return py::make_tuple(py::int_(msg_type), body_bytes);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user