Thrift Ttransport Class System principle and source code detailed analysis 1-class structure and abstract base class

Source: Internet
Author: User
Tags abstract inheritance

This chapter mainly introduces the Thrift Transport Layer function realization, the transmission way is various, may use the compression, the frame and so on, but these function realization all is mutually independent, and in the chapter introduced the Protocol class realization Way is similar, or first take a look at this part of Class diagram, as follows:

The above class diagram shows that this part of the function is quite powerful, so the class is more and the relationship is complex. But if you understand the direct relationship of these classes, it is easy to grasp this part of the implementation technology and this part of the implementation of the function. We have divided this class diagram into three parts, the first part looks at the abstract base class Ttransport class, which is the base class for all transport classes, where a large portion of the class inherits directly from it the implementation of the interface functions it provides or defines (pure virtual functions), and these transport classes are simpler to implement than single implementations The second part is the default implementation of the Ttransport abstract class and the corresponding subclass as well as the addition of a layer of virtual transport class, this transport virtual class of class inheritance framework of the previous chapter of the Protocol class virtual inheritance framework is the same implementation and technology, so that implementation can avoid the use of virtual inheritance method, Because the efficiency of virtual inheritance is lower, virtual inheritance needs dynamic binding technology (runtime to find and specify specific implementation); The third part is a variety of transfer class object generation factory class, responsible for a specific transmission class object production.

First section abstract base class Ttransport

This section describes an abstract overview of the implementation of the entire transport layer, and the interfaces implemented by the Transport layer are defined in this abstract base class. What are the interfaces that the transport layer implements?

1. The global template function of the auxiliary transport Layer ReadAll

Before starting to analyze the interface of the transport layer, look at an auxiliary template function ReadAll, which is defined and implemented as follows:

Template 

<class transport_>
uint32_t readall (Transport_ &trans, uint8_t* buf, uint32_t len) {
  uint32_t have = 0;
  uint32_t get = 0;
  while (have < Len) {get
    = Trans.read (Buf+have, len-have);//Read the remaining data that needs to be read through the specific transport class
    if (get <= 0) {//Process data to read the exception
      throw ttransportexception (Ttransportexception::end_of_file, "No more data to read.");
    }
    have + = get;//number of bytes read
  }
    
  return have;//returned bytes read
}

The reality of this function is relatively simple, that is, a while loop to ensure that the number of bytes to read can be read, if the number of bytes is not enough to throw an exception.

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.