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:de.gandev.modjn.entity.func.response.ReadInputRegistersResponse.java

License:Apache License

@Override
public ByteBuf encode() {
    ByteBuf buf = Unpooled.buffer(calculateLength());
    buf.writeByte(getFunctionCode());/*from  w  ww  .  ja  va 2s.c  om*/
    buf.writeByte(byteCount);

    for (int i = 0; i < inputRegisters.length; i++) {
        buf.writeShort(inputRegisters[i]);
    }

    return buf;
}

From source file:de.jackwhite20.cascade.shared.protocol.packet.Packet.java

License:Open Source License

public void writeString(ByteBuf byteBuf, String string) throws UnsupportedEncodingException {

    byte[] bytes = string.getBytes("utf-8");

    byteBuf.writeShort(bytes.length);
    byteBuf.writeBytes(bytes);//from   w ww  . j a v a2s.co m
}

From source file:de.sanandrew.mods.turretmod.registry.electrolytegen.ElectrolyteProcess.java

License:Creative Commons License

public void writeToByteBuf(ByteBuf buf) {
    ByteBufUtils.writeItemStack(buf, this.processStack);
    buf.writeShort(this.progress);
    buf.writeShort(this.maxProgress);
}

From source file:de.unipassau.isl.evs.ssh.core.sec.DeviceConnectInformation.java

License:Open Source License

/**
 * Serialize this Object to a String which can be converted to a QR Code
 *//* www .j a v  a2 s .c  om*/
public String toDataString() {
    final ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.heapBuffer(DATA_LENGTH, DATA_LENGTH);
    byteBuf.writeBytes(address.getAddress());
    byteBuf.writeShort(port);
    byteBuf.writeBytes(id.getIDBytes());
    byteBuf.writeBytes(encodeToken(token).getBytes());
    return encode(byteBuf).toString(Charsets.US_ASCII);
}

From source file:divconq.ctp.f.BlockCommand.java

License:Open Source License

@Override
public ByteBuf encode() {
    int size = 1 + 1; // code + CTP_F_BLOCK_TYPE_

    int type = 0;

    int hdrcnt = this.headers.size();

    if (hdrcnt > 0) {
        type |= CtpConstants.CTP_F_BLOCK_TYPE_HEADER;
        size += hdrcnt * 4 + 2; // each header is at least 4 bytes (2 attr, 2 len) + ATTR_END is 2 bytes

        for (byte[] val : this.headers.values())
            size += val.length;
    }//  w ww  . j  a v  a2  s  . co m

    if (this.data != null) {
        type |= CtpConstants.CTP_F_BLOCK_TYPE_CONTENT;
        size += 3 + this.data.readableBytes();
    }

    if (this.eof)
        type |= CtpConstants.CTP_F_BLOCK_TYPE_EOF;

    // build buffer
    ByteBuf bb = Hub.instance.getBufferAllocator().buffer(size);

    bb.writeByte(this.cmdCode);
    bb.writeByte(type);

    // add header if any
    if (hdrcnt > 0) {
        for (Entry<Integer, byte[]> val : this.headers.entrySet()) {
            bb.writeShort(val.getKey());
            bb.writeShort(val.getValue().length);
            bb.writeBytes(val.getValue());
        }

        // end of list of headers
        bb.writeShort(CtpConstants.CTP_F_ATTR_END);
    }

    // add content if any
    if (this.data != null) {
        bb.writeLong(this.streamOffset);
        bb.writeMedium(this.data.readableBytes());
        bb.writeBytes(this.data);
    }

    return bb;
}

From source file:dorkbox.network.pipeline.discovery.BroadcastServer.java

License:Apache License

/**
 * @return true if the broadcast was responded to, false if it was not a broadcast (and there was no response)
 *//*from www.  j a  va  2 s .c o m*/
