Skip to content

Commit

Permalink
Added buffered streams inbetween socket and data streams
Browse files Browse the repository at this point in the history
  • Loading branch information
bjakke committed Jun 9, 2018
1 parent fd1d4df commit d5f02ac
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/moonlander/library/SocketConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.HashMap;
import java.net.Socket;
import java.io.DataOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.util.logging.Logger;
import java.io.IOException;
Expand Down Expand Up @@ -76,8 +78,8 @@ private void initSocket(String host, int port) throws Exception {
logger.warning(String.format("Connection to %s:%d failed.", host, port));
throw e;
}
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));

logger.finer("Connection to Rocket established.");
}
Expand Down Expand Up @@ -145,7 +147,7 @@ public void controllerRowChanged(int row) {
logger.finest("Communicating row="+row+" change to rocket");

try {
out.writeInt(Commands.SET_ROW);
out.writeByte(Commands.SET_ROW);
out.writeInt(row);
out.flush();
} catch (Exception e) {
Expand Down

1 comment on commit d5f02ac

@sooda
Copy link

@sooda sooda commented on d5f02ac Jun 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The buffered output stream appears to be mostly a workaround. The bug is in the editor side AFAICT - a bare recv() should be retried if it returns less than the requested amount of bytes, but at least the greet part gets cut if fewer bytes were transmitted (which seems odd - these packets are always really small, so I wouldn't expect them to get cut). Also, now with a buffer in between, I believe explicit flushes after each complete message frame are very necessary (which you added in a previous patch). It's a good idea to only send complete frames for performance though.

Maybe the buffered input stream makes things like readInt() here work better; I don't know whether Java raises an exception if there's fewer bytes available (either reading a single byte will always block or not, idk).

Please sign in to comment.