Thrift Tprotocol Class System principle and source code detailed: Binary Protocol class Tbinaryprotocolt (Tbinaryprotocol)
This protocol is the default binary protocol supported by thrift, which writes all the data in a binary format and basically sends the original data directly. Because it inherits directly from the Tvirtualprotocol class, it is a template class. The template parameter is a class that encapsulates the delivery of a specific transmission, and this class is the one that truly implements the data transfer. The previous section of the definition of this class has already appeared and is not listed.
Below I will combine the scribe log function to perform the specific process to analyze the functions performed using this protocol to see how the binary protocol works.
RPC calls are used to the protocol part primarily in sending function-related information to the server and the receiving server to return results. Now I will combine the log function of the implementation code specific analysis. First look at the log function to send the relevant information function Send_log (in the file scribe.cpp):
void Scribeclient::send_log (const std::vector<logentry> &
Messages)
{
int32_t cseqid = 0;
Oprot_->writemessagebegin ("Log",:: Apache::thrift::p rotocol::t_call, cseqid);/write function call message
Scribe_log_pargs args;
Args.messages = &messages;
Args.write (oprot_);//Call parameter class own write function write parameter to server
oprot_->writemessageend (); Write message call write to
oprot_-> Gettransport ()->writeend ()//end of the transfer layer writes
Oprot_->gettransport ()->flush ();//flushes the transfer stream so that writes are executed immediately because the RPC calls
need to get results now
}
From the above code can be seen: first call the specific protocol of the Writemessagebegin function, of course, we analyze the binary protocol, then look at the binary protocol the implementation of this function, the code is as follows:
Template <class transport_>
uint32_t tbinaryprotocolt<transport_>::writemessagebegin (const
std::string& name,
const tmessagetype messagetype, const
int32_t seqid) {
if (this->strict_write_) {//Determine if you want to force write version number
int32_t (version_1) | ((int32_t) messagetype)//This number is the protocol number and message
type with the result
uint32_t wsize = 0;//record Write length
wsize + = writeI32 (version); Write version number
wsize + = writestring (name),//write message name, this is the function name log
wsize + = writeI32 (seqid);//write call serial number return
wsize;// Returns the write length
} else {
uint32_t wsize = 0;
Wsize + = writestring (name);
Wsize + = WriteByte ((int8_t) messagetype);
Wsize + + writeI32 (seqid);
return wsize
}
}