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:at.yawk.accordion.distributed.Node.java

License:Mozilla Public License

/**
 * Serialize this node to a given ByteBuf.
 *//*from   ww  w .j  a  va  2s  . c  o  m*/
void write(ByteBuf to) {
    // port
    to.writeInt(address.getPort());
    // IP
    InternalProtocol.writeByteArray(to, address.getAddress().getAddress());
    // tier
    to.writeInt(tier);
}

From source file:at.yawk.accordion.minecraft.example.AbstractPingPacket.java

License:Mozilla Public License

@Override
public void write(ByteBuf target) {
    target.writeInt(payload);
}

From source file:at.yawk.accordion.simulation.Simulation.java

License:Mozilla Public License

public static void main(String[] args) throws UnknownHostException, InterruptedException {
    Logger.getRootLogger().addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender.SYSTEM_OUT));
    Logger.getRootLogger().setLevel(Level.DEBUG);

    Simulation simulation = new Simulation();
    simulation.populate();/*from  w  w w. j  a v a 2  s  .co  m*/
    TimeUnit.SECONDS.sleep(1);

    simulation.nodes.get(simulation.tiers[0][0]).getConnectionManager().getChannel("test").subscribe(msg -> {
        int len = msg.readInt();
        byte[] blob = new byte[len];
        msg.readBytes(blob);
        Log.getDefaultLogger().info("Received: " + new String(blob));
    });
    TimeUnit.SECONDS.sleep(1);

    ByteBuf msg = Unpooled.buffer();
    byte[] text = "ping".getBytes();
    msg.writeInt(text.length);
    msg.writeBytes(text);

    simulation.nodes.get(simulation.tiers[0][1]).getConnectionManager().getChannel("test").publish(msg);
}

From source file:at.yawk.dbus.protocol.codec.MessageHeaderCodec.java

@Override
protected void encode(ChannelHandlerContext ctx, MessageHeader msg, ByteBuf out) throws Exception {
    out = out.order(Local.OUTBOUND_ORDER);

    AlignableByteBuf alignedBuf = AlignableByteBuf.encoding(out);
    out.writeByte(Local.OUTBOUND_ORDER == ByteOrder.LITTLE_ENDIAN ? 'l' : 'B');

    out.writeByte(msg.getMessageType().getId());

    byte flags = 0;
    if (msg.isNoReplyExpected()) {
        flags |= NO_REPLY_EXPECTED;/*from  w  w w.  j  a v a  2s. c  o  m*/
    }
    if (msg.isNoAutoStart()) {
        flags |= NO_AUTO_START;
    }
    if (msg.isAllowInteractiveAuthorization()) {
        flags |= ALLOW_INTERACTIVE_AUTHORIZATION;
    }
    out.writeByte(flags);

    byte protocolVersion = msg.getMajorProtocolVersion();
    if (protocolVersion == 0) {
        protocolVersion = PROTOCOL_VERSION;
    }
    out.writeByte(protocolVersion);

    out.writeInt((int) msg.getMessageBodyLength());

    int serial = msg.getSerial();
    if (serial == 0) {
        serial = Local.generateSerial(ctx);
    }
    out.writeInt(serial);

    checkRequiredHeaderFieldsPresent(msg);
    ArrayObject headerObject = ArrayObject.create(HEADER_FIELD_LIST_TYPE,
            msg.getHeaderFields().entrySet().stream().map(entry -> {
                BasicObject id = BasicObject.createByte(entry.getKey().getId());
                DbusObject value = entry.getValue();
                return StructObject.create(HEADER_FIELD_TYPE, Arrays.asList(id, VariantObject.create(value)));
            }).collect(Collectors.toList()));

    headerObject.serialize(alignedBuf);
    alignedBuf.alignWrite(8);
}

From source file:bftsmart.communication.client.netty.NettyTOMMessageEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext context, TOMMessage sm, ByteBuf buffer) throws Exception {
    byte[] msgData;
    byte[] signatureData = null;

    msgData = sm.serializedMessage;//from   w  w w  . ja v a 2 s.  co m
    if (sm.signed) {
        //signature was already produced before            
        signatureData = sm.serializedMessageSignature;
    }

    int dataLength = Integer.BYTES + msgData.length + Integer.BYTES
            + (signatureData != null ? signatureData.length : 0);

    /* msg size */
    buffer.writeInt(dataLength);

    /* data to be sent */
    buffer.writeInt(msgData.length);
    buffer.writeBytes(msgData);

    /* signature */
    if (signatureData != null) {
        buffer.writeInt(signatureData.length);
        buffer.writeBytes(signatureData);
    } else {
        buffer.writeInt(0);
    }

    context.flush();
}

From source file:blazingcache.network.netty.DodoMessageUtils.java

License:Apache License

private static void writeUTF8String(ByteBuf buf, String s) {
    byte[] asarray = s.getBytes(StandardCharsets.UTF_8);
    buf.writeInt(asarray.length);
    buf.writeBytes(asarray);//  w  ww .  j a va 2s .c om
}

From source file:blazingcache.network.netty.DodoMessageUtils.java

License:Apache License

