Search in sources :

Example 6 with ChannelBuffer

use of com.alibaba.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.

the class TelnetCodecTest method testEecode_assertEquals.

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();
        }
    }
}
Also used : Channel(com.alibaba.dubbo.remoting.Channel) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer)

Example 7 with ChannelBuffer

use of com.alibaba.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.

the class GrizzlyCodecAdapter method handleWrite.

@Override
public NextAction handleWrite(FilterChainContext context) throws IOException {
    Connection<?> connection = context.getConnection();
    GrizzlyChannel channel = GrizzlyChannel.getOrAddChannel(connection, url, handler);
    try {
        // Do not need to close
        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();
}
Also used : Buffer(org.glassfish.grizzly.Buffer) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer) DynamicChannelBuffer(com.alibaba.dubbo.remoting.buffer.DynamicChannelBuffer) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer) DynamicChannelBuffer(com.alibaba.dubbo.remoting.buffer.DynamicChannelBuffer)

Example 8 with ChannelBuffer

use of com.alibaba.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.

the class ExchangeCodecTest method decode.

private Object decode(byte[] request) throws IOException {
    ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(request);
    AbstractMockChannel channel = getServerSideChannel(url);
    // decode
    Object obj = codec.decode(channel, buffer);
    return obj;
}
Also used : ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer)

Example 9 with ChannelBuffer

use of com.alibaba.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.

the class ExchangeCodecTest method test_Encode_Response.

@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());
}
Also used : Response(com.alibaba.dubbo.remoting.exchange.Response) Channel(com.alibaba.dubbo.remoting.Channel) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer) Test(org.junit.Test)

Example 10 with ChannelBuffer

use of com.alibaba.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.

the class ExchangeCodecTest method test_Decode_MigicCodec_Contain_ExchangeHeader.

@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);
    // If the telnet data and request data are in the same data packet, we should guarantee that the receipt of request data won't be affected by the factor that telnet does not have an end characters.
    Assert.assertEquals(2, buffer.readerIndex());
}
Also used : Channel(com.alibaba.dubbo.remoting.Channel) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer) Test(org.junit.Test)

Aggregations

ChannelBuffer (com.alibaba.dubbo.remoting.buffer.ChannelBuffer)20 Channel (com.alibaba.dubbo.remoting.Channel)15 Test (org.junit.Test)12 Request (com.alibaba.dubbo.remoting.exchange.Request)8 Response (com.alibaba.dubbo.remoting.exchange.Response)8 Demo (com.alibaba.dubbo.rpc.gen.thrift.Demo)5 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)5 TMessage (org.apache.thrift.protocol.TMessage)5 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)5 URL (com.alibaba.dubbo.common.URL)4 RpcResult (com.alibaba.dubbo.rpc.RpcResult)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 TTransport (org.apache.thrift.transport.TTransport)3 DynamicChannelBuffer (com.alibaba.dubbo.remoting.buffer.DynamicChannelBuffer)2 DefaultFuture (com.alibaba.dubbo.remoting.exchange.support.DefaultFuture)2 RandomAccessByteArrayOutputStream (com.alibaba.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream)2 IOException (java.io.IOException)2 TApplicationException (org.apache.thrift.TApplicationException)2 Buffer (org.glassfish.grizzly.Buffer)2 UnsafeByteArrayOutputStream (com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream)1