Protobuf了解一下?

Protocol Buffers是Google公司开发的一种数据描述语言,类似于XML能够将结构化数据序列化,可用于数据存储、通信协议等方面。它不依赖于语言和平台并且可扩展性极强。现阶段官方支持C++、JAVA、Python等编程语言,但可以找到大量的几乎涵盖所有语言的第三方拓展包。

Protocol Buffers经常被简称为protobuf。

640?wx_fmt=jpeg

图文无关,就是想纪念一下消失的巴黎圣母院

项目GitHub地址:

https://github.com/google/protobuf


为什么要使用protobuf?

1、它支持多种语言,最简单的python可以很轻易的使用它。(灵活)

2、它是一种特殊的方法,可以数据项编码为单个字符串。(高效)

3、它可以将数据序列化为XML,可以与不同种类的项目进行共享数据,一般的在软硬件交互,游戏开发等情况下使用。(应用广泛)


如何使用protocolbuf?

编写一个.proto文件描述希望存储的数据结构。然后,protocolbuf编译器创建一个类,该类使用有效的二进制格式实现协议缓冲区数据的自动编码和解析。生成的类为构成协议的字段提供getter和setter方法。重要的是,protocolbuf格式支持随着时间的推移扩展格式,这样代码仍然可以读取用旧格式编码的数据。


那我们现在就开始实践吧!

首先需要安装一下protocolbuf的支持包

我们使用python的pip安装

pip install protobuf

640?wx_fmt=png


安装完之后我们就来实操一下,如将一个protobuf文件编译成python文件。

首先我们创建一个protobuf文件:a.proto

syntax = "proto2";

package tutorial;

message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;

enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}

message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}

repeated PhoneNumber phones = 4;
}

message AddressBook {
repeated Person people = 1;
}

创建的文件主要就是一个名字对应一个数据体,类似于python里面的字典。数据体里面数据格式比较对多,可以有string,int32,PhoneTyoe等,对于必要的字段需要在前面加上required关键字,如果是可有可无的字段在前面加上optional关键字即可。





在终端执行语句:

