Java Code Examples for io.netty.buffer.ByteBuf.readUnsignedByte()

The following are Jave code examples for showing how to use readUnsignedByte() of the io.netty.buffer.ByteBuf class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: j1st-mqtt   File: MqttDecoder.java   Source Code and License Vote up 6 votes
/**
 * Decodes the fixed header. It's one byte for the flags and then variable bytes for the remaining length.
 *
 * @param buffer the buffer to decode from
 * @return the fixed header
 */
private static MqttFixedHeader decodeFixedHeader(ByteBuf buffer) {
    short b1 = buffer.readUnsignedByte();

    MqttMessageType messageType = MqttMessageType.valueOf(b1 >> 4);
    boolean dupFlag = (b1 & 0x08) == 0x08;
    int qosLevel = (b1 & 0x06) >> 1;
    boolean retain = (b1 & 0x01) != 0;

    int remainingLength = 0;
    int multiplier = 1;
    short digit;
    int loops = 0;
    do {
        digit = buffer.readUnsignedByte();
        remainingLength += (digit & 127) * multiplier;
        multiplier *= 128;
        loops++;
    } while ((digit & 128) != 0 && loops < 4);

    // MQTT protocol limits Remaining Length to 4 bytes
    if (loops == 4 && (digit & 128) != 0) {
        throw new DecoderException("remaining length exceeds 4 digits (" + messageType + ')');
    }
    return new MqttFixedHeader(messageType, dupFlag, MqttQoS.valueOf(qosLevel), retain, remainingLength);
}
 
Example 2
Project: NioImapClient   File: ResponseDecoder.java   Source Code and License Vote up 6 votes
@Timed
void startBodyParse(ByteBuf in) throws UnknownFetchItemTypeException, IOException {
  skipControlCharacters(in);

  char c = ((char) in.readUnsignedByte());

  //String bodySection = ""; At some point we will need to actually store the body section that is being parsed below
  if (c != '[') {
    // This is effectively BODYSTRUCTURE which is not yet supported
    lineParser.parse(in);
    checkpoint(State.RESET);
    throw new UnknownFetchItemTypeException("BODYSTRUCTURE");
  } else {
    c = ((char) in.readUnsignedByte());
    while (c != ']') { // Skip characters within "[]"
      c = ((char) in.readUnsignedByte());
    }
  }

  checkpoint(State.FETCH_BODY);
}
 
Example 3
Project: neoscada   File: InterrogationCommand.java   Source Code and License Vote up 5 votes
public static InterrogationCommand parse ( final ProtocolOptions options, final byte length, final ASDUHeader header, final ByteBuf data )
{
    final InformationObjectAddress informationObjectAddress = InformationObjectAddress.parse ( options, data );
    final short qualifierOfInterrogation = data.readUnsignedByte ();
    final InterrogationCommand result = new InterrogationCommand ( header, informationObjectAddress, qualifierOfInterrogation );
    return result;
}
 
Example 4
Project: NioImapClient   File: ResponseDecoder.java   Source Code and License Vote up 5 votes
/**
 * Reset checks to see if we are at the end of this response line. If not it fast forwards the buffer to the end of this line to prepare for the next response.
 *
 * @param in
 */
private void reset(ByteBuf in) {
  char c = (char) in.readUnsignedByte();
  if (c == UNTAGGED_PREFIX || c == CONTINUATION_PREFIX || c == TAGGED_PREFIX) { // We are already at the end of the line
    in.readerIndex(in.readerIndex() - 1);
  } else if (!(c == HttpConstants.CR || c == HttpConstants.LF)) {
    lineParser.parse(in);
  }

  discardSomeReadBytes();
  responseBuilder = new TaggedResponse.Builder();
  checkpoint(State.START_RESPONSE);
}
 
Example 5
Project: j1st-mqtt   File: MqttDecoder.java   Source Code and License Vote up 5 votes
private static Result<MqttSubscribePayload> decodeSubscribePayload(
        ByteBuf buffer,
        int bytesRemainingInVariablePart) {
    final List<MqttTopicSubscription> subscribeTopics = new ArrayList<>();
    int numberOfBytesConsumed = 0;
    while (numberOfBytesConsumed < bytesRemainingInVariablePart) {
        final Result<String> decodedTopic = decodeString(buffer);
        numberOfBytesConsumed += decodedTopic.numberOfBytesConsumed;
        int qos = buffer.readUnsignedByte() & 0x03;
        numberOfBytesConsumed++;
        subscribeTopics.add(new MqttTopicSubscription(decodedTopic.value, MqttQoS.valueOf(qos)));
    }
    return new Result<>(new MqttSubscribePayload(subscribeTopics), numberOfBytesConsumed);
}
 
