thift源码研究-客户端代码分析
2013-01-22 21:02
405 查看
这部分代码是用于客户端请求服务器,它由thrift compiler自动生成。
使用与前一篇一样的例子: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自动生成的客户端代码:
class GatewayServiceClient : virtual public GatewayServiceIf {
public:
GatewayServiceClient(boost::shared_ptr< ::apache::thrift::protocol::TProtocol> prot) :
piprot_(prot),
poprot_(prot) {
iprot_ = prot.get();
oprot_ = prot.get();
}
GatewayServiceClient(boost::shared_ptr< ::apache::thrift::protocol::TProtocol> iprot, boost::shared_ptr< ::apache::thrift::protocol::TProtocol> oprot) :
piprot_(iprot),
poprot_(oprot) {
iprot_ = iprot.get();
oprot_ = oprot.get();
}
boost::shared_ptr< ::apache::thrift::protocol::TProtocol> getInputProtocol() {
return piprot_;
}
boost::shared_ptr< ::apache::thrift::protocol::TProtocol> getOutputProtocol() {
return poprot_;
}
int32_t GetCompereCount(const int32_t channel_id);
void send_GetCompereCount(const int32_t channel_id);
int32_t recv_GetCompereCount();
void GetCompereList(std::vector<int32_t> & _return, const int32_t channel_id, const int32_t from, const int32_t range);
void send_GetCompereList(const int32_t channel_id, const int32_t from, const int32_t range);
void recv_GetCompereList(std::vector<int32_t> & _return);
protected:
boost::shared_ptr< ::apache::thrift::protocol::TProtocol> piprot_;
boost::shared_ptr< ::apache::thrift::protocol::TProtocol> poprot_;
::apache::thrift::protocol::TProtocol* iprot_;
::apache::thrift::protocol::TProtocol* oprot_;
};
其中:
(1)int32_t GetCompereCount(const int32_t channel_id)和void
GetCompereList(std::vector<int32_t> & _return, const int32_t channel_id, const int32_t from, const int32_t range)函数供用户调用;
(2)上述两个函数都会调用对应send和rec函数,如GetCompereCount函数调用send_GetCompereCount和recv_GetCompereCount函数;
代码如下:
int32_t
GatewayServiceClient::GetCompereCount(const int32_t channel_id)
{
send_GetCompereCount(channel_id);
return recv_GetCompereCount();
}
(3)send函数,如send_GetCompereCount
代码如下:
void
GatewayServiceClient::send_GetCompereCount(const int32_t channel_id)
{
int32_t cseqid = 0;
oprot_->writeMessageBegin("GetCompereCount", ::apache::thrift::protocol::T_CALL, cseqid);
GatewayService_GetCompereCount_pargs args;
args.channel_id = &channel_id;
args.write(oprot_);
oprot_->writeMessageEnd();
oprot_->getTransport()->writeEnd();
oprot_->getTransport()->flush();
}
(4)rec函数,如recv_GetCompereCount
代码如下:
int32_t
GatewayServiceClient::recv_GetCompereCount()
{
int32_t rseqid = 0;
std::string fname;
::apache::thrift::protocol::TMessageType mtype;
iprot_->readMessageBegin(fname, mtype, rseqid);
if (mtype == ::apache::thrift::protocol::T_EXCEPTION) {
::apache::thrift::TApplicationException x;
x.read(iprot_);
iprot_->readMessageEnd();
iprot_->getTransport()->readEnd();
throw x;
}
if (mtype != ::apache::thrift::protocol::T_REPLY) {
iprot_->skip(::apache::thrift::protocol::T_STRUCT);
iprot_->readMessageEnd();
iprot_->getTransport()->readEnd();
}
if (fname.compare("GetCompereCount") != 0) {
iprot_->skip(::apache::thrift::protocol::T_STRUCT);
iprot_->readMessageEnd();
iprot_->getTransport()->readEnd();
}
int32_t _return;
GatewayService_GetCompereCount_presult result;
result.success = &_return;
result.read(iprot_);
iprot_->readMessageEnd();
iprot_->getTransport()->readEnd();
if (result.__isset.success) {
return _return;
}
if (result.__isset.err) {
throw result.err;
}
throw ::apache::thrift::TApplicationException(::apache::thrift::TApplicationException::MISSING_RESULT, "GetCompereCount failed: unknown result");
}
使用与前一篇一样的例子: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自动生成的客户端代码:
class GatewayServiceClient : virtual public GatewayServiceIf {
public:
GatewayServiceClient(boost::shared_ptr< ::apache::thrift::protocol::TProtocol> prot) :
piprot_(prot),
poprot_(prot) {
iprot_ = prot.get();
oprot_ = prot.get();
}
GatewayServiceClient(boost::shared_ptr< ::apache::thrift::protocol::TProtocol> iprot, boost::shared_ptr< ::apache::thrift::protocol::TProtocol> oprot) :
piprot_(iprot),
poprot_(oprot) {
iprot_ = iprot.get();
oprot_ = oprot.get();
}
boost::shared_ptr< ::apache::thrift::protocol::TProtocol> getInputProtocol() {
return piprot_;
}
boost::shared_ptr< ::apache::thrift::protocol::TProtocol> getOutputProtocol() {
return poprot_;
}
int32_t GetCompereCount(const int32_t channel_id);
void send_GetCompereCount(const int32_t channel_id);
int32_t recv_GetCompereCount();
void GetCompereList(std::vector<int32_t> & _return, const int32_t channel_id, const int32_t from, const int32_t range);
void send_GetCompereList(const int32_t channel_id, const int32_t from, const int32_t range);
void recv_GetCompereList(std::vector<int32_t> & _return);
protected:
boost::shared_ptr< ::apache::thrift::protocol::TProtocol> piprot_;
boost::shared_ptr< ::apache::thrift::protocol::TProtocol> poprot_;
::apache::thrift::protocol::TProtocol* iprot_;
::apache::thrift::protocol::TProtocol* oprot_;
};
其中:
(1)int32_t GetCompereCount(const int32_t channel_id)和void
GetCompereList(std::vector<int32_t> & _return, const int32_t channel_id, const int32_t from, const int32_t range)函数供用户调用;
(2)上述两个函数都会调用对应send和rec函数,如GetCompereCount函数调用send_GetCompereCount和recv_GetCompereCount函数;
代码如下:
int32_t
GatewayServiceClient::GetCompereCount(const int32_t channel_id)
{
send_GetCompereCount(channel_id);
return recv_GetCompereCount();
}
(3)send函数,如send_GetCompereCount
代码如下:
void
GatewayServiceClient::send_GetCompereCount(const int32_t channel_id)
{
int32_t cseqid = 0;
oprot_->writeMessageBegin("GetCompereCount", ::apache::thrift::protocol::T_CALL, cseqid);
GatewayService_GetCompereCount_pargs args;
args.channel_id = &channel_id;
args.write(oprot_);
oprot_->writeMessageEnd();
oprot_->getTransport()->writeEnd();
oprot_->getTransport()->flush();
}
(4)rec函数,如recv_GetCompereCount
代码如下:
int32_t
GatewayServiceClient::recv_GetCompereCount()
{
int32_t rseqid = 0;
std::string fname;
::apache::thrift::protocol::TMessageType mtype;
iprot_->readMessageBegin(fname, mtype, rseqid);
if (mtype == ::apache::thrift::protocol::T_EXCEPTION) {
::apache::thrift::TApplicationException x;
x.read(iprot_);
iprot_->readMessageEnd();
iprot_->getTransport()->readEnd();
throw x;
}
if (mtype != ::apache::thrift::protocol::T_REPLY) {
iprot_->skip(::apache::thrift::protocol::T_STRUCT);
iprot_->readMessageEnd();
iprot_->getTransport()->readEnd();
}
if (fname.compare("GetCompereCount") != 0) {
iprot_->skip(::apache::thrift::protocol::T_STRUCT);
iprot_->readMessageEnd();
iprot_->getTransport()->readEnd();
}
int32_t _return;
GatewayService_GetCompereCount_presult result;
result.success = &_return;
result.read(iprot_);
iprot_->readMessageEnd();
iprot_->getTransport()->readEnd();
if (result.__isset.success) {
return _return;
}
if (result.__isset.err) {
throw result.err;
}
throw ::apache::thrift::TApplicationException(::apache::thrift::TApplicationException::MISSING_RESULT, "GetCompereCount failed: unknown result");
}
相关文章推荐
- Nutch1.7源码再研究之---11 Fetch中的QueueFeeder线程代码分析
- caffe源码分析--Blob类代码研究
- caffe源码分析--SyncedMemory类代码研究
- thrift源码研究-TJSONProtocol代码分析
- caffe源码分析--SyncedMemory类代码研究
- caffe源码分析--Blob类代码研究
- butterknife源码分析:代码分析
- Tcpdump源码分析系列3:main函数继续研究
- HEVC码率控制算法研究与HM相应代码分析(一)——HEVC标准及编码流程介绍
- Irrlicht 0.1引擎源码分析与研究(一)
- mycat2.0源码分析02-客户端发送认证报文
- SpringMVC关于json、xml自动转换的原理研究[附带源码分析]
- hadoop源码解析之hdfs写数据全流程分析---客户端处理
- Redis源码研究—代码整体架构
- MySQL源码分析——代码结构与基本流程
- 传奇源码分析-客户端(游戏逻辑处理源分析一)
- zepto源码研究 - ajax.js($.ajax具体流程分析)
- 开源中国 OsChina Android 客户端源码分析(12)清理缓存
- BT客户端源码分析之一:总述
- ECClient红孩子android客户端源码分析之图片加载自动刷新listview