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:me.melchor9000.net.resolver.DNSResourceRecord.java

License:Open Source License

@Override
public void toByteBuf(@NotNull ByteBuf buffer) {
    writeName(buffer, name);/*from ww  w.  j a v  a  2 s. c  o m*/
    buffer.writeShort(type);
    buffer.writeShort(nclass);
    buffer.writeInt((int) ttl);
    buffer.writeShort(data != null ? data.byteBufSize() : 0);
    if (data != null)
        data.toByteBuf(buffer);
}

From source file:net.epsilony.utils.codec.modbus.func.ReadRegistersFunction.java

License:Open Source License

@Override
public void encodeRequestData(ByteBuf data) {
    data.writeShort(startingAddress);
    data.writeShort(quantity);
}

From source file:net.epsilony.utils.codec.modbus.func.WriteCoilFunction.java

License:Open Source License

@Override
public void encodeRequestData(ByteBuf data) {
    data.writeShort(address);
    data.writeShort(value ? 0xFF00 : 0x0000);

}

From source file:net.epsilony.utils.codec.modbus.func.WriteHoldingFunction.java

License:Open Source License

@Override
public void encodeRequestData(ByteBuf data) {
    data.writeShort(address);
    data.writeShort(value);

}

From source file:net.epsilony.utils.codec.modbus.handler.ModbusMasterRequestEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, ModbusRequest msg, ByteBuf out) throws Exception {
    int from = out.writerIndex();
    out.writeShort(msg.getTransectionId());
    out.writeShort(0);//from  w w w  .j ava  2s .  c o  m
    out.writeShort(2 + msg.getFunction().getRequestDataLength());
    out.writeByte(msg.getUnitId());
    out.writeByte(msg.getFunction().getCode());
    msg.getFunction().encodeRequestData(out);
    if (withCheckSum) {
        out.writeShort(checkSum(out, from));
    }
}

From source file:net.epsilony.utils.codec.modbus.handler.ModbusSlaveResponseEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, ModbusResponse msg, ByteBuf out) throws Exception {
    int from = out.writerIndex();
    msg.encode(out);//from   w w w.  j a  v  a 2s .  com
    if (withCheckSum) {
        out.writeShort(Utils.crc(out, from, out.writerIndex() - from));
    }
}

From source file:net.epsilony.utils.codec.modbus.handler.ModbusSlaveResponseEncoderTest.java

License:Open Source License

@Test
public void test() {
    ModbusSlaveResponseEncoder encoder = new ModbusSlaveResponseEncoder();
    EmbeddedChannel channel = new EmbeddedChannel(encoder);

    ModbusResponse response = new ModbusResponse() {
        {//from w  w w.  j  a v a  2 s.  c  o  m
            transectionId = 0xabcd;
            unitId = 0x83;
        }

        @Override
        public void writePduCore(ByteBuf out) {
            out.writeShort(0xabcd);
            out.writeShort(0xcdef);
            transectionId++;

        }

        @Override
        public int getPduCoreLength() {
            return 4;
        }

        @Override
        public int getFunctionCode() {
            // TODO Auto-generated method stub
            return 6;
        }
    };

    ByteBuf buf = null;
    for (int i = 0; i < 3; i++) {
        channel.writeOutbound(response);
        buf = (ByteBuf) channel.readOutbound();
        int[] buffer = new int[] { 0xab, 0xcd + i, 0x00, 0x00, 0x00, 0x06, 0x83, 0x06, 0xab, 0xcd, 0xcd, 0xef };
        assertEquals(buffer.length, buf.readableBytes());
        for (int b : buffer) {
            assertEquals(b, buf.readUnsignedByte());
        }
        assertTrue(!buf.isReadable());
        ReferenceCountUtil.release(buf);
    }

    for (int i = 0; i < 3; i++) {
        channel.writeOutbound(response);
    }

    for (int i = 0; i < 3; i++) {
        buf = (ByteBuf) channel.readOutbound();
        int[] buffer = new int[] { 0xab, 0xcd + i + 3, 0x00, 0x00, 0x00, 0x06, 0x83, 0x06, 0xab, 0xcd, 0xcd,
                0xef };
        assertEquals(buffer.length, buf.readableBytes());
        for (int b : buffer) {
            assertEquals(b, buf.readUnsignedByte());
        }
        assertTrue(!buf.isReadable());
        ReferenceCountUtil.release(buf);
    }

    encoder.setWithCheckSum(true);
    for (int i = 0; i < 3; i++) {
        channel.writeOutbound(response);
    }
    for (int i = 0; i < 3; i++) {
        buf = (ByteBuf) channel.readOutbound();
        int[] buffer = new int[] { 0xab, 0xcd + i + 6, 0x00, 0x00, 0x00, 0x06, 0x83, 0x06, 0xab, 0xcd, 0xcd,
                0xef };
        assertEquals(buffer.length, buf.readableBytes() - 2);
        for (int b : buffer) {
            assertEquals(b, buf.readUnsignedByte());
        }
        int calcCrc = Utils.crc(buf, buf.readerIndex() - buffer.length, buffer.length);
        assertEquals(calcCrc, buf.readUnsignedShort());
        assertTrue(!buf.isReadable());
        ReferenceCountUtil.release(buf);
    }
}

From source file:net.epsilony.utils.codec.modbus.reqres.ModbusResponse.java

License:Open Source License

public void encode(ByteBuf out) {
    out.writeShort(transectionId);
    out.writeShort(0);/*from ww w.j a  v a 2  s .  co m*/
    out.writeShort(getPduCoreLength() + 2);
    out.writeByte(unitId);
    out.writeByte(getFunctionCode());
    writePduCore(out);
}

From source file:net.epsilony.utils.codec.modbus.reqres.ReadWordRegistersResponse.java

License:Open Source License

@Override
public void writePduCore(ByteBuf out) {
    out.writeByte(2 * quantity);/*from w w w  .  ja  v  a  2  s. co  m*/
    for (int i = 0; i < quantity; i++) {
        out.writeShort(getValue(i));
    }
}

From source file:net.epsilony.utils.codec.modbus.reqres.WriteCoilResponse.java

License:Open Source License

@Override
public void writePduCore(ByteBuf out) {
    out.writeShort(address);
    out.writeShort(value ? 0xFF00 : 0x0000);

}