Apache Thrift C++库的TThreadPoolServer模式的完整示例

这篇具有很好参考价值的文章主要介绍了Apache Thrift C++库的TThreadPoolServer模式的完整示例。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1. 本程序功能 

1) 要有完整的request  和 response;

2) 支持多进程并行处理任务;

3)子进程任务结束后无僵尸进程

2.Apache Thrift C++库的编译和安装

步步详解:Apache Thrift C++库从编译到工作模式DEMO_北雨南萍的博客-CSDN博客

3.框架生成

数据字段定义:

cat Datainfo.thrift

# Datainfo.thrift

struct message  
{  
  1:i32 seqId,  
  2:string content  
}  
  
struct response
{
  1:i32 seqId,
  2:string content
}

service serDemo  
{  
  response put(1:message msg)  

thrift -gen cpp Datainfo.thrift

4.Server端源码

serDemo_server.skeleton.cpp

// This autogenerated skeleton file illustrates how to build a server.
// You should copy it to another filename to avoid overwriting it.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#include <iostream>
#include <stdexcept>
#include <thread>

#include "serDemo.h"

#include <thrift/concurrency/ThreadManager.h>
#include <thrift/concurrency/ThreadFactory.h>
#include <thrift/concurrency/ThreadManager.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/server/TThreadPoolServer.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>

using namespace ::apache::thrift;
using namespace ::apache::thrift::concurrency;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;


void handler(int num) {
    //我接受到了SIGCHLD的信号啦
    int status;
    int pid = waitpid(-1,&status,WNOHANG);
    if(WIFEXITED(status)) {
        printf("The child exit with code %d\n",WEXITSTATUS(status));
    }                         
}

class serDemoHandler : virtual public serDemoIf {
 public:
  serDemoHandler() {
    // 构造函数:用于初始任务处理句柄对象
  }

  void put(response& _return, const message& msg) {
    // Your implementation goes here
    printf("put starting ...\n");
    printf("receive message: id: %d, content: %s\n", msg.seqId, msg.content.c_str());
    char* ffmpeg_argv[]={"/ffmpeg",
                   "-i",
                   "/opt/videoroom-1234-user-161756615651626-1615963571436156-video.mp4",
                   "-t",
                   "60",
                   "-vcodec",
                   "libx264",
                   "-f",
                   "flv",
                   "-y",
                   "/opt/videoroom-1234-user-161756615651626-1615963571436156-video.flv",
		   NULL};
   
    printf("ffmpeg_argv:\n");
    for (int i = 0; ffmpeg_argv[i] != NULL; i++) {
        printf("%s ", ffmpeg_argv[i]);
    }
    printf("\n");

    pid_t pid = fork();
    if (pid == 0) {
        execvp(ffmpeg_argv[0], ffmpeg_argv);
	exit(0);
    } else if (pid > 0) {
        printf("This is the parent process\n");
    } else {
        printf("Fork failed\n");
    }

    // 回复消息
    _return.seqId     = msg.seqId;
    _return.content   = "This is the response message.";

    printf("put stopped.\n");
  }

};

int main(int argc, char **argv) {
  int port = 19090;
  signal(SIGCHLD,handler);
  /*** TThreadPoolServer工作模式 ***/
  try {
    //创建一个线程管理器
    std::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(16);
    std::shared_ptr<ThreadFactory> threadFactory = std::shared_ptr<ThreadFactory>(new ThreadFactory());
    threadManager->threadFactory(threadFactory);
    threadManager->start();

    std::shared_ptr<serDemoHandler> handler(new serDemoHandler());
    std::shared_ptr<TProcessor>     processor(new serDemoProcessor(handler));

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

    TThreadPoolServer server(processor, 
                             serverTransport, 
                             transportFactory, 
                             protocolFactory,
                             threadManager);

    // 启动服务
    ::std::cout << "Starting the TThreadPoolServer ..." << std::endl;
    server.serve();
    ::std::cout << "Server stopped." << std::endl;

  } catch (std::exception& e) {
    std::cerr << "Error: " << e.what() << std::endl;
  }

  return 0;
}

编译生成server

g++ -std=c++11 -g -Wall  Datainfo_types.cpp serDemo.cpp serDemo_server.skeleton.cpp -o server -lthrift -lpthread
 

5. client端源码

client.cpp

// 在同级目录下创建 client.cpp 文件
// ----------替换成自己的头文件----------
#include "serDemo.h"
// --------------------------------------
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/protocol/TBinaryProtocol.h>

using namespace apache::thrift;   
using namespace apache::thrift::protocol;   
using namespace apache::thrift::transport;   
   
using boost::shared_ptr;   
   
int main(int argc, char **argv) {   
  /* 不能用localhost, 否则会有运行时提示:"
   * TSocket::open() connect() <Host: localhost Port: 19090>: Connection refused
   */
  //std::shared_ptr<TSocket> socket(new TSocket("localhost", 19090));   
  std::shared_ptr<TSocket> socket(new TSocket("127.0.0.1", 19090));   
  std::shared_ptr<TTransport> transport(new TBufferedTransport(socket));   
  std::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));

  serDemoClient client(protocol);   
   
  transport->open();   
   
  // ----------------------------我们的代码写在这里------------------------------  
  message msg;  
  msg.seqId = 1;  
  msg.content = "client message";    
 
  response response_put;
  client.put(response_put, msg);  

  printf("seqId: %d, content: %s\n", response_put.seqId, response_put.content.c_str());
  //--------------------------------------------------------------------------  
  
  transport->close();   
   
  return 0;   
}  