public boolean isDiscoveryRequest(final Channel channel, ByteBuf byteBuf, final InetSocketAddress localAddress,
        InetSocketAddress remoteAddress) {
    if (byteBuf.readableBytes() == 1) {
        // this is a BROADCAST discovery event. Don't read the byte unless it is...
        if (byteBuf.getByte(0) == MagicBytes.broadcastID) {
            byteBuf.readByte(); // read the byte to consume it (now that we verified it is a broadcast byte)

            // absolutely MUST send packet > 0 across, otherwise netty will think it failed to write to the socket, and keep trying.
            // (this bug was fixed by netty, however we are keeping this code)
            ByteBuf directBuffer = channel.alloc().ioBuffer(bufferSize);
            directBuffer.writeByte(MagicBytes.broadcastResponseID);

            // now output the port information for TCP/UDP so the broadcast client knows which port to connect to
            // either it will be TCP or UDP, or BOTH

            int enabledFlag = 0;
            if (tcpPort > 0) {
                enabledFlag |= MagicBytes.HAS_TCP;
            }

            if (udpPort > 0) {
                enabledFlag |= MagicBytes.HAS_UDP;
            }

            directBuffer.writeByte(enabledFlag);

            // TCP is always first
            if (tcpPort > 0) {
                directBuffer.writeShort(tcpPort);
            }

            if (udpPort > 0) {
                directBuffer.writeShort(udpPort);
            }

            channel.writeAndFlush(new DatagramPacket(directBuffer, remoteAddress, localAddress));

            logger.info("Responded to host discovery from [{}]", EndPoint.getHostDetails(remoteAddress));

            byteBuf.release();
            return true;
        }
    }

    return false;
}

From source file:eu.jangos.realm.network.packet.server.auth.SMSG_ACCOUNT_DATA_TIMES.java

License:Open Source License

@Override
public void encode(ByteBuf buf) throws Exception {
    // Packet structure:
    // 2b - 2b - 128b
    // Size (Little Endian) - Opcode (Big Endian) - 0 (why ??)

    buf.writeShort(this.size);
    buf.writeShort(this.code.getValue());

    for (int i = 0; i < 16; i++) {
        buf.writeLong(0);//from   w ww  . j ava 2  s.c o  m
    }
}

From source file:eu.jangos.realm.network.packet.server.auth.SMSG_ADDON_INFO.java

License:Open Source License

@Override
public void encode(ByteBuf buf) throws Exception {
    // SMSG_ADDON_INFO happens whenever a client is connecting to a realm.
    // Packet structure:
    // 2b - 2b - [ 1b - 1b - 1b - (256b) - 4b - 1b ]
    // Size (BigEndian) - Opcode (Little Endian) - AddOn Type - Unknown - Unknown - Public key (optional)
    // - Unknown - Unknown

    buf.writeShort(this.size);
    buf.writeShort(this.code.getValue());

    for (AddonInfo info : this.listAddons) {
        buf.writeByte(2); // Blizzard Add-ons (2)
        buf.writeByte(1); // Unknown.

        // In vanilla, all-addons are Blizzard add-ons, don't know why.            
        buf.writeByte(0);/*w  ww. j  a v  a 2  s . c  om*/

        buf.writeInt(0);
        buf.writeByte(0);
    }

}

From source file:eu.jangos.realm.network.packet.server.auth.SMSG_AUTH_CHALLENGE.java

License:Open Source License

@Override
public void encode(ByteBuf buf) throws Exception {
    // SMSG_AUTH_CHALLENGE happens whenever a client is connecting to a realm.
    // Packet structure:
    // 2b - 2b - 4b
    // Size (BigEndian) - Opcode (Little Endian) - Seed

    buf.writeShort(this.size);
    buf.writeShort(this.code.getValue());
    buf.writeBytes(this.seed, 0, 4);
}

From source file:eu.jangos.realm.network.packet.server.auth.SMSG_AUTH_RESPONSE.java

License:Open Source License

@Override
public void encode(ByteBuf buf) throws Exception {
    // SMSG_AUTH_RESPONSE happens after a client requested to login.
    // Packet structure: (Failed case)
    // 2b - 2b - 1b
    // Size (Little Endian) - Opcode (Big Endian) - result

    if (this.result != AuthEnum.AUTH_SUCCESS) {
        buf.writeShort(this.size);
        buf.writeShort(this.code.getValue());
        buf.writeByte(this.result.getValue());
    } else {/*from  w  ww.jav  a2  s  .c o m*/

        // Packet structure: (Success case)
        // 2b - 1b - 1b - 4b - 1b - 1b
        // Size - Opcode - Result - BillingTimeRemaining - BillingPlanFlags - BillingTimeRested

        buf.writeShort(this.size); // Size
        buf.writeShort(this.code.getValue());
        buf.writeByte((byte) this.result.getValue());
        buf.writeInt(0);
        buf.writeByte(0);
        buf.writeInt(0);
    }
}