com.alibaba.dubbo.remoting.buffer.ChannelBuffers Java Examples
The following examples show how to use
com.alibaba.dubbo.remoting.buffer.ChannelBuffers.
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 dubbo3 with Apache License 2.0 | 6 votes |
@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 #2
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 #3
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 #4
Source File: GrizzlyCodecAdapter.java From dubbo-2.6.5 with Apache License 2.0 | 6 votes |
@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 #5
Source File: TelnetCodecTest.java From dubbo3 with Apache License 2.0 | 6 votes |
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 #6
Source File: TelnetCodecTest.java From dubbo-2.6.5 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 #7
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 #8
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 #9
Source File: ExchangeCodecTest.java From dubbox with Apache License 2.0 | 6 votes |
@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: TelnetCodecTest.java From dubbox with Apache License 2.0 | 6 votes |
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 #11
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 #12
Source File: GrizzlyCodecAdapter.java From dubbox with Apache License 2.0 | 6 votes |
@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 #13
Source File: ExchangeCodecTest.java From dubbox-hystrix with Apache License 2.0 | 6 votes |
@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 #14
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 #15
Source File: ExchangeCodecTest.java From dubbox with Apache License 2.0 | 6 votes |
@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 #16
Source File: ExchangeCodecTest.java From dubbo3 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 #17
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 #18
Source File: ExchangeCodecTest.java From dubbo3 with Apache License 2.0 | 5 votes |
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; }
Example #19
Source File: ExchangeCodecTest.java From dubbo3 with Apache License 2.0 | 5 votes |
@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 #20
Source File: TelnetCodecTest.java From dubbox-hystrix 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 #21
Source File: ExchangeCodecTest.java From dubbox-hystrix with Apache License 2.0 | 5 votes |
@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 #22
Source File: ExchangeCodecTest.java From dubbox-hystrix 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 #23
Source File: ExchangeCodecTest.java From dubbox-hystrix with Apache License 2.0 | 5 votes |
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; }
Example #24
Source File: MinaCodecAdapter.java From dubbox with Apache License 2.0 | 5 votes |
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 #25
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 #26
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 #27
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 #28
Source File: ExchangeCodecTest.java From dubbox with Apache License 2.0 | 5 votes |
@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 #29
Source File: TelnetCodecTest.java From dubbox with Apache License 2.0 | 5 votes |
private void testDecode_assertEquals(AbstractMockChannel channel, Object request,Object expectret, Object channelReceive) throws IOException{ //init channel if (channel == null){ channel = getServerSideChannel(url); } byte[] buf = objectToByte(request); ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(buf); //decode Object obj = codec.decode(channel, buffer); Assert.assertEquals(expectret, obj); Assert.assertEquals(channelReceive, channel.getReceivedMessage()); }
Example #30
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()); }