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.