From 1a2718759aad0f965983efac58c61050b0ee55ab Mon Sep 17 00:00:00 2001 From: shogo4405 Date: Fri, 15 Nov 2024 01:26:25 +0900 Subject: [PATCH] fixed #1621 --- SRTHaishinKit/Sources/SRTConnection.swift | 7 +++++- SRTHaishinKit/Sources/SRTSocket.swift | 26 +++++++++++++---------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/SRTHaishinKit/Sources/SRTConnection.swift b/SRTHaishinKit/Sources/SRTConnection.swift index 14cefd69d..3df0101fd 100644 --- a/SRTHaishinKit/Sources/SRTConnection.swift +++ b/SRTHaishinKit/Sources/SRTConnection.swift @@ -114,7 +114,12 @@ public actor SRTConnection: NetworkConnection { } func send(_ data: Data) async { - await socket?.send(data) + switch mode { + case .caller: + await socket?.send(data) + case .listener: + await clients.first?.send(data) + } } func recv() { diff --git a/SRTHaishinKit/Sources/SRTSocket.swift b/SRTHaishinKit/Sources/SRTSocket.swift index 904240199..036e35f95 100644 --- a/SRTHaishinKit/Sources/SRTSocket.swift +++ b/SRTHaishinKit/Sources/SRTSocket.swift @@ -58,17 +58,8 @@ final actor SRTSocket { case SRTS_CONNECTING: logger.trace("SRT Socket Connecting") case SRTS_CONNECTED: - connected = true - let (stream, continuation) = AsyncStream.makeStream() - outputs = continuation - Task { - for await data in stream where connected { - _ = sendmsg2(data) - totalBytesOut += data.count - queueBytesOut -= data.count - } - } logger.info("SRT Socket Connected") + didConnected() case SRTS_BROKEN: logger.warn("SRT Socket Broken") close() @@ -106,7 +97,7 @@ final actor SRTSocket { status = srt_getsockstate(socket) switch status { case SRTS_CONNECTED: - connected = true + didConnected() default: break } @@ -185,6 +176,19 @@ final actor SRTSocket { return srt_bstats(socket, &perf, 1) } + private func didConnected() { + connected = true + let (stream, continuation) = AsyncStream.makeStream() + outputs = continuation + Task { + for await data in stream where connected { + _ = sendmsg2(data) + totalBytesOut += data.count + queueBytesOut -= data.count + } + } + } + private func makeSocketError() -> SRTError { let error_message = String(cString: srt_getlasterror_str()) logger.error(error_message)