mirror of
https://gitlab.futo.org/videostreaming/grayjay.git
synced 2025-07-18 23:12:07 +00:00
Synchronized writes.
This commit is contained in:
parent
176814a715
commit
698725d611
1 changed files with 30 additions and 27 deletions
|
@ -90,6 +90,7 @@ class FCastCastingDevice : CastingDevice {
|
||||||
private var _thread: Thread? = null
|
private var _thread: Thread? = null
|
||||||
private var _pingThread: Thread? = null
|
private var _pingThread: Thread? = null
|
||||||
private var _lastPongTime = -1L
|
private var _lastPongTime = -1L
|
||||||
|
private var _outputStreamLock = Object()
|
||||||
|
|
||||||
constructor(name: String, addresses: Array<InetAddress>, port: Int) : super() {
|
constructor(name: String, addresses: Array<InetAddress>, port: Int) : super() {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
@ -476,34 +477,36 @@ class FCastCastingDevice : CastingDevice {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun send(opcode: Opcode, message: String? = null) {
|
private fun send(opcode: Opcode, message: String? = null) {
|
||||||
try {
|
synchronized (_outputStreamLock) {
|
||||||
val data: ByteArray = message?.encodeToByteArray() ?: ByteArray(0)
|
try {
|
||||||
val size = 1 + data.size
|
val data: ByteArray = message?.encodeToByteArray() ?: ByteArray(0)
|
||||||
val outputStream = _outputStream
|
val size = 1 + data.size
|
||||||
if (outputStream == null) {
|
val outputStream = _outputStream
|
||||||
Log.w(TAG, "Failed to send $size bytes, output stream is null.")
|
if (outputStream == null) {
|
||||||
return
|
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.", 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.", e)
|
|
||||||
throw e
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue