-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b7cdc5
commit 170d5e6
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
|
||
import Foundation | ||
import Network | ||
|
||
protocol RTMPConnectable { | ||
func connect(host: String, port: UInt16) async throws | ||
func sendData(_ data: Data) async throws | ||
func receiveData() async throws -> Data | ||
} | ||
|
||
actor NWConnecter: RTMPConnectable { | ||
private var connection: NWConnection? | ||
|
||
public func connect(host: String, port: UInt16) async throws { | ||
let connection = NWConnection(host: NWEndpoint.Host(host), port: NWEndpoint.Port(rawValue: port) ?? 1935, using: .tcp) | ||
self.connection = connection | ||
connection.stateUpdateHandler = { [weak self] newState in | ||
guard let self else { return } | ||
Task { | ||
switch newState { | ||
case .ready: | ||
break | ||
case .failed(let error): | ||
break | ||
default: | ||
break | ||
} | ||
} | ||
} | ||
// NWConnection.maxReadSize = Int((await windowControl.windowSize)) | ||
connection.start(queue: DispatchQueue.global(qos: .default)) | ||
} | ||
|
||
func sendData(_ data: Data) async throws { | ||
try await connection?.sendData(data) | ||
} | ||
|
||
func receiveData() async throws -> Data { | ||
guard let connection = self.connection else { return Data() } | ||
return try await connection.receiveData() | ||
} | ||
} |