From 4c4a0de804421161096cc6fa5fa31c7b1e7d1fc8 Mon Sep 17 00:00:00 2001 From: Bjakke Date: Wed, 26 Sep 2018 12:48:04 +0300 Subject: [PATCH] Fixed #167, removed extra size calculation, moved size check to start --- .../ua/transport/tcp/io/TcpConnection.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/opcfoundation/ua/transport/tcp/io/TcpConnection.java b/src/main/java/org/opcfoundation/ua/transport/tcp/io/TcpConnection.java index e12d3392..f5a59084 100644 --- a/src/main/java/org/opcfoundation/ua/transport/tcp/io/TcpConnection.java +++ b/src/main/java/org/opcfoundation/ua/transport/tcp/io/TcpConnection.java @@ -1278,7 +1278,14 @@ private void sendAsymmChunk(int secureChannelId, int requestId, SecurityMode sec out.put(chunk); } - private MessageBuffers encodeMessage(ChunkFactory cf, int len, IEncodeable request) throws ServiceResultException { + private MessageBuffers encodeMessage(ChunkFactory cf, final int len, IEncodeable request) throws ServiceResultException { + // check message max size + if ( ctx.maxMessageSize!=0 && len > ctx.maxMessageSize ) { + final EncodingException encodingException = new EncodingException(StatusCodes.Bad_EncodingLimitsExceeded, "MaxMessageSize "+ctx.maxMessageSize+" < "+len); + logger.warn("encodeMessage: failed", encodingException); + throw encodingException; + } + // Calculate chunk count final int count = (len + cf.maxPlaintextSize - 1) / cf.maxPlaintextSize; int maxSendChunkCount; @@ -1313,15 +1320,6 @@ public void onChunkComplete(ByteBuffer[] chunks, int index) { ByteBufferArrayWriteable2 outBuffer = new ByteBufferArrayWriteable2(plaintexts, listener); outBuffer.order(ByteOrder.LITTLE_ENDIAN); - EncoderCalc calc = new EncoderCalc(); - calc.setEncoderContext(ctx); - calc.putMessage( request ); - if ( ctx.maxMessageSize!=0 && calc.getLength() > ctx.maxMessageSize ) { - final EncodingException encodingException = new EncodingException(StatusCodes.Bad_EncodingLimitsExceeded, "MaxMessageSize "+ctx.maxMessageSize+" < "+len); - logger.warn("encodeMessage: failed", encodingException); - throw encodingException; - } - BinaryEncoder enc = new BinaryEncoder(outBuffer); enc.setEncoderMode(EncoderMode.NonStrict); enc.setEncoderContext(ctx);