设为首页 收藏本站
查看: 879|回复: 0

[经验分享] Cmake + protobuf-c + python自定义协议通信

[复制链接]

尚未签到

发表于 2017-5-5 11:51:56 | 显示全部楼层 |阅读模式
  Cmake是一套跨平台的工程构建工具
  sudo apt-get install cmake
  一个Cmake的例子
  生成一个demo工程,包括一个hello.cpp文件(在demo工程下)

#include <stdio.h>
int main(int argc, char **argv)
{
printf("Hello world!\n");
return 0;
}
  Cmake构建该工程
  Cmake需要CMakeLists.txt文件来配置,在demo目录下创建CMakeLists.txt文件

PROJECT (Test)
SET(SRC_LIST hello.cpp)
ADD_EXECUTABLE(test ${SRC_LIST})
  构建项目
  cmake .
  便会生成相应的Makefile文件
  Protobuf
  安装protobuf-c
  需要先安装google protobuf
  http://code.google.com/p/protobuf/downloads/list
  ./configure
  make
  make check
  make install
  下载
  http://code.google.com/p/protobuf-c/downloads/list
  ./configure --prefix=$HOME/install
  make
  make install
  A typical reason for this behaviour is a stale ld.so.cache; try to run ldconfig to update it after making sure that /usr/local/lib is listed in /etc/ld.so.conf.
  make不过时可能需要执行 ldconfig 命令
  To install into /usr like a normal package would, use --prefix=/usr
  protobuf-c Simple complete example
  protobuf-c works by taking a.proto file, and generating both .h and .c files for use in C programs.
  amessage.proto

message AMessage
{
required int32 a=1;
optional int32 b=2;
}

   Generate .h and .c files from the command-line in your current working directory:
  protoc-c --c_out=. amessage.proto 
  Serialize/pack Deserialize/upack the AMessage as follows:
  amessage_demo.c

#include <stdio.h>
#include <stdlib.h>
#include "amessage.pb-c.h"
int main (int argc, const char * argv[])
{
AMessage msg = AMESSAGE__INIT; // AMessage
AMessage *rmsg;
void *buf;                     // Buffer to store serialized data
unsigned len;                  // Length of serialized data
if (argc != 2 && argc != 3)
{   // Allow one or two integers
fprintf(stderr,"usage: amessage a \n");
return 1;
}
msg.a = atoi(argv[1]);                                   // Put an int in msg
if (argc == 3) { msg.has_b = 1; msg.b = atoi(argv[2]); } // Add another
len = amessage__get_packed_size(&msg);                   // This is calculated packing length
buf = malloc(len);        // Allocated memory for packed/encode serialized data
amessage__pack(&msg,buf); // Put it in buf now
fprintf(stderr,"Writing %d serialized bytes\n",len); // See the length of message
fwrite(buf,len,1,stdout); // Write to stdout to allow direct command line piping
rmsg = amessage__unpack(NULL, len, buf);
printf("Received: a=%d", rmsg->a);
printf(" b=%d", rmsg->b);
printf("\n");
amessage__free_unpacked(rmsg, NULL);
free(buf); // Free the allocated serialized buffer
return 0;
}
  编译:
  gcc amessage_demo.c amessage.pb-c.c -o amessage_demo -lprotobuf-c
  效果:
  ./amessage_demo 10 2
  Writing: 4 serialized bytes
  Received: a=10 b=2
  安装protobuf-python环境:
  cd protobuf目录下python目录
  python setup.py install
  Protobuf-c RPC service and Protobuf python client
  首先写一个结构,重点是加上service这样会生成相应的service方法
  rpc.proto

package demo;
message SearchRequest {
required string keyword = 1;
}
message SearchResponse {
required string result = 1;
}
service RpcService {
rpc Search (SearchRequest) returns (SearchResponse);
}

   在service中要实现的方法
  ProtobufCService *protbuf_c_rpc_client_new
  ProtobufC_RPC_Server *protobuf_c_rpc_server_new
  python实现protobuf rpc
  protobuf-socket-rpc
  python protobuf rpc using tcp/ip sockets
  http://code.google.com/p/protobuf-socket-rpc/downloads/list
  在源码中有实例
  Note: 在.proto文件中加入
  option py_generic_services = true;
  ruby实现protobuf rpc
  gem install ruby_protobuf
  rpc.proto

package demo;
message SearchRequest {
required string keyword = 1;
}
message SearchResponse {
required string result = 1;
}
service RpcService {
rpc Search (SearchRequest) returns (SearchResponse);
}

   rproto rpc.proto
  rpc_service.rb

require 'rubygems'
require 'protobuf/rpc/server'  
require 'protobuf/rpc/handler'  
require 'rpc.pb'  
class Demo::SearchHandler < Protobuf::Rpc::Handler  
request Demo::SearchRequest  
response Demo::SearchResponse  
def self.process_request(request, response)  
if request.keyword == 'google'  
response.result = 'www.google.com'  
elsif request.keyword == 'freewheel'  
response.result = 'www.freewheel.tv'  
else  
response.result = ''  
end  
end  
end  
class Demo::RpcService < Protobuf::Rpc::Server  
def setup_handlers  
@handlers = {  
:search => Demo::SearchHandler,  
}   
end  
end  
   client_search.rb

#!/usr/bin/env ruby
require 'rubygems'  
require 'protobuf/rpc/client'  
require 'rpc.pb'
# build request  
request1 = Demo::SearchRequest.new  
request1.keyword = 'google'  
request2 = Demo::SearchRequest.new  
request2.keyword = 'freewheel'  
# create blunk response  
response1 = Demo::SearchResponse.new  
response2 = Demo::SearchResponse.new  
# execute rpc  
Protobuf::Rpc::Client.new('localhost', 9999).call :search, request1, response1  
Protobuf::Rpc::Client.new('localhost', 9999).call :search, request2, response2  
p response1.result  
p response2.result  
   ruby start_rpc_service
  ruby client_search.rb  
  未完成...
  ps:
  http://code.google.com/p/protobuf/

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-373448-1-1.html 上篇帖子: 利用Python抓取和解析网页(二)补充 下篇帖子: 通过比较学python(5):IronPython引用.Net类
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表