`
zhou.xingbo
  • 浏览: 53033 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Cmake + protobuf-c + python自定义协议通信

阅读更多

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 [b]\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/

 

 

分享到:
评论

相关推荐

    cmake-3.14.0-win64-x64+protobuf-all-3.7.0.zip

    cmake-3.14.0-win64-x64+protobuf-all-3.7.0 根据《protobuf 的编译,安装与python命令行配合使用》https://blog.csdn.net/a592733740/article/details/105351465 1.生成项目的目录:\lsw\cmake-3.14.0-win64-x64\...

    windows 环境下使用 cmake+mingw-w64+qt5.10 编译 opencv3.4...

    windows 环境下使用 cmake+mingw-w64+qt5.10 编译 opencv3.4... windows 环境下使用 cmake+mingw-w64+qt5.10 编译 opencv3.4... windows 环境下使用 cmake+mingw-w64+qt5.10 编译 opencv3.4... vvvv windows 环境...

    protobuf-3.0.0-beta-2-cpp

    安装protobuf各个版本的编译代码,从protobuf-2.4.1到protobuf-3.0.0-Release之间的20个版本,在Mac上protobuf-2.6.1、protobuf-3.0.0-alpha-1、protobuf-3.0.0-beta-2、protobuf-3.0.0测试全部成功!解决了从GitHub...

    protobuf-master

    protocol buffer 常规安装步骤如下所示:... tar -xzf protobuf-2.5.0.tar.gz //解压 cd protobuf-2.5.0 //进入解压后的目录 //或者直接手动解压,进入解压后的文件夹 bash ./configure --prefix=$INSTALL_DIR make

    ubuntu 安装opencv3.2 需要的ippicv_linux_20151201+ protobuf-3.1.0

    接下来,`protobuf-3.1.0`是Protocol Buffers的版本,这是一个Google开发的数据序列化协议,常用于数据交换和存储。在OpenCV中,protobuf可能用于存储和读取配置文件,或者在不同组件之间传递结构化数据。对于OpenCV...

    Mysql5.6.11+cmake+ncurses-devel及安装文档

    这个过程对于开发者来说尤其重要,因为它允许自定义编译选项,以适应特定的系统环境和性能需求。在实际应用中,确保数据库的稳定性和效率是至关重要的,因此理解并掌握这种安装方法对于任何 IT 专业人士来说都是宝贵...

    cmake-3.16.0-Linux-x86-64.tar.gz

    压缩包中还包含一个名为“cmake-3.16.0-Linux-x86_64.tar.gz.txt”的文本文件,这通常用来存储有关压缩包的详细信息,如编译时的配置选项、使用指南、许可协议或者发布者的注释。用户可以阅读这个文件获取更多关于这...

    opencv-4.1.1+opencv_contrib-4.1.1+cmake-3.14.5-win64-x64

    opencv-4.1.1+opencv_contrib-4.1.1+cmake-3.14.5-win64-x64 OpenCV 给第三方编译用的整套环境。 OpenCV源码 + Contrib源码 + CMake 工具。

    cmake-3.17.0-win64-x64.msi

    cmake-3.17.0-win64-x64 cmake-3.17.0-win64-x64 cmake-3.17.0-win64-x64 cmake-3.17.0-win64-x64 cmake-3.17.0-win64-x64 cmake-3.17.0-win64-x64

    cmake+vs2015+opencv4.8.0+opencv-contrib4.8.0源码编译

    cmake+vs2015+opencv4.8.0+opencv-contrib4.8.0源码编译,x64,release版本

    protobuf-cpp-3.1.0.tar.gz(下载附件zip文件解压后便是需要的文件)

    解决方法:下载附件zip文件,解压后得到protobuf-cpp-3.1.0.tar.gz文件,替换/opencv_contrib-3.2.0/modules/dnn/.download/bd5e3eed635a8d32e2b99658633815ef/v3.1.0目录下的同名文件,重新cmake编译即可。...

    protobuf-cpp-3.2.0.tar.gz

    当你有一个.proto文件,你可以使用protobuf编译器生成对应语言(如C++、Java、Python等)的源代码,这些源代码提供了方便的方法来序列化和反序列化你的数据结构。在C++中,这通常涉及使用`SerializeToString()`和`...

    Git+cmake+Win32OpenSSL+libwebsockets-2.4.zip.rar

    c++ 库 libwebsockets-master 2.4 版源码包。...里边有使用到的生成工具,Git-2.22.0-64-bit+Win32OpenSSL-1_0_2p+cmake-3.6.0-win64-x64.msi+libwebsockets-2.4.zip。记得安装上之后需要配置环境变量。

    cmake-3.18.0-rc2-win64-x64.msi

    cmake-3.18.0-rc2-win64-x64.msi 安装版

    cmake-3.15.0-win64-x64.msi

    cmake-3.15.0-win64-x64.msi 官网下载地址:https://cmake.org/files/

    opencv_python-4.1.2+contrib-cp36-cp36m-win_amd64.whl

    opencv-contrib-python-4.2.0.34, 直接pip install +文件名,和cmake什么的说拜拜。 cp后面的是对应的python版本,cp36就是python3.6,那个3.x.x是opencv的版本。根据自己的python版本下载,我下的是opencv_python-...

    cmake-3.20.1-windows-x86_64.zip

    它是FORTRAN77编写的,但可以通过CMake与其他语言(如C++或C)集成。在Visual Studio中使用CMake构建项目时,可以配置CMake来链接到CLAPACK库,以便在项目中使用其功能。 使用CMake与Visual Studio结合,开发者可以...

    cmake-3.10.0-rc4-win64-x64.msi

    cmake-3.10.0-rc4-win64-x64.msi cmake-3.10.0-rc4-win64-x64.msi cmake-3.10.0-rc4-win64-x64.msi

    cmake-3.17.1-win64-x64(zip&amp;msi).7z

    CMake是一款跨平台的自动化构建系统,用于管理软件构建过程。它使用简单的语法来描述项目结构,然后可以生成各种编译器和构建工具所需的构建文件。CMake 不是直接进行编译,而是生成如Visual Studio、Makefile或其他...

    cmake-3.11.0-rc2-win64-x64.zip

    CMake是一个跨平台的自动化构建系统,用于管理软件构建过程。CMake不是直接与编译器交互,而是生成特定平台的构建工具(如Visual Studio、Makefile等)所需的项目文件。"cmake-3.11.0-rc2-win64-x64.zip"是一个针对...

Global site tag (gtag.js) - Google Analytics