fdbus examples 分析-- fdb_test_server.cpp

fdbus examples 分析-- fdb_test_server.cpp

PlantUML在线工具

0. overview

server 流程:

  • 开启 fdbus context 线程
  • 开启 worker 线程
  • 创建servers 并且绑定地址

1. fdb_test_server.cpp 分析

1.1 开启 fdbus context 线程

代码

/* start fdbus context thread */    
FDB_CONTEXT->start();

具体分析

FDB_CONTEXT是宏定义,如下:

// 定义在:`fdbus/public/common_base/CFdbContext.h`
#define FDB_CONTEXT CFdbContext::getInstance()
...
class CFdbContext : public CBaseWorker
{
};
  • CFdbContext‵ 是CBaseWorker` 的派生类
// fdbus/public/common_base/CBaseWorker.h
class CBaseWorker : public CBaseThread
{
};
  • CBaseWorkerCBaseThread 的派生类

    fdbus examples 分析-- fdb_test_server.cpp_第1张图片

1.2 开启 worker 线程

static CBaseWorker main_worker;
int main(int argc, char **argv)
{
    ...
    CBaseWorker *worker_ptr = &main_worker;
    /* start worker thread */
    worker_ptr->start();
    ...
}

1.3 创建servers 并且绑定地址

代码片段

/* create servers and bind the address: svc://service_name */        
for (int i = 1; i < argc; ++i)                                       
{                                                                    
    std::string server_name = argv[i];                               
    std::string url(FDB_URL_SVC);                                    
    url += server_name;                                              
    server_name += "_server";                                        
    auto server = new CMediaServer(server_name.c_str(), worker_ptr); 
    server->enableWatchdog(true);                                    
    server->enableUDP(true);                                         
    server->bind(url.c_str());                                       
}                                                                    

具体分析

  • 传入的参数作为server 的名字;
  • FDB_URL_SVC 宏展开后是: svc://
  • server 地址的‵是: svc://service_name
  • server_name 是在传入的参数后追加_server
  • 自己定义的具体 server 类继承CBaseServer, 构造函数以 nameCBaseWorker 指针作为参数
  • 实例化具体的server类后,依次调用类成员函数: enableWatchdog(), enab leUDP, bind()

CBaseServer 的类继承关系:
fdbus examples 分析-- fdb_test_server.cpp_第2张图片

1.4 main thread 转换为worker

/* convert main thread into worker */            
CBaseWorker background_worker;                   
background_worker.start(FDB_WORKER_EXE_IN_PLACE);

//no worker thread will be created; the main loop is running at the thread calling start()

你可能感兴趣的:(Linux,IPC,通信方案,fdbus)