Example usage for io.netty.buffer ByteBuf readSlice

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

Introduction

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

Prototype

public abstract ByteBuf readSlice(int length);

Source Link

Document

Returns a new slice of this buffer's sub-region starting at the current readerIndex and increases the readerIndex by the size of the new slice (= length ).

Usage

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:// w  w  w. jav a  2s .c  o  m
            throw new UaException(StatusCodes.Bad_TcpMessageTypeInvalid,
                    "unexpected MessageType: " + messageType);
        }
    }
}

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

License:Apache License

private void decodeMessage(ChannelHandlerContext ctx, ByteBuf buffer) throws UaException {
    int messageLength = getMessageLength(buffer);
    MessageType messageType = MessageType.fromMediumInt(buffer.getMedium(buffer.readerIndex()));

    switch (messageType) {
    case OpenSecureChannel:
        onOpenSecureChannel(ctx, buffer.readSlice(messageLength));
        break;//  w  ww  .  j  a v  a2  s  .c  om

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

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

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

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   www.j a  va 2s. co  m
            out.add(buffer.readSlice(messageLength).retain());
        }
    }
}

From source file:com.digitalpetri.opcua.stack.core.channel.ChunkDecoder.java

License:Apache License

private ByteBuf decode(Delegate delegate, SecureChannel channel, List<ByteBuf> chunkBuffers)
        throws UaException {
    CompositeByteBuf composite = BufferUtil.compositeBuffer();

    int signatureSize = delegate.getSignatureSize(channel);
    int cipherTextBlockSize = delegate.getCipherTextBlockSize(channel);

    boolean encrypted = delegate.isEncryptionEnabled(channel);
    boolean signed = delegate.isSigningEnabled(channel);

    for (ByteBuf chunkBuffer : chunkBuffers) {
        char chunkType = (char) chunkBuffer.getByte(3);

        chunkBuffer.skipBytes(SecureMessageHeader.SECURE_MESSAGE_HEADER_SIZE);

        delegate.readSecurityHeader(channel, chunkBuffer);

        if (encrypted) {
            decryptChunk(delegate, channel, chunkBuffer);
        }/*from  w w  w .java2 s  . c o  m*/

        int encryptedStart = chunkBuffer.readerIndex();
        chunkBuffer.readerIndex(0);

        if (signed) {
            delegate.verifyChunk(channel, chunkBuffer);
        }

        int paddingSize = encrypted ? getPaddingSize(cipherTextBlockSize, signatureSize, chunkBuffer) : 0;
        int bodyEnd = chunkBuffer.readableBytes() - signatureSize - paddingSize;

        chunkBuffer.readerIndex(encryptedStart);

        SequenceHeader sequenceHeader = SequenceHeader.decode(chunkBuffer);
        long sequenceNumber = sequenceHeader.getSequenceNumber();
        lastRequestId = sequenceHeader.getRequestId();

        if (lastSequenceNumber == -1) {
            lastSequenceNumber = sequenceNumber;
        } else {
            if (lastSequenceNumber + 1 != sequenceNumber) {
                String message = String.format("expected sequence number %s but received %s",
                        lastSequenceNumber + 1, sequenceNumber);

                logger.error(message);
                logger.error(ByteBufUtil.hexDump(chunkBuffer, 0, chunkBuffer.writerIndex()));

                throw new UaException(StatusCodes.Bad_SecurityChecksFailed, message);
            }

            lastSequenceNumber = sequenceNumber;
        }

        ByteBuf bodyBuffer = chunkBuffer.readSlice(bodyEnd - chunkBuffer.readerIndex());

        if (chunkType == 'A') {
            ErrorMessage errorMessage = ErrorMessage.decode(bodyBuffer);

            throw new MessageAbortedException(errorMessage.getError(), errorMessage.getReason());
        }

        composite.addComponent(bodyBuffer);
        composite.writerIndex(composite.writerIndex() + bodyBuffer.readableBytes());
    }

    return composite.order(ByteOrder.LITTLE_ENDIAN);
}

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);
            }// www  .  j  a  v  a  2s .  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 w w  w  .  j  a  va2 s .  c  om*/
            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  a  2  s.c  o  m
            out.add(buffer.readSlice(messageLength).retain());
        }
    }
}

From source file:com.eightkdata.mongowp.bson.netty.DefaultNettyBsonLowLevelReader.java

License:Open Source License

@Override
BsonArray readArray(@Loose @ModifiesIndexes ByteBuf byteBuf) throws NettyBsonReaderException {
    int length = byteBuf.readInt();
    int significantLenght = length - 4 - 1;

    ByteBuf significantSlice = byteBuf.readSlice(significantLenght);

    byte b = byteBuf.readByte();
    assert b == 0x00;

    ArrayList<BsonValue<?>> list = new ArrayList<>();
    while (significantSlice.readableBytes() > 0) {
        list.add(readArrayEntry(significantSlice));
    }//from   www. jav a  2 s  .  co  m
    return new ListBsonArray(list);
}

From source file:com.eightkdata.mongowp.bson.netty.DefaultNettyBsonLowLevelReader.java

License:Open Source License

@Override
BsonDocument readDocument(@Loose @ModifiesIndexes ByteBuf byteBuf) throws NettyBsonReaderException {
    int length = byteBuf.readInt();
    int significantLenght = length - 4 - 1;

    ByteBuf significantSlice = byteBuf.readSlice(significantLenght);

    byte b = byteBuf.readByte();
    assert b == 0x00;

    LinkedHashMap<String, BsonValue<?>> values = new LinkedHashMap<>();
    while (significantSlice.readableBytes() > 0) {
        Entry<?> entry = readDocumentEntry(significantSlice);
        values.put(entry.getKey(), entry.getValue());
    }/*w  ww  .ja  va2s . c o  m*/
    return new MapBasedBsonDocument(values);
}

From source file:com.eightkdata.mongowp.bson.netty.OffHeapNettyBsonLowLevelReader.java

License:Open Source License

@Override
BsonDocument readDocument(@Loose @ModifiesIndexes ByteBuf byteBuf) throws NettyBsonReaderException {
    int length = byteBuf.readInt();
    int significantLenght = length - 4; //the final 0x00 must be included

    ByteBuf significantSlice = byteBuf.readSlice(significantLenght);

    assert byteBuf.getByte(byteBuf.readerIndex() - 1) == 0x00;

    return new IterableNettyBsonDocument(significantSlice, this);
}