Fixed consecutive message writing

This commit is contained in:
Kai S. K. Engelbart 2020-01-11 10:53:01 +01:00
parent 5b2ffb0f7e
commit f0b007aa75
2 changed files with 9 additions and 5 deletions

View File

@ -68,7 +68,7 @@ public class Message {
if (!this.messageBuffer.expandMessage(this)) return -1;
int bytesToCopy = Math.min(remaining, capacity - length);
System.arraycopy(byteArray, offset, sharedArray, offset + this.length, bytesToCopy);
System.arraycopy(byteArray, offset, sharedArray, this.offset + this.length, bytesToCopy);
this.length += bytesToCopy;
return bytesToCopy;
}

View File

@ -19,8 +19,10 @@ public class MessageWriter {
private int bytesWritten;
public void enqueue(Message message) {
if (messageInProgress == null) messageInProgress = message;
else writeQueue.add(message);
if (messageInProgress == null) {
messageInProgress = message;
bytesWritten = 0;
} else writeQueue.add(message);
}
public void write(Socket socket, ByteBuffer byteBuffer) throws IOException {
@ -31,8 +33,10 @@ public class MessageWriter {
byteBuffer.clear();
if (bytesWritten >= messageInProgress.length) {
if (writeQueue.size() > 0) messageInProgress = writeQueue.remove(0);
else messageInProgress = null;
if (writeQueue.size() > 0) {
messageInProgress = writeQueue.remove(0);
bytesWritten = 0;
} else messageInProgress = null;
// TODO: unregister from selector
}
}