Example usage for io.netty.buffer CompositeByteBuf writerIndex

List of usage examples for io.netty.buffer CompositeByteBuf writerIndex

Introduction

In this page you can find the example usage for io.netty.buffer CompositeByteBuf writerIndex.

Prototype

@Override
    public CompositeByteBuf writerIndex(int writerIndex) 

Source Link

Usage

From source file:org.eclipse.milo.opcua.stack.server.transport.uasc.UascServerAsymmetricHandler.java

License:Open Source License

private void sendOpenSecureChannelResponse(ChannelHandlerContext ctx, long requestId,
        OpenSecureChannelRequest request) {

    serializationQueue.encode((binaryEncoder, chunkEncoder) -> {
        ByteBuf messageBuffer = BufferUtil.pooledBuffer();

        try {/*from  ww  w .j  av a2 s. c  o  m*/
            OpenSecureChannelResponse response = openSecureChannel(ctx, request);

            binaryEncoder.setBuffer(messageBuffer);
            binaryEncoder.writeMessage(null, response);

            checkMessageSize(messageBuffer);

            chunkEncoder.encodeAsymmetric(secureChannel, requestId, messageBuffer,
                    MessageType.OpenSecureChannel, new ChunkEncoder.Callback() {
                        @Override
                        public void onEncodingError(UaException ex) {
                            logger.error("Error encoding OpenSecureChannelResponse: {}", ex.getMessage(), ex);
                            ctx.fireExceptionCaught(ex);
                        }

                        @Override
                        public void onMessageEncoded(List<ByteBuf> messageChunks, long requestId) {
                            if (!symmetricHandlerAdded) {
                                UascServerSymmetricHandler symmetricHandler = new UascServerSymmetricHandler(
                                        stackServer, serializationQueue, secureChannel);

                                ctx.pipeline().addBefore(ctx.name(), null, symmetricHandler);

                                symmetricHandlerAdded = true;
                            }

                            CompositeByteBuf chunkComposite = BufferUtil.compositeBuffer();

                            for (ByteBuf chunk : messageChunks) {
                                chunkComposite.addComponent(chunk);
                                chunkComposite
                                        .writerIndex(chunkComposite.writerIndex() + chunk.readableBytes());
                            }

                            ctx.writeAndFlush(chunkComposite, ctx.voidPromise());

                            logger.debug("Sent OpenSecureChannelResponse.");
                        }
                    });
        } catch (UaException e) {
            logger.error("Error installing security token: {}", e.getStatusCode(), e);
            ctx.close();
        } catch (UaSerializationException e) {
            ctx.fireExceptionCaught(e);
        } finally {
            messageBuffer.release();
        }
    });
}

From source file:org.eclipse.milo.opcua.stack.server.transport.uasc.UascServerSymmetricHandler.java

License:Open Source License

private void sendServiceResponse(ChannelHandlerContext ctx, long requestId, UaRequestMessage request,
        UaResponseMessage response) {/*from   ww w .  ja v a 2 s .c  o m*/

    serializationQueue.encode((binaryEncoder, chunkEncoder) -> {
        ByteBuf messageBuffer = BufferUtil.pooledBuffer();

        try {
            binaryEncoder.setBuffer(messageBuffer);
            binaryEncoder.writeMessage(null, response);

            checkMessageSize(messageBuffer);

            chunkEncoder.encodeSymmetric(secureChannel, requestId, messageBuffer, MessageType.SecureMessage,
                    new ChunkEncoder.Callback() {
                        @Override
                        public void onEncodingError(UaException ex) {
                            logger.error("Error encoding {}: {}", response, ex.getMessage(), ex);

                            UInteger requestHandle = request.getRequestHeader().getRequestHandle();

                            sendServiceFault(ctx, requestId, requestHandle, ex);
                        }

                        @Override
                        public void onMessageEncoded(List<ByteBuf> messageChunks, long requestId) {
                            CompositeByteBuf chunkComposite = BufferUtil.compositeBuffer();

                            for (ByteBuf chunk : messageChunks) {
                                chunkComposite.addComponent(chunk);
                                chunkComposite
                                        .writerIndex(chunkComposite.writerIndex() + chunk.readableBytes());
                            }

                            ctx.writeAndFlush(chunkComposite, ctx.voidPromise());
                        }
                    });
        } catch (UaSerializationException ex) {
            logger.error("Error encoding response: {}", ex.getStatusCode(), ex);

            UInteger requestHandle = request.getRequestHeader().getRequestHandle();

            sendServiceFault(ctx, requestId, requestHandle, ex);
        } finally {
            messageBuffer.release();
        }
    });
}

