/** Test correct header serialization */ @Test public void testSerializeHeader() { MatchEntryBuilder builder = prepareHeader(false); ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer(); serializer.serializeHeader(builder.build(), buffer); checkHeader(buffer, false); assertTrue("Unexpected data", buffer.readableBytes() == 0); }
/** Test correct serialization */ @Test public void testSerialize() { MatchEntryBuilder builder = prepareMatchEntry(4096); ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer(); serializer.serialize(builder.build(), buffer); checkHeader(buffer, false); assertEquals("Wrong value", 4096, buffer.readUnsignedShort()); assertTrue("Unexpected data", buffer.readableBytes() == 0); }
@Override public ByteBuf translateFrame(ByteBuf readBuffer) throws LimitExedeedException, InvalidDataException { while (readBuffer.isReadable()) { switch (status) { case STATUS_H: h = readBuffer.readByte(); status = STATUS_L; break; case STATUS_L: l = readBuffer.readByte(); final int blen = Protocol.order == ByteOrder.BIG_ENDIAN ? (0x0000ff00 & (h << 8)) | (0x000000ff & l) : (0x0000ff00 & (l << 8)) | (0x000000ff & h); if (context != null) { if (blen <= 0 || blen > maxFrameSize) { throw new LimitExedeedException("帧长度非法:" + h + "/" + l + ":" + blen); } } incompleteframe = PooledByteBufAllocator.DEFAULT.buffer(blen + 16 + 2); incompleteframe = incompleteframe.order(Protocol.order); incompleteframe.writeShort(blen); status = STATUS_C; break; case STATUS_C: int len = incompleteframe.writableBytes() - 16; len = len < readBuffer.readableBytes() ? len : readBuffer.readableBytes(); // incompleteframe.writeBytes(readBuffer, len); if (readBuffer.hasMemoryAddress()) { PlatformDependent.copyMemory( readBuffer.memoryAddress() + readBuffer.readerIndex(), incompleteframe.memoryAddress() + incompleteframe.writerIndex(), len); } else if (readBuffer.hasArray()) { PlatformDependent.copyMemory( readBuffer.array(), readBuffer.arrayOffset() + readBuffer.readerIndex(), incompleteframe.memoryAddress() + incompleteframe.writerIndex(), len); } incompleteframe.writerIndex(incompleteframe.writerIndex() + len); readBuffer.readerIndex(readBuffer.readerIndex() + len); if ((incompleteframe.writableBytes() - 16) <= 0) { status = STATUS_H; return incompleteframe; } break; } } return null; }
private void sendInternal(MessageTree tree) { ChannelFuture future = m_manager.channel(); ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer(10 * 1024); // 10K m_codec.encode(tree, buf); int size = buf.readableBytes(); Channel channel = future.channel(); channel.writeAndFlush(buf); if (m_statistics != null) { m_statistics.onBytes(size); } }
@Test public void createPooledDirectBufferTest() { ByteBuf buf = PooledByteBufAllocator.DEFAULT.directBuffer(11); testBuffer(buf, true); }
@Test public void createPooledHeapBufferTest() { ByteBuf buf = PooledByteBufAllocator.DEFAULT.heapBuffer(11); testBuffer(buf, false); }