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:
		| @@ -5,10 +5,7 @@ import java.nio.ByteBuffer; | |||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
| import java.util.List; | import java.util.List; | ||||||
|  |  | ||||||
| import com.jenkov.nioserver.IMessageReader; | import com.jenkov.nioserver.*; | ||||||
| import com.jenkov.nioserver.Message; |  | ||||||
| import com.jenkov.nioserver.MessageBuffer; |  | ||||||
| import com.jenkov.nioserver.Socket; |  | ||||||
|  |  | ||||||
| import envoy.util.SerializationUtils; | import envoy.util.SerializationUtils; | ||||||
|  |  | ||||||
| @@ -48,18 +45,26 @@ public class ObjectMessageReader implements IMessageReader { | |||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		nextMessage.writeToMessage(buffer); | 		nextMessage.writeToMessage(buffer); | ||||||
|  | 		buffer.clear(); | ||||||
|  |  | ||||||
| 		// Get message length | 		// Get message length | ||||||
| 		if (nextMessage.length < 4) return; | 		if (nextMessage.length < 4) return; | ||||||
| 		int length = SerializationUtils.bytesToInt(nextMessage.sharedArray, nextMessage.offset) + 4; | 		int length = SerializationUtils.bytesToInt(nextMessage.sharedArray, nextMessage.offset) + 4; | ||||||
|  | 		do { | ||||||
|  |  | ||||||
|  | 			// Separate first complete message | ||||||
| 			if (nextMessage.length >= length) { | 			if (nextMessage.length >= length) { | ||||||
| 				Message message = messageBuffer.getMessage(); | 				Message message = messageBuffer.getMessage(); | ||||||
| 				message.writePartialMessageToMessage(nextMessage, nextMessage.offset + length); | 				message.writePartialMessageToMessage(nextMessage, nextMessage.offset + length); | ||||||
|  | 				message.length = nextMessage.length - length; | ||||||
| 				completeMessages.add(nextMessage); | 				completeMessages.add(nextMessage); | ||||||
| 				nextMessage = message; | 				nextMessage = message; | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 		buffer.clear(); | 			// Get message length | ||||||
|  | 			if (nextMessage.length < 4) return; | ||||||
|  | 			length = SerializationUtils.bytesToInt(nextMessage.sharedArray, nextMessage.offset) + 4; | ||||||
|  |  | ||||||
|  | 		} while (nextMessage.length >= length); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| @@ -14,7 +14,7 @@ import envoy.server.net.ObjectWriteProxy; | |||||||
|  * File: <strong>IdGeneratorRequestProcessor.java</strong><br> |  * File: <strong>IdGeneratorRequestProcessor.java</strong><br> | ||||||
|  * Created: <strong>28 Jan 2020</strong><br> |  * Created: <strong>28 Jan 2020</strong><br> | ||||||
|  * |  * | ||||||
|  * @author KSKE |  * @author Kai S. K. Engelbart | ||||||
|  * @since Envoy Server Standalone v0.1-alpha |  * @since Envoy Server Standalone v0.1-alpha | ||||||
|  */ |  */ | ||||||
| public class IdGeneratorRequestProcessor implements ObjectProcessor<IdGeneratorRequest> { | public class IdGeneratorRequestProcessor implements ObjectProcessor<IdGeneratorRequest> { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user