這一層主要是用於實現網絡通訊,如今都是基於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等方法。代碼以下:socket
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的通訊根本仍是使用的輸入輸出流。post
TserverSocket繼承自TserverTransport,this
TNoblockingServerTransport繼承自TserverTransport,spa
TNoblockingServerSocket繼承自TNoblockingServerTransportcode
圖中的XoaServerTransport暫時忽略(公司內部xoa框架使用)orm
以上是用於服務端,而客戶端則如右下圖所示,再也不贅述。server