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);
}
}
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());
}
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());
}
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;
}
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;
}
Aggregations