Example 6
Project: j1st-mqtt   File: MqttDecoder.java   Source Code and License Vote up 5 votes
private static Result<MqttSubAckPayload> decodeSubAckPayload(
        ByteBuf buffer,
        int bytesRemainingInVariablePart) {
    final List<MqttGrantedQoS> grantedQos = new ArrayList<>();
    int numberOfBytesConsumed = 0;
    while (numberOfBytesConsumed < bytesRemainingInVariablePart) {
        int qos = buffer.readUnsignedByte() & 0x03;
        numberOfBytesConsumed++;
        grantedQos.add(MqttGrantedQoS.valueOf(qos));
    }
    return new Result<>(new MqttSubAckPayload(grantedQos), numberOfBytesConsumed);
}
 
Example 7
Project: CustomWorldGen   File: ByteBufUtils.java   Source Code and License Vote up 5 votes
/**
 * An extended length short. Used by custom payload packets to extend size.
 *
 * @param buf
 * @return
 */
public static int readVarShort(ByteBuf buf)
{
    int low = buf.readUnsignedShort();
    int high = 0;
    if ((low & 0x8000) != 0)
    {
        low = low & 0x7FFF;
        high = buf.readUnsignedByte();
    }
    return ((high & 0xFF) << 15) | low;
}
 
Example 8
Project: DovakinMQ   File: MqttDecoder.java   Source Code and License Vote up 5 votes
/**
 * Decodes the fixed header. It's one byte for the flags and then variable bytes for the remaining length.
 *
 * @param buffer the buffer to decode from
 * @return the fixed header
 */
private static MqttFixedHeader decodeFixedHeader(ByteBuf buffer) {
    short b1 = buffer.readUnsignedByte();

    MqttMessageType messageType = MqttMessageType.valueOf(b1 >> 4);
    boolean dupFlag = (b1 & 0x08) == 0x08;
    int qosLevel = (b1 & 0x06) >> 1;
    boolean retain = (b1 & 0x01) != 0;

    int remainingLength = 0;
    int multiplier = 1;
    short digit;
    int loops = 0;
    do {
        digit = buffer.readUnsignedByte();
        remainingLength += (digit & 127) * multiplier;
        multiplier *= 128;
        loops++;
    } while ((digit & 128) != 0 && loops < 4);

    // MQTT protocol limits Remaining Length to 4 bytes
    if (loops == 4 && (digit & 128) != 0) {
        throw new DecoderException("remaining length exceeds 4 digits (" + messageType + ')');
    }
    MqttFixedHeader decodedFixedHeader =
            new MqttFixedHeader(messageType, dupFlag, MqttQoS.valueOf(qosLevel), retain, remainingLength);
    return validateFixedHeader(resetUnusedFields(decodedFixedHeader));
}
 
Example 9
Project: DovakinMQ   File: MqttDecoder.java   Source Code and License Vote up 5 votes
private static Result<MqttSubscribePayload> decodeSubscribePayload(
        ByteBuf buffer,
        int bytesRemainingInVariablePart) {
    final List<MqttTopicSubscription> subscribeTopics = new ArrayList<MqttTopicSubscription>();
    int numberOfBytesConsumed = 0;
    while (numberOfBytesConsumed < bytesRemainingInVariablePart) {
        final Result<String> decodedTopicName = decodeString(buffer);
        numberOfBytesConsumed += decodedTopicName.numberOfBytesConsumed;
        int qos = buffer.readUnsignedByte() & 0x03;
        numberOfBytesConsumed++;
        subscribeTopics.add(new MqttTopicSubscription(decodedTopicName.value, MqttQoS.valueOf(qos)));
    }
    return new Result<MqttSubscribePayload>(new MqttSubscribePayload(subscribeTopics), numberOfBytesConsumed);
}
 
Example 10
Project: DovakinMQ   File: MqttDecoder.java   Source Code and License Vote up 5 votes
private static Result<MqttSubAckPayload> decodeSubackPayload(
        ByteBuf buffer,
        int bytesRemainingInVariablePart) {
    final List<Integer> grantedQos = new ArrayList<Integer>();
    int numberOfBytesConsumed = 0;
    while (numberOfBytesConsumed < bytesRemainingInVariablePart) {
        int qos = buffer.readUnsignedByte() & 0x03;
        numberOfBytesConsumed++;
        grantedQos.add(qos);
    }
    return new Result<MqttSubAckPayload>(new MqttSubAckPayload(grantedQos), numberOfBytesConsumed);
}
 
