Example usage for io.netty.buffer ByteBuf writeShortLE

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

Introduction

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

Prototype

public abstract ByteBuf writeShortLE(int value);

Source Link

Document

Sets the specified 16-bit short integer in the Little Endian Byte Order at the current writerIndex and increases the writerIndex by 2 in this buffer.

Usage

From source file:org.lanternpowered.server.network.query.QueryHandler.java

License:MIT License

private void handleBasicStats(ChannelHandlerContext ctx, DatagramPacket packet, int sessionId) {
    LanternServer server = this.queryServer.getGame().getServer();

    // TODO: Find out how to support the size and max size properties
    final QueryServerEvent.Basic event = SpongeEventFactory.createQueryServerEventBasic(
            Cause.source(ctx.channel().remoteAddress()).build(),
            (InetSocketAddress) ctx.channel().localAddress(), "SMP", this.getWorldName(),
            server.getMotd().toPlain(), server.getMaxPlayers(), Integer.MAX_VALUE,
            server.getOnlinePlayers().size(), 0);
    Sponge.getEventManager().post(event);

    final InetSocketAddress address = event.getAddress();

    ByteBuf buf = ctx.alloc().buffer();
    buf.writeByte(ACTION_STATS);/*from   w  w  w.  j av a  2s . c o  m*/
    buf.writeInt(sessionId);
    writeString(buf, event.getMotd());
    writeString(buf, event.getGameType());
    writeString(buf, event.getMap());
    writeString(buf, String.valueOf(event.getPlayerCount()));
    writeString(buf, String.valueOf(event.getMaxPlayerCount()));
    buf.writeShortLE(address.getPort());
    writeString(buf, address.getHostString());
    ctx.write(new DatagramPacket(buf, packet.sender()));
}

From source file:org.traccar.protocol.AnytrekProtocolDecoder.java

License:Apache License

private void sendResponse(Channel channel, SocketAddress remoteAddress, int type) {
    if (channel != null) {
        ByteBuf response = Unpooled.buffer();
        response.writeShort(0x7878);//from w w w  . j  av  a  2s .  c om
        response.writeShortLE(1 + 1 + 2 + 1 + 2); // length
        response.writeByte(type);
        response.writeByte(0); // error
        response.writeShortLE(0); // report interval
        response.writeByte(0); // clear alarm
        response.writeShortLE(0); // checksum
        response.writeByte('\r');
        response.writeByte('\n');
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }
}

From source file:org.traccar.protocol.ApelProtocolDecoder.java

License:Apache License

private void sendSimpleMessage(Channel channel, short type) {
    ByteBuf request = Unpooled.buffer(8);
    request.writeShortLE(type);
    request.writeShortLE(0);/*from  w w  w.  ja v  a2  s .c  om*/
    request.writeIntLE(Checksum.crc32(request.nioBuffer(0, 4)));
    channel.writeAndFlush(new NetworkMessage(request, channel.remoteAddress()));
}

From source file:org.traccar.protocol.ApelProtocolDecoder.java

License:Apache License

private void requestArchive(Channel channel) {
    if (lastIndex == 0) {
        lastIndex = newIndex;//from  w w w .j  ava  2  s  . co m
    } else if (newIndex > lastIndex) {
        ByteBuf request = Unpooled.buffer(14);
        request.writeShortLE(MSG_REQUEST_LOG_RECORDS);
        request.writeShortLE(6);
        request.writeIntLE((int) lastIndex);
        request.writeShortLE(512);
        request.writeIntLE(Checksum.crc32(request.nioBuffer(0, 10)));
        channel.writeAndFlush(new NetworkMessage(request, channel.remoteAddress()));
    }
}

From source file:org.traccar.protocol.AutoTrackProtocolDecoder.java

License:Apache License

