Java Code Examples for com.alibaba.dubbo.remoting.buffer.ChannelBuffers#wrappedBuffer()
The following examples show how to use
com.alibaba.dubbo.remoting.buffer.ChannelBuffers#wrappedBuffer() .
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: ExchangeCodecTest.java From dubbox with Apache License 2.0 | 6 votes |
@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 2
Source File: ExchangeCodecTest.java From dubbox-hystrix with Apache License 2.0 | 6 votes |
@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 3
Source File: TelnetCodecTest.java From dubbox-hystrix with Apache License 2.0 | 6 votes |
private void testDecode_PersonWithEnterByte(byte[] enterbytes ,boolean isNeedmore) throws IOException{ //init channel Channel channel = getServerSideChannel(url); //init request string Person request = new Person(); byte[] newbuf = join(objectToByte(request), enterbytes); ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(newbuf); //decode Object obj = codec.decode(channel, buffer); if (isNeedmore){ Assert.assertEquals(Codec2.DecodeResult.NEED_MORE_INPUT , obj); }else { Assert.assertTrue("return must string ", obj instanceof String); } }
Example 4
Source File: TelnetCodecTest.java From dubbox with Apache License 2.0 | 6 votes |
private void testDecode_PersonWithEnterByte(byte[] enterbytes ,boolean isNeedmore) throws IOException{ //init channel Channel channel = getServerSideChannel(url); //init request string Person request = new Person(); byte[] newbuf = join(objectToByte(request), enterbytes); ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(newbuf); //decode Object obj = codec.decode(channel, buffer); if (isNeedmore){ Assert.assertEquals(Codec2.DecodeResult.NEED_MORE_INPUT , obj); }else { Assert.assertTrue("return must string ", obj instanceof String); } }
Example 5
Source File: TelnetCodecTest.java From dubbo-2.6.5 with Apache License 2.0 | 5 votes |
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 6
Source File: TelnetCodecTest.java From dubbox with Apache License 2.0 | 5 votes |
protected void testDecode_assertEquals(byte[] request,Object ret, boolean isServerside) throws IOException{ //init channel Channel channel = isServerside? getServerSideChannel(url) : getCliendSideChannel(url); //init request string ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(request); //decode Object obj = codec.decode(channel, buffer); Assert.assertEquals(ret, obj); }
Example 7
Source File: ExchangeCodecTest.java From dubbox with Apache License 2.0 | 5 votes |
@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 8
Source File: ExchangeCodecTest.java From dubbox with Apache License 2.0 | 5 votes |
@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 9
Source File: TelnetCodecTest.java From dubbo3 with Apache License 2.0 | 5 votes |
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 10
Source File: ExchangeCodecTest.java From dubbo3 with Apache License 2.0 | 5 votes |
@Test public void test_Decode_Error_Length() throws IOException{ byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, 0x20, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; Person person = new Person(); byte[] request = getRequestBytes(person, header); Channel channel = getServerSideChannel(url); byte[] baddata = new byte[]{1,2}; ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(join(request, baddata)); Response obj = (Response)codec.decode(channel, buffer); Assert.assertEquals(person, obj.getResult()); //only decode necessary bytes Assert.assertEquals(request.length, buffer.readerIndex()); }
Example 11
Source File: ExchangeCodecTest.java From dubbox with Apache License 2.0 | 5 votes |
@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 12
Source File: TelnetCodecTest.java From dubbox with Apache License 2.0 | 5 votes |
protected void testDecode_assertEquals(byte[] request,Object ret, boolean isServerside) throws IOException{ //init channel Channel channel = isServerside? getServerSideChannel(url) : getCliendSideChannel(url); //init request string ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(request); //decode Object obj = codec.decode(channel, buffer); Assert.assertEquals(ret, obj); }
Example 13
Source File: TelnetCodecTest.java From dubbo-2.6.5 with Apache License 2.0 | 5 votes |
protected void testDecode_assertEquals(byte[] request, Object ret, boolean isServerside) throws IOException { //init channel Channel channel = isServerside ? getServerSideChannel(url) : getCliendSideChannel(url); //init request string ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(request); //decode Object obj = codec.decode(channel, buffer); Assert.assertEquals(ret, obj); }
Example 14
Source File: ExchangeCodecTest.java From dubbox with Apache License 2.0 | 5 votes |
@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 15
Source File: ExchangeCodecTest.java From dubbox with Apache License 2.0 | 5 votes |
@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 16
Source File: MinaCodecAdapter.java From dubbo-2.6.5 with Apache License 2.0 | 4 votes |
@Override public void decode(IoSession session, ByteBuffer in, ProtocolDecoderOutput out) throws Exception { int readable = in.limit(); if (readable <= 0) return; ChannelBuffer frame; if (buffer.readable()) { if (buffer instanceof DynamicChannelBuffer) { buffer.writeBytes(in.buf()); frame = buffer; } else { int size = buffer.readableBytes() + in.remaining(); frame = ChannelBuffers.dynamicBuffer(size > bufferSize ? size : bufferSize); frame.writeBytes(buffer, buffer.readableBytes()); frame.writeBytes(in.buf()); } } else { frame = ChannelBuffers.wrappedBuffer(in.buf()); } Channel channel = MinaChannel.getOrAddChannel(session, url, handler); Object msg; int savedReadIndex; try { do { savedReadIndex = frame.readerIndex(); try { msg = codec.decode(channel, frame); } catch (Exception e) { buffer = ChannelBuffers.EMPTY_BUFFER; throw e; } if (msg == Codec2.DecodeResult.NEED_MORE_INPUT) { frame.readerIndex(savedReadIndex); break; } else { if (savedReadIndex == frame.readerIndex()) { buffer = ChannelBuffers.EMPTY_BUFFER; throw new Exception("Decode without read data."); } if (msg != null) { out.write(msg); } } } while (frame.readable()); } finally { if (frame.readable()) { frame.discardReadBytes(); buffer = frame; } else { buffer = ChannelBuffers.EMPTY_BUFFER; } MinaChannel.removeChannelIfDisconnected(session); } }
Example 17
Source File: GrizzlyCodecAdapter.java From dubbox-hystrix with Apache License 2.0 | 4 votes |
@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) { // 收到新的数据包 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(); } else { return context.getInvokeAction(); } } } while (frame.readable()); } else { // 其它事件直接往下传 return context.getInvokeAction(); } } finally { GrizzlyChannel.removeChannelIfDisconnectd(connection); } }
Example 18
Source File: Netty4CodecAdapter.java From dubbo-plus with Apache License 2.0 | 4 votes |
@Override protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object o) throws Exception { if (! (o instanceof ByteBuf)) { channelHandlerContext.fireChannelRead(o); return; } ByteBuf input = (ByteBuf) o; int readable = input.readableBytes(); if (readable <= 0) { return; } byte[] bytes= new byte[readable]; input.readBytes(bytes); ChannelBuffer message; if (buffer.readable()) { if (buffer instanceof DynamicChannelBuffer) { buffer.writeBytes(bytes); message = buffer; } else { int size = buffer.readableBytes() + input.readableBytes(); message = ChannelBuffers.dynamicBuffer( size > bufferSize ? size : bufferSize); message.writeBytes(buffer, buffer.readableBytes()); message.writeBytes(bytes); } } else { message = ChannelBuffers.wrappedBuffer( bytes); } Netty4Channel channel = Netty4Channel.getOrAddChannel(channelHandlerContext.channel(), url, handler); Object msg; int saveReaderIndex; try { // decode object. do { saveReaderIndex = message.readerIndex(); try { msg = codec.decode(channel, message); } catch (IOException e) { buffer = ChannelBuffers.EMPTY_BUFFER; throw e; } if (msg == Codec2.DecodeResult.NEED_MORE_INPUT) { message.readerIndex(saveReaderIndex); break; } else { if (saveReaderIndex == message.readerIndex()) { buffer = ChannelBuffers.EMPTY_BUFFER; throw new IOException("Decode without read data."); } if (msg != null) { //Channels.fireMessageReceived(ctx, msg, event.getRemoteAddress()); channelHandlerContext.fireChannelRead(msg); } } } while (message.readable()); } finally { if (message.readable()) { message.discardReadBytes(); buffer = message; } else { buffer = com.alibaba.dubbo.remoting.buffer.ChannelBuffers.EMPTY_BUFFER; } Netty4Channel.removeChannelIfDisconnected(channelHandlerContext.channel()); } }
Example 19
Source File: ThriftCodecTest.java From dubbox with Apache License 2.0 | 2 votes |
@Test public void testDecodeReplyResponse() throws Exception { URL url = URL.valueOf( ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.Iface.class.getName() ); Channel channel = new MockedChannel( url ); RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream( 128 ); Request request = createRequest(); DefaultFuture future = new DefaultFuture( channel, request, 10 ); TMessage message = new TMessage( "echoString", TMessageType.REPLY, ThriftCodec.getSeqId() ); Demo.echoString_result methodResult = new Demo.echoString_result(); methodResult.success = "Hello, World!"; TTransport transport = new TIOStreamTransport( bos ); TBinaryProtocol protocol = new TBinaryProtocol( transport ); int messageLength, headerLength; // prepare protocol.writeI16( ThriftCodec.MAGIC ); protocol.writeI32( Integer.MAX_VALUE ); protocol.writeI16( Short.MAX_VALUE ); protocol.writeByte( ThriftCodec.VERSION ); protocol.writeString( Demo.Iface.class.getName() ); protocol.writeI64( request.getId() ); protocol.getTransport().flush(); headerLength = bos.size(); protocol.writeMessageBegin( message ); methodResult.write( protocol ); protocol.writeMessageEnd(); protocol.getTransport().flush(); int oldIndex = messageLength = bos.size(); try { bos.setWriteIndex( ThriftCodec.MESSAGE_LENGTH_INDEX ); protocol.writeI32( messageLength ); bos.setWriteIndex( ThriftCodec.MESSAGE_HEADER_LENGTH_INDEX ); protocol.writeI16( ( short ) ( 0xffff & headerLength ) ); } finally { bos.setWriteIndex( oldIndex ); } // prepare byte[] buf = new byte[ 4 + bos.size()]; System.arraycopy( bos.toByteArray(), 0, buf, 4, bos.size() ); ChannelBuffer bis = ChannelBuffers.wrappedBuffer(buf); Object obj = codec.decode( ( Channel ) null, bis ); Assert.assertNotNull( obj ); Assert.assertEquals( true, obj instanceof Response ); Response response = ( Response ) obj; Assert.assertEquals( request.getId(), response.getId() ); Assert.assertTrue( response.getResult() instanceof RpcResult ); RpcResult result = ( RpcResult ) response.getResult(); Assert.assertTrue( result.getResult() instanceof String ); Assert.assertEquals( methodResult.success, result.getResult() ); }
Example 20
Source File: ThriftCodecTest.java From dubbox with Apache License 2.0 | 2 votes |
@Test public void testDecodeExceptionResponse() throws Exception { URL url = URL.valueOf( ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.class.getName() ); Channel channel = new MockedChannel( url ); RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream( 128 ); Request request = createRequest(); DefaultFuture future = new DefaultFuture( channel, request, 10 ); TMessage message = new TMessage( "echoString", TMessageType.EXCEPTION, ThriftCodec.getSeqId() ); TTransport transport = new TIOStreamTransport( bos ); TBinaryProtocol protocol = new TBinaryProtocol( transport ); TApplicationException exception = new TApplicationException(); int messageLength, headerLength; // prepare protocol.writeI16( ThriftCodec.MAGIC ); protocol.writeI32( Integer.MAX_VALUE ); protocol.writeI16( Short.MAX_VALUE ); protocol.writeByte( ThriftCodec.VERSION ); protocol.writeString( Demo.class.getName() ); protocol.writeI64( request.getId() ); protocol.getTransport().flush(); headerLength = bos.size(); protocol.writeMessageBegin( message ); exception.write( protocol ); protocol.writeMessageEnd(); protocol.getTransport().flush(); int oldIndex = messageLength = bos.size(); try { bos.setWriteIndex( ThriftCodec.MESSAGE_LENGTH_INDEX ); protocol.writeI32( messageLength ); bos.setWriteIndex( ThriftCodec.MESSAGE_HEADER_LENGTH_INDEX ); protocol.writeI16( ( short ) ( 0xffff & headerLength ) ); } finally { bos.setWriteIndex( oldIndex ); } // prepare ChannelBuffer bis = ChannelBuffers.wrappedBuffer(encodeFrame(bos.toByteArray())); Object obj = codec.decode( ( Channel ) null, bis ); Assert.assertNotNull( obj ); Assert.assertTrue( obj instanceof Response ); Response response = ( Response ) obj; Assert.assertTrue( response.getResult() instanceof RpcResult ); RpcResult result = ( RpcResult ) response.getResult(); Assert.assertTrue( result.hasException() ); Assert.assertTrue( result.getException() instanceof RpcException ); }