List of usage examples for io.netty.buffer ByteBuf markReaderIndex
public abstract ByteBuf markReaderIndex();
From source file:org.elasticsearch.hadoop.transport.netty4.ByteBufStreamInput.java
License:Apache License
public ByteBufStreamInput(ByteBuf buffer, int length) { if (length > buffer.readableBytes()) { throw new IndexOutOfBoundsException(); }//w w w. j a v a 2 s. c o m this.buffer = buffer; startIndex = buffer.readerIndex(); endIndex = startIndex + length; buffer.markReaderIndex(); }
From source file:org.evilco.network.rcon.common.codec.FrameCodec.java
License:Apache License
/** * {@inheritDoc}//from w ww . j a v a2 s.c o m */ @Override protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> objects) throws Exception { // set order byteBuf = byteBuf.order(ByteOrder.LITTLE_ENDIAN); // read all available packets while (byteBuf.readableBytes() >= 4) { // log getLogger().trace("Decoding frame with maximal size of " + byteBuf.readableBytes() + " bytes."); // mark reader index byteBuf.markReaderIndex(); // read length int length = byteBuf.readInt(); // log getLogger().trace("Detected frame length of " + length + " bytes."); // check whether enough data is available if (length > byteBuf.readableBytes()) { // log getLogger().debug("There are only " + byteBuf.readableBytes() + " out of " + length + " bytes available. Skipping frame until more data is available."); // reset buffer byteBuf.resetReaderIndex(); // exit loop break; } // log getLogger().trace("Frame seems to be complete reading data."); // construct buffer ByteBuf packetBuffer = channelHandlerContext.alloc().buffer(length); // read data byteBuf.readBytes(packetBuffer, length); // add to list objects.add(packetBuffer); // log getLogger().trace("Frame decoded. " + byteBuf.readableBytes() + " bytes left in buffer."); } }
From source file:org.herry2038.scadb.db.util.BufferDumper.java
License:Open Source License
public static final String dumpAsHex(ByteBuf buffer) { int length = buffer.readableBytes(); byte[] byteBuffer = new byte[length]; buffer.markReaderIndex(); buffer.readBytes(byteBuffer);//from ww w .j av a2 s .c om buffer.resetReaderIndex(); StringBuffer outputBuf = new StringBuffer(length * 4); int p = 0; int rows = length / 8; for (int i = 0; (i < rows) && (p < length); i++) { int ptemp = p; outputBuf.append(i + ": "); for (int j = 0; j < 8; j++) { String hexVal = Integer.toHexString(byteBuffer[ptemp] & 0xff); if (hexVal.length() == 1) { hexVal = "0" + hexVal; //$NON-NLS-1$ } outputBuf.append(hexVal + " "); //$NON-NLS-1$ ptemp++; } outputBuf.append(" "); //$NON-NLS-1$ for (int j = 0; j < 8; j++) { int b = 0xff & byteBuffer[p]; if (b > 32 && b < 127) { outputBuf.append((char) b + " "); //$NON-NLS-1$ } else { outputBuf.append(". "); //$NON-NLS-1$ } p++; } outputBuf.append("\n"); //$NON-NLS-1$ } outputBuf.append(rows + ": "); int n = 0; for (int i = p; i < length; i++) { String hexVal = Integer.toHexString(byteBuffer[i] & 0xff); if (hexVal.length() == 1) { hexVal = "0" + hexVal; //$NON-NLS-1$ } outputBuf.append(hexVal + " "); //$NON-NLS-1$ n++; } for (int i = n; i < 8; i++) { outputBuf.append(" "); //$NON-NLS-1$ } outputBuf.append(" "); //$NON-NLS-1$ for (int i = p; i < length; i++) { int b = 0xff & byteBuffer[i]; if (b > 32 && b < 127) { outputBuf.append((char) b + " "); //$NON-NLS-1$ } else { outputBuf.append(". "); //$NON-NLS-1$ } } outputBuf.append("\n"); //$NON-NLS-1$ outputBuf.append("Total " + byteBuffer.length + " bytes read\n"); return outputBuf.toString(); }
From source file:org.inspirenxe.pulse.network.protocol.ServerProtocol.java
License:MIT License
@Override public Codec<?> readHeader(ByteBuf buf) throws UnknownPacketException { int length = -1; int opcode = -1; try {/*from ww w. j a v a 2 s .c o m*/ length = ByteBufUtils.readVarInt(buf); buf.markReaderIndex(); opcode = ByteBufUtils.readVarInt(buf); return getCodecLookupService(INBOUND).find(opcode); } catch (IOException e) { throw new UnknownPacketException("Failed to read packet data (corrupt?)", opcode, length); } catch (IllegalOpcodeException e) { buf.resetReaderIndex(); throw new UnknownPacketException("Opcode received is not a registered codec on the pulse!", opcode, length); } }
From source file:org.jfxvnc.net.rfb.codec.decoder.ServerCutTextDecoder.java
License:Apache License
@Override public boolean decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (!in.isReadable(8)) { return false; }/*from w w w .jav a 2s. c om*/ in.markReaderIndex(); in.skipBytes(4); int length = in.readInt(); if (!in.isReadable(length)) { in.resetReaderIndex(); return false; } byte[] text = new byte[length]; in.readBytes(text); out.add(new ServerCutTextEvent(new String(text, StandardCharsets.ISO_8859_1))); return true; }
From source file:org.jmqtt.core.codec.MQTTDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { in.markReaderIndex(); if (!MqttUtils.checkHeaderAvailability(in)) { in.resetReaderIndex();// ww w .ja va 2 s.c om return; } in.resetReaderIndex(); byte messageType = MqttUtils.readMessageType(in); DemuxDecoder decoder = decoderMap.get(messageType); if (decoder == null) { throw new CorruptedFrameException("Can't find any suitable decoder for message type: " + messageType); } decoder.decode(ctx, in, out); }
From source file:org.lanternpowered.pingy.PingyFramingHandler.java
License:MIT License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> output) throws Exception { while (readableVarInt(buf)) { buf.markReaderIndex(); final int length = readVarInt(buf); if (buf.readableBytes() < length) { buf.resetReaderIndex();//www. j a v a 2 s .c om break; } final ByteBuf msg = ctx.alloc().buffer(length); buf.readBytes(msg, length); output.add(msg); } }
From source file:org.lanternpowered.pingy.PingyLegacyHandler.java
License:MIT License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg0) throws Exception { final ByteBuf msg = (ByteBuf) msg0; boolean legacy = false; msg.markReaderIndex(); try {/*from ww w . j a va 2 s . c o m*/ // Try first as a legacy ping message int messageId = msg.readUnsignedByte(); // Make sure that old clients don't attempt to login if (messageId == 0x02) { legacy = this.tryHandleLegacyJoin(ctx, msg); } else if (messageId == 0xfe) { legacy = this.tryHandleLegacyPing(ctx, msg); } } catch (Exception e) { } if (!legacy) { msg.resetReaderIndex(); ctx.pipeline().remove(this); ctx.fireChannelRead(msg); } else { msg.release(); } }
From source file:org.lanternpowered.server.network.pipeline.LegacyProtocolHandler.java
License:MIT License
@Override public void channelRead(ChannelHandlerContext ctx, Object object) throws Exception { final LanternServer server = this.session.getServer(); ByteBuf buf = (ByteBuf) object; buf.markReaderIndex(); // Whether it was a valid legacy message boolean legacy = false; try {//w ww . ja v a2s.c o m final int messageId = buf.readUnsignedByte(); // Old client's are not so smart, make sure that // they don't attempt to login if (messageId == 0x02) { int protocol = buf.readByte(); // Protocol version int value = buf.readShort(); // Check the length if (value < 0 || value > 16) { return; } buf.readBytes(value << 1); // Username value = buf.readShort(); // Check the length if (value < 0 || value > 255) { return; } buf.readBytes(value << 1); // Host address buf.readInt(); // Port if (buf.readableBytes() > 0) { return; } legacy = true; sendDisconnectMessage(ctx, LanternTexts.toPlain(t("multiplayer.disconnect.outdated_client", Lantern.getGame().getPlatform().getMinecraftVersion().getName()))); final MinecraftVersion clientVersion = Lantern.getGame().getMinecraftVersionCache() .getVersionOrUnknown(protocol, true); if (clientVersion == LanternMinecraftVersion.UNKNOWN_LEGACY) { Lantern.getLogger().debug( "Client with unknown legacy protocol version {} attempted to join the server.", protocol); } else { Lantern.getLogger().debug( "Client with legacy protocol version {} (mc-version {}) attempted to join the server.", protocol, clientVersion.getName()); } return; } // Check for the ping message id. if (messageId != 0xfe) { return; } int readable = buf.readableBytes(); boolean full = false; // The version used to ping the server int protocol = V1_3_2_PROTOCOL; // Versions 1.4 - 1.5.x + 1.6 - Can request full data. if (readable > 0) { // Is always 1 if (buf.readUnsignedByte() != 1) { return; } full = true; protocol = V1_5_2_PROTOCOL; } // The virtual address that was used to join the server InetSocketAddress virtualAddress = null; // Version 1.6 - Used extra data. if (readable > 1) { if (buf.readUnsignedByte() != 0xfa) { return; } byte[] bytes = new byte[buf.readShort() << 1]; buf.readBytes(bytes); if (!new String(bytes, StandardCharsets.UTF_16BE).equals("MC|PingHost")) { return; } // Not used buf.readShort(); // The protocol version is present protocol = buf.readUnsignedByte(); // There is extra host and port data if (protocol >= 73) { bytes = new byte[buf.readShort() << 1]; buf.readBytes(bytes); final String host = new String(bytes, StandardCharsets.UTF_16BE); final int port = buf.readInt(); virtualAddress = InetSocketAddress.createUnresolved(host, port); } readable = buf.readableBytes(); if (readable > 0) { Lantern.getLogger().warn("Trailing bytes on a legacy ping message: {}b", readable); } } final MinecraftVersion clientVersion = Lantern.getGame().getMinecraftVersionCache() .getVersionOrUnknown(protocol, true); if (clientVersion == LanternMinecraftVersion.UNKNOWN) { Lantern.getLogger().debug("Client with unknown legacy protocol version {} pinged the server.", protocol); } // The message was successfully decoded as a legacy one legacy = true; final MinecraftVersion serverVersion = Lantern.getGame().getPlatform().getMinecraftVersion(); Text description = server.getMotd(); final InetSocketAddress address = (InetSocketAddress) ctx.channel().remoteAddress(); final LanternStatusClient client = new LanternStatusClient(address, clientVersion, virtualAddress); final ClientPingServerEvent.Response.Players players = LanternStatusHelper.createPlayers(server); final LanternStatusResponse response = new LanternStatusResponse(serverVersion, server.getFavicon(), description, players); final ClientPingServerEvent event = SpongeEventFactory .createClientPingServerEvent(Cause.source(client).build(), client, response); Sponge.getEventManager().post(event); // Cancelled, we are done here if (event.isCancelled()) { ctx.channel().close(); return; } description = response.getDescription(); int online = players.getOnline(); final int max = players.getMax(); // The players should be hidden, this will replace the player count // with ??? if (!response.getPlayers().isPresent()) { online = -1; } final String data; if (full) { final String description0 = getFirstLine( TextSerializers.LEGACY_FORMATTING_CODE.serialize(description)); // 1. This value is always 1. // 2. The protocol version, just use a value out of range // of the available ones. // 3. The version/name string of the server. // 4. The motd of the server. In legacy format. // 5. The online players // 6. The maximum amount of players data = String.format("\u00A7%s\u0000%s\u0000%s\u0000%s\u0000%s\u0000%s", 1, 127, response.getVersion().getName(), description0, online, max); } else { final String description0 = getFirstLine(TextSerializers.PLAIN.serialize(description)); // 1. The motd of the server. In legacy format. // 2. The online players // 3. The maximum amount of players data = String.format("%s\u00A7%s\u00A7%s", description0, online, max); } sendDisconnectMessage(ctx, data); } catch (Exception ignore) { } finally { if (legacy) { buf.release(); } else { buf.resetReaderIndex(); ctx.channel().pipeline().remove(this); ctx.fireChannelRead(buf); } } }
From source file:org.lanternpowered.server.network.rcon.RconFramingHandler.java
License:MIT License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < 4) { return;/*from w ww . j a va 2 s . c om*/ } in.markReaderIndex(); int length = in.readIntLE(); if (in.readableBytes() < length) { in.resetReaderIndex(); return; } final ByteBuf buf = ctx.alloc().buffer(length); in.readBytes(buf, length); out.add(buf); }