Search in sources :

Example 1 with MessageEncodeException

use of org.eclipse.milo.opcua.stack.core.channel.MessageEncodeException in project milo by eclipse.

the class UascClientMessageHandler method encode.

@Override
protected void encode(ChannelHandlerContext ctx, UaTransportRequest request, ByteBuf buffer) {
    serializationQueue.encode((binaryEncoder, chunkEncoder) -> {
        ByteBuf messageBuffer = BufferUtil.pooledBuffer();
        try {
            binaryEncoder.setBuffer(messageBuffer);
            binaryEncoder.writeMessage(null, request.getRequest());
            checkMessageSize(messageBuffer);
            EncodedMessage encodedMessage = chunkEncoder.encodeSymmetric(secureChannel, requestIdSequence.getAndIncrement(), messageBuffer, MessageType.SecureMessage);
            long requestId = encodedMessage.getRequestId();
            List<ByteBuf> messageChunks = encodedMessage.getMessageChunks();
            pending.put(requestId, request);
            // No matter how we complete, make sure the entry in pending is removed.
            // This covers the case where the request fails due to a timeout in the
            // transport layer as well as normal completion.
            request.getFuture().whenComplete((r, x) -> pending.remove(requestId));
            CompositeByteBuf chunkComposite = BufferUtil.compositeBuffer();
            for (ByteBuf chunk : messageChunks) {
                chunkComposite.addComponent(chunk);
                chunkComposite.writerIndex(chunkComposite.writerIndex() + chunk.readableBytes());
            }
            ctx.writeAndFlush(chunkComposite, ctx.voidPromise());
        } catch (MessageEncodeException e) {
            logger.error("Error encoding {}: {}", request.getRequest(), e.getMessage(), e);
            request.getFuture().completeExceptionally(e);
            ctx.close();
        } catch (UaSerializationException e) {
            logger.error("Error serializing {}: {}", request.getRequest(), e.getMessage(), e);
            request.getFuture().completeExceptionally(e);
        } finally {
            messageBuffer.release();
        }
    });
}
Also used : EncodedMessage(org.eclipse.milo.opcua.stack.core.channel.ChunkEncoder.EncodedMessage) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) UaSerializationException(org.eclipse.milo.opcua.stack.core.UaSerializationException) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) MessageEncodeException(org.eclipse.milo.opcua.stack.core.channel.MessageEncodeException)

Example 2 with MessageEncodeException

use of org.eclipse.milo.opcua.stack.core.channel.MessageEncodeException in project milo by eclipse.

the class UascServerSymmetricHandler method sendServiceResponse.

private void sendServiceResponse(ChannelHandlerContext ctx, long requestId, UaRequestMessage request, UaResponseMessage response) {
    serializationQueue.encode((binaryEncoder, chunkEncoder) -> {
        ByteBuf messageBuffer = BufferUtil.pooledBuffer();
        try {
            binaryEncoder.setBuffer(messageBuffer);
            binaryEncoder.writeMessage(null, response);
            checkMessageSize(messageBuffer);
            EncodedMessage encodedMessage = chunkEncoder.encodeSymmetric(secureChannel, requestId, messageBuffer, MessageType.SecureMessage);
            CompositeByteBuf chunkComposite = BufferUtil.compositeBuffer();
            for (ByteBuf chunk : encodedMessage.getMessageChunks()) {
                chunkComposite.addComponent(chunk);
                chunkComposite.writerIndex(chunkComposite.writerIndex() + chunk.readableBytes());
            }
            ctx.writeAndFlush(chunkComposite, ctx.voidPromise());
        } catch (MessageEncodeException e) {
            logger.error("Error encoding {}: {}", response, e.getMessage(), e);
            UInteger requestHandle = request.getRequestHeader().getRequestHandle();
            sendServiceFault(ctx, requestId, requestHandle, e);
        } catch (UaSerializationException e) {
            logger.error("Error serializing response: {}", e.getStatusCode(), e);
            UInteger requestHandle = request.getRequestHeader().getRequestHandle();
            sendServiceFault(ctx, requestId, requestHandle, e);
        } finally {
            messageBuffer.release();
        }
    });
}
Also used : EncodedMessage(org.eclipse.milo.opcua.stack.core.channel.ChunkEncoder.EncodedMessage) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) UaSerializationException(org.eclipse.milo.opcua.stack.core.UaSerializationException) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) MessageEncodeException(org.eclipse.milo.opcua.stack.core.channel.MessageEncodeException)

Example 3 with MessageEncodeException

use of org.eclipse.milo.opcua.stack.core.channel.MessageEncodeException in project milo by eclipse.

the class UascServerSymmetricHandler method sendServiceFault.

