Example usage for java.nio.channels AsynchronousSocketChannel read

List of usage examples for java.nio.channels AsynchronousSocketChannel read

Introduction

In this page you can find the example usage for java.nio.channels AsynchronousSocketChannel read.

Prototype

@Override
public abstract Future<Integer> read(ByteBuffer dst);

Source Link

Usage

From source file:com.chicm.cmraft.rpc.PacketUtils.java

public static RpcCall parseRpcRequestFromChannel(AsynchronousSocketChannel channel, BlockingService service)
        throws InterruptedException, ExecutionException, IOException {
    RpcCall call = null;//  ww w . j av a2  s. com
    long t = System.currentTimeMillis();
    InputStream in = Channels.newInputStream(channel);
    byte[] datasize = new byte[MESSAGE_LENGHT_FIELD_SIZE];
    in.read(datasize);
    int nDataSize = bytes2Int(datasize);

    int len = 0;
    ByteBuffer buf = ByteBuffer.allocateDirect(nDataSize);
    for (; len < nDataSize;) {
        len += channel.read(buf).get();
    }
    if (len < nDataSize) {
        LOG.error("SOCKET READ FAILED, len:" + len);
        return call;
    }
    byte[] data = new byte[nDataSize];
    buf.flip();
    buf.get(data);
    int offset = 0;
    CodedInputStream cis = CodedInputStream.newInstance(data, offset, nDataSize - offset);
    int headerSize = cis.readRawVarint32();
    offset += cis.getTotalBytesRead();
    RequestHeader header = RequestHeader.newBuilder().mergeFrom(data, offset, headerSize).build();

    offset += headerSize;
    cis.skipRawBytes(headerSize);
    cis.resetSizeCounter();
    int bodySize = cis.readRawVarint32();
    offset += cis.getTotalBytesRead();
    //LOG.debug("header parsed:" + header.toString());

    MethodDescriptor md = service.getDescriptorForType().findMethodByName(header.getRequestName());
    Builder builder = service.getRequestPrototype(md).newBuilderForType();
    Message body = null;
    if (builder != null) {
        body = builder.mergeFrom(data, offset, bodySize).build();
        //LOG.debug("server : request parsed:" + body.toString());
    }
    call = new RpcCall(header.getId(), header, body, md);
    if (LOG.isTraceEnabled()) {
        LOG.trace("Parse Rpc request from socket: " + call.getCallId() + ", takes"
                + (System.currentTimeMillis() - t) + " ms");
    }

    return call;
}

From source file:com.chicm.cmraft.rpc.PacketUtils.java

public static RpcCall parseRpcResponseFromChannel(AsynchronousSocketChannel channel, BlockingService service)
        throws InterruptedException, ExecutionException, IOException {
    RpcCall call = null;/*from   w w w . j a  v a 2 s. com*/
    long t = System.currentTimeMillis();
    InputStream in = Channels.newInputStream(channel);
    byte[] datasize = new byte[MESSAGE_LENGHT_FIELD_SIZE];
    in.read(datasize);
    int nDataSize = bytes2Int(datasize);

    LOG.debug("message size: " + nDataSize);

    int len = 0;
    ByteBuffer buf = ByteBuffer.allocateDirect(nDataSize);
    for (; len < nDataSize;) {
        len += channel.read(buf).get();
    }
    if (len < nDataSize) {
        LOG.error("SOCKET READ FAILED, len:" + len);
        return call;
    }
    byte[] data = new byte[nDataSize];
    buf.flip();
    buf.get(data);
    int offset = 0;
    CodedInputStream cis = CodedInputStream.newInstance(data, offset, nDataSize - offset);
    int headerSize = cis.readRawVarint32();
    offset += cis.getTotalBytesRead();
    ResponseHeader header = ResponseHeader.newBuilder().mergeFrom(data, offset, headerSize).build();

    offset += headerSize;
    cis.skipRawBytes(headerSize);
    cis.resetSizeCounter();
    int bodySize = cis.readRawVarint32();
    offset += cis.getTotalBytesRead();

    MethodDescriptor md = service.getDescriptorForType().findMethodByName(header.getResponseName());
    Builder builder = service.getResponsePrototype(md).newBuilderForType();
    Message body = null;
    if (builder != null) {
        body = builder.mergeFrom(data, offset, bodySize).build();
    }
    call = new RpcCall(header.getId(), header, body, md);
    if (LOG.isTraceEnabled()) {
        LOG.trace("Parse Rpc response from socket: " + call.getCallId() + ", takes"
                + (System.currentTimeMillis() - t) + " ms");
    }

    return call;
}