这部分代码是用于客户端请求服务器,它由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");
}