private Position decodeTelemetry(Channel channel, SocketAddress remoteAddress, DeviceSession deviceSession,
        ByteBuf buf) {/*from ww w.  j  a va2  s  .c  o  m*/

    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());

    position.setTime(new Date(1009843200000L + buf.readUnsignedIntLE() * 1000)); // seconds since 2002
    position.setLatitude(buf.readIntLE() * 0.0000001);
    position.setLongitude(buf.readIntLE() * 0.0000001);

    position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());
    position.set(Position.KEY_FUEL_USED, buf.readUnsignedIntLE());

    position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShortLE()));
    buf.readUnsignedShortLE(); // max speed

    position.set(Position.KEY_INPUT, buf.readUnsignedShortLE());
    buf.readUnsignedIntLE(); // di 3 count
    buf.readUnsignedIntLE(); // di 4 count

    for (int i = 0; i < 5; i++) {
        position.set(Position.PREFIX_ADC + (i + 1), buf.readUnsignedShortLE());
    }

    position.setCourse(buf.readUnsignedShortLE());

    position.set(Position.KEY_STATUS, buf.readUnsignedShortLE());
    position.set(Position.KEY_EVENT, buf.readUnsignedShortLE());
    position.set(Position.KEY_DRIVER_UNIQUE_ID, buf.readLongLE());

    int index = buf.readUnsignedShortLE();

    buf.readUnsignedShortLE(); // checksum

    if (channel != null) {
        ByteBuf response = Unpooled.buffer();
        response.writeInt(0xF1F1F1F1); // sync
        response.writeByte(MSG_TELEMETRY_CONFIRM);
        response.writeShortLE(2); // length
        response.writeShortLE(index);
        response.writeShort(Checksum.crc16(Checksum.CRC16_XMODEM, response.nioBuffer()));
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }

    return position;
}

From source file:org.traccar.protocol.AutoTrackProtocolDecoder.java

License:Apache License

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {

    ByteBuf buf = (ByteBuf) msg;/*from  ww w  . j  av a2  s .c o  m*/

    buf.skipBytes(4); // sync
    int type = buf.readUnsignedByte();
    buf.readUnsignedShortLE(); // length

    switch (type) {
    case MSG_LOGIN_REQUEST:
        String imei = ByteBufUtil.hexDump(buf.readBytes(8));
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
        if (deviceSession == null) {
            return null;
        }
        int fuelConst = buf.readUnsignedShortLE();
        int tripConst = buf.readUnsignedShortLE();
        if (channel != null) {
            ByteBuf response = Unpooled.buffer();
            response.writeInt(0xF1F1F1F1); // sync
            response.writeByte(MSG_LOGIN_CONFIRM);
            response.writeShortLE(12); // length
            response.writeBytes(ByteBufUtil.decodeHexDump(imei));
            response.writeShortLE(fuelConst);
            response.writeShortLE(tripConst);
            response.writeShort(Checksum.crc16(Checksum.CRC16_XMODEM, response.nioBuffer()));
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }
        return null;
    case MSG_TELEMETRY_1:
    case MSG_TELEMETRY_2:
    case MSG_TELEMETRY_3:
        deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession == null) {
            return null;
        }
        return decodeTelemetry(channel, remoteAddress, deviceSession, buf);
    default:
        return null;
    }
}

From source file:org.traccar.protocol.BceProtocolDecoder.java