private void sendServiceFault(ChannelHandlerContext ctx, long requestId, UInteger requestHandle, Throwable fault) {
    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);
            EncodedMessage encodedMessage = chunkEncoder.encodeSymmetric(secureChannel, requestId, messageBuffer, MessageType.SecureMessage);
            CompositeByteBuf chunkComposite = BufferUtil.compositeBuffer();
            for (ByteBuf chunk : encodedMessage.getMessageChunks()) {
                chunkComposite.addComponent(chunk);
                chunkComposite.writerIndex(chunkComposite.writerIndex() + chunk.readableBytes());
            }
            ctx.writeAndFlush(chunkComposite, ctx.voidPromise());
        } catch (MessageEncodeException e) {
            logger.error("Error encoding {}: {}", serviceFault, e.getMessage(), e);
        } catch (UaSerializationException e) {
            logger.error("Error serializing ServiceFault: {}", e.getStatusCode(), e);
        } finally {
            messageBuffer.release();
        }
    });
}
Also used : EncodedMessage(org.eclipse.milo.opcua.stack.core.channel.ChunkEncoder.EncodedMessage) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) UaSerializationException(org.eclipse.milo.opcua.stack.core.UaSerializationException) ResponseHeader(org.eclipse.milo.opcua.stack.core.types.structured.ResponseHeader) ServiceFault(org.eclipse.milo.opcua.stack.core.types.structured.ServiceFault) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) MessageEncodeException(org.eclipse.milo.opcua.stack.core.channel.MessageEncodeException) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)

Example 4 with MessageEncodeException

use of org.eclipse.milo.opcua.stack.core.channel.MessageEncodeException in project milo by eclipse.

the class ChunkSerializationTest method testSymmetric4096.

@Test
public void testSymmetric4096() throws Exception {
    ChannelParameters parameters = defaultParameters;
    ChunkEncoder encoder = new ChunkEncoder(parameters);
    ChunkDecoder decoder = new ChunkDecoder(parameters, EncodingLimits.DEFAULT);
    SecureChannel[] channels = generateChannels4096();
    ClientSecureChannel clientChannel = (ClientSecureChannel) channels[0];
    ServerSecureChannel serverChannel = (ServerSecureChannel) channels[1];
    clientChannel.attr(ClientSecureChannel.KEY_REQUEST_ID_SEQUENCE).setIfAbsent(new LongSequence(1L, UInteger.MAX_VALUE));
    LongSequence requestId = clientChannel.attr(ClientSecureChannel.KEY_REQUEST_ID_SEQUENCE).get();
    for (int messageSize = 0; messageSize < 1024; messageSize++) {
        byte[] messageBytes = new byte[messageSize];
        for (int i = 0; i < messageBytes.length; i++) {
            messageBytes[i] = (byte) i;
        }
        ByteBuf messageBuffer = BufferUtil.pooledBuffer().writeBytes(messageBytes);
        List<ByteBuf> chunkBuffers = new ArrayList<>();
        try {
            ChunkEncoder.EncodedMessage message = encoder.encodeSymmetric(clientChannel, requestId.getAndIncrement(), messageBuffer, MessageType.OpenSecureChannel);
            chunkBuffers.addAll(message.getMessageChunks());
        } catch (MessageEncodeException e) {
            fail("encoding error", e);
        }
        try {
            ChunkDecoder.DecodedMessage decodedMessage = decoder.decodeSymmetric(serverChannel, chunkBuffers);
            ByteBuf message = decodedMessage.getMessage();
            messageBuffer.readerIndex(0);
            assertEquals(message, messageBuffer);
            ReferenceCountUtil.release(message);
            ReferenceCountUtil.release(messageBuffer);
        } catch (Throwable t) {
            fail("decoding error", t);
        }
    }
}
Also used : ServerSecureChannel(org.eclipse.milo.opcua.stack.core.channel.ServerSecureChannel) ChannelParameters(org.eclipse.milo.opcua.stack.core.channel.ChannelParameters) ChunkEncoder(org.eclipse.milo.opcua.stack.core.channel.ChunkEncoder) ClientSecureChannel(org.eclipse.milo.opcua.stack.client.transport.uasc.ClientSecureChannel) ArrayList(java.util.ArrayList) ServerSecureChannel(org.eclipse.milo.opcua.stack.core.channel.ServerSecureChannel) SecureChannel(org.eclipse.milo.opcua.stack.core.channel.SecureChannel) ClientSecureChannel(org.eclipse.milo.opcua.stack.client.transport.uasc.ClientSecureChannel) ByteBuf(io.netty.buffer.ByteBuf) ChunkDecoder(org.eclipse.milo.opcua.stack.core.channel.ChunkDecoder) LongSequence(org.eclipse.milo.opcua.stack.core.util.LongSequence) MessageEncodeException(org.eclipse.milo.opcua.stack.core.channel.MessageEncodeException) Test(org.testng.annotations.Test)

Example 5 with MessageEncodeException

