Example usage for io.netty.buffer ByteBuf order

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

Introduction

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

Prototype

@Deprecated
public abstract ByteBuf order(ByteOrder endianness);

Source Link

Document

Returns a buffer with the specified endianness which shares the whole region, indexes, and marks of this buffer.

Usage

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 .ja  v a2s  . com*/
    }
    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:com.chat.common.netty.handler.decode.LengthFieldBasedFrameDecoder.java

License:Apache License

/**
 * Decodes the specified region of the buffer into an unadjusted frame length.  The default implementation is
 * capable of decoding the specified region into an unsigned 8/16/24/32/64 bit integer.  Override this method to
 * decode the length field encoded differently.  Note that this method must not modify the state of the specified
 * buffer (e.g. {@code readerIndex}, {@code writerIndex}, and the content of the buffer.)
 *
 * @throws DecoderException if failed to decode the specified region
 *//*  w w  w  .j  a  va2  s  . co  m*/
protected long getUnadjustedFrameLength(ByteBuf buf, int offset, int length, ByteOrder order) {
    buf = buf.order(order);
    long frameLength;
    switch (length) {
    case 1:
        frameLength = buf.getUnsignedByte(offset);
        break;
    case 2:
        frameLength = buf.getUnsignedShort(offset);
        break;
    case 3:
        frameLength = buf.getUnsignedMedium(offset);
        break;
    case 4:
        frameLength = buf.getUnsignedInt(offset);
        break;
    case 8:
        frameLength = buf.getLong(offset);
        break;
    default:
        throw new DecoderException(
                "unsupported lengthFieldLength: " + lengthFieldLength + " (expected: 1, 2, 3, 4, or 8)");
    }
    return frameLength;
}

From source file:com.cloudhopper.smpp.transcoder.DefaultPduTranscoder.java

License:Apache License

@Override
public ByteBuf encode(Pdu pdu) throws UnrecoverablePduException, RecoverablePduException {
    // see if we can map the command status into a message
    if (pdu instanceof PduResponse) {
        PduResponse response = (PduResponse) pdu;
        if (response.getResultMessage() == null) {
            response.setResultMessage(context.lookupResultMessage(pdu.getCommandStatus()));
        }/*w w w  .j  a  va 2  s  .c  o  m*/
    }

    // if the pdu length hasn't been assigned yet, calculate it now
    // NOTE: it may be safest to recalculate it, but we won't since the SmppSession
    // should really be the only interface creating PDUs
    if (!pdu.hasCommandLengthCalculated()) {
        pdu.calculateAndSetCommandLength();
    }

    // create the buffer and add the header
    ByteBuf buffer = Unpooled.buffer(pdu.getCommandLength());
    buffer.order(ByteOrder.BIG_ENDIAN);

    buffer.writeInt(pdu.getCommandLength());
    buffer.writeInt(pdu.getCommandId());
    buffer.writeInt(pdu.getCommandStatus());
    buffer.writeInt(pdu.getSequenceNumber());

    // add mandatory body (a noop if no body exists)
    pdu.writeBody(buffer);

    // add optional parameters (a noop if none exist)
    pdu.writeOptionalParameters(buffer, context);

    // NOTE: at this point, the entire buffer written MUST match the command length
    // from earlier -- if it doesn't match, the our encoding process went awry
    if (buffer.readableBytes() != pdu.getCommandLength()) {
        throw new NotEnoughDataInBufferException(
                "During PDU encoding the expected commandLength did not match the actual encoded (a serious error with our own encoding process)",
                pdu.getCommandLength(), buffer.readableBytes());
    }

    return buffer;
}

From source file:com.digitalpetri.opcua.stack.client.handlers.UaTcpClientAcknowledgeHandler.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
    buffer = buffer.order(ByteOrder.LITTLE_ENDIAN);

    while (buffer.readableBytes() >= HEADER_LENGTH && buffer.readableBytes() >= getMessageLength(buffer)) {

        int messageLength = getMessageLength(buffer);
        MessageType messageType = MessageType.fromMediumInt(buffer.getMedium(buffer.readerIndex()));

        switch (messageType) {
        case Acknowledge:
            onAcknowledge(ctx, buffer.readSlice(messageLength));
            break;

        case Error:
            onError(ctx, buffer.readSlice(messageLength));
            break;

        default:// w  w  w  .  j a va2s.  com
            out.add(buffer.readSlice(messageLength).retain());
        }
    }
}

