thrift 源码剖析1 :TProcessor

本文深入剖析Thrift的TProcessor层,解释其如何与Handler协同工作。Handler作为应用层,通常只需实现服务逻辑。Processor通过调用逻辑流,根据RPC传递的函数名在ProcessMap中查找并执行对应方法,执行业务处理并返回结果。
摘要由CSDN通过智能技术生成

TProcessor

  这层主要负责应用层也就是需要我们平常自己实现的一层,它里面封装了Handler类。一般thrift 生成的代码中我们只需要负责写Handler类的逻辑即可,Handler中的逻辑就是我们自己定义的服务逻辑。

分析 demo
Service Serv {
    string  put(1:i32 value) ,
}
Handler

在这里插入图片描述
在这里插入图片描述
  可以看到Handler 类继承了ServIf 类,thrift 是通过c++中Base类指针可以指向Dervied类对象,从而实现通过 Processor来操作我们自己实现的Handler类。

Processor
thrift 内部文件代码:
class TProcessorEventHandler {
   
public:
  virtual ~TProcessorEventHandler() {
   }

  /**
   * Called before calling other callback methods.
   * Expected to return some sort of context object.
   * The return value is passed to all other callbacks
   * for that function invocation.
   */
  virtual void* getContext(const char* fn_name, void* serverContext) {
   
    (void)fn_name;
    (void)serverContext;
    return NULL;
  }

  /**
   * Expected to free resources associated with a context.
   */
  virtual void freeContext(void* ctx, const char* fn_name) {
   
    (void)ctx;
    (void)fn_name;
  }

  /**
   * Called before reading arguments.
   */
  virtual void preRead(void* ctx, const char* fn_name) {
   
    (void)ctx;
    (void)fn_name;
  }

  /**
   * Called between reading arguments and calling the handler.
   */
  virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
   
    (void)ctx;
    (void)fn_name;
    (void)bytes;
  }

  /**
   * Called between calling the handler and writing the response.
   */
  virtual void preWrite(void* ctx, const char* fn_name) {
   
    (void)ctx;
    (void)fn_name;
  }

  /**
   * Called after writing the response.
   */
  virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
   
    (void)ctx;
    (void)fn_name;
    (void)bytes;
  }

  /**
   * Called when an async function call completes successfully.
   */
  virtual void asyncComplete(void* ctx, const char* fn_name) {
   
    (void)ctx;
    (void)fn_name;
  }

  /**
   * Called if the handler throws an undeclared exception.
   */
  virtual void handlerError(void* ctx, const char* fn_name) {
   
    (void)ctx;
    (void)fn_name;
  }

protected:
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值