这一层主要是用于实现网络通信,现在都是基于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的更多相关文章

  1. Thrift源码解析--TBinaryProtocol

    本文为原创,未经许可禁止转载. 关于Tprotocol层都是一些通信协议,个人感觉内容较大,很难分类描述清楚.故打算以TBinaryProtocol为例,分析客户端发请求以及接收服务端返回数据的整个过 ...

  2. Thrift源码学习二——Server层

    Thrift 提供了如图五种模式:TSimpleServer.TNonblockingServer.THsHaServer.TThreadPoolServer.TThreadSelectorServe ...

  3. OKHttp源码解析

    http://frodoking.github.io/2015/03/12/android-okhttp/ Android为我们提供了两种HTTP交互的方式:HttpURLConnection 和 A ...

  4. 第四章 dubbo内核之aop源码解析

    ExtensionLoader<Protocol> loader = ExtensionLoader.getExtensionLoader(Protocol.class); final P ...

  5. 渣渣菜鸡的 ElasticSearch 源码解析 —— 启动流程(下)

    关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/08/12/es-code03/ 前提 上篇文章写完了 ES 流程启动的一部分,main 方法都入 ...

  6. Flink 源码解析 —— 深度解析 Flink 是如何管理好内存的?

    前言 如今,许多用于分析大型数据集的开源系统都是用 Java 或者是基于 JVM 的编程语言实现的.最着名的例子是 Apache Hadoop,还有较新的框架,如 Apache Spark.Apach ...

  7. Springboot打包执行源码解析

    一.打包 Springboot打包的时候,需要配置一个maven插件[spring-boot-maven-plugin] <build> <plugins> <plugi ...

  8. iOS即时通讯之CocoaAsyncSocket源码解析一

    申明:本文内容属于转载整理,原文连接 前言: CocoaAsyncSocket是谷歌的开发者,基于BSD-Socket写的一个IM框架,它给Mac和iOS提供了易于使用的.强大的异步套接字库,向上封装 ...

  9. twisted reactor 实现源码解析

    twisted reactor 实现源码解析 1.      reactor源码解析 1.1.    案例分析代码: from twisted.internet import protocol fro ...

随机推荐

  1. android弹出dialog后,activity得到焦点

    1.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,WindowManager.LayoutParams.FLAG ...

  2. 逆向思维 UVA 11853

    题目大意:紫书175 思路:看书...2333 关键点就是利用已知条件来逆向思考是否能走通,而不是傻傻的从某个点开始出发啊啥的.

  3. MySQL的保留关键字,使用时尽量避免

    今天用phpmyadmin时,注意到一个提示: 列名 'update' 是一个MySQL 保留关键字. 突然意识到还是应该尽量避免这些保留关键字,也百度了一下.找到了这些关键字,列出来下 使用mysq ...

  4. java.net.ConnectException connect refured

    原因:tomcat 连接拒绝:tomcat没有完全重启 只是部分重启 解决方案: 连接tomcat服务 命令:1:ps -ef|grep java : 2:kill -9 21060 3:查看tomc ...

  5. 本地缓存FMDB的使用(iOS)

    一.简单说明 1.什么是FMDB FMDB是iOS平台的SQLite数据库框架 FMDB以OC的方式封装了SQLite的C语言API 2.FMDB的优点 使用起来更加面向对象,省去了很多麻烦.冗余的C ...

  6. Okhttp设置http缓存,在没有网络的情况下加载http缓存里面的内容

    HTTP_CACHE_FILENAME为缓存地址根路径: private final String HTTP_CACHE_FILENAME = "HttpCache"; priva ...

  7. 淘淘商城_day06_课堂笔记

    今日大纲 实现单点登录系统 基于单点登录系统实现,用户的注册和登录 商品数据同步问题 问题 后台系统中将商品修改,前台系统没有进行数据的同步,导致前端系统不能够实时显示最新的数据. 解决 后台系统中商 ...

  8. 转换成maven时报错

    转自:将项目加入maven管理时报错 将项目加入maven管理时报错: Convert to maven project: An internal error occurred during: “En ...

  9. php 语音参考

    如果文件内容是纯 PHP 代码,最好在文件末尾删除 PHP 结束标记.这可以避免在 PHP 结束标记之后万一意外加入了空格或者换行符,会导致 PHP 开始输出这些空白,而脚本中此时并无输出的意图.   ...

  10. Spring Security(10)——退出登录logout

    要实现退出登录的功能我们需要在http元素下定义logout元素,这样Spring Security将自动为我们添加用于处理退出登录的过滤器LogoutFilter到FilterChain.当我们指定 ...