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

This commit is contained in:
Cameron Gutman 2014-10-10 18:14:43 -07:00
parent d9c2d58519
commit ac6120adc4

View File

@ -80,8 +80,11 @@ public class ControlStream implements ConnectionStatusListener {
private void sendPacket(NvCtlPacket packet) throws IOException private void sendPacket(NvCtlPacket packet) throws IOException
{ {
packet.write(out); // Prevent multiple clients from writing to the stream at the same time
out.flush(); synchronized (this) {
packet.write(out);
out.flush();
}
} }
private ControlStream.NvCtlResponse sendAndGetReply(NvCtlPacket packet) throws IOException private ControlStream.NvCtlResponse sendAndGetReply(NvCtlPacket packet) throws IOException
@ -257,6 +260,7 @@ public class ControlStream implements ConnectionStatusListener {
public byte[] payload; public byte[] payload;
private static final ByteBuffer headerBuffer = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN); 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 public NvCtlPacket(InputStream in) throws IOException
{ {
@ -356,15 +360,15 @@ public class ControlStream implements ConnectionStatusListener {
public void write(OutputStream out) throws IOException public void write(OutputStream out) throws IOException
{ {
// Use the class's header buffer to construct the wireform to send // Use the class's serialization buffer to construct the wireform to send
synchronized (headerBuffer) { synchronized (serializationBuffer) {
headerBuffer.rewind(); serializationBuffer.rewind();
headerBuffer.putShort(type); serializationBuffer.putShort(type);
headerBuffer.putShort(paylen); serializationBuffer.putShort(paylen);
out.write(headerBuffer.array()); serializationBuffer.put(payload);
}
out.write(payload); out.write(serializationBuffer.array(), 0, serializationBuffer.position());
}
} }
} }