Thrift源码解析--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等方法。代码如下:
public class TIOStreamTransport extends TTransport {
private static final Logger LOGGER = LoggerFactory.getLogger(TIOStreamTransport.class.getName());
/** Underlying inputStream */
protected InputStream inputStream_ = null;
/** Underlying outputStream */
protected OutputStream outputStream_ = null;
/**
* Subclasses can invoke the default constructor and then assign the input
* streams in the open method.
*/
protected TIOStreamTransport() {}
/**
* Input stream constructor.
*
* @param is Input stream to read from
*/
public TIOStreamTransport(InputStream is) {
inputStream_ = is;
}
/**
* Output stream constructor.
*
* @param os Output stream to read from
*/
public TIOStreamTransport(OutputStream os) {
outputStream_ = os;
}
/**
* Two-way stream constructor.
*
* @param is Input stream to read from
* @param os Output stream to read from
*/
public TIOStreamTransport(InputStream is, OutputStream os) {
inputStream_ = is;
outputStream_ = os;
}
/**
* The streams must already be open at construction time, so this should
* always return true.
*
* @return true
*/
public boolean isOpen() {
return true;
}
/**
* The streams must already be open. This method does nothing.
*/
public void open() throws TTransportException {}
/**
* Closes both the input and output streams.
*/
public void close() {
if (inputStream_ != null) {
try {
inputStream_.close();
} catch (IOException iox) {
LOGGER.warn("Error closing input stream.", iox);
}
inputStream_ = null;
}
if (outputStream_ != null) {
try {
outputStream_.close();
} catch (IOException iox) {
LOGGER.warn("Error closing output stream.", iox);
}
outputStream_ = null;
}
}
/**
* Reads from the underlying input stream if not null.
*/
public int read(byte[] buf, int off, int len) throws TTransportException {
if (inputStream_ == null) {
throw new TTransportException(TTransportException.NOT_OPEN, "Cannot read from null inputStream");
}
int bytesRead;
try {
bytesRead = inputStream_.read(buf, off, len);
} catch (IOException iox) {
throw new TTransportException(TTransportException.UNKNOWN, iox);
}
if (bytesRead < 0) {
throw new TTransportException(TTransportException.END_OF_FILE);
}
return bytesRead;
}
/**
* Writes to the underlying output stream if not null.
*/
public void write(byte[] buf, int off, int len) throws TTransportException {
if (outputStream_ == null) {
throw new TTransportException(TTransportException.NOT_OPEN, "Cannot write to null outputStream");
}
try {
outputStream_.write(buf, off, len);
} catch (IOException iox) {
throw new TTransportException(TTransportException.UNKNOWN, iox);
}
}
/**
* Flushes the underlying output stream if not null.
*/
public void flush() throws TTransportException {
if (outputStream_ == null) {
throw new TTransportException(TTransportException.NOT_OPEN, "Cannot flush null outputStream");
}
try {
outputStream_.flush();
} catch (IOException iox) {
throw new TTransportException(TTransportException.UNKNOWN, iox);
}
}
}
TSocket继承了TIOStreamTransport类,封装了Socket接口,含有host,port,socket,timeout几个私有变量,并且有open,isOpen,initSocket,getSocket,setTimeout方法,由于继承自TIOStreamTransport,因此父类的读写方法都可以使用,也就是说TSocket的通信根本还是使用的输入输出流。
TserverSocket继承自TserverTransport,
TNoblockingServerTransport继承自TserverTransport,
TNoblockingServerSocket继承自TNoblockingServerTransport
图中的XoaServerTransport暂时忽略(公司内部xoa框架使用)
以上是用于服务端,而客户端则如右下图所示,不再赘述。


