List of usage examples for io.netty.buffer ByteBuf retain
@Override public abstract ByteBuf retain();
From source file:com.eightkdata.mongowp.server.decoder.MyBsonContext.java
License:Open Source License
public MyBsonContext(@Loose @Retains @ConservesIndexes ByteBuf byteBuf) { this.byteBuf = byteBuf.retain(); }
From source file:com.flowpowered.networking.fake.ChannelHandlerContextFaker.java
License:MIT License
public static FakeChannelHandlerContext setup() { if (context == null) { alloc();// ww w . jav a 2s .c om context = Mockito.mock(FakeChannelHandlerContext.class, Mockito.CALLS_REAL_METHODS); channel = Mockito.mock(Channel.class); config = Mockito.mock(ChannelConfig.class); Mockito.doReturn(channel).when(context).channel(); Mockito.when(channel.config()).thenReturn(config); Mockito.when(config.getAllocator()).thenReturn(alloc); Answer<ByteBuf> answer = new Answer<ByteBuf>() { @Override public ByteBuf answer(InvocationOnMock invocation) throws Throwable { ByteBuf buffer = Unpooled.buffer(); buffer.retain(); return buffer; } }; Mockito.when(alloc.buffer()).thenAnswer(answer); Mockito.when(alloc.buffer(Mockito.anyInt())).thenAnswer(answer); } return context; }
From source file:com.flowpowered.networking.pipeline.MessageProcessorDecoderTest.java
License:MIT License
@Test public void test() throws Exception { // Preprocessor basically is split into two parts // Part 1 is just a direct copy // Part 2 negates all bytes before copying final AtomicReference<MessageProcessor> processor = new AtomicReference<>(); MessageProcessorDecoder processorDecoder = new MessageProcessorDecoder(null) { @Override/*from w w w.j av a 2 s. c o m*/ protected MessageProcessor getProcessor() { return processor.get(); } }; // Set up a fake ChannelHandlerContext FakeChannelHandlerContext fake = ChannelHandlerContextFaker.setup(); AtomicReference<ByteBuf> ref = new AtomicReference<>(); fake.setReference(ref); LinkedList<byte[]> outputList = new LinkedList<byte[]>(); Random r = new Random(); // Get some random bytes for data byte[] input = new byte[LENGTH]; r.nextBytes(input); boolean breakOccured = false; int position = 0; for (int i = 0; i < input.length;) { // Simulate real data read int burstSize = r.nextInt(512); // With a 1/10 chance of having an extra-large burst if (r.nextInt(10) == 0) { burstSize *= 10; } // Final burst needs to be clamped if (i + burstSize > input.length) { burstSize = input.length - i; } // And we can't negate in the middle of a burst if (i + burstSize > BREAK && !breakOccured) { burstSize = BREAK - i; } // Write info to a new ByteBuf final ByteBuf buf = Unpooled.buffer(burstSize); buf.retain(); buf.writeBytes(input, i, burstSize); i += burstSize; // Fake a read processorDecoder.channelRead(fake, buf); final ByteBuf returned = ref.get(); while (returned != null && true) { int packetSize = r.nextInt(128) + 1; if (r.nextInt(10) == 0) { packetSize *= 20; } if (packetSize > returned.readableBytes()) { packetSize = returned.readableBytes(); } if (position + packetSize > BREAK && !breakOccured) { packetSize = BREAK - position; } if (position + packetSize > LENGTH) { packetSize = LENGTH - position; } if (packetSize == 0) { break; } byte[] array = new byte[packetSize]; returned.readBytes(array); position += packetSize; if (position == BREAK) { processor.set(new NegatingProcessor(512)); breakOccured = true; } outputList.add(array); } } // Get the output data and combine into one array byte[] output = new byte[LENGTH]; int i = 0; for (byte[] array : outputList) { for (int j = 0; j < array.length; j++) { output[i++] = array[j]; } } for (i = 0; i < input.length; i++) { byte expected = i < BREAK ? input[i] : (byte) ~input[i]; if (output[i] != expected) { for (int j = Math.max(0, i - 10); j <= i + 10; j++) { System.out.println(j + ") " + Integer.toBinaryString(j < BREAK ? input[j] : (byte) ~input[j]) + " " + Integer.toBinaryString(output[j])); } } if (i < BREAK) { assertTrue("Input/Output mismatch at position " + i + ". Expected " + input[i] + " but got " + output[i] + ". Break is: " + BREAK, output[i] == input[i]); } else { assertTrue( "Input/Output mismatch at position " + i + ", after the processor change. Expected " + (byte) ~input[i] + " but got " + output[i] + ". Break is: " + BREAK, output[i] == (byte) ~input[i]); } } }
From source file:com.flysoloing.learning.network.netty.http2.helloworld.server.HelloWorldHttp2Handler.java
License:Apache License
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) { int processed = data.readableBytes() + padding; if (endOfStream) { sendResponse(ctx, streamId, data.retain()); }/* w w w. j a v a 2s . com*/ return processed; }
From source file:com.github.sparkfy.network.protocol.ChunkFetchSuccess.java
License:Apache License
/** Decoding uses the given ByteBuf as our data, and will retain() it. */ public static ChunkFetchSuccess decode(ByteBuf buf) { StreamChunkId streamChunkId = StreamChunkId.decode(buf); buf.retain(); NettyManagedBuffer managedBuf = new NettyManagedBuffer(buf.duplicate()); return new ChunkFetchSuccess(streamChunkId, managedBuf); }
From source file:com.github.sparkfy.network.protocol.OneWayMessage.java
License:Apache License
public static OneWayMessage decode(ByteBuf buf) { // See comment in encodedLength(). buf.readInt();/*from w w w .j a va 2s . c o m*/ return new OneWayMessage(new NettyManagedBuffer(buf.retain())); }
From source file:com.github.sparkfy.network.protocol.RpcRequest.java
License:Apache License
public static RpcRequest decode(ByteBuf buf) { long requestId = buf.readLong(); // See comment in encodedLength(). buf.readInt();//from ww w .j a v a2 s . c o m return new RpcRequest(requestId, new NettyManagedBuffer(buf.retain())); }
From source file:com.github.sparkfy.network.protocol.RpcResponse.java
License:Apache License
public static RpcResponse decode(ByteBuf buf) { long requestId = buf.readLong(); // See comment in encodedLength(). buf.readInt();/*from w w w . j a va 2 s . c o m*/ return new RpcResponse(requestId, new NettyManagedBuffer(buf.retain())); }
From source file:com.github.sparkfy.network.util.TransportFrameDecoder.java
License:Apache License
/** * Takes the first buffer in the internal list, and either adjust it to fit in the frame * (by taking a slice out of it) or remove it from the internal list. *///from w w w . j a v a 2 s . c o m private ByteBuf nextBufferForFrame(int bytesToRead) { ByteBuf buf = buffers.getFirst(); ByteBuf frame; if (buf.readableBytes() > bytesToRead) { frame = buf.retain().readSlice(bytesToRead); totalSize -= bytesToRead; } else { frame = buf; buffers.removeFirst(); totalSize -= frame.readableBytes(); } return frame; }
From source file:com.hazelcast.simulator.protocol.handler.ForwardToCoordinatorHandler.java
License:Open Source License
@Override protected void channelRead0(final ChannelHandlerContext ctx, final ByteBuf buffer) throws Exception { if (isSimulatorMessage(buffer)) { long messageId = getMessageId(buffer); if (LOGGER.isTraceEnabled()) { LOGGER.trace(//from ww w . j a v a 2s .c om format("[%d] %s %s forwarding message to parent", messageId, addressLevel, localAddress)); } workerJvmManager.updateLastSeenTimestamp(buffer); Iterator<Channel> iterator = connectionManager.getChannels().iterator(); if (!iterator.hasNext()) { ctx.writeAndFlush(new Response(messageId, getSourceAddress(buffer), localAddress, FAILURE_COORDINATOR_NOT_FOUND)); return; } buffer.retain(); iterator.next().writeAndFlush(buffer); } }