Example usage for io.netty.buffer ByteBuf writeShort

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

Introduction

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

Prototype

public abstract ByteBuf writeShort(int value);

Source Link

Document

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

Usage

From source file:com.ogarproject.ogar.server.net.packet.outbound.PacketOutUpdateNodes.java

License:Open Source License

@Override
public void writeData(ByteBuf buf) {
    // Removals by eating
    int lengthIndex = buf.writerIndex();
    int eaten = 0;
    buf.writerIndex(lengthIndex + 2);//from   ww  w . j  ava2 s  .  c  o  m
    for (EntityImpl entity : removals) {
        if (entity.getConsumer() > 0) {
            eaten++;
            buf.writeInt(entity.getConsumer());
            buf.writeInt(entity.getID());
        }
    }
    buf.markWriterIndex();
    buf.writerIndex(lengthIndex);
    buf.writeShort(eaten);
    buf.resetWriterIndex();

    // Updates
    for (int id : updates) {
        EntityImpl entity = world.getEntity(id);
        if (entity == null) {
            // TODO - Theoretically this could be ignored, but it might cause other issues,
            // like having nonexistent entities on the player's screen. Re-evaluate this later?
            throw new MalformedPacketException("Attempted to update nonexistent entity");
        }

        buf.writeInt(entity.getID());
        buf.writeInt((int) entity.getPosition().getX());
        buf.writeInt((int) entity.getPosition().getY());
        buf.writeShort(entity.getPhysicalSize());
        buf.writeByte(entity.getColor().getRed());
        buf.writeByte(entity.getColor().getGreen());
        buf.writeByte(entity.getColor().getBlue());
        buf.writeBoolean(entity.isSpiked());
        // buf.skipBytes(18);
        if (entity instanceof CellImpl) {
            CellImpl cell = (CellImpl) entity;
            if (cell.getName() == null) {
                writeUTF16(buf, "");
            } else {
                writeUTF16(buf, cell.getName());
            }
        } else {
            writeUTF16(buf, "");
        }
    }
    buf.writeInt(0);

    // General removals
    buf.writeInt(removals.size());
    for (EntityImpl entity : removals) {
        buf.writeInt(entity.getID());
    }
}

From source file:com.pubkit.platform.messaging.protocol.mqtt.proto.parser.ConnectEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext chc, ConnectMessage message, ByteBuf out) {
    ByteBuf staticHeaderBuff = chc.alloc().buffer(12);
    ByteBuf buff = chc.alloc().buffer();
    ByteBuf variableHeaderBuff = chc.alloc().buffer(12);
    try {//from  w w w. j av a2 s.com
        staticHeaderBuff.writeBytes(Utils.encodeString("MQIsdp"));

        //version 
        staticHeaderBuff.writeByte(0x03);

        //connection flags and Strings
        byte connectionFlags = 0;
        if (message.isCleanSession()) {
            connectionFlags |= 0x02;
        }
        if (message.isWillFlag()) {
            connectionFlags |= 0x04;
        }
        connectionFlags |= ((message.getWillQos() & 0x03) << 3);
        if (message.isWillRetain()) {
            connectionFlags |= 0x020;
        }
        if (message.isPasswordFlag()) {
            connectionFlags |= 0x040;
        }
        if (message.isUserFlag()) {
            connectionFlags |= 0x080;
        }
        staticHeaderBuff.writeByte(connectionFlags);

        //Keep alive timer
        staticHeaderBuff.writeShort(message.getKeepAlive());

        //Variable part
        if (message.getClientID() != null) {
            variableHeaderBuff.writeBytes(Utils.encodeString(message.getClientID()));
            if (message.isWillFlag()) {
                variableHeaderBuff.writeBytes(Utils.encodeString(message.getWillTopic()));
                variableHeaderBuff.writeBytes(Utils.encodeString(message.getWillMessage()));
            }
            if (message.isUserFlag() && message.getUsername() != null) {
                variableHeaderBuff.writeBytes(Utils.encodeString(message.getUsername()));
                if (message.isPasswordFlag() && message.getPassword() != null) {
                    variableHeaderBuff.writeBytes(Utils.encodeString(message.getPassword()));
                }
            }
        }

        int variableHeaderSize = variableHeaderBuff.readableBytes();
        buff.writeByte(AbstractMessage.CONNECT << 4);
        buff.writeBytes(Utils.encodeRemainingLength(12 + variableHeaderSize));
        buff.writeBytes(staticHeaderBuff).writeBytes(variableHeaderBuff);

        out.writeBytes(buff);
    } finally {
        staticHeaderBuff.release();
        buff.release();
        variableHeaderBuff.release();
    }
}