Thrift源码解析--transport的更多相关文章
- Thrift源码解析--TBinaryProtocol
本文为原创,未经许可禁止转载. 关于Tprotocol层都是一些通信协议,个人感觉内容较大,很难分类描述清楚.故打算以TBinaryProtocol为例,分析客户端发请求以及接收服务端返回数据的整个过 ...
- Thrift源码学习二——Server层
Thrift 提供了如图五种模式:TSimpleServer.TNonblockingServer.THsHaServer.TThreadPoolServer.TThreadSelectorServe ...
- OKHttp源码解析
http://frodoking.github.io/2015/03/12/android-okhttp/ Android为我们提供了两种HTTP交互的方式:HttpURLConnection 和 A ...
- 第四章 dubbo内核之aop源码解析
ExtensionLoader<Protocol> loader = ExtensionLoader.getExtensionLoader(Protocol.class); final P ...
- 渣渣菜鸡的 ElasticSearch 源码解析 —— 启动流程(下)
关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/08/12/es-code03/ 前提 上篇文章写完了 ES 流程启动的一部分,main 方法都入 ...
- Flink 源码解析 —— 深度解析 Flink 是如何管理好内存的?
前言 如今,许多用于分析大型数据集的开源系统都是用 Java 或者是基于 JVM 的编程语言实现的.最着名的例子是 Apache Hadoop,还有较新的框架,如 Apache Spark.Apach ...
- Springboot打包执行源码解析
一.打包 Springboot打包的时候,需要配置一个maven插件[spring-boot-maven-plugin] <build> <plugins> <plugi ...
- iOS即时通讯之CocoaAsyncSocket源码解析一
申明:本文内容属于转载整理,原文连接 前言: CocoaAsyncSocket是谷歌的开发者,基于BSD-Socket写的一个IM框架,它给Mac和iOS提供了易于使用的.强大的异步套接字库,向上封装 ...
- twisted reactor 实现源码解析
twisted reactor 实现源码解析 1. reactor源码解析 1.1. 案例分析代码: from twisted.internet import protocol fro ...
随机推荐
- .Net 第三方工具包整理
抓取数据 Jumony (http://www.jumony.net/)是一个基于 .NET 技术,用 C# 编写的一个 HTML 引擎,其可以用来分析解读 HTML 文档中的数据,也可以修改和绑定数 ...
- LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method
System.Data.Objects.EntityFunctions和System.Data.Objects.SqlClient.SqlFunctions中的方法进行比较,如下 where Syst ...
- 使用SyncToy 同步两台机器上的文件夹
@echo off echo 准备启动同步... net use \\WIN-AJH8QENQQGK "123456" /user:Administrator Z:\SyncToy ...
- 【Python之路】第二篇--初识Python
Python简介 Python可以应用于众多领域,如:数据分析.组件集成.网络服务.图像处理.数值计算和科学计算等众多领域.目前业内几乎所有大中型互联网企业都在使用Python,如:Youtube.D ...
- set, list 和map知识总结
set就是集,无序,不可重复 举例:HashSet,LinkedHashSet List是映射,通过键值对存储,无序,不可重复 举例:HashMap,HashTable,LinkedHashMap 在 ...
- File对象的常用方法
File对象不仅可以表示文件,还可以表示目录,源码注释是这么说的:An abstract representation of file and directory pathnames. File类最常 ...
- 初识SilkTest
简介 编辑 SilkTest 是业界领先的.用于对企业级应用进行功能测试的产品,可用于测试Web.Java或是传统的C/S结构.SilkTest 提供了许多功能,使用户能够高效率地进行软件自动化测试. ...
- 重点+超详细:ajax和json及案例
不用jQuery的ajax流程 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "ht ...
- Design Pattern——开放封闭原则
两个特征: 1.对于扩展是开放的 2.对于更改是封闭的 意思就是说:程序在设计的时候,时刻要考虑,尽量让这个类是足够好,写好了就不要去修改了,如果有新的需求来,我们就增加一个类来解决问题,而不要更改原 ...
- 《JS权威指南学习总结--8.3 函数的实参和形参》
内容要点: 一.可选形参 当调用函数的时候传入的实参比函数声明时指定的形参个数要少,剩下的形参都将设置为undefined值. 例如: //将对象o中可枚举的属性名追加至数组 //如果省略a,则创建一 ...