Example usage for io.netty.buffer ByteBuf readUnsignedShort

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

Introduction

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

Prototype

public abstract int readUnsignedShort();

Source Link

Document

Gets an unsigned 16-bit short integer at the current readerIndex and increases the readerIndex by 2 in this buffer.

Usage

From source file:me.melchor9000.net.resolver.DNSResourceData.java

License:Open Source License

static DNSResourceData forData(int type, ByteBuf data) {
    data.readUnsignedShort(); //length
    switch (type) {
    case 1://from   w ww . j  a va  2  s.com
        return new DNSA(data);
    case 5:
        return new DNSCNAME(data);
    case 15:
        return new DNSMX(data);
    case 28:
        return new DNSAAAA(data);
    default:
        return null;
    }
}

From source file:me.melchor9000.net.resolver.DNSResourceRecord.java

License:Open Source License

private int rs(ByteBuf buf) {
    if (buf.readableBytes() < 2)
        throw new DataNotRepresentsObject("Is an incomplete DNS Resource Record", buf);
    return buf.readUnsignedShort();
}

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

License:Open Source License

@Override
public void decodeRequestData(ByteBuf data) {
    if (data.readableBytes() < 4) {
        throw new DecoderException();
    }/*  w  w  w  .j ava  2s .com*/
    startingAddress = data.readUnsignedShort();
    setQuantity(data.readUnsignedShort());
}

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

License:Open Source License

@Override
public void decodeResponseData(ByteBuf data, ModbusResponse response) {
    if (data.readableBytes() < getResponseDataLength()) {
        throw new DecoderException();
    }/*from   w  w w  . j  ava  2  s.c o  m*/

    ReadWordRegistersResponse readWordRegistersResponse = (ReadWordRegistersResponse) response;

    readWordRegistersResponse.setStartingAddress(startingAddress);

    int dataContentBytes = data.readUnsignedByte();
    if (dataContentBytes != getResponseDataLength() - 1) {
        throw new DecoderException();
    }

    readWordRegistersResponse.setQuantityAndAllocate(quantity);
    for (int i = 0; i < quantity; i++) {
        readWordRegistersResponse.setValue(i, data.readUnsignedShort());
    }
}

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

License:Open Source License

@Override
public void decodeRequestData(ByteBuf data) {
    setAddress(data.readUnsignedShort());
    value = data.readUnsignedShort() == 0xFF00;
}

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

License:Open Source License

@Override
public void decodeResponseData(ByteBuf data, ModbusResponse response) {
    WriteCoilResponse rResponse = (WriteCoilResponse) response;
    rResponse.setAddress(data.readUnsignedShort());
    rResponse.setValue(data.readUnsignedShort() == 0xFF00);
    if (rResponse.getAddress() != address || rResponse.getValue() != value) {
        throw new UnexpectedResponseException();
    }/*from w  w  w .java 2 s  .  com*/
}

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

License:Open Source License

@Override
public void decodeRequestData(ByteBuf data) {
    address = data.readUnsignedShort();
    value = data.readUnsignedShort();
}

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

License:Open Source License