use of org.eclipse.milo.opcua.stack.core.channel.MessageEncodeException in project milo by eclipse.

the class ChunkSerializationTest method testAsymmetric4096.

@Test
public void testAsymmetric4096() throws Exception {
    ChannelParameters parameters = defaultParameters;
    ChunkEncoder encoder = new ChunkEncoder(parameters);
    ChunkDecoder decoder = new ChunkDecoder(parameters, EncodingLimits.DEFAULT);
    SecureChannel[] channels = generateChannels4096();
    ClientSecureChannel clientChannel = (ClientSecureChannel) channels[0];
    ServerSecureChannel serverChannel = (ServerSecureChannel) channels[1];
    clientChannel.attr(ClientSecureChannel.KEY_REQUEST_ID_SEQUENCE).setIfAbsent(new LongSequence(1L, UInteger.MAX_VALUE));
    LongSequence requestId = clientChannel.attr(ClientSecureChannel.KEY_REQUEST_ID_SEQUENCE).get();
    for (int messageSize = 0; messageSize < 512; messageSize++) {
        byte[] messageBytes = new byte[messageSize];
        for (int i = 0; i < messageBytes.length; i++) {
            messageBytes[i] = (byte) i;
        }
        ByteBuf messageBuffer = BufferUtil.pooledBuffer().writeBytes(messageBytes);
        List<ByteBuf> chunkBuffers = new ArrayList<>();
        try {
            ChunkEncoder.EncodedMessage message = encoder.encodeAsymmetric(clientChannel, requestId.getAndIncrement(), messageBuffer, MessageType.OpenSecureChannel);
            chunkBuffers.addAll(message.getMessageChunks());
        } catch (MessageEncodeException e) {
            fail("encoding error", e);
        }
        try {
            ChunkDecoder.DecodedMessage decodedMessage = decoder.decodeAsymmetric(serverChannel, chunkBuffers);
            ByteBuf message = decodedMessage.getMessage();
            messageBuffer.readerIndex(0);
            assertEquals(message, messageBuffer);
            ReferenceCountUtil.release(message);
            ReferenceCountUtil.release(messageBuffer);
        } catch (Throwable t) {
            fail("decoding error", t);
        }
    }
}
Also used : ServerSecureChannel(org.eclipse.milo.opcua.stack.core.channel.ServerSecureChannel) ChannelParameters(org.eclipse.milo.opcua.stack.core.channel.ChannelParameters) ChunkEncoder(org.eclipse.milo.opcua.stack.core.channel.ChunkEncoder) ClientSecureChannel(org.eclipse.milo.opcua.stack.client.transport.uasc.ClientSecureChannel) ArrayList(java.util.ArrayList) ServerSecureChannel(org.eclipse.milo.opcua.stack.core.channel.ServerSecureChannel) SecureChannel(org.eclipse.milo.opcua.stack.core.channel.SecureChannel) ClientSecureChannel(org.eclipse.milo.opcua.stack.client.transport.uasc.ClientSecureChannel) ByteBuf(io.netty.buffer.ByteBuf) ChunkDecoder(org.eclipse.milo.opcua.stack.core.channel.ChunkDecoder) LongSequence(org.eclipse.milo.opcua.stack.core.util.LongSequence) MessageEncodeException(org.eclipse.milo.opcua.stack.core.channel.MessageEncodeException) Test(org.testng.annotations.Test)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)10 MessageEncodeException (org.eclipse.milo.opcua.stack.core.channel.MessageEncodeException)10 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)6 EncodedMessage (org.eclipse.milo.opcua.stack.core.channel.ChunkEncoder.EncodedMessage)6 UaSerializationException (org.eclipse.milo.opcua.stack.core.UaSerializationException)5 ArrayList (java.util.ArrayList)4 ClientSecureChannel (org.eclipse.milo.opcua.stack.client.transport.uasc.ClientSecureChannel)4 ChannelParameters (org.eclipse.milo.opcua.stack.core.channel.ChannelParameters)4 ChunkDecoder (org.eclipse.milo.opcua.stack.core.channel.ChunkDecoder)4 ChunkEncoder (org.eclipse.milo.opcua.stack.core.channel.ChunkEncoder)4 SecureChannel (org.eclipse.milo.opcua.stack.core.channel.SecureChannel)4 ServerSecureChannel (org.eclipse.milo.opcua.stack.core.channel.ServerSecureChannel)4 LongSequence (org.eclipse.milo.opcua.stack.core.util.LongSequence)4 Test (org.testng.annotations.Test)4 UaException (org.eclipse.milo.opcua.stack.core.UaException)1 ChannelSecurity (org.eclipse.milo.opcua.stack.core.channel.ChannelSecurity)1 ByteString (org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)1 StatusCode (org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)1 UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)1 OpenSecureChannelRequest (org.eclipse.milo.opcua.stack.core.types.structured.OpenSecureChannelRequest)1