Example 11
Project: Quavo   File: UpdateEncoder.java   Source Code and License Vote up 5 votes
@Override
protected void encode(ChannelHandlerContext ctx, UpdateResponse msg, ByteBuf out) throws Exception {
	int type = msg.getType();
	int id = msg.getId();
	ByteBuf container = msg.getContainer();

	int compression = container.readUnsignedByte();
	int length = container.readInt();

	out.writeByte(type);
	out.writeShort(id);
	out.writeByte(compression);
	out.writeInt(length);

	int bytes = container.readableBytes();
	if (bytes > 504) {
		bytes = 504;
	}
	out.writeBytes(container.readBytes(bytes));
	while ((bytes = container.readableBytes()) != 0) {
		if (bytes == 0) {
			break;
		} else if (bytes > 511) {
			bytes = 511;
		}
		out.writeByte(0xff);
		out.writeBytes(container.readBytes(bytes));
	}
}
 
Example 12
Project: Quavo   File: UpdateDecoder.java   Source Code and License Vote up 5 votes
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
	if (!in.isReadable() || in.readableBytes() < 4) {
		return;
	}

	Optional<UpdateType> request = UpdateType.getType(in.readUnsignedByte());
	if (request.isPresent()) {
		UpdateType updateType = request.get();
		switch (updateType) {
		case LOW_PRIORITY_UPDATE:
		case HIGH_PRIORITY_UPDATE:
			int uid = in.readUnsignedMedium();
			int type = (uid >> 16);
			int id = (uid & 0xffff);

			out.add(new UpdateRequest(this, type, id, updateType == UpdateType.HIGH_PRIORITY_UPDATE));
			break;
		case XOR_ENCRYPTION_UPDATE:
			int key = in.readUnsignedByte();
			in.readUnsignedShort();
			out.add(new XOREncryptionRequest(this, key));
			break;
		}
	} else {
		in.readUnsignedMedium();
	}
}
 
Example 13
Project: drift   File: HeaderMessageEncoding.java   Source Code and License Vote up 4 votes
@Override
@SuppressFBWarnings("DLS_DEAD_LOCAL_STORE")
public Object readResponse(ByteBuf buffer, int sequenceId, MethodMetadata method)
        throws Exception
{
    short magic = buffer.readShort();
    verify(magic == HEADER_MAGIC, "Unexpected response header magic");
    short flags = buffer.readShort();
    verify(flags == 1, "Unexpected response header flags");

    int frameSequenceId = buffer.readInt();
    if (frameSequenceId != sequenceId) {
        throw new TApplicationException(BAD_SEQUENCE_ID, method.getName() + " failed: out of sequence response");
    }
    int headerSize = buffer.readShort() << 2;

    ByteBuf messageHeader = buffer.readBytes(headerSize);
    int protocolId = messageHeader.readUnsignedByte();
    verify(protocolId == this.protocolId, "response protocol is different than request protocol");

    int numberOfTransforms = messageHeader.readUnsignedByte();
    verify(numberOfTransforms < 128, "Too many transforms for response");

    boolean gzipCompressed = false;
    for (int i = 0; i < numberOfTransforms; i++) {
        int transform = messageHeader.readUnsignedByte();
        verify(transform == 0x01, "Unsupported response transform");
        gzipCompressed = true;
    }

    // Currently we ignore response headers from the server because there is no API to fetch the headers
    Map<String, String> normalHeaders = decodeHeaders(NORMAL_HEADERS, messageHeader);
    Map<String, String> persistentHeaders = decodeHeaders(PERSISTENT_HEADERS, messageHeader);

    ByteBuf message = buffer.readBytes(buffer.readableBytes());
    if (gzipCompressed) {
        // todo decompress
        throw new TTransportException("gzip compression not implemented");
    }

    return MessageEncoding.decodeResponse(protocolFactory, message, sequenceId, method);
}
 
