Changed length of encoded message length to 4 bytes

This commit is contained in:
Kai S. K. Engelbart 2019-12-29 10:48:15 +02:00
parent 6c094bdccd
commit 19ec7c120e
3 changed files with 19 additions and 7 deletions

View File

@ -5,7 +5,7 @@
<groupId>informatik-ag-ngl</groupId>
<artifactId>envoy-server-standalone</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.1-alpha</version>
<name>Envoy Server</name>
<url>https://github.com/informatik-ag-ngl/envoy-server-standalone</url>
@ -18,6 +18,11 @@
</properties>
<dependencies>
<dependency>
<groupId>informatik-ag-ngl</groupId>
<artifactId>envoy-common</artifactId>
<version>0.2-alpha</version>
</dependency>
<dependency>
<groupId>informatik-ag-ngl</groupId>
<artifactId>java-nio-server</artifactId>

View File

@ -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) {

View File

@ -42,8 +42,11 @@ 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);
@ -53,6 +56,11 @@ public class ObjectMessageReader implements IMessageReader {
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<Message> getMessages() { return completeMessages; }
}