Thrift's Tprotocol class system principle and source code detailed: Class Inheritance architecture analysis

Source: Internet
Author: User
Tags abstract inheritance

This part of the related class mainly to implement the content of the Protocol, the Protocol here refers to the data transmission format of the Protocol, the implementation of different protocols to adapt to different scenarios of data transmission, because in different scenarios for the data transmission of different protocols is a great difference in efficiency. The following is a class diagram of the related classes in this section:

The above class diagram shows that all protocol classes are inherited directly or indirectly from the Tprotocol class, and each protocol class has a corresponding production object Factory (protocol factory). Tprotocol is an abstract class that cannot be used directly, and it has a direct subclass that implements all of the methods (NULL implementations), if we need to define our own data transfer protocol that can be inherited directly from this class.

Section one class inheritance schema analysis

Why do we need to analyze this part of the Class inheritance architecture? Isn't there a clear class inheritance diagram up there? But Facebook is not simply inheriting it when it comes to implementation, and Facebook has an added layer of inheritance for the scalability of the post protocol and for allowing other organizations, teams, or individuals to implement their own data transfer (primarily in data format encapsulation). is the class diagram of the Tvirtualprotocol class, from the name of the class can be seen that this is a virtual protocol. How to understand this virtual agreement? By reading the code I think I can understand this: because it is defined as a template class, the template class has two parameters, a real protocol for data transmission, one is used to inherit, it does not implement the specific content of the protocol, so it is a virtual protocol class. The following is a detailed analysis of this class inheritance architecture combined with code implementation.

1 abstract class Tprotocol and default implementation class Tprotocoldefaults

Abstract class for each data type provides the beginning and the introduction of reading and writing methods, where the read and write method should be for network IO Read and write, but the real realization of network reading and writing is not the method here, where the method mainly processing data, such as the data format adjustment. The true realization of network IO reading and writing is implemented by the Ttransport related classes introduced in the next chapter, where there is also a corresponding control over the way of transmission, such as whether or not to compress.

In addition to the specific data types have write and read methods, the message is also required to pass through the network, so also defines the message transmission and read-write methods. Of course, some common functions are defined, such as skipping a certain structure, resizing and resizing of data, host byte order and network byte-order conversion.

(1) First define pure virtual functions:

Virtual uint32_t writemessagebegin_virt (const 

std::string& name,
    
                                  const Tmessagetype MessageType,  Const int32_t 

seqid) = 0;
    
  Virtual uint32_t writemessageend_virt () = 0;
    
  Virtual uint32_t writestructbegin_virt (const char* name) = 0;
    
  Virtual uint32_t writestructend_virt () = 0;

(2) Then define the function that calls the corresponding pure virtual function:

uint32_t writemessagebegin (const std::string& name, const tmessagetype 

messagetype, const int32_t seqid) {
    
   T_virtual_call ()//Print Call log function return
    
   writemessagebegin_virt (name, MessageType, seqid);
    
 uint32_t writemessageend () {
    
   t_virtual_call ();
    
   return Writemessageend_virt ();
    
 }
    
 uint32_t writestructbegin (const char* name) {//write structure begins
    
   t_virtual_call ();
    
   return Writestructbegin_virt (name);
    
 uint32_t writestructend () {//write structure body end
    
   t_virtual_call ();
    
   return Writestructend_virt ();
    
 }

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.