Working on reading multiple messages at once

The ObjectMessageReader does function normally for single messages but
will deliver corrupted objects when a message consisting of multiple
objects is received.
This commit is contained in:
Kai S. K. Engelbart 2020-01-29 07:45:59 +01:00
parent f1a8606825
commit ad937bd154
3 changed files with 18 additions and 13 deletions

View File

@ -86,7 +86,7 @@ public class User {
public String getName() { return name; }
/**
* @param name the user name to set
* @param name the username to set
* @since Envoy Server Standalone v0.1-alpha
* @see User#getName()
*/

View File

@ -5,10 +5,7 @@ import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import com.jenkov.nioserver.IMessageReader;
import com.jenkov.nioserver.Message;
import com.jenkov.nioserver.MessageBuffer;
import com.jenkov.nioserver.Socket;
import com.jenkov.nioserver.*;
import envoy.util.SerializationUtils;
@ -48,18 +45,26 @@ public class ObjectMessageReader implements IMessageReader {
}
nextMessage.writeToMessage(buffer);
buffer.clear();
// Get message length
if (nextMessage.length < 4) return;
int length = SerializationUtils.bytesToInt(nextMessage.sharedArray, nextMessage.offset) + 4;
do {
if (nextMessage.length >= length) {
Message message = messageBuffer.getMessage();
message.writePartialMessageToMessage(nextMessage, nextMessage.offset + length);
completeMessages.add(nextMessage);
nextMessage = message;
}
// Separate first complete message
if (nextMessage.length >= length) {
Message message = messageBuffer.getMessage();
message.writePartialMessageToMessage(nextMessage, nextMessage.offset + length);
message.length = nextMessage.length - length;
completeMessages.add(nextMessage);
nextMessage = message;
}
buffer.clear();
// Get message length
if (nextMessage.length < 4) return;
length = SerializationUtils.bytesToInt(nextMessage.sharedArray, nextMessage.offset) + 4;
} while (nextMessage.length >= length);
}
}

View File

@ -14,7 +14,7 @@ import envoy.server.net.ObjectWriteProxy;
* File: <strong>IdGeneratorRequestProcessor.java</strong><br>
* Created: <strong>28 Jan 2020</strong><br>
*
* @author KSKE
* @author Kai S. K. Engelbart
* @since Envoy Server Standalone v0.1-alpha
*/
public class IdGeneratorRequestProcessor implements ObjectProcessor<IdGeneratorRequest> {