List of usage examples for io.netty.buffer ByteBuf refCnt
int refCnt();
From source file:com.vethrfolnir.game.network.mu.MuChannelHandler.java
License:Open Source License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf buff = (msg instanceof ByteBuffer) ? ctx.alloc().buffer().writeBytes((ByteBuffer) msg) : (ByteBuf) msg;/*from ww w . ja va 2s . co m*/ buff.readerIndex(2); int opcode = buff.readUnsignedByte(); switch (opcode) { // double opcode case 0xf1: case 0xf3: case 0x0e: case 0x03: buff.readerIndex(buff.readerIndex() - 1); opcode = buff.readUnsignedShort(); // ex 0xF1_03 break; default: break; } if (opcode == 0xe00) { // Time packet? buff.clear(); buff.release(); return; } ReadPacket packet = clientpackets.get(opcode); if (packet != null) { MuClient client = ctx.channel().attr(MuClient.ClientKey).get(); //System.out.println("Got opcode: 0x"+PrintData.fillHex(opcode, 2)+ " packet = \n"+packet.getClass().getSimpleName()); packet.read(client, buff); } else { log.warn("Unknown packet[opcode = 0x" + PrintData.fillHex(opcode, 2) + "]. Dump: "); log.warn(PrintData.printData(buff.nioBuffer(0, buff.writerIndex()))); } //log.warn(PrintData.printData(buff.nioBuffer(0, buff.writerIndex()))); if (buff.refCnt() > 0) { //System.out.println("Handler Release when packet[opcode = 0x"+PrintData.fillHex(opcode, 2)+"]"); buff.release(); } }
From source file:com.whizzosoftware.wzwave.codec.ZWaveFrameDecoderTest.java
License:Open Source License
@Test public void testRandom2() throws Exception { ZWaveFrameDecoder decoder = new ZWaveFrameDecoder(); List<Object> out = new ArrayList<Object>(); ByteBuf in = wrappedBuffer(new byte[] { 0x01, 0x25, 0x01, 0x02, 0x05, 0x00, 0x1d, 0x01, 0x00, 0x00, 0x00, (byte) 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x01, 0x02 }); assertEquals(39, in.readableBytes()); decoder.decode(null, in, out);//from w ww . j a v a 2 s . c o m assertEquals(0, in.readableBytes()); assertEquals(1, in.refCnt()); }
From source file:com.whizzosoftware.wzwave.codec.ZWaveFrameDecoderTest.java
License:Open Source License
@Test public void testIncompleteWithMultipleAttempts2() throws Exception { ZWaveFrameDecoder decoder = new ZWaveFrameDecoder(); List<Object> out = new ArrayList<>(); ByteBuf buf = wrappedBuffer(new byte[] { 0x06 }); assertEquals(1, buf.readableBytes()); decoder.decode(null, buf, out);/*from w w w . jav a 2s .c o m*/ assertEquals(1, out.size()); assertEquals(0, buf.readableBytes()); buf = wrappedBuffer(new byte[] { 0x01, 0x25, 0x01 }); assertEquals(3, buf.readableBytes()); decoder.decode(null, buf, out); assertEquals(1, out.size()); assertEquals(0, buf.readableBytes()); assertEquals(1, buf.refCnt()); buf = wrappedBuffer(new byte[] { 0x01, 0x25, 0x01, 0x02, 0x05, 0x00, 0x1D, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }); assertEquals(19, buf.readableBytes()); decoder.decode(null, buf, out); assertEquals(1, out.size()); assertEquals(0, buf.readableBytes()); assertEquals(1, buf.refCnt()); buf = wrappedBuffer(new byte[] { 0x01, 0x25, 0x01, 0x02, 0x05, 0x00, 0x1D, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, (byte) 0xC3 }); assertEquals(39, buf.readableBytes()); decoder.decode(null, buf, out); assertEquals(2, out.size()); assertEquals(0, buf.readableBytes()); assertTrue(out.get(0) instanceof ACK); assertTrue(out.get(1) instanceof InitData); out = new ArrayList<>(); decoder.decode(null, wrappedBuffer( new byte[] { 0x06, 0x01, 0x09, 0x01, 0x41, (byte) 0x93, 0x16, 0x01, 0x02, 0x02, 0x01, 0x33 }), out); assertEquals(2, out.size()); assertTrue(out.get(0) instanceof ACK); assertTrue(out.get(1) instanceof NodeProtocolInfo); }
From source file:com.yahoo.pulsar.common.compression.CompressorCodecTest.java
License:Apache License
@Test(dataProvider = "codec") void testCompressDecompress(CompressionType type) throws IOException { CompressionCodec codec = CompressionCodecProvider.getCompressionCodec(type); byte[] data = text.getBytes(); ByteBuf raw = PooledByteBufAllocator.DEFAULT.buffer(); raw.writeBytes(data);// www.j a v a 2s. c o m ByteBuf compressed = codec.encode(raw); assertEquals(raw.readableBytes(), data.length); int compressedSize = compressed.readableBytes(); ByteBuf uncompressed = codec.decode(compressed, data.length); assertEquals(compressed.readableBytes(), compressedSize); assertEquals(uncompressed.readableBytes(), data.length); assertEquals(uncompressed, raw); raw.release(); compressed.release(); uncompressed.release(); // Verify compression codecs have the same behavior with buffers ref counting assertEquals(raw.refCnt(), 0); assertEquals(compressed.refCnt(), 0); assertEquals(compressed.refCnt(), 0); }
From source file:divconq.api.internal.UploadPutHandler.java
License:Open Source License
public void start(final HyperSession parent, ScatteringByteChannel src, String chanid, Map<String, Cookie> cookies, long size, long offset, final OperationCallback callback) { this.src = src; this.cookies = cookies; this.callback = callback; this.dest = this.allocateChannel(parent, callback); if (this.callback.hasErrors()) { callback.complete();/*w w w . j ava2s. co m*/ return; } // send a request to get things going HttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT, "/upload/" + chanid + "/Final"); req.headers().set(Names.HOST, parent.getInfo().getHost()); req.headers().set(Names.USER_AGENT, "DivConq HyperAPI Client 1.0"); req.headers().set(Names.CONNECTION, HttpHeaders.Values.CLOSE); req.headers().set(Names.COOKIE, ClientCookieEncoder.encode(this.cookies.values())); req.headers().set(HttpHeaders.Names.CONTENT_LENGTH, size - offset); // send request headers - must flush here in case CL = 0 this.dest.writeAndFlush(req); // now start sending the file long sent = offset; callback.getContext().setAmountCompleted((int) (sent * 100 / size)); ByteBuf bb = null; try { bb = Hub.instance.getBufferAllocator().directBuffer(64 * 1024); // TODO review if direct is best here long toskip = offset; if (src instanceof SeekableByteChannel) { ((SeekableByteChannel) src).position(toskip); } else { while (toskip > 0) { int skip = (int) Math.min(bb.capacity(), toskip); toskip -= bb.writeBytes(src, skip); bb.clear(); } } // now start writing the upload int amt = bb.writeBytes(src, bb.capacity()); while (amt != -1) { bb.retain(); // this ups ref cnt to 2 - we plan to reuse the buffer this.dest.writeAndFlush(bb).sync(); sent += amt; if (size > 0) callback.getContext().setAmountCompleted((int) (sent * 100 / size)); // by the time we get here, that buffer has been used up and we can use it for the next buffer if (bb.refCnt() != 1) throw new IOException("Buffer reference count is not correct"); // stop writing if canceled if (!this.dest.isOpen()) { this.finish(); // might already be finished but to be sure (this is helpful when api.abortStream is called) break; } bb.clear(); amt = bb.writeBytes(src, bb.capacity()); } // we are now done with it bb.release(); } catch (Exception x) { try { if (bb != null) bb.release(); } catch (Exception x2) { } callback.error(1, "Local read error: " + x); this.finish(); } }
From source file:divconq.api.LocalSession.java
License:Open Source License
@Override public void sendStream(ScatteringByteChannel in, long size, long offset, final String channelid, final OperationCallback callback) { final DataStreamChannel chan = this.session.getChannel(channelid); if (chan == null) { callback.error(1, "Missing channel"); callback.complete();/*w w w. java2s. c o m*/ return; } chan.setDriver(new IStreamDriver() { @Override public void cancel() { callback.error(1, "Transfer canceled"); chan.complete(); callback.complete(); } @Override public void message(StreamMessage msg) { if (msg.isFinal()) { System.out.println("Final on channel: " + channelid); chan.complete(); callback.complete(); } } @Override public void nextChunk() { // won't chunk so won't happen here } }); long sent = offset; int seq = 0; if (size > 0) { callback.getContext().setAmountCompleted((int) (sent * 100 / size)); chan.getContext().setAmountCompleted((int) (sent * 100 / size)); // keep the channel active so it does not timeout } try { ByteBuf bb = Hub.instance.getBufferAllocator().directBuffer(64 * 1024); long toskip = offset; if (in instanceof SeekableByteChannel) { ((SeekableByteChannel) in).position(toskip); } else { while (toskip > 0) { int skip = (int) Math.min(bb.capacity(), toskip); toskip -= bb.writeBytes(in, skip); bb.clear(); } } chan.touch(); // now start writing the upload int amt = bb.writeBytes(in, bb.capacity()); while (amt != -1) { bb.retain(); // this ups ref cnt to 2 - we plan to reuse the buffer StreamMessage b = new StreamMessage("Block", bb); b.setField("Sequence", seq); OperationResult sr = chan.send(b); if (sr.hasErrors()) { chan.close(); break; } seq++; sent += amt; if (size > 0) { callback.getContext().setAmountCompleted((int) (sent * 100 / size)); chan.getContext().setAmountCompleted((int) (sent * 100 / size)); // keep the channel active so it does not timeout } callback.touch(); chan.touch(); // by the time we get here, that buffer has been used up and we can use it for the next buffer if (bb.refCnt() != 1) throw new IOException("Buffer reference count is not correct"); // stop writing if canceled if (chan.isClosed()) break; bb.clear(); amt = bb.writeBytes(in, bb.capacity()); } // we are now done with it bb.release(); // final only if not canceled if (!chan.isClosed()) chan.send(MessageUtil.streamFinal()); } catch (IOException x) { callback.error(1, "Local read error: " + x); chan.send(MessageUtil.streamError(1, "Source read error: " + x)); chan.close(); callback.complete(); } finally { try { in.close(); } catch (IOException x) { } } }
From source file:divconq.web.HttpUploadDecoder.java
License:Open Source License
@Override public void offer(HttpContent chunk) { if (this.channel.isClosed()) return; // TODO somehow connect the cancel back to netsession if (chunk.content().readableBytes() > this.max) { this.channel.abort(); // TODO somehow connect the cancel back to netsession return;//from w ww . j a v a2 s . c o m } ByteBuf bb = chunk.content(); bb.retain(); // we will use it in upcoming send System.out.println("ref count a: " + bb.refCnt()); StreamMessage b = new StreamMessage("Block", bb); b.setField("Sequence", this.seq); OperationResult or = this.channel.send(b); // bb should now be back to 1 System.out.println("ref count b: " + bb.refCnt()); if (or.hasErrors()) { this.channel.close(); return; } this.seq++; // TODO track progress if possible // final only if not canceled if (chunk instanceof LastHttpContent) this.channel.send(MessageUtil.streamFinal()); }
From source file:eu.stratosphere.runtime.io.network.netty.InboundEnvelopeDecoderTest.java
License:Apache License
@Test public void testBufferStaging() throws Exception { final InboundEnvelopeDecoder decoder = new InboundEnvelopeDecoder(this.bufferProviderBroker); final EmbeddedChannel ch = new EmbeddedChannel(new OutboundEnvelopeEncoder(), decoder); when(this.bufferProviderBroker.getBufferProvider(anyJobId(), anyChannelId())) .thenReturn(this.bufferProvider); // -------------------------------------------------------------------- Envelope[] envelopes = nextEnvelopes(3, true); ByteBuf buf = encode(ch, envelopes); when(this.bufferProvider .registerBufferAvailabilityListener(Matchers.<BufferAvailabilityListener>anyObject())) .thenReturn(BufferAvailabilityRegistration.SUCCEEDED_REGISTERED); Buffer buffer = allocBuffer(envelopes[2].getBuffer().size()); when(this.bufferProvider.requestBuffer(anyInt())).thenReturn(null, null, buffer, null); // -------------------------------------------------------------------- // slices: [0] => full envelope, [1] => half envelope, [2] => remaining half + full envelope ByteBuf[] slices = slice(buf, OutboundEnvelopeEncoder.HEADER_SIZE + envelopes[0].getBuffer().size(), OutboundEnvelopeEncoder.HEADER_SIZE + envelopes[1].getBuffer().size() / 2); // 1. no buffer available, incoming slice contains all data int refCount = slices[0].refCnt(); decodeAndVerify(ch, slices[0]);//from w ww . j a v a2s.c o m Assert.assertEquals(refCount + 1, slices[0].refCnt()); Assert.assertFalse(ch.config().isAutoRead()); // notify of available buffer (=> bufferAvailable() callback does return a buffer // of the current network buffer size; the decoder needs to adjust its size to the // requested size decoder.bufferAvailable(allocBuffer(envelopes[0].getBuffer().size() * 2)); ch.runPendingTasks(); Assert.assertEquals(refCount - 1, slices[0].refCnt()); Assert.assertTrue(ch.config().isAutoRead()); decodeAndVerify(ch, envelopes[0]); // 2. no buffer available, incoming slice does NOT contain all data refCount = slices[1].refCnt(); decodeAndVerify(ch, slices[1]); Assert.assertEquals(refCount + 1, slices[1].refCnt()); Assert.assertFalse(ch.config().isAutoRead()); decoder.bufferAvailable(allocBuffer()); ch.runPendingTasks(); Assert.assertEquals(refCount - 1, slices[1].refCnt()); Assert.assertTrue(ch.config().isAutoRead()); decodeAndVerify(ch); // 3. buffer available refCount = slices[2].refCnt(); decodeAndVerify(ch, slices[2], envelopes[1], envelopes[2]); Assert.assertEquals(refCount - 1, slices[2].refCnt()); Assert.assertTrue(ch.config().isAutoRead()); Assert.assertEquals(1, buf.refCnt()); buf.release(); }
From source file:eu.stratosphere.runtime.io.network.netty.InboundEnvelopeDecoderTest.java
License:Apache License
@Test public void testBufferStagingStagedBufferException() throws Exception { final EmbeddedChannel ch = new EmbeddedChannel(new OutboundEnvelopeEncoder(), new InboundEnvelopeDecoder(this.bufferProviderBroker)); when(this.bufferProviderBroker.getBufferProvider(anyJobId(), anyChannelId())) .thenReturn(this.bufferProvider); // -------------------------------------------------------------------- ByteBuf buf = encode(ch, nextEnvelope(true)); when(this.bufferProvider.requestBuffer(anyInt())).thenReturn(null); when(this.bufferProvider .registerBufferAvailabilityListener(Matchers.<BufferAvailabilityListener>anyObject())) .thenReturn(BufferAvailabilityRegistration.SUCCEEDED_REGISTERED); // -------------------------------------------------------------------- int refCount = buf.refCnt(); decodeAndVerify(ch, buf);/*from w w w .j a va 2 s .c om*/ Assert.assertFalse(ch.config().isAutoRead()); Assert.assertEquals(refCount + 1, buf.refCnt()); try { decodeAndVerify(ch, buf); Assert.fail("Expected IllegalStateException not thrown"); } catch (IllegalStateException e) { // expected exception } buf.release(); }
From source file:eu.stratosphere.runtime.io.network.netty.InboundEnvelopeDecoderTest.java
License:Apache License
@Test public void testBufferAvailabilityRegistrationBufferAvailable() throws Exception { final EmbeddedChannel ch = new EmbeddedChannel(new OutboundEnvelopeEncoder(), new InboundEnvelopeDecoder(this.bufferProviderBroker)); when(this.bufferProviderBroker.getBufferProvider(anyJobId(), anyChannelId())) .thenReturn(this.bufferProvider); // -------------------------------------------------------------------- Envelope[] envelopes = new Envelope[] { nextEnvelope(true), nextEnvelope() }; when(this.bufferProvider .registerBufferAvailabilityListener(Matchers.<BufferAvailabilityListener>anyObject())) .thenReturn(BufferAvailabilityRegistration.FAILED_BUFFER_AVAILABLE); when(this.bufferProvider.requestBuffer(anyInt())).thenReturn(null) .thenReturn(allocBuffer(envelopes[0].getBuffer().size())); // -------------------------------------------------------------------- ByteBuf buf = encode(ch, envelopes); decodeAndVerify(ch, buf, envelopes); Assert.assertEquals(0, buf.refCnt()); }