Example 14
Project: DecompiledMinecraft   File: PingResponseHandler.java   Source Code and License Vote up 4 votes
public void channelRead(ChannelHandlerContext p_channelRead_1_, Object p_channelRead_2_) throws Exception
{
    ByteBuf bytebuf = (ByteBuf)p_channelRead_2_;
    bytebuf.markReaderIndex();
    boolean flag = true;

    try
    {
        if (bytebuf.readUnsignedByte() == 254)
        {
            InetSocketAddress inetsocketaddress = (InetSocketAddress)p_channelRead_1_.channel().remoteAddress();
            MinecraftServer minecraftserver = this.networkSystem.getServer();
            int i = bytebuf.readableBytes();

            switch (i)
            {
                case 0:
                    logger.debug("Ping: (<1.3.x) from {}:{}", new Object[] {inetsocketaddress.getAddress(), Integer.valueOf(inetsocketaddress.getPort())});
                    String s2 = String.format("%s\u00a7%d\u00a7%d", new Object[] {minecraftserver.getMOTD(), Integer.valueOf(minecraftserver.getCurrentPlayerCount()), Integer.valueOf(minecraftserver.getMaxPlayers())});
                    this.writeAndFlush(p_channelRead_1_, this.getStringBuffer(s2));
                    break;

                case 1:
                    if (bytebuf.readUnsignedByte() != 1)
                    {
                        return;
                    }

                    logger.debug("Ping: (1.4-1.5.x) from {}:{}", new Object[] {inetsocketaddress.getAddress(), Integer.valueOf(inetsocketaddress.getPort())});
                    String s = String.format("\u00a71\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d", new Object[] {Integer.valueOf(127), minecraftserver.getMinecraftVersion(), minecraftserver.getMOTD(), Integer.valueOf(minecraftserver.getCurrentPlayerCount()), Integer.valueOf(minecraftserver.getMaxPlayers())});
                    this.writeAndFlush(p_channelRead_1_, this.getStringBuffer(s));
                    break;

                default:
                    boolean flag1 = bytebuf.readUnsignedByte() == 1;
                    flag1 = flag1 & bytebuf.readUnsignedByte() == 250;
                    flag1 = flag1 & "MC|PingHost".equals(new String(bytebuf.readBytes(bytebuf.readShort() * 2).array(), Charsets.UTF_16BE));
                    int j = bytebuf.readUnsignedShort();
                    flag1 = flag1 & bytebuf.readUnsignedByte() >= 73;
                    flag1 = flag1 & 3 + bytebuf.readBytes(bytebuf.readShort() * 2).array().length + 4 == j;
                    flag1 = flag1 & bytebuf.readInt() <= 65535;
                    flag1 = flag1 & bytebuf.readableBytes() == 0;

                    if (!flag1)
                    {
                        return;
                    }

                    logger.debug("Ping: (1.6) from {}:{}", new Object[] {inetsocketaddress.getAddress(), Integer.valueOf(inetsocketaddress.getPort())});
                    String s1 = String.format("\u00a71\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d", new Object[] {Integer.valueOf(127), minecraftserver.getMinecraftVersion(), minecraftserver.getMOTD(), Integer.valueOf(minecraftserver.getCurrentPlayerCount()), Integer.valueOf(minecraftserver.getMaxPlayers())});
                    ByteBuf bytebuf1 = this.getStringBuffer(s1);

                    try
                    {
                        this.writeAndFlush(p_channelRead_1_, bytebuf1);
                    }
                    finally
                    {
                        bytebuf1.release();
                    }
            }

            bytebuf.release();
            flag = false;
            return;
        }
    }
    catch (RuntimeException var21)
    {
        return;
    }
    finally
    {
        if (flag)
        {
            bytebuf.resetReaderIndex();
            p_channelRead_1_.channel().pipeline().remove("legacy_query");
            p_channelRead_1_.fireChannelRead(p_channelRead_2_);
        }
    }
}
 
