Thrift源码解析--transport

本文涉及的产品
云解析 DNS,旗舰版 1个月
全局流量管理 GTM,标准版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: 这一层主要是用于实现网络通信,现在都是基于Tcp/Ip,而Tcp/Ip协议栈由socket来实现,换句话说就是现在网络通信服务底层大都是通过socket实现的,在thrift源码中,就是将socket包装成各种transport来使用。

这一层主要是用于实现网络通信,现在都是基于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框架使用)

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

做人第一,做学问第二。
目录
相关文章
|
Java Go Apache
gRPC vs Thrift
远程过程调用(Remote Procedure Call,RPC)服务于分布式架构,本文从分布式构架面临的问题,期望的结果,引出两种比较受关注的RPC框架,并从框架的出身、实现原理、特性、性能等方面做了对比分析,从而给出两者之间的选择建议。
11473 0
|
6月前
|
安全 网络协议 应用服务中间件
AJP Connector:深入解析及在Apache HTTP Server中的应用
【9月更文挑战第6天】在Java Web应用开发中,Tomcat作为广泛使用的Servlet容器,经常与Apache HTTP Server结合使用,以提供高效、稳定的Web服务。而AJP Connector(Apache JServ Protocol Connector)作为连接Tomcat和Apache HTTP Server的重要桥梁,扮演着至关重要的角色
153 2
|
9月前
|
网络协议 PHP
Swoole 源码分析之 Http Server 模块
想要了解到 `Http Server` 的全貌,其实只要把那张整体的实现图看懂就足以了。但是,如果想要有足够的深度,那么就还需要深入 `Swoole` 的源代码中,就着源码自行分析一遍。同时,也希望这一次的分析,能够给大家带来对 `Swoole` 更多的一些了解。并不要求要深刻的掌握,因为,很多的事情都不可能一蹴而就。从自己的实力出发,勿忘初心。
103 0
Swoole 源码分析之 Http Server 模块
|
存储 开发工具 git
如何构建 Protocol Buffers(protobuf)并解决常见问题
如何构建 Protocol Buffers(protobuf)并解决常见问题
252 0
|
开发框架 .NET API
.net gRPC初探 - 从一个简单的Demo中了解并学习gRPC
.net gRPC初探 - 从一个简单的Demo中了解并学习gRPC
113 0
|
分布式计算 数据挖掘 程序员
Thrift简介—续
Thrift简介—续
Thrift简介—续
|
存储 自然语言处理 负载均衡
Thrift简介
Thrift简介
|
Java 测试技术
Thrift源码解析--TBinaryProtocol
本文为原创:http://www.cnblogs.com/leehfly/p/4958206.html,未经许可禁止转载。 关于Tprotocol层都是一些通信协议,个人感觉内容较大,很难分类描述清楚。
1485 0

热门文章

最新文章