Thrift Tprotocol Class System principle and source code detailed: Compact Protocol class Tcompactprotocolt (tcom

Source: Internet
Author: User
Tags compact integer

Thrift Tprotocol Class System principle and source code detailed: Compact Protocol class Tcompactprotocolt (Tcompactprotocol)

This protocol class uses zigzag encoding, which is based on the Variable-length quantity encoding, because the Variable-length quantity encoding requires a very long number of bytes for the encoding of negative numbers, while zigzag Code for the absolute value of small numbers, both positive and negative can be expressed in less bytes, the full use of varint technology. So this protocol class uses zigzag coding to save the transmission space and make the data transmission more efficient. As for the zigzag specific coding implementation can be online search, in fact, from the low to the last one still exists 1 (binary system) of the highest bit to express it. This Protocol class provides a method that is the same as the binary protocol described above, so that users can easily change from one protocol to another.

Below I also combine the log method provided by scribe to analyze the function of this Protocol class, but not like the above binary protocol in the whole process analysis, I will only analyze the relevant parts of the protocol, analysis of some of the more difficult to understand some function functions, analysis of the idea or according to the function call process to analyze.

First, the Writemessagebegin function is parsed, and the following is the implementation code for this function:

Template <class transport_> uint32_t 

tcompactprotocolt<transport_>::writemessagebegin (
    
    const std::string& name, const tmessagetype messagetype, const int32_t 

seqid) {uint32_t
    
  wsize = 0;
    
  Wsize + = WriteByte (protocol_id)//product ID number written to this protocol: 0x82
    
  wsize + + writebyte (version_n & Version_mask) | (((int32_t) messagetype 

<< type_shift_amount) & Type_mask);//The version number and message type written to this protocol: the first 3

bits are the message types, The following 5 bits are protocol version number
    
  wsize + = WriteVarint32 (seqid);/write request serial number
    
  Wsize + + = writestring (name);//write message name (that is, function call name)
    
  Return wsize;//returns the size of the write, how many bytes
    
}

Because these protocol classes are template classes, each function is also a template function. The function's specific function code has a detailed annotation, where the WriteByte function is to write a byte to the server. Here, unlike the binary protocol, this is the WriteVarint32 function that is invoked here to write the request sequence number (that is, for all integers), which is to write the integer number to the server using the zigzag encoding, as follows:

Template <class transport_> uint32_t 

tcompactprotocolt<transport_>::writevarint32 (uint32_t N) {
    
  uint8_t buf[5];//for an integer, the zigzag encoding has a maximum of 5 bytes to save
    
  uint32_t wsize = 0;
    
  while (true) {
    
    if ((n & ~0x7f) = = 0) {//To determine if there are other high levels of 1 (binary) buf[wsize++] = (int8_t) In addition to the minimum 7 digits
    
      n;//No representation is the last byte 
  break;//exit Loop
    
    } else {
    
      buf[wsize++] = (int8_t) ((N & 0x7F) | 0x80);//Take the minimum 7 digits plus the 8th bit (for the 1 generation

table followed by bytes that belong to this integer, For 0 represents this is the last byte of this integer.
    
      N >>= 7;//Remove the number of digits that have been encoded
    
    }
    
  }
    
  Trans_->write (buf, wsize),//write encoded number of bytes
    
  return wsize;//Returns the number of bytes written
    
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.