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 ...
随机推荐
- iosAPP打包上架xcode中Archive提交成功以后,不提示构建版本问题
最近在项目更新时遇到Archive提交到开发者中心成功后,一直不提示构建版本信息,可能导致的原因是由于ios10以后对于APP中调用手机相册或摄像头麦克风时需要配置plist文件,配置如下内容或许会解 ...
- ef code first 您没有所需权限
在进行数据库更新操作时,update-database -force,反复提示没有改表或者没有所需权限,昨晚折腾了4个小时没有搞定,太晚了,今天Google了一下,这方面的错误,提到的都很少,在一篇文 ...
- linux下安装tomcat,并设置自动启动
在linux系统下,设置某个服务自启动的话,需要在/etc/rcX.d下挂载,还要在/etc/init.d/下写启动脚本的 在/etc/init.d/下新建一个文件tomcat(需要在root权限下操 ...
- response.setContentType与 request.setCharacterEncoding 区别
1.request.setCharacterEncoding()是设置从request中取得的值或从数据库中取出的值的编码 2.response.setContentType指定 HTTP 响应的编码 ...
- New Year Tree 【DFS序+先段数区间查询修改+二进制保存状态】
题目链接[http://codeforces.com/problemset/problem/620/E] 题意:给出n个数,每个数有一个初始的颜色.由这n个数组成一颗树.有两种操作1.将以节点u为根的 ...
- php 微信 自定义分享接口
<?php class JSSDK { private $appId; private $appSecret; public function __construct($appId, $appS ...
- FileZilla客户端源码解析
FileZilla客户端源码解析 FTP是TCP/IP协议组的协议,有指令通路和数据通路两条通道.一般来说,FTP标准命令TCP端口号是21,Port方式数据传输端口是20. FileZilla作为p ...
- open()的模块
对文件操作流程: 1.打开文件,得到文件句柄并赋值给一个变量 2.通过句柄对文件进行操作 3.关闭文件 mode can be: * 'r' 只读. * 'w' 写入,如果之前有就覆盖 * 'a' ...
- Oracle中 union 和 union all 的区别
如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字. union(或称为联合)的作用是将多个结果合并在一起显示出来. union和uni ...
- 郑州尚学堂:链表的C语言如何实现动态内存分配
一.为什么用动态内存分配 但我们未学习链表的时候,如果要存储数量比较多的同类型或同结构的数据的时候,总是使用一个数组.比如说我们要存储一个班级学生的某科分数,总是定义一个float型(存在0.5分)数 ...