编译生成client

g++ -std=c++11 -g -Wall  Datainfo_types.cpp serDemo.cpp client.cpp -o client -lthrift -lpthread文章来源地址https://www.toymoban.com/news/detail-626714.html

到了这里,关于Apache Thrift C++库的TThreadPoolServer模式的完整示例的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • (三)行为模式:2、命令模式(Command Pattern)(C++示例)

    目录 1、命令模式(Command Pattern)含义 2、命令模式的UML图学习 3、命令模式的应用场景 4、命令模式的优缺点 5、C++实现命令模式的实例 1、命令模式(Command Pattern)含义 命令模式(Command),将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排

    2024年02月12日
    浏览(28)
  • 【C++设计模式之装饰模式:结构型】分析及示例

    装饰模式(Decorator Pattern)是一种结构型设计模式,它允许在运行时动态地给一个对象添加额外的行为。 描述 装饰模式通过创建一个包装器(Wrapper)来包裹原始对象,并在原始对象的行为前后添加额外的功能。通过这种方式,可以实现在不改变原始对象结构的情况下,动态

    2024年02月07日
    浏览(28)
  • (三)行为模式:4、迭代器模式(Iterator Pattern)(C++示例)

    目录 1、迭代器模式(Iterator Pattern)含义 2、迭代器模式的UML图学习 3、迭代器模式的应用场景 4、迭代器模式的优缺点 (1)优点 (2)缺点 5、C++实现迭代器模式的实例 1、迭代器模式(Iterator Pattern)含义 迭代器模式(Iterator),提供一种方法顺序访问一个聚合对象中各个元

    2024年02月11日
    浏览(25)
  • (三)行为模式:6、备忘录模式(Memento Pattern)(C++示例)

    目录 1、备忘录模式(Memento Pattern)含义 2、备忘录模式的UML图学习 3、备忘录模式的应用场景 4、备忘录模式的优缺点 (1)优点: (2)缺点 5、C++实现备忘录模式的实例 1、备忘录模式(Memento Pattern)含义 备忘录(Memento):在不破坏封装性的前提下,捕获一个对象的内部状

    2024年02月10日
    浏览(33)
  • (二)结构型模式:8、代理模式(Proxy Pattern)(C++示例)

    目录 1、代理模式(Proxy Pattern)含义 2、代理模式的UML图学习 3、代理模式的应用场景  4、代理模式的优缺点 5、C++实现代理模式的实例 1、代理模式(Proxy Pattern)含义 代理模式(Proxy),为其他对象提供一种代理以控制对这个对象的访问。 代理模式(Proxy Pattern)是一种结构

    2024年02月12日
    浏览(30)
  • (三)行为模式:7、观察者模式(Observer Pattern)(C++示例)

    目录 1、观察者模式(Observer Pattern)含义 2、观察者模式的UML图学习 3、观察者模式的应用场景 4、观察者模式的优缺点 (1)优点: (2)缺点 5、C++实现观察者模式的实例 1、观察者模式(Observer Pattern)含义 观察者模式(Observer)定义了一种一对多的依赖关系,让多个观察者

    2024年02月09日
    浏览(32)
  • 设计模式(四) —— 观察者模式/发布订阅模式,c和c++示例代码

    往期地址: 设计模式(一)——简单工厂模式 设计模式(二)——策略模式 设计模式(三)——装饰模式 本期主题: 使用c和c++代码,讲解观察者模式、发布订阅模式 发布-订阅模式是一种行为设计模式,它允许多个对象通过事件的发布和订阅来进行通信; 在这种模式中,

    2023年04月17日
    浏览(25)
  • (三)行为型模式:3、解释器模式(Interpreter Pattern)(C++示例)

    目录 1、解释器模式(Interpreter Pattern)含义 2、解释器模式的UML图学习 3、解释器模式的应用场景 4、解释器模式的优缺点 5、C++实现解释器模式的实例 1、解释器模式(Interpreter Pattern)含义 解释器模式(Interpreter Pattern),给定一个语言,定义它的方法的一种表示,并定义一个

    2024年02月12日
    浏览(38)
  • 【C++】运算符重载 ⑩ ( 下标 [] 运算符重载 | 函数原型 int& operator[](int i) | 完整代码示例 )

    在之前的博客 【C++】面向对象示例 - 数组类 ( 示例需求 | 创建封装类 | 数组类头文件 Array.h | 数组类实现 Array.cpp | 测试类 Test.cpp - 主函数入口 ) 中 , 实现了一个数组类 , 在一个类中 , 封装了一个 int 类型的指针 , 该指针指向堆内存的 内存空间 , 用于存放一个数组 ; 核心是 2

    2024年02月07日
    浏览(29)
  • 一文带你通俗理解23种软件设计模式(推荐收藏,适合小白学习,附带C++例程完整源码)

    作者:翟天保Steven 版权声明:著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处        设计模式是为了解决在软件开发过程中遇到的某些问题而形成的思想。同一场景有多种设计模式可以应用,不同的模式有各自的优缺点,开发者可以基于自身需求

    2024年02月09日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包