com.alibaba.dubbo.remoting.buffer.ChannelBuffer Java Examples

The following examples show how to use com.alibaba.dubbo.remoting.buffer.ChannelBuffer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GrizzlyCodecAdapter.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Override
public NextAction handleWrite(FilterChainContext context) throws IOException {
    Connection<?> connection = context.getConnection();
    GrizzlyChannel channel = GrizzlyChannel.getOrAddChannel(connection, url, handler);
    try {
        ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer(1024); // Do not need to close

        Object msg = context.getMessage();
        codec.encode(channel, channelBuffer, msg);

        GrizzlyChannel.removeChannelIfDisconnected(connection);
        Buffer buffer = connection.getTransport().getMemoryManager().allocate(channelBuffer.readableBytes());
        buffer.put(channelBuffer.toByteBuffer());
        buffer.flip();
        buffer.allowBufferDispose(true);
        context.setMessage(buffer);
    } finally {
        GrizzlyChannel.removeChannelIfDisconnected(connection);
    }
    return context.getInvokeAction();
}
 
Example #2
Source File: TelnetCodecTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
protected void testEecode_assertEquals(Object request,byte[] ret, boolean isServerside) throws IOException{
    //init channel
    Channel channel = isServerside? getServerSideChannel(url) : getCliendSideChannel(url);

    ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(1024);

    codec.encode(channel, buffer, request);
    byte[] data = new byte[buffer.readableBytes()];
    buffer.readBytes(data);

    Assert.assertEquals(ret.length, data.length);
    for(int i=0;i<ret.length;i++){
        if (ret[i] != data[i]){
            Assert.fail();
        }
    }
}
 
Example #3
Source File: GrizzlyCodecAdapter.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@Override
public NextAction handleWrite(FilterChainContext context) throws IOException {
    Connection<?> connection = context.getConnection();
    GrizzlyChannel channel = GrizzlyChannel.getOrAddChannel(connection, url, handler);
    try {
        ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer(1024); // 不需要关闭
        
        Object msg = context.getMessage();
        codec.encode(channel, channelBuffer, msg);
        
        GrizzlyChannel.removeChannelIfDisconnectd(connection);
        Buffer buffer = connection.getTransport().getMemoryManager().allocate(channelBuffer.readableBytes());
        buffer.put(channelBuffer.toByteBuffer());
        buffer.flip();
        buffer.allowBufferDispose(true);
        context.setMessage(buffer);
    } finally {
        GrizzlyChannel.removeChannelIfDisconnectd(connection);
    }
    return context.getInvokeAction();
}
 
Example #4
Source File: DubboCountCodec.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
    int save = buffer.readerIndex();
    MultiMessage result = MultiMessage.create();
    do {
        Object obj = codec.decode(channel, buffer);
        if (Codec2.DecodeResult.NEED_MORE_INPUT == obj) {
            buffer.readerIndex(save);
            break;
        } else {
            result.addMessage(obj);
            logMessageLength(obj, buffer.readerIndex() - save);
            save = buffer.readerIndex();
        }
    } while (true);
    if (result.isEmpty()) {
        return Codec2.DecodeResult.NEED_MORE_INPUT;
    }
    if (result.size() == 1) {
        return result.get(0);
    }
    return result;
}
 
Example #5
Source File: ThriftNativeCodec.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
protected void encodeRequest(Channel channel, ChannelBuffer buffer, Request request)
        throws IOException {
    Invocation invocation = (Invocation) request.getData();
    TProtocol protocol = newProtocol(channel.getUrl(), buffer);
    try {
        protocol.writeMessageBegin(new TMessage(
                invocation.getMethodName(), TMessageType.CALL,
                thriftSeq.getAndIncrement()));
        protocol.writeStructBegin(new TStruct(invocation.getMethodName() + "_args"));
        for (int i = 0; i < invocation.getParameterTypes().length; i++) {
            Class<?> type = invocation.getParameterTypes()[i];

        }
    } catch (TException e) {
        throw new IOException(e.getMessage(), e);
    }

}
 
