`
默默的小熊
  • 浏览: 232752 次
社区版块
存档分类
最新评论

TTransport

 
阅读更多

package org.apache.thrift.transport;

public abstract class TTransport {

  /**
   * Queries whether the transport is open.
   *
   * @return True if the transport is open.
   */
  public abstract boolean isOpen();

  /**
   * Is there more data to be read?
   *
   * @return True if the remote side is still alive and feeding us
   */
  public boolean peek() {
    return isOpen();
  }

  /**
   * Opens the transport for reading/writing.
   *
   * @throws TTransportException if the transport could not be opened
   */
  public abstract void open()
    throws TTransportException;

  /**
   * Closes the transport.
   */
  public abstract void close();

  /**
   * Reads up to len bytes into buffer buf, starting at offset off.
   *
   * @param buf Array to read into
   * @param off Index to start reading at
   * @param len Maximum number of bytes to read
   * @return The number of bytes actually read
   * @throws TTransportException if there was an error reading data
   */
  public abstract int read(byte[] buf, int off, int len)
    throws TTransportException;

  /**
   * Guarantees that all of len bytes are actually read off the transport.
   *
   * @param buf Array to read into
   * @param off Index to start reading at
   * @param len Maximum number of bytes to read
   * @return The number of bytes actually read, which must be equal to len
   * @throws TTransportException if there was an error reading data
   */
  public int readAll(byte[] buf, int off, int len)
    throws TTransportException {
    int got = 0;
    int ret = 0;
    while (got < len) {
      ret = read(buf, off+got, len-got);
      if (ret <= 0) {
        throw new TTransportException(
            "Cannot read. Remote side has closed. Tried to read "
                + len
                + " bytes, but only got "
                + got
                + " bytes. (This is often indicative of an internal error on the server side. Please check your server logs.)");
      }
      got += ret;
    }
    return got;
  }

  /**
   * Writes the buffer to the output
   *
   * @param buf The output data buffer
   * @throws TTransportException if an error occurs writing data
   */
  public void write(byte[] buf) throws TTransportException {
    write(buf, 0, buf.length);
  }

  /**
   * Writes up to len bytes from the buffer.
   *
   * @param buf The output data buffer
   * @param off The offset to start writing from
   * @param len The number of bytes to write
   * @throws TTransportException if there was an error writing data
   */
  public abstract void write(byte[] buf, int off, int len)
    throws TTransportException;

  /**
   * Flush any pending data out of a transport buffer.
   *
   * @throws TTransportException if there was an error writing out data.
   */
  public void flush()
    throws TTransportException {}

  /**
   * Access the protocol's underlying buffer directly. If this is not a
   * buffered transport, return null.
   * @return protocol's Underlying buffer
   */
  public byte[] getBuffer() {
    return null;
  }

  /**
   * Return the index within the underlying buffer that specifies the next spot
   * that should be read from.
   * @return index within the underlying buffer that specifies the next spot
   * that should be read from
   */
  public int getBufferPosition() {
    return 0;
  }

  /**
   * Get the number of bytes remaining in the underlying buffer. Returns -1 if
   * this is a non-buffered transport.
   * @return the number of bytes remaining in the underlying buffer. <br> Returns -1 if
   * this is a non-buffered transport.
   */
  public int getBytesRemainingInBuffer() {
    return -1;
  }

  /**
   * Consume len bytes from the underlying buffer.
   * @param len
   */
  public void consumeBuffer(int len) {}
}
 
分享到:
评论

相关推荐

    Thrift框架分析.pdf

    2. **TServer**:服务器端的核心,它使用TServerTransport创建TTransport,通过TTransportFactory和TProtocolFactory生成TTransport和TProtocol实例,最后调用TProcessor处理请求。 在实际应用中,Thrift通过`...

    从Thrift服务框架思考服务器框架-真的很有收获

    TTransport(负则传输的模块,就是底层I/O的实现):每一种传输方式都对应一个该模块,比如TSocket负则Socket通信,负责传输的对象就是Message。 TProtocol:这个就是协议模块,因为对Message的传输需要统一,否则就...

    【Thrift之C++远程调用helloworld菜鸟教程】

    在客户端,我们同样需要创建一个`TTransport`和`TProtocol`,然后通过`HelloWorldClient`与服务器建立连接并调用服务方法。例如: ```cpp std::shared_ptr&lt;TTransport&gt; transport(new TSocket("localhost", 9090));...

    Java通过thrift连接hbase.docx

    `TTransport`和`TProtocol`是Thrift框架的核心组件,它们负责建立与Thrift服务的连接以及数据的序列化和反序列化。`Hbase.Client`对象是与HBase进行交互的接口,提供了一系列方法来执行读写操作。\n\n注意,实际使用...

    python操作hbase

    transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Hbase.Client(protocol) try: transport.open() # 创建表 tablename = 'myTable' ...

    thrift通过http传输的java例子

    然后,创建一个TTransport实例,如TFramedTransport,以及一个TProtocol实例,如TBinaryProtocol。使用这些实例创建Thrift的客户端,调用服务方法进行通信。 在Java中,这个过程可能看起来像这样: ```java ...

    thriftTest java案例代码

    客户端则创建一个`TTransport`对象(如`TSocket`)连接到服务器,然后构建一个`TProtocol`,并通过`TestService$Client`与服务交互: ```java public class Client { public static void main(String[] args) ...

    基于Thrift框架RPC的研究与实现_田翠珍.pdf

    在架构上,Thrift 分为多个层次,包括业务逻辑代码、IDL生成的代码、传输协议(TProtocol)、传输层(TTransport)以及底层I/O通信。用户定义的业务逻辑位于最高层,通过Thrift编译器生成的代码与中间层交互,这个...

    Java Thrift demo例子

    在客户端,同样使用生成的Java代码,创建`ThriftService.Client`实例,通过`TTransport`连接到服务器,调用服务方法。例如: ```java public static void main(String[] args) throws Exception { TTransport ...

    基于thrift开发的客户端和服务端

    在客户端,你需要创建一个`TTransport`实例(如`TBufferedTransport`)和一个`TProtocol`实例(如`TBinaryProtocol`),然后使用这些实例创建一个`IServiceClient`对象。之后,就可以通过这个客户端调用服务端的方法...

    Thrift双向通讯java代码

    客户端也需要使用Thrift编译器生成的代码,创建一个`TTransport`对象,然后创建`TProtocol`和`TestService.Client`对象,以便调用服务: ```java TTransport transport = new TSocket("localhost", 9090); ...

    Thrift使用示例代码

    5. **创建客户端**:在客户端,我们需要创建一个`TTransport`对象,一个`TProtocol`对象,然后实例化服务的客户端类。例如: ```java TTransport transport = new TSocket("localhost", 9090); TProtocol ...

    C++访问操作读写Hbase[参考].pdf

    参考Hbase源码安装包中的`examples/thrift/DemoClient.cpp`示例,创建一个连接到Hbase服务器的`TSocket`对象,如`boost::shared_ptr&lt;TTransport&gt; socket(new TSocket("localhost", 9090))`。接着,需要一个`...

    Thrift 简单使用

    客户端通过创建TTransport对象,然后构造TProtocol和客户端代理对象,调用服务端的方法: ```java public class CalculatorClient { public static void main(String[] args) throws Exception { TTransport ...

    python thrift2 connect hbase

    from thrift import TTransport, TSocket, TException from thrift.protocol import TBinaryProtocol from gen_py.hbase.ttypes import * # 创建socket连接 transport = TSocket.TSocket('localhost', 9090) ...

    php_thrift_python安装测试记录

    tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) print("Starting the ...

    Thrift下java服务器与客户端开发指南.pdf

    TTransport transport = new TSocket("localhost", 9090); TBinaryProtocol protocol = new TBinaryProtocol(transport); Something.Client client = new Something.Client(protocol); transport.open(); ...

Global site tag (gtag.js) - Google Analytics