From 6f90cf7bb6111753c4f1744c588e698911719915 Mon Sep 17 00:00:00 2001 From: Koen Date: Sun, 7 Jan 2024 14:16:03 +0100 Subject: [PATCH] Added synchronization to sender. --- .../com/futo/fcast/receiver/FCastSession.kt | 57 ++++++++++--------- .../com/futo/fcast/receiver/NetworkService.kt | 2 +- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/receivers/android/app/src/main/java/com/futo/fcast/receiver/FCastSession.kt b/receivers/android/app/src/main/java/com/futo/fcast/receiver/FCastSession.kt index 40b5369..8102cd8 100644 --- a/receivers/android/app/src/main/java/com/futo/fcast/receiver/FCastSession.kt +++ b/receivers/android/app/src/main/java/com/futo/fcast/receiver/FCastSession.kt @@ -62,37 +62,40 @@ class FCastSession(outputStream: OutputStream, private val _remoteSocketAddress: private var _packetLength = 0 private var _state = SessionState.WaitingForLength private var _outputStream: DataOutputStream? = DataOutputStream(outputStream) + private var _outputStreamLock = Object() val id = UUID.randomUUID() fun send(opcode: Opcode, message: String? = null) { - try { - val data: ByteArray = message?.encodeToByteArray() ?: ByteArray(0) - val size = 1 + data.size - val outputStream = _outputStream - if (outputStream == null) { - Log.w(TAG, "Failed to send $size bytes, output stream is null.") - return + synchronized(_outputStreamLock) { + try { + val data: ByteArray = message?.encodeToByteArray() ?: ByteArray(0) + val size = 1 + data.size + val outputStream = _outputStream + if (outputStream == null) { + Log.w(TAG, "Failed to send $size bytes, output stream is null.") + return + } + + val serializedSizeLE = ByteArray(4) + serializedSizeLE[0] = (size and 0xff).toByte() + serializedSizeLE[1] = (size shr 8 and 0xff).toByte() + serializedSizeLE[2] = (size shr 16 and 0xff).toByte() + serializedSizeLE[3] = (size shr 24 and 0xff).toByte() + outputStream.write(serializedSizeLE) + + val opcodeBytes = ByteArray(1) + opcodeBytes[0] = opcode.value + outputStream.write(opcodeBytes) + + if (data.isNotEmpty()) { + outputStream.write(data) + } + + Log.d(TAG, "Sent $size bytes: (opcode: $opcode, body: $message).") + } catch (e: Throwable) { + Log.i(TAG, "Failed to send message ${id}.", e) + throw e } - - val serializedSizeLE = ByteArray(4) - serializedSizeLE[0] = (size and 0xff).toByte() - serializedSizeLE[1] = (size shr 8 and 0xff).toByte() - serializedSizeLE[2] = (size shr 16 and 0xff).toByte() - serializedSizeLE[3] = (size shr 24 and 0xff).toByte() - outputStream.write(serializedSizeLE) - - val opcodeBytes = ByteArray(1) - opcodeBytes[0] = opcode.value - outputStream.write(opcodeBytes) - - if (data.isNotEmpty()) { - outputStream.write(data) - } - - Log.d(TAG, "Sent $size bytes: (opcode: $opcode, body: $message).") - } catch (e: Throwable) { - Log.i(TAG, "Failed to send message ${id}.", e) - throw e } } diff --git a/receivers/android/app/src/main/java/com/futo/fcast/receiver/NetworkService.kt b/receivers/android/app/src/main/java/com/futo/fcast/receiver/NetworkService.kt index 5ebfb9c..0820057 100644 --- a/receivers/android/app/src/main/java/com/futo/fcast/receiver/NetworkService.kt +++ b/receivers/android/app/src/main/java/com/futo/fcast/receiver/NetworkService.kt @@ -142,7 +142,7 @@ class NetworkService : Service() { session.send(opcode, message) Log.i(TAG, "Opcode sent (opcode = $opcode) ${session.id}") } catch (e: Throwable) { - Log.w(TAG, "Failed to send playback error", e) + Log.w(TAG, "Failed to send opcode (opcode = $opcode) ${session.id}", e) } } }