如何把thrift服务端handler作为动态库使用?

发布于 2022-09-12 01:14:40 字数 3505 浏览 24 评论 0

我把thrift服务端handler作为DLL导出,然后我写了一个动态添加服务的主程序,用户输入动态库路径,主程序有个线程会把服务动态库handler注册到全局变量thrift的TMultiplexedProcessor(TMultiplexedProcessor->registerProcessor),但是当客户端访问时却提示服务未注册!请问大神们哪里出了问题?该如何动态添加服务?我的代码如下


==========================================================================================

int addUserServiceDLLFun(std::string &serviceName,std::string &dllName){
    
    HINSTANCE hdll;
    hdll=LoadLibrary(dllName.c_str()); 
    std::cout<<"hdll:"<<hdll<<std::endl;
    if(hdll==NULL){
        FreeLibrary(hdll);
    }
    PointerDllServiceFun pdllServiceFun;
    pdllServiceFun = (PointerDllServiceFun)GetProcAddress(hdll,"getTProcessorHandler");//获取函数地址
    std::cout<<"pdllServiceFun:"<<pdllServiceFun<<std::endl;
    if(pdllServiceFun==NULL){
        FreeLibrary(hdll);
        return -1;
    }
    boost::shared_ptr<TProcessor> tprocessor=pdllServiceFun();
    
    globeProcessor->registerProcessor(serviceName,tprocessor);

    return 1;
}
==========================================================================================
extern "C" _declspec(dllexport) boost::shared_ptr<TProcessor> getTProcessorHandler(){
    boost::shared_ptr<TProcessor> processor(new MyHelloServiceJellyProcessor(boost::shared_ptr<MyHelloServiceJellyHandler>(new MyHelloServiceJellyHandler())));
    return processor;
}
==========================================================================================
void* loadUserServiceDLLFun(void* args){
    std::string dllPath;
    while (true){

        std::cin>>dllPath;

        string::size_type iPos = dllPath.find_last_of('\\')+1;
        cout<< dllPath <<endl;

        string filename = dllPath.substr(iPos, dllPath.length() - iPos);
        cout << filename << endl;

        
        string name = filename.substr(0, filename.rfind("."));
        cout << name << endl;

        
        string suffix_str = filename.substr(filename.find_last_of('.') + 1);
        cout << suffix_str << endl;

        addUserServiceDLLFun(filename,dllPath);
    }
}
==========================================================================================
//main.cpp

    pthread_t loadUserDllThread;
    int ret = pthread_create(&loadUserDllThread, NULL,loadUserServiceDLLFun, NULL);

    if (ret != 0){
        cout << "pthread_create error: error_code=" << ret << endl;
    }

    boost::shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
    boost::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
    boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

    boost::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount);
    boost::shared_ptr<BoostThreadFactory> threadFactory = boost::shared_ptr<BoostThreadFactory>(new BoostThreadFactory());
    threadManager->threadFactory(threadFactory);
    threadManager->start();

    TThreadPoolServer server(globeProcessor,
                             serverTransport,
                             transportFactory,
                             protocolFactory,
                             threadManager);
    server.serve();
==========================================================================================
!!服务端报错 !!

Do you forget to call registerProcessor()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文