Example #6
Source File: ExchangeCodecTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@Test
public void testMessageLengthExceedPayloadLimitWhenEncode() throws Exception {
    Request request = new Request(1L);
    request.setData("hello");
    ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(512);
    AbstractMockChannel channel = getCliendSideChannel(url.addParameter(Constants.PAYLOAD_KEY, 4));
    try {
        codec.encode(channel, encodeBuffer, request);
        Assert.fail();
    } catch (IOException e) {
        Assert.assertTrue(e.getMessage().startsWith("Data length too large: " + 6));
    }

    Response response = new Response(1L);
    response.setResult("hello");
    encodeBuffer = ChannelBuffers.dynamicBuffer(512);
    channel = getServerSideChannel(url.addParameter(Constants.PAYLOAD_KEY, 4));
    codec.encode(channel, encodeBuffer, response);
    Assert.assertTrue(channel.getReceivedMessage() instanceof Response);
    Response receiveMessage = (Response) channel.getReceivedMessage();
    Assert.assertEquals(Response.BAD_RESPONSE, receiveMessage.getStatus());
    Assert.assertTrue(receiveMessage.getErrorMessage().contains("Data length too large: "));
}
 
Example #7
Source File: ExchangeCodecTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void testMessageLengthExceedPayloadLimitWhenEncode() throws Exception {
    Request request = new Request(1L);
    request.setData("hello");
    ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(512);
    AbstractMockChannel channel = getCliendSideChannel(url.addParameter(Constants.PAYLOAD_KEY, 4));
    try {
        codec.encode(channel, encodeBuffer, request);
        Assert.fail();
    } catch (IOException e) {
        Assert.assertTrue(e.getMessage().startsWith("Data length too large: " + 6));
    }

    Response response = new Response(1L);
    response.setResult("hello");
    encodeBuffer = ChannelBuffers.dynamicBuffer(512);
    channel = getServerSideChannel(url.addParameter(Constants.PAYLOAD_KEY, 4));
    codec.encode(channel, encodeBuffer, response);
    Assert.assertTrue(channel.getReceivedMessage() instanceof Response);
    Response receiveMessage = (Response) channel.getReceivedMessage();
    Assert.assertEquals(Response.BAD_RESPONSE, receiveMessage.getStatus());
    Assert.assertTrue(receiveMessage.getErrorMessage().contains("Data length too large: "));
}
 
Example #8
Source File: TelnetCodecTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
protected void testEecode_assertEquals(Object request,byte[] ret, boolean isServerside) throws IOException{
    //init channel
    Channel channel = isServerside? getServerSideChannel(url) : getCliendSideChannel(url);

    ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(1024);

    codec.encode(channel, buffer, request);
    byte[] data = new byte[buffer.readableBytes()];
    buffer.readBytes(data);

    Assert.assertEquals(ret.length, data.length);
    for(int i=0;i<ret.length;i++){
        if (ret[i] != data[i]){
            Assert.fail();
        }
    }
}
 
Example #9
Source File: ExchangeCodecTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testMessageLengthExceedPayloadLimitWhenEncode() throws Exception {
    Request request = new Request(1L);
    request.setData("hello");
    ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(512);
    AbstractMockChannel channel = getCliendSideChannel(url.addParameter(Constants.PAYLOAD_KEY, 4));
    try {
        codec.encode(channel, encodeBuffer, request);
        Assert.fail();
    } catch (IOException e) {
        Assert.assertTrue(e.getMessage().startsWith("Data length too large: " + 6));
    }

    Response response = new Response(1L);
    response.setResult("hello");
    encodeBuffer = ChannelBuffers.dynamicBuffer(512);
    channel = getServerSideChannel(url.addParameter(Constants.PAYLOAD_KEY, 4));
    codec.encode(channel, encodeBuffer, response);
    Assert.assertTrue(channel.getReceivedMessage() instanceof Response);
    Response receiveMessage = (Response) channel.getReceivedMessage();
    Assert.assertEquals(Response.BAD_RESPONSE, receiveMessage.getStatus());
    Assert.assertTrue(receiveMessage.getErrorMessage().contains("Data length too large: "));
}
 
