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

View File

@ -20,9 +20,8 @@ public class ObjectMessageProcessor implements IMessageProcessor {
@Override @Override
public void process(Message message, WriteProxy writeProxy) { 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(); Object obj = in.readObject();
// TODO: Process pipeline // TODO: Process pipeline
System.out.println("Read object: " + obj.toString()); System.out.println("Read object: " + obj.toString());
} catch (IOException | ClassNotFoundException e) { } catch (IOException | ClassNotFoundException e) {

View File

@ -41,18 +41,26 @@ public class ObjectMessageReader implements IMessageReader {
} }
nextMessage.writeToMessage(buffer); nextMessage.writeToMessage(buffer);
int length = nextMessage.sharedArray[nextMessage.offset]; // Get message length
if(nextMessage.length - nextMessage.offset >= 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 message = messageBuffer.getMessage();
message.writePartialMessageToMessage(nextMessage, nextMessage.offset + length); message.writePartialMessageToMessage(nextMessage, nextMessage.offset + length);
completeMessages.add(nextMessage); completeMessages.add(nextMessage);
nextMessage = message; nextMessage = message;
} }
buffer.clear(); 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 @Override
public List<Message> getMessages() { return completeMessages; } public List<Message> getMessages() { return completeMessages; }
} }