TProcessor类体系属于Processor层,是协议层与用户实现的服务代码的纽带,它由基类TProcessor和thrift compiler生成的Processor代码组成。
例如:thrift文件
/**
* gateway service definition.
*/
service GatewayService
{
i32 GetCompereCount( 1:i32 channel_id ),
list< i32 > GetCompereList( 1:i32 channel_id,
2:i32 from, 3:i32 range )
}
由thrift compiler生成对应的Processor类为:
class GatewayServiceProcessor : public ::apache::thrift::TProcessor {
protected:
boost::shared_ptr<GatewayServiceIf> iface_;
virtual bool process_fn(apache::thrift::protocol::TProtocol* iprot, apache::thrift::protocol::TProtocol* oprot, std::string& fname, int32_t seqid, void* callContext);
private:
std::map<std::string, void (GatewayServiceProcessor::*)(int32_t, apache::thrift::protocol::TProtocol*, apache::thrift::protocol::TProtocol*, void*)> processMap_;
void process_GetCompereCount(int32_t seqid, apache::thrift::protocol::TProtocol* iprot, apache::thrift::protocol::TProtocol* oprot, void* callContext);
void process_GetCompereList(int32_t seqid, apache::thrift::protocol::TProtocol* iprot, apache::thrift::protocol::TProtocol* oprot, void* callContext);
GatewayServiceProcessor(boost::shared_ptr<GatewayServiceIf> iface) :
iface_(iface) {
processMap_["GetCompereCount"] = &GatewayServiceProcessor::process_GetCompereCount;
processMap_["GetCompereList"] = &GatewayServiceProcessor::process_GetCompereList;
}
virtual bool process(boost::shared_ptr<apache::thrift::protocol::TProtocol> piprot, boost::shared_ptr<apache::thrift::protocol::TProtocol> poprot, void* callContext);
virtual ~GatewayServiceProcessor() {}
}
其中:
(1)boost::shared_ptr<GatewayServiceIf> iface_用于保存用户实现的服务对象;
(2)process_fn()函数根据函数名来查找调用processMap_对应的函数,并调用之,该函数在Server类中被调用;
(3)processMap_保存该对象的所有处理成员函数,如process_GetCompereCount函数;
(4)处理成员函数,类的核心部分,这类函数首先从网络I/O读取参数,然后传给用户实现的函数接口并将返回值写回网络I/O,
成员函数例子:
void GatewayServiceProcessor::process_GetCompereList(int32_t seqid, ::apache::thrift::protocol::TProtocol* iprot, ::apache::thrift::protocol::TProtocol* oprot, void* callContext)
{
// 获取上下文
void* ctx = NULL;
if (this->eventHandler_.get() != NULL) {
ctx = this->eventHandler_->getContext("GatewayService.GetCompereList", callContext);
}
apache::thrift::TProcessorContextFreer freer(this->eventHandler_.get(), ctx, "GatewayService.GetCompereList");
// 读数据之前的操作
if (this->eventHandler_.get() != NULL) {
this->eventHandler_->preRead(ctx, "GatewayService.GetCompereList");
}
// 读参数
GatewayService_GetCompereList_args args;
args.read(iprot);
iprot->readMessageEnd();
uint32_t bytes = iprot->getTransport()->readEnd();
// 读完数据之后的操作
if (this->eventHandler_.get() != NULL) {
this->eventHandler_->postRead(ctx, "GatewayService.GetCompereList", bytes);
}
// 调用用户实现的服务接口GetCompereList
GatewayService_GetCompereList_result result;
try {
iface_->GetCompereList(result.success, args.channel_id, args.from, args.range);
result.__isset.success = true;
} catch ( ::yy::comperedb::GatewayServiceException &err) {
result.err = err;
result.__isset.err = true;
} catch (const std::exception& e) {
if (this->eventHandler_.get() != NULL) {
this->eventHandler_->handlerError(ctx, "GatewayService.GetCompereList");
}
apache::thrift::TApplicationException x(e.what());
oprot->writeMessageBegin("GetCompereList", apache::thrift::protocol::T_EXCEPTION, seqid);
x.write(oprot);
oprot->writeMessageEnd();
oprot->getTransport()->writeEnd();
oprot->getTransport()->flush();
return;
}
// 写返回值前的操作
if (this->eventHandler_.get() != NULL) {
this->eventHandler_->preWrite(ctx, "GatewayService.GetCompereList");
}
// 写返回值
oprot->writeMessageBegin("GetCompereList", apache::thrift::protocol::T_REPLY, seqid);
result.write(oprot);
oprot->writeMessageEnd();
bytes = oprot->getTransport()->writeEnd();
oprot->getTransport()->flush();
// 写返回值后的操作
if (this->eventHandler_.get() != NULL) {
this->eventHandler_->postWrite(ctx, "GatewayService.GetCompereList", bytes);
}
}