Example #10
Source File: ExchangeCodecTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void test_Encode_Request() throws IOException {
    ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(2014);
    Channel channel = getCliendSideChannel(url);
    Request request = new Request();
    Person person = new Person();
    request.setData(person);

    codec.encode(channel, encodeBuffer, request);

    //encode resault check need decode
    byte[] data = new byte[encodeBuffer.writerIndex()];
    encodeBuffer.readBytes(data);
    ChannelBuffer decodeBuffer = ChannelBuffers.wrappedBuffer(data);
    Request obj = (Request) codec.decode(channel, decodeBuffer);
    Assert.assertEquals(request.isBroken(), obj.isBroken());
    Assert.assertEquals(request.isHeartbeat(), obj.isHeartbeat());
    Assert.assertEquals(request.isTwoWay(), obj.isTwoWay());
    Assert.assertEquals(person, obj.getData());
}
 
Example #11
Source File: ExchangeCodecTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@Test 
public void test_Encode_Request() throws IOException{
    ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(2014);
    Channel channel = getCliendSideChannel(url);
    Request request = new Request();
    Person person = new Person();
    request.setData(person);
    
    codec.encode(channel, encodeBuffer, request);

    //encode resault check need decode
    byte[] data = new byte[encodeBuffer.writerIndex()];
    encodeBuffer.readBytes(data);
    ChannelBuffer decodeBuffer = ChannelBuffers.wrappedBuffer(data);
    Request obj = (Request)codec.decode(channel, decodeBuffer);
    Assert.assertEquals(request.isBroken(), obj.isBroken());
    Assert.assertEquals(request.isHeartbeat(), obj.isHeartbeat());
    Assert.assertEquals(request.isTwoWay(), obj.isTwoWay());
    Assert.assertEquals(person, obj.getData());
}
 
Example #12
Source File: ThriftNativeCodec.java    From dubbox with Apache License 2.0 5 votes vote down vote up
protected static TProtocol newProtocol(URL url, ChannelBuffer buffer) throws IOException {
    String protocol = url.getParameter(ThriftConstants.THRIFT_PROTOCOL_KEY,
                                       ThriftConstants.DEFAULT_PROTOCOL);
    if (ThriftConstants.BINARY_THRIFT_PROTOCOL.equals(protocol)) {
        return new TBinaryProtocol(new TIOStreamTransport(new ChannelBufferOutputStream(buffer)));
    }
    throw new IOException("Unsupported protocol type " + protocol);
}
 
Example #13
Source File: NettyBackedChannelBuffer.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Override
public void readBytes(ChannelBuffer dst, int length) {
    // carefule
    if (length > dst.writableBytes()) {
        throw new IndexOutOfBoundsException();
    }
    readBytes(dst, dst.writerIndex(), length);
    dst.writerIndex(dst.writerIndex() + length);
}
 
Example #14
Source File: NettyBackedChannelBuffer.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
public void setBytes(int index, ChannelBuffer src, int length) {
    // careful
    if (length > src.readableBytes()) {
        throw new IndexOutOfBoundsException();
    }
    setBytes(index, src, src.readerIndex(), length);
    src.readerIndex(src.readerIndex() + length);
}
 
Example #15
Source File: NettyBackedChannelBuffer.java    From dubbo-remoting-netty4 with Apache License 2.0 5 votes vote down vote up
@Override
public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) {
    // careful
    byte[] data = new byte[length];
    buffer.getBytes(srcIndex, data, 0, length);
    setBytes(0, data, index, length);
}
 