Example 15
Project: DecompiledMinecraft   File: PingResponseHandler.java   Source Code and License Vote up 4 votes
public void channelRead(ChannelHandlerContext p_channelRead_1_, Object p_channelRead_2_) throws Exception
{
    ByteBuf bytebuf = (ByteBuf)p_channelRead_2_;
    bytebuf.markReaderIndex();
    boolean flag = true;

    try
    {
        if (bytebuf.readUnsignedByte() == 254)
        {
            InetSocketAddress inetsocketaddress = (InetSocketAddress)p_channelRead_1_.channel().remoteAddress();
            MinecraftServer minecraftserver = this.networkSystem.getServer();
            int i = bytebuf.readableBytes();

            switch (i)
            {
                case 0:
                    logger.debug("Ping: (<1.3.x) from {}:{}", new Object[] {inetsocketaddress.getAddress(), Integer.valueOf(inetsocketaddress.getPort())});
                    String s2 = String.format("%s\u00a7%d\u00a7%d", new Object[] {minecraftserver.getMOTD(), Integer.valueOf(minecraftserver.getCurrentPlayerCount()), Integer.valueOf(minecraftserver.getMaxPlayers())});
                    this.writeAndFlush(p_channelRead_1_, this.getStringBuffer(s2));
                    break;

                case 1:
                    if (bytebuf.readUnsignedByte() != 1)
                    {
                        return;
                    }

                    logger.debug("Ping: (1.4-1.5.x) from {}:{}", new Object[] {inetsocketaddress.getAddress(), Integer.valueOf(inetsocketaddress.getPort())});
                    String s = String.format("\u00a71\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d", new Object[] {Integer.valueOf(127), minecraftserver.getMinecraftVersion(), minecraftserver.getMOTD(), Integer.valueOf(minecraftserver.getCurrentPlayerCount()), Integer.valueOf(minecraftserver.getMaxPlayers())});
                    this.writeAndFlush(p_channelRead_1_, this.getStringBuffer(s));
                    break;

                default:
                    boolean flag1 = bytebuf.readUnsignedByte() == 1;
                    flag1 = flag1 & bytebuf.readUnsignedByte() == 250;
                    flag1 = flag1 & "MC|PingHost".equals(new String(bytebuf.readBytes(bytebuf.readShort() * 2).array(), Charsets.UTF_16BE));
                    int j = bytebuf.readUnsignedShort();
                    flag1 = flag1 & bytebuf.readUnsignedByte() >= 73;
                    flag1 = flag1 & 3 + bytebuf.readBytes(bytebuf.readShort() * 2).array().length + 4 == j;
                    flag1 = flag1 & bytebuf.readInt() <= 65535;
                    flag1 = flag1 & bytebuf.readableBytes() == 0;

                    if (!flag1)
                    {
                        return;
                    }

                    logger.debug("Ping: (1.6) from {}:{}", new Object[] {inetsocketaddress.getAddress(), Integer.valueOf(inetsocketaddress.getPort())});
                    String s1 = String.format("\u00a71\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d", new Object[] {Integer.valueOf(127), minecraftserver.getMinecraftVersion(), minecraftserver.getMOTD(), Integer.valueOf(minecraftserver.getCurrentPlayerCount()), Integer.valueOf(minecraftserver.getMaxPlayers())});
                    ByteBuf bytebuf1 = this.getStringBuffer(s1);

                    try
                    {
                        this.writeAndFlush(p_channelRead_1_, bytebuf1);
                    }
                    finally
                    {
                        bytebuf1.release();
                    }
            }

            bytebuf.release();
            flag = false;
            return;
        }
    }
    catch (RuntimeException var21)
    {
        return;
    }
    finally
    {
        if (flag)
        {
            bytebuf.resetReaderIndex();
            p_channelRead_1_.channel().pipeline().remove("legacy_query");
            p_channelRead_1_.fireChannelRead(p_channelRead_2_);
        }
    }
}
 
Example 16
Project: NioImapClient   File: ResponseDecoder.java   Source Code and License Vote up 4 votes
@Timed
private void parseFetch(ByteBuf in) throws UnknownFetchItemTypeException, IOException, ResponseParseException {
  skipControlCharacters(in);

  char next = ((char) in.readUnsignedByte());
  if (next != LPAREN && next != RPAREN) {
    in.readerIndex(in.readerIndex() - 1);
  } else if (next == RPAREN) { // Check to see if this is the end of this fetch response
    char second = ((char) in.readUnsignedByte());
    if (second == HttpConstants.CR || second == HttpConstants.LF) {
      // At the end of the fetch, add the current message to the untagged responses and reset
      messageComplete();
      return;
    } else {
      in.readerIndex(in.readerIndex() - 2);
    }
  }

  String fetchItemString = fetchResponseTypeParser.parse(in);
  if (StringUtils.isBlank(fetchItemString)) {
    checkpoint(State.FETCH);
    return;
  }

  FetchDataItemType fetchType = FetchDataItemType.getFetchType(fetchItemString);
  switch (fetchType) {
    case FLAGS:
      List<String> flags = nestedArrayParserRecycler.get().parse(in).stream()
          .map(o -> ((String) o))
          .collect(Collectors.toList());
      currentMessage.setFlagStrings(flags);
      break;
    case INTERNALDATE:
      String internalDate = atomOrStringParser.parse(in);
      currentMessage.setInternalDate(internalDate);
      break;
    case RFC822_SIZE:
      currentMessage.setSize(Ints.checkedCast(numberParser.parse(in)));
      break;
    case UID:
      currentMessage.setUid(numberParser.parse(in));
      break;
    case ENVELOPE:
      currentMessage.setEnvelope(parseEnvelope(in));
      break;
    case BODY:
      startBodyParse(in);
      return;
    case X_GM_MSGID:
      currentMessage.setGmailMessageId(numberParser.parse(in));
      break;
    case X_GM_THRID:
      currentMessage.setGmailThreadId(numberParser.parse(in));
      break;
    case X_GM_LABELS:
      currentMessage.setGMailLabels(
          nestedArrayParserRecycler.get().parse(in).stream()
              .filter(o -> !(o instanceof NilMarker))
              .map(o -> ((String) o))
              .map(GMailLabel::get)
              .collect(Collectors.toSet())
      );
      break;
    case INVALID:
    default:
      throw new UnknownFetchItemTypeException(fetchItemString);
  }

  checkpoint(State.FETCH);
}
 