From source file:com.digitalpetri.opcua.stack.client.handlers.UaTcpClientAsymmetricHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
    buffer = buffer.order(ByteOrder.LITTLE_ENDIAN);

    while (buffer.readableBytes() >= HEADER_LENGTH && buffer.readableBytes() >= getMessageLength(buffer)) {

        int messageLength = getMessageLength(buffer);
        MessageType messageType = MessageType.fromMediumInt(buffer.getMedium(buffer.readerIndex()));

        switch (messageType) {
        case OpenSecureChannel:
            onOpenSecureChannel(ctx, buffer.readSlice(messageLength));
            break;

        case Error:
            onError(ctx, buffer.readSlice(messageLength));
            break;

        default:/*from w w  w  . j  av a2s.c om*/
            throw new UaException(StatusCodes.Bad_TcpMessageTypeInvalid,
                    "unexpected MessageType: " + messageType);
        }
    }
}

From source file:com.digitalpetri.opcua.stack.client.handlers.UaTcpClientMessageHandler.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
    if (buffer.readableBytes() >= HEADER_LENGTH) {
        buffer = buffer.order(ByteOrder.LITTLE_ENDIAN);

        if (buffer.readableBytes() >= getMessageLength(buffer)) {
            decodeMessage(ctx, buffer);/*from   w  w  w.j  av a2 s .  co  m*/
        }
    }
}

From source file:com.digitalpetri.opcua.stack.client.handlers.UaTcpClientSymmetricHandler.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
    buffer = buffer.order(ByteOrder.LITTLE_ENDIAN);

    while (buffer.readableBytes() >= HEADER_LENGTH && buffer.readableBytes() >= getMessageLength(buffer)) {

        int messageLength = getMessageLength(buffer);
        MessageType messageType = MessageType.fromMediumInt(buffer.getMedium(buffer.readerIndex()));

        switch (messageType) {
        case SecureMessage:
            onSecureMessage(ctx, buffer.readSlice(messageLength));
            break;

        case Error:
            onError(ctx, buffer.readSlice(messageLength));
            break;

        default://from ww  w . j a va2  s  . co m
            out.add(buffer.readSlice(messageLength).retain());
        }
    }
}

From source file:com.digitalpetri.opcua.stack.server.handlers.UaTcpServerAsymmetricHandler.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
    buffer = buffer.order(ByteOrder.LITTLE_ENDIAN);

    while (buffer.readableBytes() >= HEADER_LENGTH && buffer.readableBytes() >= getMessageLength(buffer)) {

        int messageLength = getMessageLength(buffer);
        MessageType messageType = MessageType.fromMediumInt(buffer.getMedium(buffer.readerIndex()));

        switch (messageType) {
        case OpenSecureChannel:
            onOpenSecureChannel(ctx, buffer.readSlice(messageLength));
            break;

        case CloseSecureChannel:
            logger.debug("Received CloseSecureChannelRequest");
            if (secureChannel != null) {
                server.closeSecureChannel(secureChannel);
            }/*from   w ww.  j  a va 2 s.  c o m*/
            buffer.skipBytes(messageLength);
            break;

        default:
            throw new UaException(StatusCodes.Bad_TcpMessageTypeInvalid,
                    "unexpected MessageType: " + messageType);
        }
    }
}

From source file:com.digitalpetri.opcua.stack.server.handlers.UaTcpServerHelloHandler.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
    buffer = buffer.order(ByteOrder.LITTLE_ENDIAN);

    while (buffer.readableBytes() >= HEADER_LENGTH && buffer.readableBytes() >= getMessageLength(buffer)) {

        int messageLength = getMessageLength(buffer);
        MessageType messageType = MessageType.fromMediumInt(buffer.getMedium(buffer.readerIndex()));

        switch (messageType) {
        case Hello:
            onHello(ctx, buffer.readSlice(messageLength));
            break;

        default:/*from  ww w . j  a v a2  s.  c o m*/
            throw new UaException(StatusCodes.Bad_TcpMessageTypeInvalid,
                    "unexpected MessageType: " + messageType);
        }
    }
}

From source file:com.digitalpetri.opcua.stack.server.handlers.UaTcpServerSymmetricHandler.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
    buffer = buffer.order(ByteOrder.LITTLE_ENDIAN);

    while (buffer.readableBytes() >= HEADER_LENGTH && buffer.readableBytes() >= getMessageLength(buffer)) {

        int messageLength = getMessageLength(buffer);
        MessageType messageType = MessageType.fromMediumInt(buffer.getMedium(buffer.readerIndex()));

        switch (messageType) {
        case SecureMessage:
            onSecureMessage(ctx, buffer.readSlice(messageLength), out);
            break;

        default://  w  ww. j a  v a2  s  .com
            out.add(buffer.readSlice(messageLength).retain());
        }
    }
}