博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Thrift源码解析--transport
阅读量:6848 次
发布时间:2019-06-26

本文共 4502 字,大约阅读时间需要 15 分钟。

这一层主要是用于实现网络通信,现在都是基于Tcp/Ip,而Tcp/Ip协议栈由socket来实现,换句话说就是现在网络通信服务底层大都是通过socket实现的,在thrift源码中,就是将socket包装成各种transport来使用。

TTransport:这是一个基类,并且是一个抽象类。

TIOStreamTransport继承TTransport类,是最常用的base transport, It takes an InputStream and an OutputStream and uses those to perform all transport operations.主要是由inputStream和OutputStream进行读写操作,主要有open,close,read,write,flush等方法。代码如下:

1 public class TIOStreamTransport extends TTransport {  2   3   private static final Logger LOGGER = LoggerFactory.getLogger(TIOStreamTransport.class.getName());  4   5   /** Underlying inputStream */  6   protected InputStream inputStream_ = null;  7   8   /** Underlying outputStream */  9   protected OutputStream outputStream_ = null; 10  11   /** 12    * Subclasses can invoke the default constructor and then assign the input 13    * streams in the open method. 14    */ 15   protected TIOStreamTransport() {} 16  17   /** 18    * Input stream constructor. 19    * 20    * @param is Input stream to read from 21    */ 22   public TIOStreamTransport(InputStream is) { 23     inputStream_ = is; 24   } 25  26   /** 27    * Output stream constructor. 28    * 29    * @param os Output stream to read from 30    */ 31   public TIOStreamTransport(OutputStream os) { 32     outputStream_ = os; 33   } 34  35   /** 36    * Two-way stream constructor. 37    * 38    * @param is Input stream to read from 39    * @param os Output stream to read from 40    */ 41   public TIOStreamTransport(InputStream is, OutputStream os) { 42     inputStream_ = is; 43     outputStream_ = os; 44   } 45  46   /** 47    * The streams must already be open at construction time, so this should 48    * always return true. 49    * 50    * @return true 51    */ 52   public boolean isOpen() { 53     return true; 54   } 55  56   /** 57    * The streams must already be open. This method does nothing. 58    */ 59   public void open() throws TTransportException {} 60  61   /** 62    * Closes both the input and output streams. 63    */ 64   public void close() { 65     if (inputStream_ != null) { 66       try { 67         inputStream_.close(); 68       } catch (IOException iox) { 69         LOGGER.warn("Error closing input stream.", iox); 70       } 71       inputStream_ = null; 72     } 73     if (outputStream_ != null) { 74       try { 75         outputStream_.close(); 76       } catch (IOException iox) { 77         LOGGER.warn("Error closing output stream.", iox); 78       } 79       outputStream_ = null; 80     } 81   } 82  83   /** 84    * Reads from the underlying input stream if not null. 85    */ 86   public int read(byte[] buf, int off, int len) throws TTransportException { 87     if (inputStream_ == null) { 88       throw new TTransportException(TTransportException.NOT_OPEN, "Cannot read from null inputStream"); 89     } 90     int bytesRead; 91     try { 92       bytesRead = inputStream_.read(buf, off, len); 93     } catch (IOException iox) { 94       throw new TTransportException(TTransportException.UNKNOWN, iox); 95     } 96     if (bytesRead < 0) { 97       throw new TTransportException(TTransportException.END_OF_FILE); 98     } 99     return bytesRead;100   }101 102   /**103    * Writes to the underlying output stream if not null.104    */105   public void write(byte[] buf, int off, int len) throws TTransportException {106     if (outputStream_ == null) {107       throw new TTransportException(TTransportException.NOT_OPEN, "Cannot write to null outputStream");108     }109     try {110       outputStream_.write(buf, off, len);111     } catch (IOException iox) {112       throw new TTransportException(TTransportException.UNKNOWN, iox);113     }114   }115 116   /**117    * Flushes the underlying output stream if not null.118    */119   public void flush() throws TTransportException {120     if (outputStream_ == null) {121       throw new TTransportException(TTransportException.NOT_OPEN, "Cannot flush null outputStream");122     }123     try {124       outputStream_.flush();125     } catch (IOException iox) {126       throw new TTransportException(TTransportException.UNKNOWN, iox);127     }128   }129 }

TSocket继承了TIOStreamTransport类,封装了Socket接口,含有host,port,socket,timeout几个私有变量,并且有open,isOpen,initSocket,getSocket,setTimeout方法,由于继承自TIOStreamTransport,因此父类的读写方法都可以使用,也就是说TSocket的通信根本还是使用的输入输出流。

 TserverSocket继承自TserverTransport,

TNoblockingServerTransport继承自TserverTransport,

TNoblockingServerSocket继承自TNoblockingServerTransport

图中的XoaServerTransport暂时忽略(公司内部xoa框架使用)

以上是用于服务端,而客户端则如右下图所示,不再赘述。

做人第一,做学问第二。

转载地址:http://qopul.baihongyu.com/

你可能感兴趣的文章