Example usage for io.netty.buffer ByteBuf readShort

List of usage examples for io.netty.buffer ByteBuf readShort

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf readShort.

Prototype

public abstract short readShort();

Source Link

Document

Gets a 16-bit short integer at the current readerIndex and increases the readerIndex by 2 in this buffer.

Usage

From source file:org.eclipse.neoscada.protocol.iec60870.asdu.types.TypeHelper.java

License:Open Source License

/**
 * Parse Short integer number with quality descriptor
 *//*  w w  w . j ava  2 s .  c  o m*/
public static Value<Short> parseScaledValue(final ProtocolOptions options, final ByteBuf data,
        final boolean withTimestamp) {
    final short value = data.readShort();

    final byte qds = data.readByte();

    final QualityInformation qualityInformation = QualityInformation.parse(qds);
    final boolean overflow = (qds & 0b00000001) > 0;

    final long timestamp = withTimestamp ? parseTimestamp(options, data) : System.currentTimeMillis();

    return new Value<Short>(value, timestamp, qualityInformation, overflow);
}

From source file:org.eclipse.neoscada.protocol.iec60870.asdu.types.TypeHelper.java

License:Open Source License

/**
 * Parse Double as normalized value with quality descriptor
 *///from   www.  j  av  a  2 s  .  co  m
public static Value<Double> parseNormalizedValue(final ProtocolOptions options, final ByteBuf data,
        final boolean withTimestamp) {
    final short value = data.readShort();

    final byte qds = data.readByte();

    final QualityInformation qualityInformation = QualityInformation.parse(qds);
    final boolean overflow = (qds & 0b00000001) > 0;

    final long timestamp = withTimestamp ? parseTimestamp(options, data) : System.currentTimeMillis();

    return new Value<Double>((double) (value / 32768.0), timestamp, qualityInformation, overflow);
}

From source file:org.evilco.mc.defense.common.network.DefenseStationRegisterUserPacket.java

License:Apache License

/**
 * Constructs a new DefenseStationRegisterUserPacket from a serialized packet buffer.
 * @param buffer The packet buffer./*from  www  .ja  va  2s.  c o m*/
 */
public DefenseStationRegisterUserPacket(ByteBuf buffer) {
    super(buffer);

    // read coordinates
    this.xCoord = buffer.readInt();
    this.yCoord = buffer.readInt();
    this.zCoord = buffer.readInt();

    // read mode
    this.blacklist = buffer.readBoolean();

    // read length
    int length = buffer.readShort();

    // create buffer
    byte[] usernameRaw = new byte[length];

    // read buffer
    buffer.readBytes(usernameRaw);

    // convert string
    this.username = new String(usernameRaw, Charsets.UTF_8);
}

From source file:org.evilco.mc.defense.common.network.DefenseStationUnregisterPacket.java

License:Apache License

/**
 * Constructs a new DefenseStationUnregisterPacket.
 * @param buffer The packet buffer./*w ww . j  av a 2s  . c o m*/
 */
public DefenseStationUnregisterPacket(ByteBuf buffer) {
    super(buffer);

    this.xCoord = buffer.readInt();
    this.yCoord = buffer.readInt();
    this.zCoord = buffer.readInt();

    // read length
    int length = buffer.readShort();

    // create buffer
    byte[] buf = new byte[length];

    // read data
    buffer.readBytes(buf);

    // construct UUID
    this.userID = UUID.fromString(new String(buf, Charsets.UTF_8));
}

From source file:org.hawkular.metrics.clients.ptrans.collectd.packet.CollectdPacketDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext context, DatagramPacket packet, List<Object> out) throws Exception {
    long start = System.currentTimeMillis();
    ByteBuf content = packet.content();
    List<Part> parts = new ArrayList<>(100);
    for (;;) {//from  w ww.j  a  v a  2  s . c o  m
        if (!hasReadableBytes(content, 4)) {
            break;
        }
        short partTypeId = content.readShort();
        PartType partType = PartType.findById(partTypeId);
        int partLength = content.readUnsignedShort();
        int valueLength = partLength - 4;
        if (!hasReadableBytes(content, valueLength)) {
            break;
        }
        if (partType == null) {
            content.skipBytes(valueLength);
            continue;
        }
        Part part;
        switch (partType) {
        case HOST:
        case PLUGIN:
        case PLUGIN_INSTANCE:
        case TYPE:
        case INSTANCE:
            part = new StringPart(partType, readStringPartContent(content, valueLength));
            break;
        case TIME:
        case TIME_HIGH_RESOLUTION:
        case INTERVAL:
        case INTERVAL_HIGH_RESOLUTION:
            part = new NumericPart(partType, readNumericPartContent(content));
            break;
        case VALUES:
            part = new ValuePart(partType, readValuePartContent(content, valueLength));
            break;
        default:
            part = null;
            content.skipBytes(valueLength);
        }
        //noinspection ConstantConditions
        if (part != null) {
            logger.trace("Decoded part: {}", part);
            parts.add(part);
        }
    }

    if (logger.isTraceEnabled()) {
        long stop = System.currentTimeMillis();
        logger.trace("Decoded datagram in {} ms", stop - start);
    }

    if (parts.size() > 0) {
        CollectdPacket collectdPacket = new CollectdPacket(parts);
        out.add(collectdPacket);
    } else {
        logger.debug("No parts decoded, no CollectdPacket output");
    }
}

From source file:org.jfxvnc.net.rfb.codec.decoder.rect.CopyRectDecoder.java