@Override
public void decodeResponseData(ByteBuf data, ModbusResponse response) {
    WriteHoldingResponse hResponse = (WriteHoldingResponse) response;
    hResponse.setAddress(data.readUnsignedShort());
    hResponse.setValue(data.readUnsignedShort());
    if (hResponse.getAddress() != address || hResponse.getValue() != value) {
        throw new UnexpectedResponseException();
    }/*from   w w w . j  a  v  a 2  s.  c o m*/
}

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

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (in.readableBytes() < 6) {
        return;/*  w  ww.  ja v  a 2s .  com*/
    }
    int adupLength = in.getUnsignedShort(in.readerIndex() + 4);
    int wholeLength = adupLength + 6 + (withCheckSum ? 2 : 0);
    if (wholeLength > in.readableBytes()) {
        return;
    }
    if (withCheckSum) {
        checkSum(in, wholeLength);
    }
    int transectionId = in.readUnsignedShort();
    ModbusRequest request = transectingRequestRetriever.apply(transectionId);
    if (null == request) {
        in.readerIndex(in.readerIndex() + wholeLength - 2);
        out.add(new MissMatchResponse(transectionId));
        return;
    }
    in.readerIndex(in.readerIndex() + 4);
    int unitId = in.readUnsignedByte();
    if (unitId != request.getUnitId()) {
        throw new DecoderException();
    }
    int functionCode = in.readUnsignedByte();
    if ((functionCode & 0x7F) != request.getFunction().getCode()) {
        throw new DecoderException();
    }

    ModbusResponse response;

    if ((functionCode & 0x80) == 0) {
        switch (functionCode) {
        case 0x01: {
            ReadBooleanRegistersResponse bResponse = new ReadBooleanRegistersResponse();
            bResponse.setRegisterType(ModbusRegisterType.COIL);
            response = bResponse;
        }
            break;
        case 0x02: {
            ReadBooleanRegistersResponse bResponse = new ReadBooleanRegistersResponse();
            bResponse.setRegisterType(ModbusRegisterType.DISCRETE_INPUT);
            response = bResponse;
        }
            break;
        case 0x03: {
            ReadWordRegistersResponse rResponse = new ReadWordRegistersResponse();
            rResponse.setRegisterType(ModbusRegisterType.HOLDING);
            response = rResponse;
            break;
        }
        case 0x04: {
            ReadWordRegistersResponse rResponse = new ReadWordRegistersResponse();
            rResponse.setRegisterType(ModbusRegisterType.INPUT);
            response = rResponse;
        }
            break;
        case 0x05:
            response = new WriteCoilResponse();
            break;
        case 0x06:
            response = new WriteHoldingResponse();
            break;
        default:
            throw new UnsupportedFunctionCodeException();
        }
        request.getFunction().decodeResponseData(in, response);
    } else {
        ExceptionResponse exResponse = new ExceptionResponse();
        exResponse.setFunctionCode(functionCode);
        exResponse.setExceptionCode(in.readUnsignedByte());
        response = exResponse;
    }
    response.setTransectionId(transectionId);
    response.setUnitId(unitId);

    out.add(response);

    if (withCheckSum) {
        in.readerIndex(in.readerIndex() + 2);
    }

}

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

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (in.readableBytes() < 6) {
        return;//  w  ww. j  av a 2 s  . c  om
    }
    int aduLength = in.getUnsignedShort(in.readerIndex() + 4);
    int wholeLength = 6 + aduLength + (withCheckSum ? 2 : 0);
    if (in.readableBytes() < wholeLength) {
        return;
    }

    if (withCheckSum) {
        checkSum(in, wholeLength);
    }
    int transectionId = in.readUnsignedShort();
    in.readerIndex(in.readerIndex() + 4);
    int unitId = in.readUnsignedByte();
    int functionCode = in.readUnsignedByte();
    ModbusFunction function;
    switch (functionCode) {
    case 0x01:
        function = new ReadBooleanRegistersFunction(ModbusRegisterType.COIL);
        break;
    case 0x02:
        function = new ReadBooleanRegistersFunction(ModbusRegisterType.DISCRETE_INPUT);
        break;
    case 0x03:
        function = new ReadWordRegistersFunction(ModbusRegisterType.HOLDING);
        break;
    case 0x04:
        function = new ReadWordRegistersFunction(ModbusRegisterType.INPUT);
        break;
    case 0x05:
        function = new WriteCoilFunction();
        break;
    case 0x06:
        function = new WriteHoldingFunction();
        break;
    default:
        throw new UnsupportedFunctionCodeException();
    }
    try {
        function.decodeRequestData(in);
    } catch (Throwable ex) {
        if (ex instanceof DecoderException) {
            throw ex;
        } else {
            throw new DecoderException(ex);
        }
    }
    ModbusRequest request = new ModbusRequest(transectionId, unitId, function);
    out.add(request);
    if (withCheckSum) {
        in.readerIndex(in.readerIndex() + 2);
    }
}