-
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
e7ae515
commit 93f51c7
Showing
5 changed files
with
176 additions
and
79 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,32 @@ | ||
{ | ||
"pins" : [ | ||
{ | ||
"identity" : "swift-atomics", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-atomics.git", | ||
"state" : { | ||
"revision" : "cd142fd2f64be2100422d658e7411e39489da985", | ||
"version" : "1.2.0" | ||
} | ||
}, | ||
{ | ||
"identity" : "swift-collections", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-collections.git", | ||
"state" : { | ||
"revision" : "a902f1823a7ff3c9ab2fba0f992396b948eda307", | ||
"version" : "1.0.5" | ||
} | ||
}, | ||
{ | ||
"identity" : "swift-nio", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-nio", | ||
"state" : { | ||
"revision" : "702cd7c56d5d44eeba73fdf83918339b26dc855c", | ||
"version" : "2.62.0" | ||
} | ||
} | ||
], | ||
"version" : 2 | ||
} |
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
This file was deleted.
Oops, something went wrong.
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,128 @@ | ||
import Foundation | ||
import NIO | ||
|
||
protocol NetworkConnectable { | ||
func connect(host: String, port: Int) async throws | ||
func sendData(_ data: Data) async throws | ||
func receiveData() async throws -> Data | ||
func invalidate() | ||
} | ||
|
||
final class RTMPClientHandler: ChannelInboundHandler { | ||
typealias InboundIn = Data | ||
private let responseCallback: (Data) -> Void | ||
|
||
init(responseCallback: @escaping (Data) -> Void) { | ||
self.responseCallback = responseCallback | ||
} | ||
|
||
func channelRead(context: ChannelHandlerContext, data: NIOAny) { | ||
let data = self.unwrapInboundIn(data) | ||
guard !data.isEmpty else { return } | ||
responseCallback(data) | ||
} | ||
} | ||
|
||
final class DataDecoder: ByteToMessageDecoder { | ||
typealias InboundIn = ByteBuffer | ||
typealias InboundOut = Data | ||
|
||
func decode(context: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState { | ||
var data = Data() | ||
buffer.readWithUnsafeReadableBytes { ptr in | ||
data.append(ptr.baseAddress!.assumingMemoryBound(to: UInt8.self), count: ptr.count) | ||
return ptr.count | ||
} | ||
context.fireChannelRead(wrapInboundOut(data)) | ||
return .continue | ||
} | ||
} | ||
|
||
final class DataEncoder: MessageToByteEncoder { | ||
typealias OutboundIn = Data | ||
typealias OutboundOut = ByteBuffer | ||
|
||
func encode(data: Data, out: inout ByteBuffer) throws { | ||
out.writeBytes(data) | ||
} | ||
} | ||
|
||
class NetworkClient: NetworkConnectable { | ||
private let group: EventLoopGroup | ||
private var channel: Channel? | ||
private var host: String? | ||
private var port: Int? | ||
|
||
private var cachedReceivedData: Data = .init() | ||
private var dataPromise: EventLoopPromise<Data>? | ||
|
||
init() { | ||
self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1) | ||
} | ||
|
||
func connect(host: String, port: Int) async throws { | ||
self.host = host | ||
self.port = port | ||
let bootstrap = ClientBootstrap(group: group) | ||
.channelInitializer { channel in | ||
channel.pipeline.addHandlers([ | ||
ByteToMessageHandler(DataDecoder()), | ||
MessageToByteHandler(DataEncoder()), | ||
RTMPClientHandler(responseCallback: self.responseReceived) | ||
]) | ||
} | ||
|
||
do { | ||
self.channel = try await bootstrap.connect(host: host, port: Int(port)).get() | ||
print("Connected to \(host):\(port)") | ||
} catch { | ||
print("Failed to connect: \(error)") | ||
throw error | ||
} | ||
} | ||
|
||
func sendData(_ data: Data) async throws { | ||
guard let channel = self.channel else { | ||
print("Connection not established") | ||
throw NSError(domain: "RTMPClientError", code: 1, userInfo: [NSLocalizedDescriptionKey: "Connection not established"]) | ||
} | ||
|
||
try await channel.writeAndFlush(data) | ||
} | ||
|
||
func receiveData() async throws -> Data { | ||
guard let channel = self.channel else { | ||
print("Connection not established") | ||
throw NSError(domain: "RTMPClientError", code: 1, userInfo: [NSLocalizedDescriptionKey: "Connection not established"]) | ||
} | ||
if !cachedReceivedData.isEmpty { | ||
let data = cachedReceivedData | ||
cachedReceivedData = Data() | ||
return data | ||
} | ||
|
||
self.dataPromise = channel.eventLoop.makePromise(of: Data.self) | ||
return try await dataPromise!.futureResult.get() | ||
} | ||
|
||
private func responseReceived(data: Data) { | ||
cachedReceivedData.append(data) | ||
if let dataPromise { | ||
dataPromise.succeed(cachedReceivedData) | ||
cachedReceivedData = Data() | ||
self.dataPromise = nil | ||
} | ||
} | ||
|
||
func shutdown() { | ||
do { | ||
try group.syncShutdownGracefully() | ||
} catch { | ||
print("Error shutting down: \(error)") | ||
} | ||
} | ||
|
||
func invalidate() { | ||
shutdown() | ||
} | ||
} |
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