FDBUS学习笔记

FDBUS需要用到protobuf,所以需要提前安装好protobuf。

protobuf下载地址:

GitHub - protocolbuffers/protobuf: Protocol Buffers - Google's data interchange formathttps://github.com/protocolbuffers/protobuf下载好后安装到环境,安装可自行百度,或参考如下网址

protobuf 安装 - 知乎 (zhihu.com)https://zhuanlan.zhihu.com/p/160249058(protoc --cpp_out=./ *.proto 可以吧proto文件编译成cc和h文件,用于后续加入fdbus工程使用。)

fdbus下载地址:

fdbus: Fast Distributed Bus (FDBus) (gitee.com)https://gitee.com/jeremyczhen/fdbus下载好之后,根据指导文档编译即可,编译文档如下网址:

Introduction — FDbus 1.0 documentationhttps://fdbus.readthedocs.io/en/latest/readme.html#for-ubuntu-host-version-running-at-host-machine编译好之后如下,(这里我使用ubuntu的g++/gcc 直接编译):

先启动 name_server

然后可以启动 fdbxserver 和 fdbxclient  就可以看到有打印信息

说明编译没问题,而且fdbus相应的功能都能用了。最后make install 将相关文件安装到指定目录。

接下来,如何开发自己的fdbus工程。

fdbus文件夹中有很多server和client的例子,这里我们可以参考example下的 server 和 client 相关源码去构建自己的代码。

server 头文件:

#ifndef __FDBSERVER_H__ 
#define __FDBSERVER_H__

#include "fdbDemo.pb.h"
#include <common_base/fdbus.h>
#include <common_base/CFdbProtoMsgBuilder.h>

static CBaseWorker main_worker;

class CMediaServer : public CBaseServer
{
public:
    CMediaServer(const char *name, CBaseWorker *worker = nullptr);

    void onOnline(FdbSessionId_t sid, bool is_first);
    
    void onOffline(FdbSessionId_t sid, bool is_last) {}

    /* called when client calls invoke() */
    void onInvoke(CBaseJob::Ptr &msg_ref);

    /* called when client call subscribe() to register message */
    void onSubscribe(CBaseJob::Ptr &msg_ref);
};

#endif ///__FDBSERVER_H__

client 头文件:

#ifndef __FDBCLIENT_H__ 
#define __FDBCLIENT_H__

#include "fdbDemo.pb.h"
#include <common_base/fdbus.h>
#include <common_base/CFdbProtoMsgBuilder.h>

static CBaseWorker main_worker;

class CMediaClient : public CBaseClient
{
public:
    CMediaClient(const char *name, CBaseWorker *worker = nullptr);

    /* called when connected to the server */
    void onOnline(FdbSessionId_t sid, bool isFirst);

    /* called when disconnected from server */
    void onOffline(FdbSessionId_t sid, bool isLast);

    /* called when events broadcasted from server is received */
    void onBroadcast(CBaseJob::Ptr &msgRef);
};
#endif ///__FDBCLIENT_H__

 Server和Client端把自己需要的几个基本函数都列出来,然后实现。

具体实现可以参考fdbus的example。

最后贴上我这边工程目录及CMakeList

#cmake最小需要版本
cmake_minimum_required(VERSION 3.0)
#项目名字
project(fdbDemo)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/fdbus/include
                    ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/protobuf/include
                    ${CMAKE_CURRENT_SOURCE_DIR}/proto)

aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/proto PROTO)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/source/fdbserver FDBSERVER)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/source/fdbclient FDBCLIENT)

LINK_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/fdbus/lib
                 ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/protobuf/lib)

set(LIB_LIST protobuf common_base pthread)

#编译参数
set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS} -O2 -s")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -g -Wall -O2 -s")

ADD_EXECUTABLE(fdbServer ${FDBSERVER} ${PROTO})
TARGET_LINK_LIBRARIES(fdbServer ${LIB_LIST})

ADD_EXECUTABLE(fdbClient ${FDBCLIENT} ${PROTO})
TARGET_LINK_LIBRARIES(fdbClient ${LIB_LIST})

有以下注意点:

1. fdbus和protobuf尽量不要直接装在ubuntu环境里,建一个install文件夹,装进去,工程建立需要就去相应的路径找

2. 把proto文件生成的 cc文件也要编译到工程执行文件里。不编译的话,会报一堆 undefined reference 的错误。

2. 注意写CMakeList的时候,链接protobuf库 不要链接protobuf-lite,否则,invoke或broadcast builder的时候会报错。(不知道是否有解决办法,我这边换成用protobuf去链接,就没有任何错误了)

  • 8
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值