From 19ec7c120e6309a589f4a034b71183d7d98b7c5b Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sun, 29 Dec 2019 10:48:15 +0200 Subject: [PATCH] Changed length of encoded message length to 4 bytes --- pom.xml | 7 ++++++- .../envoy/server/ObjectMessageProcessor.java | 3 +-- .../java/envoy/server/ObjectMessageReader.java | 16 ++++++++++++---- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index 144ebd0..b85a456 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ informatik-ag-ngl envoy-server-standalone - 0.0.1-SNAPSHOT + 0.1-alpha Envoy Server https://github.com/informatik-ag-ngl/envoy-server-standalone @@ -18,6 +18,11 @@ + + informatik-ag-ngl + envoy-common + 0.2-alpha + informatik-ag-ngl java-nio-server diff --git a/src/main/java/envoy/server/ObjectMessageProcessor.java b/src/main/java/envoy/server/ObjectMessageProcessor.java index bd2a735..3f52e07 100644 --- a/src/main/java/envoy/server/ObjectMessageProcessor.java +++ b/src/main/java/envoy/server/ObjectMessageProcessor.java @@ -20,9 +20,8 @@ public class ObjectMessageProcessor implements IMessageProcessor { @Override public void process(Message message, WriteProxy writeProxy) { - try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(message.sharedArray, message.offset + 1, message.length - 1))) { + try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(message.sharedArray, message.offset + 4, message.length - 4))) { Object obj = in.readObject(); - // TODO: Process pipeline System.out.println("Read object: " + obj.toString()); } catch (IOException | ClassNotFoundException e) { diff --git a/src/main/java/envoy/server/ObjectMessageReader.java b/src/main/java/envoy/server/ObjectMessageReader.java index 2249954..9a20c7f 100644 --- a/src/main/java/envoy/server/ObjectMessageReader.java +++ b/src/main/java/envoy/server/ObjectMessageReader.java @@ -41,18 +41,26 @@ public class ObjectMessageReader implements IMessageReader { } nextMessage.writeToMessage(buffer); - - int length = nextMessage.sharedArray[nextMessage.offset]; - if(nextMessage.length - nextMessage.offset >= length) { + + // Get message length + if (nextMessage.length - nextMessage.offset < 4) return; + int length = fromByteArray(nextMessage.sharedArray, nextMessage.offset) + 4; + + if (nextMessage.length - nextMessage.offset >= length) { Message message = messageBuffer.getMessage(); message.writePartialMessageToMessage(nextMessage, nextMessage.offset + length); completeMessages.add(nextMessage); nextMessage = message; } - + buffer.clear(); } + private int fromByteArray(byte[] bytes, int offset) { + return ((bytes[offset] & 0xFF) << 24) | ((bytes[offset + 1] & 0xFF) << 16) | ((bytes[offset + 2] & 0xFF) << 8) + | ((bytes[offset + 3] & 0xFF) << 0); + } + @Override public List getMessages() { return completeMessages; } } \ No newline at end of file