Example #16
Source File: ExchangeCodecTest.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
@Test
public void test_Decode_MigicCodec_Contain_ExchangeHeader() throws IOException{
    //
    byte[] header = new byte[] { 0, 0, MAGIC_HIGH , MAGIC_LOW , 0 ,0 ,0 ,0 ,0 , 0 ,0 ,0 ,0  };
    
    Channel channel = getServerSideChannel(url);
    ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(header);
    Object obj = codec.decode(channel, buffer);
    Assert.assertEquals(TelnetCodec.DecodeResult.NEED_MORE_INPUT, obj);
    //如果telnet数据与request数据在同一个数据包中,不能因为telnet没有结尾字符而影响其他数据的接收.
    Assert.assertEquals(2, buffer.readerIndex());
}
 
Example #17
Source File: NettyBackedChannelBuffer.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) {
    // careful
    byte[] data = new byte[length];
    buffer.getBytes(index, data, 0, length);
    dst.setBytes(dstIndex, data, 0, length);
}
 
Example #18
Source File: NettyBackedChannelBuffer.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
public void writeBytes(ChannelBuffer src, int length) {
    // careful
    if (length > src.readableBytes()) {
        throw new IndexOutOfBoundsException();
    }
    writeBytes(src, src.readerIndex(), length);
    src.readerIndex(src.readerIndex() + length);
}
 
Example #19
Source File: TransportCodec.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Override
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
    InputStream input = new ChannelBufferInputStream(buffer);
    ObjectInput objectInput = getSerialization(channel).deserialize(channel.getUrl(), input);
    Object object = decodeData(channel, objectInput);
    if (objectInput instanceof Cleanable) {
        ((Cleanable) objectInput).cleanup();
    }
    return object;
}
 
Example #20
Source File: NettyBackedChannelBuffer.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
public void setBytes(int index, ChannelBuffer src, int length) {
    // careful
    if (length > src.readableBytes()) {
        throw new IndexOutOfBoundsException();
    }
    setBytes(index, src, src.readerIndex(), length);
    src.readerIndex(src.readerIndex() + length);
}
 
Example #21
Source File: MinaCodecAdapter.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public void encode(IoSession session, Object msg, ProtocolEncoderOutput out) throws Exception {
    ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(1024);
    MinaChannel channel = MinaChannel.getOrAddChannel(session, url, handler);
    try {
    	codec.encode(channel, buffer, msg);
    } finally {
        MinaChannel.removeChannelIfDisconnectd(session);
    }
    out.write(ByteBuffer.wrap(buffer.toByteBuffer()));
    out.flush();
}
 
Example #22
Source File: ExchangeCodecTest.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
@Test 
    public void test_Encode_Error_Response() throws IOException{
        ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(1024);
        Channel channel = getCliendSideChannel(url);
        Response response = new Response();
        response.setHeartbeat(true);
        response.setId(1001l);
        response.setStatus((byte)10 );
        response.setVersion("11");
        String badString = "bad" ;
        response.setErrorMessage(badString);
        Person person = new Person();
        response.setResult(person);
        
        codec.encode(channel, encodeBuffer, response);
        byte[] data = new byte[encodeBuffer.writerIndex()];
        encodeBuffer.readBytes(data);

        //encode resault check need decode 
        ChannelBuffer decodeBuffer = ChannelBuffers.wrappedBuffer(data);
        Response obj = (Response)codec.decode(channel, decodeBuffer);
        Assert.assertEquals(response.getId(), obj.getId());
        Assert.assertEquals(response.getStatus(), obj.getStatus());
        Assert.assertEquals(response.isHeartbeat(), obj.isHeartbeat());
        Assert.assertEquals(badString, obj.getErrorMessage());
        Assert.assertEquals(null, obj.getResult());
//        Assert.assertEquals(response.getVersion(), obj.getVersion());
    }
 
Example #23
Source File: NettyBackedChannelBuffer.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Override
public void setBytes(int index, ChannelBuffer src, int length) {
    // careful
    if (length > src.readableBytes()) {
        throw new IndexOutOfBoundsException();
    }
    setBytes(index, src, src.readerIndex(), length);
    src.readerIndex(src.readerIndex() + length);
}
 
