Search in sources :

Example 11 with ChannelBuffer

use of org.apache.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.

the class GrizzlyCodecAdapter method handleRead.

@Override
public NextAction handleRead(FilterChainContext context) throws IOException {
    Object message = context.getMessage();
    Connection<?> connection = context.getConnection();
    Channel channel = GrizzlyChannel.getOrAddChannel(connection, url, handler);
    try {
        if (message instanceof Buffer) {
            // receive a new packet
            // buffer
            Buffer grizzlyBuffer = (Buffer) message;
            ChannelBuffer frame;
            if (previousData.readable()) {
                if (previousData instanceof DynamicChannelBuffer) {
                    previousData.writeBytes(grizzlyBuffer.toByteBuffer());
                    frame = previousData;
                } else {
                    int size = previousData.readableBytes() + grizzlyBuffer.remaining();
                    frame = ChannelBuffers.dynamicBuffer(size > bufferSize ? size : bufferSize);
                    frame.writeBytes(previousData, previousData.readableBytes());
                    frame.writeBytes(grizzlyBuffer.toByteBuffer());
                }
            } else {
                frame = ChannelBuffers.wrappedBuffer(grizzlyBuffer.toByteBuffer());
            }
            Object msg;
            int savedReadIndex;
            do {
                savedReadIndex = frame.readerIndex();
                try {
                    msg = codec.decode(channel, frame);
                } catch (Exception e) {
                    previousData = ChannelBuffers.EMPTY_BUFFER;
                    throw new IOException(e.getMessage(), e);
                }
                if (msg == Codec2.DecodeResult.NEED_MORE_INPUT) {
                    frame.readerIndex(savedReadIndex);
                    return context.getStopAction();
                } else {
                    if (savedReadIndex == frame.readerIndex()) {
                        previousData = ChannelBuffers.EMPTY_BUFFER;
                        throw new IOException("Decode without read data.");
                    }
                    if (msg != null) {
                        context.setMessage(msg);
                    }
                    return context.getInvokeAction();
                }
            } while (frame.readable());
        } else {
            // Other events are passed down directly
            return context.getInvokeAction();
        }
    } finally {
        GrizzlyChannel.removeChannelIfDisconnected(connection);
    }
}
Also used : ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) Buffer(org.glassfish.grizzly.Buffer) DynamicChannelBuffer(org.apache.dubbo.remoting.buffer.DynamicChannelBuffer) Channel(org.apache.dubbo.remoting.Channel) DynamicChannelBuffer(org.apache.dubbo.remoting.buffer.DynamicChannelBuffer) IOException(java.io.IOException) IOException(java.io.IOException) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) DynamicChannelBuffer(org.apache.dubbo.remoting.buffer.DynamicChannelBuffer)

Example 12 with ChannelBuffer

use of org.apache.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.

the class ThriftCodecTest method testEncodeReplyResponse.

