项目作者: aicrobo

项目描述 :
aic_commu library 是一个基于 libzmq 以及 protobuf 封装而成的通讯库。主要提供心跳、自动重连等一些网络状态检测以及恢复功能,支持请求应答、订阅发布两种模式。
高级语言: C++
项目地址: git://github.com/aicrobo/aic_communication.git
创建时间: 2018-11-19T08:01:54Z
项目社区:https://github.com/aicrobo/aic_communication

开源协议:MIT License

下载


aic_commu library

Description

Aic_commu library is a communication library based on libzmq. It mainly provides some network status detection and recovery functions, such as heartbeat detection, disconnection or automatic reconnection of heartbeat timeout. It supports two modes: request response and subscription publishing.

Using this library is simpler than programming directly with zmq, shielding many underlying details, and transferring data between business layer and communication library through callback function, so that callers only care about business logic.


QUICK START

  • request client

    1. auto obj = AicCommuFactory::newSocket(AicCommuType::CLIENT_REQUEST, "127.0.0.1", 60005, "req");
    2. obj->setRecvCall(&req_recv_func, false);
    3. obj->run();
    4. sting msg = "test req msg";
    5. bytes_ptr pack = std::make_shared<bytes_vec>(msg.data(),msg.data()+msg.size());
    6. obj->send();
  • response server

    1. auto obj = AicCommuFactory::newSocket(AicCommuType::SERVER_REPLY, "*", 60005, "rep");
    2. obj->setRecvCall(&rep_recv_func, false);
    3. obj->run();
  • subscribe client

    1. auto obj = AicCommuFactory::newSocket(AicCommuType::CLIENT_SUBSCRIBE, "127.0.0.1", 60006, "sub");
    2. obj->setRecvCall(&sub_recv_func, false);
    3. obj->run();
    4. obj->alterSubContent("sub-test", true);
  • publish server

    1. auto obj = AicCommuFactory::newSocket(AicCommuType::SERVER_PUBLISH, "*", 60006, "pub");
    2. obj->run();
    3. std::string msg = "test pub msg";
    4. bytes_ptr pack = std::make_shared<bytes_vec>(msg.data(),msg.data() + msg.length());
    5. obj->publish("sub-test", pack);

DEPENDS:

  • Windows 10, Linux
  • CMake 3.5 (on Linux & Windows)
  • Visual Studio 2015 Update 3 (on Windows)
  • C compiler and GNU C++ compiler (on Linux)
  • libzmq-master(commit 12005bd92629c2cca108ae1731a495e93a3aef91)

BUILD(linux):

  1. download aic_communication source code。
  2. download libzmq source code
    libzmq
  3. build zmq to static library
    1. cd libzmq
    2. ./autogen.sh
    3. ./configure --enable-static --with-pic
    4. make
    5. sudo make install
  4. build aic_communication

    1. cd aic_communication
    2. mkdir build
    3. cd build
    4. cmake ..
    5. make
    6. sudo make install
  5. build example

    • in step 4,replace cmake .. with
      • cmake .. -DTEST_JSON=YES (only build json version example )
      • cmake .. -DTEST_PROTOBUF=YES (only build protobuf version example)
      • cmake .. -DTEST=YES (build all version example)

BUILD(windows):

  1. download aic_communication source code。
  2. download libzmq source code
    libzmq
  3. build zmq
    1. cd aic_communication/builds
    2. mkdir windows
    3. cd windows
    4. cmake ../..
    5. double click libzmq.vcxprojbuild dll and lib
  4. build aic_communication
    1. cd aic_communication
    2. mkdir build
    3. cmake ..
    4. double click aic_commu.slnright click aic_commu projectclick propertyconfig header and lib file paths of zmq and protobufbuild aic_commu.dll and aic_commu.lib
  5. build example
    • in step 4,replace cmake .. with
      • cmake .. -DTEST_JSON=YES (only build json version example )
      • cmake .. -DTEST_PROTOBUF=YES (only build protobuf version example)
      • cmake .. -DTEST=YES (build all version example)
    • notice: if you build protobuf version example,you must download and build protobuf first, then run commands:’cd example/protobuf’ ‘protoc —cpp_out=. packet.proto’.then you will get two new files:packet.pb.h,packet.pb.cc

USING

prepare:config right ips in source code,rebuild project

request-reply mode

  • open one console,run req_json or req_proto
  • open another console,run rep_json or rep_proto

publish-subscribe mode

  • open one console,run sub_json or sub_proto
  • open another console,run pub_json or pub_proto

notice: _json and _json is a pair,_proto and _proto is a pair,you can’t run one _json and one _proto

—————-VERSION LOG—————-

1.2 version

  • update:
    • remove libprotobuf dependence

1.1.7 version

  • fix bugs:

    • fix bug what you never can send and recv data after request timeout when in request mode。
    • fix bug what deadlock caused by socket destruction。
  • new functions:

    • add param to send function,it will discard send packet when connect with server hasn’t build。

Copyright (c) 2018, AicRobo. All rights reserved.