Example 17
Project: NioImapClient   File: BufferedBodyParser.java   Source Code and License Vote up 4 votes
@Override
public Optional<byte[]> parse(ByteBuf in) {
  for (;;) {
    switch (state) {
      case START:
        char c = ((char) in.readUnsignedByte());
        if (c == '{') {
          in.readerIndex(in.readerIndex() - 1);
          expectedSize = sizeParser.parse(in);
          state = State.SKIP_CRLF;
          return Optional.empty();
        } else if (Character.isWhitespace(c)) {
          continue;
        } else {
          in.readerIndex(in.readerIndex() - 1);
          state = State.PARSE_STRING;
          continue;
        }
      case SKIP_CRLF:
        in.readBytes(2);
        state = State.PARSE_SIZE;
        continue;
      case PARSE_STRING:
        return Optional.of(stringParser.parse(in).getBytes(StandardCharsets.UTF_8));
      case PARSE_SIZE:
        if (buf == null) {
          buf = PooledByteBufAllocator.DEFAULT.buffer(expectedSize);
        }

        pos = in.readerIndex();
        try {
          while (size < expectedSize) {
            buf.writeByte((char) in.readUnsignedByte());
            inc();
          }
        } catch (Signal e) {
          e.expect(REPLAYING_SIGNAL);
          in.readerIndex(pos);
          return Optional.empty();
        }

        byte[] result = new byte[buf.readableBytes()];
        buf.getBytes(buf.readerIndex(), result);
        reset();

        return Optional.of(result);
    }
  }

}
 
Example 18
Project: CustomWorldGen   File: LegacyPingHandler.java   Source Code and License Vote up 4 votes
public void channelRead(ChannelHandlerContext p_channelRead_1_, Object p_channelRead_2_) throws Exception
{
    ByteBuf bytebuf = (ByteBuf)p_channelRead_2_;
    bytebuf.markReaderIndex();
    boolean flag = true;

    try
    {
        if (bytebuf.readUnsignedByte() == 254)
        {
            InetSocketAddress inetsocketaddress = (InetSocketAddress)p_channelRead_1_.channel().remoteAddress();
            MinecraftServer minecraftserver = this.networkSystem.getServer();
            int i = bytebuf.readableBytes();

            switch (i)
            {
                case 0:
                    LOGGER.debug("Ping: (<1.3.x) from {}:{}", new Object[] {inetsocketaddress.getAddress(), Integer.valueOf(inetsocketaddress.getPort())});
                    String s2 = String.format("%s\u00a7%d\u00a7%d", new Object[] {minecraftserver.getMOTD(), Integer.valueOf(minecraftserver.getCurrentPlayerCount()), Integer.valueOf(minecraftserver.getMaxPlayers())});
                    this.writeAndFlush(p_channelRead_1_, this.getStringBuffer(s2));
                    break;
                case 1:

                    if (bytebuf.readUnsignedByte() != 1)
                    {
                        return;
                    }

                    LOGGER.debug("Ping: (1.4-1.5.x) from {}:{}", new Object[] {inetsocketaddress.getAddress(), Integer.valueOf(inetsocketaddress.getPort())});
                    String s = String.format("\u00a71\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d", new Object[] {Integer.valueOf(127), minecraftserver.getMinecraftVersion(), minecraftserver.getMOTD(), Integer.valueOf(minecraftserver.getCurrentPlayerCount()), Integer.valueOf(minecraftserver.getMaxPlayers())});
                    this.writeAndFlush(p_channelRead_1_, this.getStringBuffer(s));
                    break;
                default:
                    boolean flag1 = bytebuf.readUnsignedByte() == 1;
                    flag1 = flag1 & bytebuf.readUnsignedByte() == 250;
                    flag1 = flag1 & "MC|PingHost".equals(new String(bytebuf.readBytes(bytebuf.readShort() * 2).array(), Charsets.UTF_16BE));
                    int j = bytebuf.readUnsignedShort();
                    flag1 = flag1 & bytebuf.readUnsignedByte() >= 73;
                    flag1 = flag1 & 3 + bytebuf.readBytes(bytebuf.readShort() * 2).array().length + 4 == j;
                    flag1 = flag1 & bytebuf.readInt() <= 65535;
                    flag1 = flag1 & bytebuf.readableBytes() == 0;

                    if (!flag1)
                    {
                        return;
                    }

                    LOGGER.debug("Ping: (1.6) from {}:{}", new Object[] {inetsocketaddress.getAddress(), Integer.valueOf(inetsocketaddress.getPort())});
                    String s1 = String.format("\u00a71\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d", new Object[] {Integer.valueOf(127), minecraftserver.getMinecraftVersion(), minecraftserver.getMOTD(), Integer.valueOf(minecraftserver.getCurrentPlayerCount()), Integer.valueOf(minecraftserver.getMaxPlayers())});
                    ByteBuf bytebuf1 = this.getStringBuffer(s1);

                    try
                    {
                        this.writeAndFlush(p_channelRead_1_, bytebuf1);
                    }
                    finally
                    {
                        bytebuf1.release();
                    }
            }

            bytebuf.release();
            flag = false;
            return;
        }
    }
    catch (RuntimeException var21)
    {
        return;
    }
    finally
    {
        if (flag)
        {
            bytebuf.resetReaderIndex();
            p_channelRead_1_.channel().pipeline().remove("legacy_query");
            p_channelRead_1_.fireChannelRead(p_channelRead_2_);
        }
    }
}
 
