这次写的通讯程序使用protobuf作为自定义协议。protobuf buffer是google 的一种数据交换的格式,它独立于语言,独立于平台。由于它是一种二进制的格式,比使用xml 进行数据交换快许多。可以把它用于分布式应用之间的数据通信或者异构环境下的数据交换。作为一种效率和兼容性都很优秀的二进制数据传输格式,可以用于诸如网络传输、配置文件、数据存储等诸多领域。下面是我用protobuf的自定义消息类型。下面是protobuf自定义的协议
//注册消息
message Register_Request
{
required string name = 1;
required string code = 2;
optional string birthday = 3;
optional string telphone = 4;
optional string mymaxim = 5;
optional string image = 6; //注册头像
optional string image_name = 7;
}
//普通消息
message Message_Normal
{
optional string sender = 1;
optional string reciver = 2;
optional string text = 3;
optional string time = 4;
optional string image = 5;
optional message_type type = 6;
optional int32 messagetype = 7;
optional string image_name = 8;
repeated string messageall = 9;
optional string group_name = 10;
optional string st_name = 11;
optional string time_new = 12;
}
//服务器消息
message Command
{
optional string use1 = 1;
optional string use2 = 2;
optional int32 is_ok = 3;
optional string other_use = 4;
optional int32 other_use1 = 5;
optional string use3 = 6;
optional string random_ = 7;
}
//普通消息类型
enum message_type
{
normal = 1;
sound = 2;
video = 3;
unknown = 4;
system_ = 5;
query_friend = 6; //添加好友消息类型
query_friend_ack = 7; //添加好友确认消息类型
group = 8; //群组消息类型
file_message = 9; //文件消息类型
}
//消息类型
enum Command_Type
{
add_friend = 1;
get_friend_info = 2; //获取好友详细信息
delete_friend= 3;
change_other = 4; //改变其他
change_my_name = 5;
change_my_passwd = 6;
change_my_status = 7;
quit = 8;
register_request = 9;
login_request = 10; //登录请求
login_respnose = 11; //登录回复
error = 12;
heart = 13; //心跳
request_address = 14; //请求ip地址以用于文件或视频p2p传输
udp_ack = 15; //发送确认ack消息
add_friend_ack = 16;
friend_message = 17;
not_ok = 18;
get_friend_address = 19;
friend_login = 20;
friend_exit = 21;
create_group = 22;
group_message = 23;
change_friend_palace = 24;
remove_my_friend = 25;
change_mymaxim__ = 26;
exit_group = 27;
joinGroupRequest = 28;
groupmessage = 29;
camera_direct_send = 30;
camera_direct_recv = 31;
camera_direct_send_ack = 32;
camera_direct_recv_ack = 33;
camera_end = 34;
open_file_tcp = 35;
open_file_tcp_infor = 36;
file_detail = 37;
file_download = 38;
file_upload__ = 39;
passwd_protect = 40; //设置密保消息
findbackcode = 41; //密保消息
passwd_change = 42; //通过密保找回密码
}
message Message_
{
required Command_Type type = 1;
optional Command command = 2;
repeated Message_Normal message_normal = 3;
optional Register_Request register_request = 4;
optional string id = 5;
}
protobuf有三种消息类型:repeated,optional,required。从字面意思可以看出,repeated:一个消息可以设置多个相同类型字段,类似于c++里的vector。optional可以设置也可以不设置,required必须设置,否则程序虽然会编译通过但运行会崩溃。
protobuf设置enum字段:以上面协议为例:设置消息类型为添加好友类型(add_friend):Message message; message.set_type(add_friend),只需要设置主消息类.set_枚举类型变量名(枚举类型)即可。解析enum:Message message; Command_type type = message.type(),type即为设置的类型 设置repeated类型:以上面协议为例:Message_Normal *ms = message.add_message_normal();只需要主类.add_+repeated字段类型变量名即可。ms消息类的使用与一般消息类使用相同。解析repeated:Message_ message. int num = message.message_normal_size();获取repeated的个数,然后for循环 for(int i = 0; i < num; i++) { Message_Normal ms = message.message_normal(i); //获取到每个repeated消息,后与一般消息处理方法相同 } 设置嵌套消息:定义一个嵌套消息的指针例如上面协议的Command *cmd = new Command; Message message. message.set_allocated_command(cmd);使用set_allocated_+类消息名把嵌套的消息加到主消息上; 注意:1.一定要记住message.set_allocated_command(cmd)这条语句,在编程时很容易忘记 2.Command *cmd = new Command不能自己delete,在添加进主类后,函数作用域结束protobuf会自动释放new的内存,如果自己在显示delete,会导致程序崩溃. 3.一定记住要把proto文件生成的.h和.cc文件加入到项目里,否则会导致链接出错。
转载请注明原文地址: https://ju.6miu.com/read-600405.html