protoc --proto_path=proto --python_out=pblib/proto proto/*.proto 

--proto_path:默认路径

--python_out:生成代码的路径

蓝色的代表代码所在的路径,可以给绝对路径也可以给相对路径

640?wx_fmt=png


「注:如果使用MAC执行这个语句,

需要先执行brew install grpc protobuf」


生成python文件:

# -*- coding: utf-8 -*-
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: a.proto
import sys
_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1'))
from google.protobuf import descriptor as _descriptor
from google.protobuf import message as _message
from google.protobuf import reflection as _reflection
from google.protobuf import symbol_database as _symbol_database
# @@protoc_insertion_point(imports)
_sym_db = _symbol_database.Default()




DESCRIPTOR = _descriptor.FileDescriptor(
name='a.proto',
package='tutorial',
syntax='proto2',
serialized_options=None,
serialized_pb=_b('\n\x07\x61.proto\x12\x08tutorial\"\xdb\x01\n\x06Person\x12\x0c\n\x04name\x18\x01 \x02(\t\x12\n\n\x02id\x18\x02 \x02(\x05\x12\r\n\x05\x65mail\x18\x03 \x01(\t\x12,\n\x06phones\x18\x04 \x03(\x0b\x32\x1c.tutorial.Person.PhoneNumber\x1aM\n\x0bPhoneNumber\x12\x0e\n\x06number\x18\x01 \x02(\t\x12.\n\x04type\x18\x02 \x01(\x0e\x32\x1a.tutorial.Person.PhoneType:\x04HOME\"+\n\tPhoneType\x12\n\n\x06MOBILE\x10\x00\x12\x08\n\x04HOME\x10\x01\x12\x08\n\x04WORK\x10\x02\"/\n\x0b\x41\x64\x64ressBook\x12 \n\x06people\x18\x01 \x03(\x0b\x32\x10.tutorial.Person')
)



_PERSON_PHONETYPE = _descriptor.EnumDescriptor(
name='PhoneType',
full_name='tutorial.Person.PhoneType',
filename=None,
file=DESCRIPTOR,
values=[
_descriptor.EnumValueDescriptor(
name='MOBILE', index=0, number=0,
serialized_options=None,
type=None),
_descriptor.EnumValueDescriptor(
name='HOME', index=1, number=1,
serialized_options=None,
type=None),
_descriptor.EnumValueDescriptor(
name='WORK', index=2, number=2,
serialized_options=None,
type=None),
],
containing_type=None,
serialized_options=None,
serialized_start=198,
serialized_end=241,
)
_sym_db.RegisterEnumDescriptor(_PERSON_PHONETYPE)


_PERSON_PHONENUMBER = _descriptor.Descriptor(
name='PhoneNumber',
full_name='tutorial.Person.PhoneNumber',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='number', full_name='tutorial.Person.PhoneNumber.number', index=0,
number=1, type=9, cpp_type=9, label=2,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
_descriptor.FieldDescriptor(
name='type', full_name='tutorial.Person.PhoneNumber.type', index=1,
number=2, type=14, cpp_type=8, label=1,
has_default_value=True, default_value=1,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto2',
extension_ranges=[],
oneofs=[
],
serialized_start=119,
serialized_end=196,
)

_PERSON = _descriptor.Descriptor(
name='Person',
full_name='tutorial.Person',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='name', full_name='tutorial.Person.name', index=0,
number=1, type=9, cpp_type=9, label=2,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
_descriptor.FieldDescriptor(
name='id', full_name='tutorial.Person.id', index=1,
number=2, type=5, cpp_type=1, label=2,
has_default_value=False, default_value=0,
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
_descriptor.FieldDescriptor(
name='email', full_name='tutorial.Person.email', index=2,
number=3, type=9, cpp_type=9, label=1,
has_default_value=False, default_value=_b("").decode('utf-8'),
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
_descriptor.FieldDescriptor(
name='phones', full_name='tutorial.Person.phones', index=3,
number=4, type=11, cpp_type=10, label=3,
has_default_value=False, default_value=[],
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
],
extensions=[
],
nested_types=[_PERSON_PHONENUMBER, ],
enum_types=[
_PERSON_PHONETYPE,
],
serialized_options=None,
is_extendable=False,
syntax='proto2',
extension_ranges=[],
oneofs=[
],
serialized_start=22,
serialized_end=241,
)


_ADDRESSBOOK = _descriptor.Descriptor(
name='AddressBook',
full_name='tutorial.AddressBook',
filename=None,
file=DESCRIPTOR,
containing_type=None,
fields=[
_descriptor.FieldDescriptor(
name='people', full_name='tutorial.AddressBook.people', index=0,
number=1, type=11, cpp_type=10, label=3,
has_default_value=False, default_value=[],
message_type=None, enum_type=None, containing_type=None,
is_extension=False, extension_scope=None,
serialized_options=None, file=DESCRIPTOR),
],
extensions=[
],
nested_types=[],
enum_types=[
],
serialized_options=None,
is_extendable=False,
syntax='proto2',
extension_ranges=[],
oneofs=[
],
serialized_start=243,
serialized_end=290,
)

_PERSON_PHONENUMBER.fields_by_name['type'].enum_type = _PERSON_PHONETYPE
_PERSON_PHONENUMBER.containing_type = _PERSON
_PERSON.fields_by_name['phones'].message_type = _PERSON_PHONENUMBER
_PERSON_PHONETYPE.containing_type = _PERSON
_ADDRESSBOOK.fields_by_name['people'].message_type = _PERSON
DESCRIPTOR.message_types_by_name['Person'] = _PERSON
DESCRIPTOR.message_types_by_name['AddressBook'] = _ADDRESSBOOK
_sym_db.RegisterFileDescriptor(DESCRIPTOR)

Person = _reflection.GeneratedProtocolMessageType('Person', (_message.Message,), dict(

PhoneNumber = _reflection.GeneratedProtocolMessageType('PhoneNumber', (_message.Message,), dict(
DESCRIPTOR = _PERSON_PHONENUMBER,
__module__ = 'a_pb2'
# @@protoc_insertion_point(class_scope:tutorial.Person.PhoneNumber)
))
,
DESCRIPTOR = _PERSON,
__module__ = 'a_pb2'
# @@protoc_insertion_point(class_scope:tutorial.Person)
))
_sym_db.RegisterMessage(Person)
_sym_db.RegisterMessage(Person.PhoneNumber)

AddressBook = _reflection.GeneratedProtocolMessageType('AddressBook', (_message.Message,), dict(
DESCRIPTOR = _ADDRESSBOOK,
__module__ = 'a_pb2'
# @@protoc_insertion_point(class_scope:tutorial.AddressBook)
))
_sym_db.RegisterMessage(AddressBook)


# @@protoc_insertion_point(module_scope)



只要一个简单的命令就可以将proto文件编译成python文件。

所以,之后想要使用protobuf,就可以在proto文件中创建类似python字典形式的数据,之后再使用:

protoc --python_out=输出路径 proto文件路径即可


这就是protobuf最基本的使用啦。

640?wx_fmt=gif

“每天学习一点点“



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
给您提供一份修改后的代码: ```cpp #include "ros/ros.h" #include <websocketpp/config/asio_client.hpp> #include <websocketpp/client.hpp> #include <websocketpp/common/thread.hpp> #include <iostream> typedef websocketpp::client<websocketpp::config::asio_tls_client> client; class WebSocketClient { public: WebSocketClient() { // 设置websocket的回调函数 m_client.set_message_handler(std::bind(&WebSocketClient::on_message, this, std::placeholders::_1, std::placeholders::_2)); } void start() { // 连接到websocket服务器 websocketpp::lib::error_code ec; client::connection_ptr con = m_client.get_connection("wss://autopilot-test.t3go.cn:443/api/v1/vehicle/push/message/LFB1FV696M2L43840", ec); if (ec) { std::cout << "连接失败: " << ec.message() << std::endl; return; } // 运行websocket的事件循环 m_client.init_asio(); m_client.start_perpetual(); m_client.connect(con); websocketpp::lib::thread asio_thread(&client::run, &m_client); asio_thread.join(); } void perceptionCallback(const std_msgs::String::ConstPtr& msg) { // 发送消息到websocket服务器 std::string serialized_data = msg->data; m_client.send(serialized_data, websocketpp::frame::opcode::text); } private: void on_message(websocketpp::connection_hdl hdl, client::message_ptr msg) { // 处理websocket服务器返回的消息 std::cout << "收到消息: " << msg->get_payload() << std::endl; } client m_client; }; int main(int argc, char** argv) { ros::init(argc, argv, "websocket_client"); ros::NodeHandle nh; WebSocketClient ws_client; // 订阅感知话题 ros::Subscriber sub = nh.subscribe("perception_topic", 1000, &WebSocketClient::perceptionCallback, &ws_client); // 运行ROS事件循环 ros::spin(); return 0; } ``` 这份代码使用了websocketpp库来创建websocket客户端,并在ROS中订阅了一个感知话题。当收到该话题的消息时,就会将消息序列化后发送到websocket服务器。同时,也会监听websocket服务器返回的消息并进行处理。请注意,这份代码需要安装websocketpp库才能编译通过。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值