Example 19
Project: BaseClient   File: PingResponseHandler.java   Source Code and License Vote up 4 votes
public void channelRead(ChannelHandlerContext p_channelRead_1_, Object p_channelRead_2_) throws Exception
{
    ByteBuf bytebuf = (ByteBuf)p_channelRead_2_;
    bytebuf.markReaderIndex();
    boolean flag = true;

    try
    {
        if (bytebuf.readUnsignedByte() == 254)
        {
            InetSocketAddress inetsocketaddress = (InetSocketAddress)p_channelRead_1_.channel().remoteAddress();
            MinecraftServer minecraftserver = this.networkSystem.getServer();
            int i = bytebuf.readableBytes();

            switch (i)
            {
                case 0:
                    logger.debug("Ping: (<1.3.x) from {}:{}", new Object[] {inetsocketaddress.getAddress(), Integer.valueOf(inetsocketaddress.getPort())});
                    String s2 = String.format("%s\u00a7%d\u00a7%d", new Object[] {minecraftserver.getMOTD(), Integer.valueOf(minecraftserver.getCurrentPlayerCount()), Integer.valueOf(minecraftserver.getMaxPlayers())});
                    this.writeAndFlush(p_channelRead_1_, this.getStringBuffer(s2));
                    break;

                case 1:
                    if (bytebuf.readUnsignedByte() != 1)
                    {
                        return;
                    }

                    logger.debug("Ping: (1.4-1.5.x) from {}:{}", new Object[] {inetsocketaddress.getAddress(), Integer.valueOf(inetsocketaddress.getPort())});
                    String s = String.format("\u00a71\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d", new Object[] {Integer.valueOf(127), minecraftserver.getMinecraftVersion(), minecraftserver.getMOTD(), Integer.valueOf(minecraftserver.getCurrentPlayerCount()), Integer.valueOf(minecraftserver.getMaxPlayers())});
                    this.writeAndFlush(p_channelRead_1_, this.getStringBuffer(s));
                    break;

                default:
                    boolean flag1 = bytebuf.readUnsignedByte() == 1;
                    flag1 = flag1 & bytebuf.readUnsignedByte() == 250;
                    flag1 = flag1 & "MC|PingHost".equals(new String(bytebuf.readBytes(bytebuf.readShort() * 2).array(), Charsets.UTF_16BE));
                    int j = bytebuf.readUnsignedShort();
                    flag1 = flag1 & bytebuf.readUnsignedByte() >= 73;
                    flag1 = flag1 & 3 + bytebuf.readBytes(bytebuf.readShort() * 2).array().length + 4 == j;
                    flag1 = flag1 & bytebuf.readInt() <= 65535;
                    flag1 = flag1 & bytebuf.readableBytes() == 0;

                    if (!flag1)
                    {
                        return;
                    }

                    logger.debug("Ping: (1.6) from {}:{}", new Object[] {inetsocketaddress.getAddress(), Integer.valueOf(inetsocketaddress.getPort())});
                    String s1 = String.format("\u00a71\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d", new Object[] {Integer.valueOf(127), minecraftserver.getMinecraftVersion(), minecraftserver.getMOTD(), Integer.valueOf(minecraftserver.getCurrentPlayerCount()), Integer.valueOf(minecraftserver.getMaxPlayers())});
                    ByteBuf bytebuf1 = this.getStringBuffer(s1);

                    try
                    {
                        this.writeAndFlush(p_channelRead_1_, bytebuf1);
                    }
                    finally
                    {
                        bytebuf1.release();
                    }
            }

            bytebuf.release();
            flag = false;
            return;
        }
    }
    catch (RuntimeException var21)
    {
        return;
    }
    finally
    {
        if (flag)
        {
            bytebuf.resetReaderIndex();
            p_channelRead_1_.channel().pipeline().remove("legacy_query");
            p_channelRead_1_.fireChannelRead(p_channelRead_2_);
        }
    }
}
 