License:Apache License

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {

    ByteBuf buf = (ByteBuf) msg;/*from  w  w  w.j ava  2s. com*/

    String imei = String.format("%015d", buf.readLongLE());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }

    List<Position> positions = new LinkedList<>();

    while (buf.readableBytes() > 1) {

        int dataEnd = buf.readUnsignedShortLE() + buf.readerIndex();
        int type = buf.readUnsignedByte();

        if (type != MSG_ASYNC_STACK && type != MSG_TIME_TRIGGERED) {
            return null;
        }

        int confirmKey = buf.readUnsignedByte() & 0x7F;

        while (buf.readerIndex() < dataEnd) {

            Position position = new Position(getProtocolName());
            position.setDeviceId(deviceSession.getDeviceId());

            int structEnd = buf.readUnsignedByte() + buf.readerIndex();

            long time = buf.readUnsignedIntLE();
            if ((time & 0x0f) == DATA_TYPE) {

                time = time >> 4 << 1;
                time += 0x47798280; // 01/01/2008
                position.setTime(new Date(time * 1000));

                // Read masks
                int mask;
                List<Integer> masks = new LinkedList<>();
                do {
                    mask = buf.readUnsignedShortLE();
                    masks.add(mask);
                } while (BitUtil.check(mask, 15));

                mask = masks.get(0);

                if (BitUtil.check(mask, 0)) {
                    position.setValid(true);
                    position.setLongitude(buf.readFloatLE());
                    position.setLatitude(buf.readFloatLE());
                    position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));

                    int status = buf.readUnsignedByte();
                    position.set(Position.KEY_SATELLITES, BitUtil.to(status, 4));
                    position.set(Position.KEY_HDOP, BitUtil.from(status, 4));

                    position.setCourse(buf.readUnsignedByte() * 2);
                    position.setAltitude(buf.readUnsignedShortLE());

                    position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());
                }

                if (BitUtil.check(mask, 1)) {
                    position.set(Position.KEY_INPUT, buf.readUnsignedShortLE());
                }

                for (int i = 1; i <= 8; i++) {
                    if (BitUtil.check(mask, i + 1)) {
                        position.set(Position.PREFIX_ADC + i, buf.readUnsignedShortLE());
                    }
                }

                if (BitUtil.check(mask, 10)) {
                    buf.skipBytes(4);
                }
                if (BitUtil.check(mask, 11)) {
                    buf.skipBytes(4);
                }
                if (BitUtil.check(mask, 12)) {
                    buf.skipBytes(2);
                }
                if (BitUtil.check(mask, 13)) {
                    buf.skipBytes(2);
                }

                if (BitUtil.check(mask, 14)) {
                    position.setNetwork(new Network(CellTower.from(buf.readUnsignedShortLE(),
                            buf.readUnsignedByte(), buf.readUnsignedShortLE(), buf.readUnsignedShortLE(),
                            buf.readUnsignedByte())));
                    buf.readUnsignedByte();
                }

                if (BitUtil.check(mask, 0)) {
                    positions.add(position);
                }
            }

            buf.readerIndex(structEnd);
        }

        // Send response
        if (type == MSG_ASYNC_STACK && channel != null) {
            ByteBuf response = Unpooled.buffer(8 + 2 + 2 + 1);
            response.writeLongLE(Long.parseLong(imei));
            response.writeShortLE(2);
            response.writeByte(MSG_STACK_COFIRM);
            response.writeByte(confirmKey);

            int checksum = 0;
            for (int i = 0; i < response.writerIndex(); i++) {
                checksum += response.getUnsignedByte(i);
            }
            response.writeByte(checksum);

            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }
    }

    return positions;
}

From source file:org.traccar.protocol.BceProtocolEncoder.java

License:Apache License

@Override
protected Object encodeCommand(Command command) {

    if (command.getType().equals(Command.TYPE_OUTPUT_CONTROL)) {
        ByteBuf buf = Unpooled.buffer();

        buf.writeLongLE(Long.parseLong(getUniqueId(command.getDeviceId())));
        buf.writeShortLE(1 + 1 + 3 + 1); // length
        buf.writeByte(BceProtocolDecoder.MSG_OUTPUT_CONTROL);
        buf.writeByte(command.getInteger(Command.KEY_INDEX) == 1 ? 0x0A : 0x0B);
        buf.writeByte(0xFF); // index
        buf.writeByte(0x00); // form id
        buf.writeShortLE(Integer.parseInt(command.getString(Command.KEY_DATA)) > 0 ? 0x0055 : 0x0000);
        buf.writeByte(Checksum.sum(buf.nioBuffer()));

        return buf;
    } else {/*from  w  w  w  .  j a v a 2  s .  co m*/
        return null;
    }
}

From source file:org.traccar.protocol.BlackKiteProtocolDecoder.java

License:Apache License

private void sendReply(Channel channel, int checksum) {
    if (channel != null) {
        ByteBuf reply = Unpooled.buffer(3);
        reply.writeByte(0x02);//from  ww  w  . ja v a  2 s .  co  m
        reply.writeShortLE((short) checksum);
        channel.writeAndFlush(new NetworkMessage(reply, channel.remoteAddress()));
    }
}

From source file:org.traccar.protocol.CastelProtocolDecoder.java

License:Apache License

private void sendResponse(Channel channel, SocketAddress remoteAddress, int version, ByteBuf id, short type,
        ByteBuf content) {//from   w w  w .j  av  a 2  s.  c om

    if (channel != null) {
        int length = 2 + 2 + 1 + id.readableBytes() + 2 + 2 + 2;
        if (content != null) {
            length += content.readableBytes();
        }

        ByteBuf response = Unpooled.buffer(length);
        response.writeByte('@');
        response.writeByte('@');
        response.writeShortLE(length);
        response.writeByte(version);
        response.writeBytes(id);
        response.writeShort(type);
        if (content != null) {
            response.writeBytes(content);
            content.release();
        }
        response.writeShortLE(
                Checksum.crc16(Checksum.CRC16_X25, response.nioBuffer(0, response.writerIndex())));
        response.writeByte(0x0D);
        response.writeByte(0x0A);
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }
}