License:Apache License

@Override
public boolean decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

    if (!in.isReadable(4)) {
        return false;
    }/*from  w w  w  .ja  va  2s.com*/
    int sryX = in.readShort();
    int srcY = in.readShort();

    out.add(new CopyImageRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight(), sryX, srcY));
    return true;
}

From source file:org.jupiter.transport.netty.handler.ProtocolDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    switch (state()) {
    case HEADER_MAGIC:
        checkMagic(in.readShort()); // MAGIC
        checkpoint(State.HEADER_SIGN);
    case HEADER_SIGN:
        header.sign(in.readByte()); // ??
        checkpoint(State.HEADER_STATUS);
    case HEADER_STATUS:
        header.status(in.readByte()); // ??
        checkpoint(State.HEADER_ID);
    case HEADER_ID:
        header.id(in.readLong()); // ?id
        checkpoint(State.HEADER_BODY_LENGTH);
    case HEADER_BODY_LENGTH:
        header.bodyLength(in.readInt()); // ?
        checkpoint(State.BODY);/*from w  w w.j a v a2  s.c  o m*/
    case BODY:
        switch (header.messageCode()) {
        case JProtocolHeader.HEARTBEAT:
            break;
        case JProtocolHeader.REQUEST: {
            int length = checkBodyLength(header.bodyLength());
            byte[] bytes = new byte[length];
            in.readBytes(bytes);

            JRequestBytes request = new JRequestBytes(header.id());
            request.timestamp(SystemClock.millisClock().now());
            request.bytes(header.serializerCode(), bytes);

            out.add(request);

            break;
        }
        case JProtocolHeader.RESPONSE: {
            int length = checkBodyLength(header.bodyLength());
            byte[] bytes = new byte[length];
            in.readBytes(bytes);

            JResponseBytes response = new JResponseBytes(header.id());
            response.status(header.status());
            response.bytes(header.serializerCode(), bytes);

            out.add(response);

            break;
        }
        default:
            throw IoSignals.ILLEGAL_SIGN;
        }
        checkpoint(State.HEADER_MAGIC);
    }
}

From source file:org.lanternpowered.pingy.PingyHandler.java

License:MIT License

private void handleHandshake(ChannelHandlerContext ctx, ByteBuf msg) {
    if (this.state != ProtocolState.HANDSHAKE) {
        ctx.channel().close();/*from  w w  w .  j a v a 2 s . c  o m*/
        throw new DecoderException("Received unexpected handshake message");
    }
    this.protocolVersion = readVarInt(msg); // Protocol version
    readByteArray(msg, 255 * 4); // Hostname
    msg.readShort(); // Port

    final ProtocolState state = ProtocolState.fromId(readVarInt(msg)); // Protocol state
    switch (state) {
    case HANDSHAKE:
    case PLAY:
        ctx.channel().close();
        throw new DecoderException("Received unexpected handshake message");
    case LOGIN:
        sendMessage(ctx, 0x00, buf -> writeByteArray(buf,
                GSON.toJson(fixJson(this.properties.getDisconnectMessage())).getBytes(StandardCharsets.UTF_8)))
                        .addListener(ChannelFutureListener.CLOSE);
        return;
    case STATUS:
        this.state = state;
        return;
    default:
        throw new IllegalStateException("Unsupported protocol state: " + state);
    }
}

From source file:org.lanternpowered.pingy.PingyLegacyHandler.java

License:MIT License

private boolean tryHandleLegacyPing(ChannelHandlerContext ctx, ByteBuf msg) {
    int readable = msg.readableBytes();

    // Full message, contains more info
    boolean full = false;

    if (readable > 0) {
        // Is always 1
        if (msg.readUnsignedByte() != 1) {
            return false;
        }//from   w  w w  .  j a  va  2s  . c  o m
        full = true;
    }

    if (readable > 1) {
        if (msg.readUnsignedByte() != 0xfa) {
            return false;
        }
        byte[] bytes = new byte[msg.readShort() << 1];
        msg.readBytes(bytes);
        if (!new String(bytes, StandardCharsets.UTF_16BE).equals("MC|PingHost")) {
            return false;
        }
    }

    final String motd = getFirstLine(this.properties.getLegacyMessageOfTheDay());
    if (full) {
        sendLegacyDisconnectMessage(ctx, String.format("\u00A7%s\0%s\0%s\0%s\0%s\0%s", 1, 127,
                this.properties.getOutdatedMessage(), motd, -1, -1));
    } else {
        sendLegacyDisconnectMessage(ctx, String.format("%s\u00A7%s\u00A7%s", motd, -1, -1));
    }

    return true;
}

From source file:org.lanternpowered.pingy.PingyLegacyHandler.java

License:MIT License

private boolean tryHandleLegacyJoin(ChannelHandlerContext ctx, ByteBuf msg) {
    msg.readByte(); // Protocol version
    int value = msg.readShort();
    // Check the length
    if (value < 0 || value > 16) {
        return false;
    }/*ww w.ja  v a  2 s.  com*/
    msg.readBytes(value << 1); // Username
    value = msg.readShort();
    // Check the length
    if (value < 0 || value > 255) {
        return false;
    }
    msg.readBytes(value << 1); // Host address
    msg.readInt(); // Port
    if (msg.readableBytes() > 0) {
        return false;
    }
    sendLegacyDisconnectMessage(ctx, this.properties.getLegacyDisconnectMessage());
    return true;
}