From ac6120adc4cb6a6bacceeb088b8f41a890a5fda0 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Fri, 10 Oct 2014 18:14:43 -0700 Subject: [PATCH] Send control messages as a single TCP packet because the streamer on the PC can choke if it doesn't receive the header and body at the same time --- .../nvstream/control/ControlStream.java | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/moonlight-common/src/com/limelight/nvstream/control/ControlStream.java b/moonlight-common/src/com/limelight/nvstream/control/ControlStream.java index b34fceb8..0255d073 100644 --- a/moonlight-common/src/com/limelight/nvstream/control/ControlStream.java +++ b/moonlight-common/src/com/limelight/nvstream/control/ControlStream.java @@ -80,8 +80,11 @@ public class ControlStream implements ConnectionStatusListener { private void sendPacket(NvCtlPacket packet) throws IOException { - packet.write(out); - out.flush(); + // Prevent multiple clients from writing to the stream at the same time + synchronized (this) { + packet.write(out); + out.flush(); + } } private ControlStream.NvCtlResponse sendAndGetReply(NvCtlPacket packet) throws IOException @@ -257,6 +260,7 @@ public class ControlStream implements ConnectionStatusListener { public byte[] payload; private static final ByteBuffer headerBuffer = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN); + private static final ByteBuffer serializationBuffer = ByteBuffer.allocate(128).order(ByteOrder.LITTLE_ENDIAN); public NvCtlPacket(InputStream in) throws IOException { @@ -356,15 +360,15 @@ public class ControlStream implements ConnectionStatusListener { public void write(OutputStream out) throws IOException { - // Use the class's header buffer to construct the wireform to send - synchronized (headerBuffer) { - headerBuffer.rewind(); - headerBuffer.putShort(type); - headerBuffer.putShort(paylen); - out.write(headerBuffer.array()); + // Use the class's serialization buffer to construct the wireform to send + synchronized (serializationBuffer) { + serializationBuffer.rewind(); + serializationBuffer.putShort(type); + serializationBuffer.putShort(paylen); + serializationBuffer.put(payload); + + out.write(serializationBuffer.array(), 0, serializationBuffer.position()); } - - out.write(payload); } }