Thrift 中有 TTransport
和 TServerTransport
,封装了底层传输层的数据读写;分别用于客户端和服务端
TTransport
方法
- open
用于建立与 Server 端的连接
public abstract void open() throws TTransportException;
- close
关闭连接
public abstract void close();
- read
用于读取数据
public abstract int read(byte[] buf, int off, int len) throws TTransportException;
- write
用于写入数据
public abstract void write(byte[] buf, int off, int len) throws TTransportException;
- flush
清空缓冲区中的数据,发送给服务端
public void flush() throws TTransportException {
}
实现类
非封装的 Transport
TNonblockingTransport
: 非阻塞的 Transport 的抽象类,底层使用 NIOTNonblockingSocket
:TNonblockingTransport
的实现类,基于 SocketChannel 的 Transport,是非阻塞的TIOStreamTransport
: 基于 IO 流的 TransportTSocket
:TIOStreamTransport
的子类,底层使用Socket
TSimpleFileTransport
:基于文件的 Transport,会将流写入文件或者从文件读取流TFileTransport
: 基于文件的 Transport,会将流写入文件或者从文件读取流THttpClient
:基于HttpClient
或HttpURLConnection
,会通过 HTTP 的方式发送请求,通常用于TServlet
的服务端ByteBuffer
: 基于 ByteBuffer 的 TransportTMemoryInputTransport
:基于内存数组的 Transport,会从底层的数组读取,用于测试场景TMemoryBuffer
:使用内存数组作为缓冲区的 Transport,用于测试场景
封装的 Transport
TZlibTransport
: 压缩的 Transport,会将流压缩后再发送AutoExpandingBufferReadTransport
: 可扩展读缓冲区的 Transport,使用可变数组作为缓冲区AutoExpandingBufferWriteTransport
: 可扩展写缓冲区的 Transport,使用可变数组作为缓冲区TSaslTransport
:支持 SASL(Simple Authentication and Security Layer) 认证的 Transport,有两个实现类,用于客户端的TSaslClientTransport
和用于服务端的TSaslServerTransport
TFramedTransport
:缓冲的 Transport,通过在前面带有4字节帧大小的消息来确保每次都完全读取消息TFastFramedTransport
: 复用并扩展了读写缓冲区的 Transport,避免每次都创建新的 byte 数组
TServerTransport
方法
- listen
监听指定的端口
public abstract void listen() throws TTransportException;
- accept
用与接受连接
public final TTransport accept() throws TTransportException {
TTransport transport = acceptImpl();
if (transport == null) {
throw new TTransportException("accept() may not return NULL");
}
return transport;
}
- close
断开连接,停止监听端口,关闭服务
public abstract void close();
实现类
TNonblockingServerTransport
:非阻塞服务端抽象类,提供了选择器的注册TNonblockingServerSocket
:TNonblockingServerTransport
的实现类,底层使用 NIO 的ServerSocketChannel
非阻塞 TransportTServerSocket
:使用ServerSocket
的阻塞 Transport