Example #24
Source File: ExchangeCodecTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test 
    public void test_Encode_Response() throws IOException{
        ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(1024);
        Channel channel = getCliendSideChannel(url);
        Response response = new Response();
        response.setHeartbeat(true);
        response.setId(1001l);
        response.setStatus((byte)20 );
        response.setVersion("11");
        Person person = new Person();
        response.setResult(person);
        
        codec.encode(channel, encodeBuffer, response);
        byte[] data = new byte[encodeBuffer.writerIndex()];
        encodeBuffer.readBytes(data);

        //encode resault check need decode 
        ChannelBuffer decodeBuffer = ChannelBuffers.wrappedBuffer(data);
        Response obj = (Response)codec.decode(channel, decodeBuffer);
        
        Assert.assertEquals(response.getId(), obj.getId());
        Assert.assertEquals(response.getStatus(), obj.getStatus());
        Assert.assertEquals(response.isHeartbeat(), obj.isHeartbeat());
        Assert.assertEquals(person, obj.getResult());
        // encode response verson ?? 
//        Assert.assertEquals(response.getVersion(), obj.getVersion());
        
    }
 
Example #25
Source File: NettyBackedChannelBuffer.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
public void readBytes(ChannelBuffer dst, int length) {
    // carefule
    if (length > dst.writableBytes()) {
        throw new IndexOutOfBoundsException();
    }
    readBytes(dst, dst.writerIndex(), length);
    dst.writerIndex(dst.writerIndex() + length);
}
 
Example #26
Source File: NettyBackedChannelBuffer.java    From dubbo-remoting-netty4 with Apache License 2.0 5 votes vote down vote up
@Override
public void writeBytes(ChannelBuffer src, int srcIndex, int length) {
    // careful
    byte[] data = new byte[length];
    src.getBytes(srcIndex, data, 0, length);
    writeBytes(data, 0, length);
}
 
Example #27
Source File: NettyBackedChannelBuffer.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Override
public void writeBytes(ChannelBuffer src, int srcIndex, int length) {
    // careful
    byte[] data = new byte[length];
    src.getBytes(srcIndex, data, 0, length);
    writeBytes(data, 0, length);
}
 
Example #28
Source File: NettyBackedChannelBuffer.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@Override
public void writeBytes(ChannelBuffer src, int srcIndex, int length) {
    // careful
    byte[] data = new byte[length];
    src.getBytes(srcIndex, data, 0, length);
    writeBytes(data, 0, length);
}
 
Example #29
Source File: TelnetCodecTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private void testDecode_WithExitByte(byte[] exitbytes ,boolean isChannelClose) throws IOException{
    //init channel
    Channel channel = getServerSideChannel(url);
    ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(exitbytes);

    //decode
    codec.decode(channel, buffer);
    Assert.assertEquals(isChannelClose, channel.isClosed());
}
 
Example #30
Source File: ExchangeCodecTest.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Test
    public void test_Encode_Error_Response() throws IOException {
        ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(1024);
        Channel channel = getCliendSideChannel(url);
        Response response = new Response();
        response.setHeartbeat(true);
        response.setId(1001l);
        response.setStatus((byte) 10);
        response.setVersion("11");
        String badString = "bad";
        response.setErrorMessage(badString);
        Person person = new Person();
        response.setResult(person);

        codec.encode(channel, encodeBuffer, response);
        byte[] data = new byte[encodeBuffer.writerIndex()];
        encodeBuffer.readBytes(data);

        //encode resault check need decode
        ChannelBuffer decodeBuffer = ChannelBuffers.wrappedBuffer(data);
        Response obj = (Response) codec.decode(channel, decodeBuffer);
        Assert.assertEquals(response.getId(), obj.getId());
        Assert.assertEquals(response.getStatus(), obj.getStatus());
        Assert.assertEquals(response.isHeartbeat(), obj.isHeartbeat());
        Assert.assertEquals(badString, obj.getErrorMessage());
        Assert.assertEquals(null, obj.getResult());
//        Assert.assertEquals(response.getProtocolVersion(), obj.getVersion());
    }