List of usage examples for io.netty.buffer ByteBuf resetReaderIndex
public abstract ByteBuf resetReaderIndex();
From source file:org.spongepowered.clean.network.netty.PacketSplitter.java
License:MIT License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { in.markReaderIndex();/*w w w . ja va2 s . c o m*/ int numRead = 0; int length = 0; byte read; do { if (!in.isReadable()) { in.resetReaderIndex(); return; } read = in.readByte(); int value = (read & 0b01111111); length |= (value << (7 * numRead)); numRead++; if (numRead > 5) { throw new RuntimeException("Error decoding packet, length not a varint!"); } } while ((read & 0b10000000) != 0); if (in.readableBytes() >= length) { out.add(in.readBytes(length)); } else { in.resetReaderIndex(); } }
From source file:org.starmod.net.pipeline.FramingHandler.java
License:MIT License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { in.markReaderIndex();/*from w w w . j av a2s. com*/ if (in.readableBytes() < 4) return; int size = in.readInt(); if (in.readableBytes() < size) { in.resetReaderIndex(); return; } out.add(in.readBytes(size)); }
From source file:org.traccar.protocol.Gt06ProtocolDecoder.java
License:Apache License
private Object decodeWifi(Channel channel, ByteBuf buf, DeviceSession deviceSession, int type) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); ByteBuf time = buf.readSlice(6); DateBuilder dateBuilder = new DateBuilder().setYear(BcdUtil.readInteger(time, 2)) .setMonth(BcdUtil.readInteger(time, 2)).setDay(BcdUtil.readInteger(time, 2)) .setHour(BcdUtil.readInteger(time, 2)).setMinute(BcdUtil.readInteger(time, 2)) .setSecond(BcdUtil.readInteger(time, 2)); getLastLocation(position, dateBuilder.getDate()); Network network = new Network(); int wifiCount = buf.getByte(2); for (int i = 0; i < wifiCount; i++) { String mac = String.format("%02x:%02x:%02x:%02x:%02x:%02x", buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()); network.addWifiAccessPoint(WifiAccessPoint.from(mac, buf.readUnsignedByte())); }//from ww w.ja v a2 s . c o m int cellCount = buf.readUnsignedByte(); int mcc = buf.readUnsignedShort(); int mnc = buf.readUnsignedByte(); for (int i = 0; i < cellCount; i++) { network.addCellTower(CellTower.from(mcc, mnc, buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedByte())); } position.setNetwork(network); if (channel != null) { ByteBuf response = Unpooled.buffer(); response.writeShort(0x7878); response.writeByte(0); response.writeByte(type); response.writeBytes(time.resetReaderIndex()); response.writeByte('\r'); response.writeByte('\n'); channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress())); } return position; }
From source file:org.waarp.ftp.core.data.handler.FtpDataModeCodec.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception { // First test if the connection is fully ready (block might be // transfered // by client before connection is ready) if (!isReady) { if (!codecLocked.await(FtpConfiguration.DATATIMEOUTCON)) { throw new InvalidArgumentException("Codec not unlocked while should be"); }/* w w w. ja v a 2s. c om*/ isReady = true; } // If STREAM Mode, no task to do, just next filter if (mode == TransferMode.STREAM) { dataBlock = new DataBlock(); int length = buf.readableBytes(); // Except if RECORD Structure! if (structure == TransferStructure.RECORD) { out.add(decodeRecord(buf, length)); return; } dataBlock.setBlock(buf.readBytes(length)); out.add(dataBlock); return; } else if (mode == TransferMode.BLOCK) { // Now we are in BLOCK Mode // Make sure if the length field was received. if (buf.readableBytes() < 3) { // The length field was not received yet - return null. // This method will be invoked again when more packets are // received and appended to the buffer. return; } // The length field is in the buffer. // Mark the current buffer position before reading the length field // because the whole frame might not be in the buffer yet. // We will reset the buffer position to the marked position if // there's not enough bytes in the buffer. buf.markReaderIndex(); if (dataBlock == null) { dataBlock = new DataBlock(); } // Read the descriptor dataBlock.setDescriptor(buf.readByte()); // Read the length field. byte upper = buf.readByte(); byte lower = buf.readByte(); dataBlock.setByteCount(upper, lower); // Make sure if there's enough bytes in the buffer. if (buf.readableBytes() < dataBlock.getByteCount()) { // The whole bytes were not received yet - return null. // This method will be invoked again when more packets are // received and appended to the buffer. // Reset to the marked position to read the length field again // next time. buf.resetReaderIndex(); return; } if (dataBlock.getByteCount() > 0) { // There's enough bytes in the buffer. Read it. dataBlock.setBlock(buf.readBytes(dataBlock.getByteCount())); } DataBlock returnDataBlock = dataBlock; // Free the datablock for next frame dataBlock = null; // Successfully decoded a frame. Return the decoded frame. out.add(returnDataBlock); return; } // Type unimplemented throw new InvalidArgumentException("Mode unimplemented: " + mode.name()); }
From source file:org.waarp.openr66.protocol.localhandler.packet.LocalPacketCodec.java
License:Open Source License
public static AbstractLocalPacket decodeNetworkPacket(ByteBuf buf) throws OpenR66ProtocolPacketException { // Mark the current buffer position buf.markReaderIndex();//w w w.j a v a2s.c o m // Read the length field final int length = buf.readInt(); if (buf.readableBytes() < length) { buf.resetReaderIndex(); return null; } // Now we can read the header // Header: Header length field (4 bytes) = Middle length field (4 // bytes), End length field (4 bytes), type field (1 byte), ... final int middleLength = buf.readInt(); final int endLength = buf.readInt(); // check if the packet is complete if (middleLength + endLength + length - 8 > buf.readableBytes()) { buf.resetReaderIndex(); return null; } // createPacketFromByteBuf read the buffer return LocalPacketFactory.createPacketFromByteBuf(length - 8, middleLength, endLength, buf); }
From source file:org.waarp.openr66.protocol.networkhandler.packet.NetworkPacketCodec.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception { // Make sure if the length field was received. if (buf.readableBytes() < 4) { // The length field was not received yet - return null. // This method will be invoked again when more packets are // received and appended to the buffer. return;//from w w w. ja va 2 s .c o m } // Mark the current buffer position buf.markReaderIndex(); // Read the length field final int length = buf.readInt(); if (length < 9) { throw new OpenR66ProtocolPacketException( "Incorrect decode first field in Network Packet: " + length + " < 9"); } if (buf.readableBytes() < length) { buf.resetReaderIndex(); return; } // Now we can read the two Ids final int localId = buf.readInt(); final int remoteId = buf.readInt(); final byte code = buf.readByte(); int readerInder = buf.readerIndex(); ByteBuf buffer = buf.slice(readerInder, length - 9); buffer.retain(); buf.skipBytes(length - 9); NetworkPacket networkPacket = new NetworkPacket(localId, remoteId, code, buffer); if (code == LocalPacketFactory.KEEPALIVEPACKET) { KeepAlivePacket keepAlivePacket = (KeepAlivePacket) LocalPacketCodec .decodeNetworkPacket(networkPacket.getBuffer()); if (keepAlivePacket.isToValidate()) { keepAlivePacket.validate(); NetworkPacket response = new NetworkPacket(ChannelUtils.NOCHANNEL, ChannelUtils.NOCHANNEL, keepAlivePacket, null); NetworkChannelReference nc = NetworkTransaction.getImmediateNetworkChannel(ctx.channel()); if (nc != null) { nc.useIfUsed(); } ctx.writeAndFlush(response.getNetworkPacket()); buffer.release(); } // Replaced by a NoOp packet networkPacket = new NetworkPacket(localId, remoteId, new NoOpPacket(), null); NetworkServerHandler nsh = (NetworkServerHandler) ctx.pipeline().last(); nsh.setKeepAlivedSent(); } out.add(networkPacket); }
From source file:org.waarp.openr66.proxy.network.NetworkPacketCodec.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception { // Make sure if the length field was received. if (buf.readableBytes() < 4) { // The length field was not received yet - return null. // This method will be invoked again when more packets are // received and appended to the buffer. return;// w ww . ja v a 2 s . co m } // Mark the current buffer position buf.markReaderIndex(); // Read the length field final int length = buf.readInt(); if (buf.readableBytes() < length) { buf.resetReaderIndex(); return; } // Now we can read the two Ids // Slight change in Proxy = first is remote and second is local! final int remoteId = buf.readInt(); final int localId = buf.readInt(); final byte code = buf.readByte(); int readerInder = buf.readerIndex(); ByteBuf buffer = buf.slice(readerInder, length - 9); buffer.retain(); buf.skipBytes(length - 9); NetworkPacket networkPacket = new NetworkPacket(localId, remoteId, code, buffer); if (code == LocalPacketFactory.KEEPALIVEPACKET) { KeepAlivePacket keepAlivePacket = (KeepAlivePacket) LocalPacketCodec .decodeNetworkPacket(networkPacket.getBuffer()); if (keepAlivePacket.isToValidate()) { keepAlivePacket.validate(); NetworkPacket response = new NetworkPacket(ChannelUtils.NOCHANNEL, ChannelUtils.NOCHANNEL, keepAlivePacket, null); ctx.writeAndFlush(response.getNetworkPacket()); } // Replaced by a NoOp packet networkPacket = new NetworkPacket(localId, remoteId, new NoOpPacket(), null); NetworkServerHandler nsh = (NetworkServerHandler) ctx.pipeline().last(); nsh.setKeepAlivedSent(); } out.add(networkPacket); }
From source file:org.wso2.carbon.stream.processor.core.ha.transport.handlers.MessageDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { if (in.readableBytes() < 5) { return;/* w ww. j av a2 s . c o m*/ } int protocol = in.readByte(); int messageSize = in.readInt(); if (protocol != 2 || messageSize > in.readableBytes()) { in.resetReaderIndex(); return; } byte[] bytes = new byte[messageSize]; in.readBytes(bytes); in.markReaderIndex(); in.resetReaderIndex(); try { byteBufferQueue.put(ByteBuffer.wrap(bytes)); } catch (InterruptedException e) { log.error("Error while waiting for the insertion of ByteBufferQueue " + e.getMessage(), e); } if (log.isDebugEnabled()) { synchronized (this) { if (startTime == 0L) { startTime = new Date().getTime(); } count++; if (count % TPS_EVENT_BATCH_THRESHOLD == 0) { endTime = new Date().getTime(); log.info("Server Event Batch TPS: " + (((TPS_EVENT_BATCH_THRESHOLD * 1000) / (endTime - startTime)))); startTime = new Date().getTime(); } } } in.markReaderIndex(); }
From source file:org.wso2.carbon.tcp.transport.EventDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { if (in.readableBytes() < 5) { return;/*ww w .j a v a 2s . c om*/ } int protocol = in.readByte(); int messageSize = in.readInt(); if (protocol != 2 || messageSize > in.readableBytes()) { in.resetReaderIndex(); return; } StreamTypeHolder streamTypeHolder = TransportStreamManager.getInstance().getStreamTypeHolder(-1234); List<SiddhiEventComposite> testList = SiddhiEventConverter.getConverter().toEventList(in, streamTypeHolder); out.add(testList); }
From source file:org.wso2.extension.siddhi.io.tcp.transport.handlers.MessageDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { if (in.readableBytes() < 5) { return;/* www . j av a2 s.c om*/ } try { int protocol = in.readByte(); int messageSize = in.readInt(); if (protocol != 2 || messageSize > in.readableBytes()) { in.resetReaderIndex(); return; } flowController.barrier(); int sessionIdSize = in.readInt(); BinaryMessageConverterUtil.getString(in, sessionIdSize); int channelIdSize = in.readInt(); String channelId = BinaryMessageConverterUtil.getString(in, channelIdSize); int dataLength = in.readInt(); byte[] bytes = new byte[dataLength]; in.readBytes(bytes); StreamListener streamListener = streamInfoHolder.getStreamListener(channelId); if (streamListener == null) { in.markReaderIndex(); LOG.error("Events with unknown channelId : '" + channelId + "' hence dropping the events!"); return; } streamListener.onMessage(bytes); } catch (UnsupportedEncodingException e) { LOG.error(e.getMessage(), e); } finally { in.markReaderIndex(); } }