From source file:com.pubkit.platform.messaging.protocol.mqtt.proto.parser.PubRelEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext chc, PubRelMessage msg, ByteBuf out) {
    out.writeByte(AbstractMessage.PUBREL << 4);
    out.writeBytes(Utils.encodeRemainingLength(2));
    out.writeShort(msg.getMessageID());
}

From source file:com.pubkit.platform.messaging.protocol.mqtt.proto.parser.SubAckEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext chc, SubAckMessage message, ByteBuf out) {
    if (message.types().isEmpty()) {
        throw new IllegalArgumentException("Found a suback message with empty topics");
    }/*from  ww  w .jav  a  2  s  .  co  m*/

    int variableHeaderSize = 2 + message.types().size();
    ByteBuf buff = chc.alloc().buffer(6 + variableHeaderSize);
    try {
        buff.writeByte(AbstractMessage.SUBACK << 4);
        buff.writeBytes(Utils.encodeRemainingLength(variableHeaderSize));
        buff.writeShort(message.getMessageID());
        for (AbstractMessage.QOSType c : message.types()) {
            buff.writeByte(c.ordinal());
        }

        out.writeBytes(buff);
    } finally {
        buff.release();
    }
}

From source file:com.pubkit.platform.messaging.protocol.mqtt.proto.parser.SubscribeEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext chc, SubscribeMessage message, ByteBuf out) {
    if (message.subscriptions().isEmpty()) {
        throw new IllegalArgumentException("Found a subscribe message with empty topics");
    }//  w  w  w . j  a va 2s .  co m

    if (message.getQos() != AbstractMessage.QOSType.LEAST_ONE) {
        throw new IllegalArgumentException("Expected a message with QOS 1, found " + message.getQos());
    }

    ByteBuf variableHeaderBuff = chc.alloc().buffer(4);
    ByteBuf buff = null;
    try {
        variableHeaderBuff.writeShort(message.getMessageID());
        for (SubscribeMessage.Couple c : message.subscriptions()) {
            variableHeaderBuff.writeBytes(Utils.encodeString(c.getTopicFilter()));
            variableHeaderBuff.writeByte(c.getQos());
        }

        int variableHeaderSize = variableHeaderBuff.readableBytes();
        byte flags = Utils.encodeFlags(message);
        buff = chc.alloc().buffer(2 + variableHeaderSize);

        buff.writeByte(AbstractMessage.SUBSCRIBE << 4 | flags);
        buff.writeBytes(Utils.encodeRemainingLength(variableHeaderSize));
        buff.writeBytes(variableHeaderBuff);

        out.writeBytes(buff);
    } finally {
        variableHeaderBuff.release();
        buff.release();
    }
}

From source file:com.pubkit.platform.messaging.protocol.mqtt.proto.parser.Utils.java

License:Open Source License

/**
 * Return the IoBuffer with string encoded as MSB, LSB and UTF-8 encoded
 * string content./*from   w  w w  .  ja v  a  2 s .  c  o  m*/
 */