@Test
public void testEncodeReplyResponse() throws Exception {
    int port = NetUtils.getAvailablePort();
    URL url = URL.valueOf(ThriftProtocol.NAME + "://127.0.0.1:" + port + "/" + Demo.Iface.class.getName());
    Channel channel = new MockedChannel(url);
    Request request = createRequest();
    AppResponse appResponse = new AppResponse();
    appResponse.setValue("Hello, World!");
    Response response = new Response();
    response.setResult(appResponse);
    response.setId(request.getId());
    ChannelBuffer bos = ChannelBuffers.dynamicBuffer(1024);
    ThriftCodec.RequestData rd = ThriftCodec.RequestData.create(ThriftCodec.getSeqId(), Demo.Iface.class.getName(), "echoString");
    ThriftCodec.CACHED_REQUEST.putIfAbsent(request.getId(), rd);
    codec.encode(channel, bos, response);
    byte[] buf = new byte[bos.writerIndex() - 4];
    System.arraycopy(bos.array(), 4, buf, 0, bos.writerIndex() - 4);
    ByteArrayInputStream bis = new ByteArrayInputStream(buf);
    if (bis.markSupported()) {
        bis.mark(0);
    }
    TIOStreamTransport transport = new TIOStreamTransport(bis);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    Assertions.assertEquals(ThriftCodec.MAGIC, protocol.readI16());
    Assertions.assertEquals(protocol.readI32() + 4, bos.writerIndex());
    int headerLength = protocol.readI16();
    Assertions.assertEquals(ThriftCodec.VERSION, protocol.readByte());
    Assertions.assertEquals(Demo.Iface.class.getName(), protocol.readString());
    Assertions.assertEquals(request.getId(), protocol.readI64());
    if (bis.markSupported()) {
        bis.reset();
        bis.skip(headerLength);
    }
    TMessage message = protocol.readMessageBegin();
    Assertions.assertEquals("echoString", message.name);
    Assertions.assertEquals(TMessageType.REPLY, message.type);
    // Assertions.assertEquals(ThriftCodec.getSeqId(), message.seqid);
    Demo.echoString_result result = new Demo.echoString_result();
    result.read(protocol);
    protocol.readMessageEnd();
    Assertions.assertEquals(appResponse.getValue(), result.getSuccess());
}
Also used : Demo(org.apache.dubbo.rpc.gen.thrift.Demo) Channel(org.apache.dubbo.remoting.Channel) Request(org.apache.dubbo.remoting.exchange.Request) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) URL(org.apache.dubbo.common.URL) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) AppResponse(org.apache.dubbo.rpc.AppResponse) Response(org.apache.dubbo.remoting.exchange.Response) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) ByteArrayInputStream(java.io.ByteArrayInputStream) TMessage(org.apache.thrift.protocol.TMessage) AppResponse(org.apache.dubbo.rpc.AppResponse) Test(org.junit.jupiter.api.Test)

Example 13 with ChannelBuffer

use of org.apache.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.

the class ThriftCodecTest method testEncodeExceptionResponse.

@Test
public void testEncodeExceptionResponse() throws Exception {
    int port = NetUtils.getAvailablePort();
    URL url = URL.valueOf(ThriftProtocol.NAME + "://127.0.0.1:" + port + "/" + Demo.Iface.class.getName());
    Channel channel = new MockedChannel(url);
    Request request = createRequest();
    AppResponse appResponse = new AppResponse();
    String exceptionMessage = "failed";
    appResponse.setException(new RuntimeException(exceptionMessage));
    Response response = new Response();
    response.setResult(appResponse);
    response.setId(request.getId());
    ChannelBuffer bos = ChannelBuffers.dynamicBuffer(1024);
    ThriftCodec.RequestData rd = ThriftCodec.RequestData.create(ThriftCodec.getSeqId(), Demo.Iface.class.getName(), "echoString");
    ThriftCodec.CACHED_REQUEST.put(request.getId(), rd);
    codec.encode(channel, bos, response);
    byte[] buf = new byte[bos.writerIndex() - 4];
    System.arraycopy(bos.array(), 4, buf, 0, bos.writerIndex() - 4);
    ByteArrayInputStream bis = new ByteArrayInputStream(buf);
    if (bis.markSupported()) {
        bis.mark(0);
    }
    TIOStreamTransport transport = new TIOStreamTransport(bis);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    Assertions.assertEquals(ThriftCodec.MAGIC, protocol.readI16());
    Assertions.assertEquals(protocol.readI32() + 4, bos.writerIndex());
    int headerLength = protocol.readI16();
    Assertions.assertEquals(ThriftCodec.VERSION, protocol.readByte());
    Assertions.assertEquals(Demo.Iface.class.getName(), protocol.readString());
    Assertions.assertEquals(request.getId(), protocol.readI64());
    if (bis.markSupported()) {
        bis.reset();
        bis.skip(headerLength);
    }
    TMessage message = protocol.readMessageBegin();
    Assertions.assertEquals("echoString", message.name);
    Assertions.assertEquals(TMessageType.EXCEPTION, message.type);
    Assertions.assertEquals(ThriftCodec.getSeqId(), message.seqid);
    TApplicationException exception = TApplicationException.readFrom(protocol);
    protocol.readMessageEnd();
    Assertions.assertEquals(exceptionMessage, exception.getMessage());
}
Also used : Demo(org.apache.dubbo.rpc.gen.thrift.Demo) Channel(org.apache.dubbo.remoting.Channel) Request(org.apache.dubbo.remoting.exchange.Request) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) URL(org.apache.dubbo.common.URL) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) TApplicationException(org.apache.thrift.TApplicationException) AppResponse(org.apache.dubbo.rpc.AppResponse) Response(org.apache.dubbo.remoting.exchange.Response) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) ByteArrayInputStream(java.io.ByteArrayInputStream) TMessage(org.apache.thrift.protocol.TMessage) AppResponse(org.apache.dubbo.rpc.AppResponse) Test(org.junit.jupiter.api.Test)

