Skip to content

Commit

Permalink
add 100ms timeout to close command avoiding block
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Sep 17, 2024
1 parent 9a22578 commit dd4ded4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
5 changes: 3 additions & 2 deletions rtmp/src/main/java/com/pedro/rtmp/rtmp/RtmpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeoutOrNull
import java.io.*
import java.net.*
import java.nio.ByteBuffer
Expand Down Expand Up @@ -515,8 +516,8 @@ class RtmpClient(private val connectChecker: ConnectChecker) {
private suspend fun disconnect(clear: Boolean) {
if (isStreaming) rtmpSender.stop(clear)
runCatching {
socket?.let { socket ->
commandsManager.sendClose(socket)
withTimeoutOrNull(100) {
socket?.let { commandsManager.sendClose(it) }
}
}
closeConnection()
Expand Down
6 changes: 4 additions & 2 deletions rtsp/src/main/java/com/pedro/rtsp/rtsp/RtspClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,10 @@ class RtspClient(private val connectChecker: ConnectChecker) {
private suspend fun disconnect(clear: Boolean) {
if (isStreaming) rtspSender.stop()
val error = runCatching {
writer?.write(commandsManager.createTeardown())
writer?.flush()
withTimeoutOrNull(100) {
writer?.write(commandsManager.createTeardown())
writer?.flush()
}
connectionSocket?.close()
reader?.close()
reader = null
Expand Down
5 changes: 4 additions & 1 deletion srt/src/main/java/com/pedro/srt/srt/SrtClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeoutOrNull
import java.io.IOException
import java.net.SocketTimeoutException
import java.net.URISyntaxException
Expand Down Expand Up @@ -257,7 +258,9 @@ class SrtClient(private val connectChecker: ConnectChecker) {
private suspend fun disconnect(clear: Boolean) {
if (isStreaming) srtSender.stop(clear)
runCatching {
commandsManager.writeShutdown(socket)
withTimeoutOrNull(100) {
commandsManager.writeShutdown(socket)
}
}
socket?.close()
if (clear) {
Expand Down

0 comments on commit dd4ded4

Please sign in to comment.