Example usage for io.netty.buffer ByteBuf writeInt

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

Introduction

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

Prototype

public abstract ByteBuf writeInt(int value);

Source Link

Document

Sets the specified 32-bit integer at the current writerIndex and increases the writerIndex by 4 in this buffer.

Usage

From source file:com.facebook.nifty.core.NiftyDispatcher.java

License:Apache License

private ByteBuf addFraming(ChannelHandlerContext ctx, ByteBuf response, ThriftTransportType transportType) {
    if (!response.isReadable()) {
        // Empty response from one-way message, don't frame it
        return response;
    }/*ww w  .ja  va2s.c o  m*/

    if (transportType == ThriftTransportType.UNFRAMED) {
        return response;
    } else if (transportType == ThriftTransportType.FRAMED) {
        ByteBuf frameSizeBuffer = ctx.alloc().buffer(4);
        frameSizeBuffer.writeInt(response.readableBytes());
        return Unpooled.wrappedBuffer(frameSizeBuffer, response);
    } else {
        throw new UnsupportedOperationException("Header protocol is not supported");
    }
}

From source file:com.farincorporated.frameutils.client.packet.FrameFieldPacket.java

@Override
public void toBytes(ByteBuf buf) {
    ByteBufUtils.writeUTF8String(buf, type);
    buf.writeInt(x);
    buf.writeInt(y);
    buf.writeInt(z);
}

From source file:com.feihong.newzxclient.tcp.NettyClientHandler.java

License:Apache License

protected void sendMsg(Msg.CommonMessage msg) {
    if (mContext != null) {
        byte[] result = msg.toByteArray();

        ByteBuf buf = Unpooled.buffer(4 + result.length);
        buf.writeInt(result.length);
        buf.writeBytes(result);//from   w  w w  . ja va 2 s .c o  m

        //System.out.println("send Msg success!");
        mContext.writeAndFlush(buf);
    }
}

From source file:com.fireball1725.firecore.common.network.messages.PacketButtonClick.java

License:Open Source License

@Override
public void toBytes(ByteBuf buf) {
    buf.writeInt(buttonID);
    buf.writeInt(blockX);//from w  ww  .jav  a  2s .co  m
    buf.writeInt(blockY);
    buf.writeInt(blockZ);
    ByteBufUtils.writeUTF8String(buf, playerUUID.toString());
}

From source file:com.fireball1725.firelib.guimaker.util.NetworkHelper.java

License:Open Source License

public static void writeBlockPos(ByteBuf byteBuf, BlockPos blockPos) {
    byteBuf.writeInt(blockPos.getX());
    byteBuf.writeInt(blockPos.getY());//from  w  w w. java  2 s  .  c  om
    byteBuf.writeInt(blockPos.getZ());
}

From source file:com.fjn.helper.frameworkex.netty.v4.timetest.TimeEncoder.java

License:Apache License

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
    UnixTime m = (UnixTime) msg;//from  w ww.  j  a va2 s  . c  o m
    ByteBuf encoded = ctx.alloc().buffer(4);
    encoded.writeInt((int) m.value());
    ctx.write(encoded, promise); // (1)
}

From source file:com.flowpowered.engine.network.codec.ChunkDataCodec.java

License:MIT License

@Override
public ByteBuf encode(ByteBuf buffer, ChunkDataMessage message) throws IOException {
    if (message.isUnload()) {
        buffer.writeByte(ISUNLOAD); // we're unloading
        buffer.writeInt(message.getX());
        buffer.writeInt(message.getY());
        buffer.writeInt(message.getZ());
    } else {/*from   w  w w  .  ja v a2  s  .co m*/
        int dataSize = INTIAL_DATA_SIZE;

        byte[] uncompressedData = new byte[dataSize];
        byte[] compressedData = new byte[dataSize];

        int index = 0;
        for (int i : message.getBlocks()) {
            uncompressedData[index++] = (byte) i;
            uncompressedData[index++] = (byte) (i >> 8);
            uncompressedData[index++] = (byte) (i >> 16);
            uncompressedData[index++] = (byte) (i >> 24);
        }

        Deflater deflater = new Deflater();
        deflater.setInput(uncompressedData);
        deflater.finish();
        int compressedSize = deflater.deflate(compressedData);
        deflater.end();

        if (compressedSize == 0) {
            throw new IOException("Not all data compressed!");
        }

        buffer.writeByte(0); // Not unload
        buffer.writeInt(message.getX());
        buffer.writeInt(message.getY());
        buffer.writeInt(message.getZ());
        buffer.writeInt(compressedSize);
        buffer.writeBytes(compressedData, 0, compressedSize);
    }
    return buffer;
}

From source file:com.flowpowered.engine.network.codec.InputSnapshotCodec.java

License:MIT License

@Override
public ByteBuf encode(ByteBuf buf, InputSnapshotMessage message) throws IOException {
    buf.writeInt(message.getKeyEvents().size());
    for (KeyboardEvent e : message.getKeyEvents()) {
        buf.writeByte(e.getKeyId());//from ww  w .  j a v  a  2s.  c om
        buf.writeBoolean(e.wasPressedDown());
    }
    buf.writeInt(message.getMouseEvents().size());
    for (MouseEvent e : message.getMouseEvents()) {
        buf.writeInt(e.getX());
        buf.writeInt(e.getY());
        buf.writeInt(e.getDX());
        buf.writeInt(e.getDY());
        buf.writeInt(e.getDWheel());
        buf.writeInt(e.getButton());
        buf.writeBoolean(e.wasPressedDown());
    }
    buf.writeFloat(message.getDt());
    buf.writeBoolean(message.isMouseGrabbed());
    return buf;
}

From source file:com.flowpowered.engine.network.codec.UpdateEntityCodec.java

License:MIT License

@Override
public ByteBuf encode(ByteBuf buf, UpdateEntityMessage message) {
    buf.writeByte(message.getAction().ordinal());
    buf.writeInt(message.getEntityId());
    switch (message.getAction()) {
    case REMOVE:/*from  ww w  . j  a va 2 s . c om*/
        break;
    case ADD:
    case TRANSFORM:
        FlowByteBufUtils.writeTransform(buf, message.getTransform());
        break;
    case POSITION:
        throw new UnsupportedOperationException("Position is unimplemented!");
    default:
        throw new IllegalArgumentException("Unknown UpdateAction!");
    }
    return buf;
}

From source file:com.flowpowered.examples.networking.message.TestMessageCodec.java

License:MIT License

@Override
public ByteBuf encode(ByteBuf buf, TestMessage message) throws IOException {
    buf.writeInt(message.getNumber());
    return buf;//from   w  w w .  j  a  v a 2 s .  c  o m
}