From source file:org.eclipse.milo.opcua.stack.server.transport.uasc.UascServerSymmetricHandler.java

License:Open Source License

private void sendServiceFault(ChannelHandlerContext ctx, long requestId, UInteger requestHandle,
        Throwable fault) {/*  w  ww .  j  av a 2  s .  c  o m*/

    StatusCode statusCode = UaException.extract(fault).map(UaException::getStatusCode).orElse(StatusCode.BAD);

    ServiceFault serviceFault = new ServiceFault(
            new ResponseHeader(DateTime.now(), requestHandle, statusCode, null, null, null));

    serializationQueue.encode((binaryEncoder, chunkEncoder) -> {
        ByteBuf messageBuffer = BufferUtil.pooledBuffer();

        try {
            binaryEncoder.setBuffer(messageBuffer);
            binaryEncoder.writeMessage(null, serviceFault);

            checkMessageSize(messageBuffer);

            chunkEncoder.encodeSymmetric(secureChannel, requestId, messageBuffer, MessageType.SecureMessage,
                    new ChunkEncoder.Callback() {
                        @Override
                        public void onEncodingError(UaException ex) {
                            logger.error("Error encoding {}: {}", serviceFault, ex.getMessage(), ex);
                        }

                        @Override
                        public void onMessageEncoded(List<ByteBuf> messageChunks, long requestId) {
                            CompositeByteBuf chunkComposite = BufferUtil.compositeBuffer();

                            for (ByteBuf chunk : messageChunks) {
                                chunkComposite.addComponent(chunk);
                                chunkComposite
                                        .writerIndex(chunkComposite.writerIndex() + chunk.readableBytes());
                            }

                            ctx.writeAndFlush(chunkComposite, ctx.voidPromise());
                        }
                    });
        } catch (UaSerializationException ex) {
            logger.error("Error encoding ServiceFault: {}", ex.getStatusCode(), ex);
        } finally {
            messageBuffer.release();
        }
    });
}

From source file:org.wso2.carbon.gateway.internal.mediation.camel.CarbonMessageTypeConverter.java

License:Open Source License

private CompositeByteBuf aggregateChunks(Pipe pipe) {
    ByteBufInputStream byteBufInputStream = null;
    //Create an instance of composite byte buffer to hold the content chunks
    CompositeByteBuf content = new UnpooledByteBufAllocator(true).compositeBuffer();
    try {/*  w w w  .  jav  a  2 s .com*/
        //Check whether the pipe is filled with HTTP content chunks up to last chunk
        while (pipe.isEmpty() || !pipe.isLastChunkAdded()) {
            Thread.sleep(10);
        }
        //Get a clone of content chunk queue from the pipe
        BlockingQueue<ContentChunk> clonedContent = pipe.getClonedContentQueue();
        //Traverse through the http content chunks and create the composite buffer
        while (true) {
            if (!clonedContent.isEmpty()) {
                //Retrieve the HTTP content chunk from cloned queue
                HttpContent chunk = ((HTTPContentChunk) clonedContent.take()).getHttpContent();
                // Append the content of the chunk to the composite buffer
                if (chunk.content().isReadable()) {
                    chunk.retain();
                    content.addComponent(chunk.content());
                    content.writerIndex(content.writerIndex() + chunk.content().readableBytes());
                }

            } else {
                //When all the content chunks are read, break from the loop
                break;
            }
        }
    } catch (Exception e) {
        log.error("Error occurred during conversion from CarbonMessage", e);
    }
    //Return the composite buffer
    return content;
}