public static ByteBuf encodeString(String str) {
    ByteBuf out = Unpooled.buffer(2);
    byte[] raw;
    try {
        raw = str.getBytes("UTF-8");
        //NB every Java platform has got UTF-8 encoding by default, so this 
        //exception are never raised.
    } catch (UnsupportedEncodingException ex) {
        LoggerFactory.getLogger(Utils.class).error(null, ex);
        return null;
    }
    //Utils.writeWord(out, raw.length);
    out.writeShort(raw.length);
    out.writeBytes(raw);
    return out;
}

From source file:com.quavo.osrs.network.protocol.codec.game.GamePacketEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, GamePacketResponse msg, ByteBuf out) throws Exception {
    PacketEncoder<PacketContext> packet = msg.getPacket();
    PacketType type = packet.getPacket().getType();
    ByteBuf buffer = packet.getBuilder().getBuffer();

    out.writeByte(packet.getPacket().getId()/* + encoder.nextInt() */);
    if (type == PacketType.VARIABLE_BYTE) {
        out.writeByte(buffer.writerIndex());
    } else if (type == PacketType.VARIABLE_SHORT) {
        out.writeShort(buffer.writerIndex());
    }//ww w.  j a  va  2s. c  o m
    out.writeBytes(buffer);
}

From source file:com.quavo.osrs.network.protocol.codec.login.world.WorldLoginEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, WorldLoginResponse msg, ByteBuf out) throws Exception {
    ClientMessage message = msg.getMessage();

    out.writeByte(message.getId());/* ww w. j ava  2  s . c  o  m*/
    if (message == ClientMessage.SUCCESSFUL) {
        out.writeByte(0);
        out.writeByte(0);
        out.writeByte(0);
        out.writeByte(0);
        out.writeByte(0);
        out.writeByte(2);// rights
        out.writeByte(0);
        out.writeShort(1);// index
        out.writeByte(1);
    }

    ctx.pipeline().remove(this);
}

From source file:com.quavo.osrs.network.protocol.codec.update.UpdateEncoder.java

License:Open Source License

@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);/*w  w  w.j  a v a2 s.c  o m*/
    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));
    }
}

From source file:com.rs3e.network.protocol.worldlist.WorldListEncoder.java

License:Open Source License

@Override
public void encode(ChannelHandlerContext ctx, WorldListMessage list, ByteBuf out) throws Exception {
    ByteBuf buf = Unpooled.buffer();
    buf.writeByte(1);//from   w ww .ja va 2 s  .  c o  m
    buf.writeByte(1);

    Country[] countries = list.getCountries();
    ByteBufUtils.writeSmart(buf, countries.length);
    for (Country country : countries) {
        ByteBufUtils.writeSmart(buf, country.getFlag());
        ByteBufUtils.writeWorldListString(buf, country.getName());
    }

    World[] worlds = list.getWorlds();
    int minId = worlds[0].getId();
    int maxId = worlds[0].getId();
    for (int i = 1; i < worlds.length; i++) {
        World world = worlds[i];
        int id = world.getId();

        if (id > maxId)
            maxId = id;
        if (id < minId)
            minId = id;
    }

    ByteBufUtils.writeSmart(buf, minId);
    ByteBufUtils.writeSmart(buf, maxId);
    ByteBufUtils.writeSmart(buf, worlds.length);

    for (World world : worlds) {
        ByteBufUtils.writeSmart(buf, world.getId() - minId);
        buf.writeByte(world.getCountry());
        buf.writeInt(world.getFlags());
        ByteBufUtils.writeWorldListString(buf, world.getActivity());
        ByteBufUtils.writeWorldListString(buf, world.getIp());
    }

    buf.writeInt(list.getSessionId());
    for (int i = 0; i < worlds.length; i++) {
        World world = worlds[i];
        ByteBufUtils.writeSmart(buf, world.getId() - minId);
        buf.writeShort(0);
    }

    out.writeByte(0); // 0 = ok, 7/9 = world full
    out.writeShort(buf.readableBytes());
    out.writeBytes(buf);
}