Example 20
Project: BaseClient   File: PingResponseHandler.java   Source Code and License Vote up 4 votes
public void channelRead(ChannelHandlerContext p_channelRead_1_, Object p_channelRead_2_) throws Exception
{
    ByteBuf bytebuf = (ByteBuf)p_channelRead_2_;
    bytebuf.markReaderIndex();
    boolean flag = true;

    try
    {
        if (bytebuf.readUnsignedByte() == 254)
        {
            InetSocketAddress inetsocketaddress = (InetSocketAddress)p_channelRead_1_.channel().remoteAddress();
            MinecraftServer minecraftserver = this.networkSystem.getServer();
            int i = bytebuf.readableBytes();

            switch (i)
            {
                case 0:
                    logger.debug("Ping: (<1.3.x) from {}:{}", new Object[] {inetsocketaddress.getAddress(), Integer.valueOf(inetsocketaddress.getPort())});
                    String s2 = String.format("%s\u00a7%d\u00a7%d", new Object[] {minecraftserver.getMOTD(), Integer.valueOf(minecraftserver.getCurrentPlayerCount()), Integer.valueOf(minecraftserver.getMaxPlayers())});
                    this.writeAndFlush(p_channelRead_1_, this.getStringBuffer(s2));
                    break;

                case 1:
                    if (bytebuf.readUnsignedByte() != 1)
                    {
                        return;
                    }

                    logger.debug("Ping: (1.4-1.5.x) from {}:{}", new Object[] {inetsocketaddress.getAddress(), Integer.valueOf(inetsocketaddress.getPort())});
                    String s = String.format("\u00a71\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d", new Object[] {Integer.valueOf(127), minecraftserver.getMinecraftVersion(), minecraftserver.getMOTD(), Integer.valueOf(minecraftserver.getCurrentPlayerCount()), Integer.valueOf(minecraftserver.getMaxPlayers())});
                    this.writeAndFlush(p_channelRead_1_, this.getStringBuffer(s));
                    break;

                default:
                    boolean flag1 = bytebuf.readUnsignedByte() == 1;
                    flag1 = flag1 & bytebuf.readUnsignedByte() == 250;
                    flag1 = flag1 & "MC|PingHost".equals(new String(bytebuf.readBytes(bytebuf.readShort() * 2).array(), Charsets.UTF_16BE));
                    int j = bytebuf.readUnsignedShort();
                    flag1 = flag1 & bytebuf.readUnsignedByte() >= 73;
                    flag1 = flag1 & 3 + bytebuf.readBytes(bytebuf.readShort() * 2).array().length + 4 == j;
                    flag1 = flag1 & bytebuf.readInt() <= 65535;
                    flag1 = flag1 & bytebuf.readableBytes() == 0;

                    if (!flag1)
                    {
                        return;
                    }

                    logger.debug("Ping: (1.6) from {}:{}", new Object[] {inetsocketaddress.getAddress(), Integer.valueOf(inetsocketaddress.getPort())});
                    String s1 = String.format("\u00a71\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d", new Object[] {Integer.valueOf(127), minecraftserver.getMinecraftVersion(), minecraftserver.getMOTD(), Integer.valueOf(minecraftserver.getCurrentPlayerCount()), Integer.valueOf(minecraftserver.getMaxPlayers())});
                    ByteBuf bytebuf1 = this.getStringBuffer(s1);

                    try
                    {
                        this.writeAndFlush(p_channelRead_1_, bytebuf1);
                    }
                    finally
                    {
                        bytebuf1.release();
                    }
            }

            bytebuf.release();
            flag = false;
            return;
        }
    }
    catch (RuntimeException var21)
    {
        return;
    }
    finally
    {
        if (flag)
        {
            bytebuf.resetReaderIndex();
            p_channelRead_1_.channel().pipeline().remove("legacy_query");
            p_channelRead_1_.fireChannelRead(p_channelRead_2_);
        }
    }
}