public static void encodeMessage(ByteBuf encoded, Message m) {
    encoded.writeByte(VERSION);//from  www . j  av a 2 s. co  m
    encoded.writeInt(m.type);
    writeUTF8String(encoded, m.messageId);
    if (m.replyMessageId != null) {
        encoded.writeByte(OPCODE_REPLYMESSAGEID);
        writeUTF8String(encoded, m.replyMessageId);
    }
    if (m.clientId != null) {
        encoded.writeByte(OPCODE_WORKERPROCESSID);
        writeUTF8String(encoded, m.clientId);
    }
    if (m.parameters != null) {
        encoded.writeByte(OPCODE_PARAMETERS);
        encoded.writeInt(m.parameters.size());
        for (Map.Entry<String, Object> p : m.parameters.entrySet()) {
            writeEncodedSimpleValue(encoded, p.getKey());
            writeEncodedSimpleValue(encoded, p.getValue());
        }
    }

}

From source file:blazingcache.network.netty.DodoMessageUtils.java

License:Apache License

private static void writeEncodedSimpleValue(ByteBuf encoded, Object o) {
    if (o == null) {
        encoded.writeByte(OPCODE_NULL_VALUE);
    } else if (o instanceof String) {
        encoded.writeByte(OPCODE_STRING_VALUE);
        writeUTF8String(encoded, (String) o);
    } else if (o instanceof Integer) {
        encoded.writeByte(OPCODE_INT_VALUE);
        encoded.writeInt((Integer) o);
    } else if (o instanceof Long) {
        encoded.writeByte(OPCODE_LONG_VALUE);
        encoded.writeLong((Long) o);
    } else if (o instanceof Set) {
        Set set = (Set) o;
        encoded.writeByte(OPCODE_SET_VALUE);
        encoded.writeInt(set.size());/*ww w.  j av  a2  s . c o m*/
        for (Object o2 : set) {
            writeEncodedSimpleValue(encoded, o2);
        }
    } else if (o instanceof List) {
        List set = (List) o;
        encoded.writeByte(OPCODE_LIST_VALUE);
        encoded.writeInt(set.size());
        for (Object o2 : set) {
            writeEncodedSimpleValue(encoded, o2);
        }

    } else if (o instanceof byte[]) {
        byte[] set = (byte[]) o;
        encoded.writeByte(OPCODE_BYTEARRAY_VALUE);
        encoded.writeInt(set.length);
        encoded.writeBytes(set);
    } else if (o instanceof Map) {
        Map set = (Map) o;
        encoded.writeByte(OPCODE_MAP_VALUE);
        encoded.writeInt(set.size());
        for (Map.Entry entry : (Iterable<Entry>) set.entrySet()) {
            writeEncodedSimpleValue(encoded, entry.getKey());
            writeEncodedSimpleValue(encoded, entry.getValue());
        }
    } else {
        throw new RuntimeException("unsupported class " + o.getClass());
    }
}

From source file:blazingcache.network.netty.MessageUtils.java

License:Apache License

private static void writeEncodedSimpleValue(ByteBuf encoded, Object o) {
    if (o == null) {
        encoded.writeByte(OPCODE_NULL_VALUE);
    } else if (o instanceof String) {
        encoded.writeByte(OPCODE_STRING_VALUE);
        writeUTF8String(encoded, (String) o);
    } else if (o instanceof RawString) {
        encoded.writeByte(OPCODE_STRING_VALUE);
        writeUTF8String(encoded, ((RawString) o).toString());
    } else if (o instanceof Integer) {
        encoded.writeByte(OPCODE_INT_VALUE);
        encoded.writeInt((Integer) o);
    } else if (o instanceof Long) {
        encoded.writeByte(OPCODE_LONG_VALUE);
        encoded.writeLong((Long) o);
    } else if (o instanceof Set) {
        Set set = (Set) o;
        encoded.writeByte(OPCODE_SET_VALUE);
        encoded.writeInt(set.size());/*  w  ww .  j  a v  a  2  s . c o m*/
        for (Object o2 : set) {
            writeEncodedSimpleValue(encoded, o2);
        }
    } else if (o instanceof List) {
        List set = (List) o;
        encoded.writeByte(OPCODE_LIST_VALUE);
        encoded.writeInt(set.size());
        for (Object o2 : set) {
            writeEncodedSimpleValue(encoded, o2);
        }

    } else if (o instanceof byte[]) {
        byte[] set = (byte[]) o;
        encoded.writeByte(OPCODE_BYTEARRAY_VALUE);
        encoded.writeInt(set.length);
        encoded.writeBytes(set);
    } else if (o instanceof Map) {
        Map set = (Map) o;
        encoded.writeByte(OPCODE_MAP_VALUE);
        encoded.writeInt(set.size());
        for (Map.Entry entry : (Iterable<Entry>) set.entrySet()) {
            writeEncodedSimpleValue(encoded, entry.getKey());
            writeEncodedSimpleValue(encoded, entry.getValue());
        }
    } else {
        throw new RuntimeException("unsupported class " + o.getClass());
    }
}

From source file:blusunrize.immersiveengineering.common.util.network.MessageBirthdayParty.java

@Override
public void toBytes(ByteBuf buf) {
    buf.writeInt(this.entityId);
}