Example 14 with ChannelBuffer

use of org.apache.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.

the class DubboTelnetDecodeTest method createDubboByteBuf.

private ByteBuf createDubboByteBuf() throws IOException {
    Request request = new Request();
    RpcInvocation rpcInvocation = new RpcInvocation();
    rpcInvocation.setMethodName("sayHello");
    rpcInvocation.setParameterTypes(new Class[] { String.class });
    rpcInvocation.setParameterTypesDesc(ReflectUtils.getDesc(new Class[] { String.class }));
    rpcInvocation.setArguments(new String[] { "dubbo" });
    rpcInvocation.setAttachment("path", DemoService.class.getName());
    rpcInvocation.setAttachment("interface", DemoService.class.getName());
    rpcInvocation.setAttachment("version", "0.0.0");
    request.setData(rpcInvocation);
    request.setVersion("2.0.2");
    ByteBuf dubboByteBuf = Unpooled.buffer();
    ChannelBuffer buffer = new NettyBackedChannelBuffer(dubboByteBuf);
    DubboCodec dubboCodec = new DubboCodec();
    dubboCodec.encode(new MockChannel(), buffer, request);
    return dubboByteBuf;
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) DecodeableRpcInvocation(org.apache.dubbo.rpc.protocol.dubbo.DecodeableRpcInvocation) DubboCodec(org.apache.dubbo.rpc.protocol.dubbo.DubboCodec) NettyBackedChannelBuffer(org.apache.dubbo.remoting.transport.netty4.NettyBackedChannelBuffer) Request(org.apache.dubbo.remoting.exchange.Request) DemoService(org.apache.dubbo.rpc.protocol.dubbo.support.DemoService) ByteBuf(io.netty.buffer.ByteBuf) NettyBackedChannelBuffer(org.apache.dubbo.remoting.transport.netty4.NettyBackedChannelBuffer) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer)

Example 15 with ChannelBuffer

use of org.apache.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(org.apache.dubbo.remoting.buffer.ChannelBuffer)

Aggregations

ChannelBuffer (org.apache.dubbo.remoting.buffer.ChannelBuffer)22 Channel (org.apache.dubbo.remoting.Channel)16 Test (org.junit.jupiter.api.Test)13 Request (org.apache.dubbo.remoting.exchange.Request)9 Response (org.apache.dubbo.remoting.exchange.Response)9 Demo (org.apache.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 (org.apache.dubbo.common.URL)4 AppResponse (org.apache.dubbo.rpc.AppResponse)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 TTransport (org.apache.thrift.transport.TTransport)3 DynamicChannelBuffer (org.apache.dubbo.remoting.buffer.DynamicChannelBuffer)2 DefaultFuture (org.apache.dubbo.remoting.exchange.support.DefaultFuture)2 RandomAccessByteArrayOutputStream (org.apache.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream)2 TApplicationException (org.apache.thrift.TApplicationException)2 Buffer (org.glassfish.grizzly.Buffer)2 ByteBuf (io.netty.buffer.ByteBuf)1