Example usage for io.netty.buffer ByteBuf writeByte

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

Introduction

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

Prototype

public abstract ByteBuf writeByte(int value);

Source Link

Document

Sets the specified byte at the current writerIndex and increases the writerIndex by 1 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   www  . j a  va 2  s  .co 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.ogarproject.ogar.server.net.packet.Packet.java

License:Open Source License

public static String readUTF8(ByteBuf in) {
    ByteBuf buffer = in.alloc().buffer();
    byte b;/*www .j  ava  2 s . com*/
    while (in.readableBytes() > 0 && (b = in.readByte()) != 0) {
        buffer.writeByte(b);
    }

    return buffer.toString(Charsets.UTF_8);
}

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

License:Open Source License

public static void writeUTF8(ByteBuf out, String s) {
    out.writeBytes(s.getBytes(Charsets.UTF_8));
    out.writeByte(0);
}

From source file:com.ogarproject.ogar.server.net.PacketEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, Packet packet, List out) throws Exception {
    ByteBuf buf = ctx.alloc().buffer().order(ByteOrder.LITTLE_ENDIAN);
    int packetId = PacketRegistry.CLIENTBOUND.getPacketId(packet.getClass());
    if (packetId == -1) {
        throw new IllegalArgumentException("Provided packet is not registered as a clientbound packet!");
    }//from ww  w  .  ja  v a 2 s  .co m

    buf.writeByte(packetId);
    packet.writeData(buf);
    out.add(new BinaryWebSocketFrame(buf));

    OgarServer.log.finest("Sent packet ID " + packetId + " (" + packet.getClass().getSimpleName() + ") to "
            + ctx.channel().remoteAddress());
}

From source file:com.phei.netty.protocol.netty.codec.NettyMessageEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, NettyMessage msg, ByteBuf sendBuf) throws Exception {
    if (msg == null || msg.getHeader() == null)
        throw new Exception("The encode message is null");
    sendBuf.writeInt((msg.getHeader().getCrcCode()));
    sendBuf.writeInt((msg.getHeader().getLength()));
    sendBuf.writeLong((msg.getHeader().getSessionID()));
    sendBuf.writeByte((msg.getHeader().getType()));
    sendBuf.writeByte((msg.getHeader().getPriority()));
    sendBuf.writeInt((msg.getHeader().getAttachment().size()));
    String key = null;//from   w  w w. j  a v  a  2s.c om
    byte[] keyArray = null;
    Object value = null;
    for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) {
        key = param.getKey();
        keyArray = key.getBytes("UTF-8");
        sendBuf.writeInt(keyArray.length);
        sendBuf.writeBytes(keyArray);
        value = param.getValue();
        marshallingEncoder.encode(value, sendBuf);
    }
    key = null;
    keyArray = null;
    value = null;
    //body?4int
    if (msg.getBody() != null) {
        marshallingEncoder.encode(msg.getBody(), sendBuf);
    } else {
        sendBuf.writeInt(0);
    }
    sendBuf.setInt(4, sendBuf.readableBytes() - 8);
}

From source file:com.projectzed.mod.handler.message.MessageTileEntityFluidTank.java

License:Open Source License

@Override
public void toBytes(ByteBuf buf) {
    buf.writeInt(this.x);
    buf.writeInt(this.y);
    buf.writeInt(this.z);
    buf.writeByte(this.tier);
    buf.writeInt(this.fluidAmount);
    buf.writeInt(this.fluidID);
}

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 a va 2  s.co m
        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  w  w  w.  java  2 s .c  o  m*/
}

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   w ww  . j a v  a 2 s . c  o  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");
    }//from   ww w.j a va 2s  .c  om

    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();
    }
}