Last active
January 3, 2026 00:08
-
-
Save lhoward/1ecb6c7dea604707f3d0d2519a93fad0 to your computer and use it in GitHub Desktop.
IORingSwift + OSCKit
This file contains hidden or 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
| import FoundationEssentials | |
| #else | |
| import Foundation | |
| #endif | |
| import OSCKitCore | |
| import SocketAddress | |
| import IORing | |
| import IORingUtils | |
| actor IORingOSCServer { | |
| let address: any SocketAddress | |
| var task: Task<(), Error>? | |
| init(address: any SocketAddress) { | |
| self.address = address | |
| } | |
| deinit { | |
| task?.cancel() | |
| } | |
| private func _makeSocket() async throws -> Socket { | |
| let socket: Socket | |
| socket = try Socket(ring: IORing.shared, domain: address.family, type: SOCK_DGRAM, protocol: 0) | |
| try socket.bind(to: address) | |
| } | |
| private func _run() async throws { | |
| let socket = try await _makeSocket() | |
| repeat { | |
| do { | |
| for try await pdu in try await socket.receiveMessages(count: 1500) { | |
| try? await _handle(message: Data(pdu.buffer), from: AnySocketAddress(bytes: pdu.name)) | |
| } | |
| } catch Errno.canceled {} | |
| } while !Task.isCancelled | |
| } | |
| func run() { | |
| task?.cancel() | |
| task = Task { | |
| try await _run() | |
| } | |
| } | |
| func stop() { | |
| task?.cancel() | |
| task = nil | |
| } | |
| private func _handle(message: OSCMessage, from address: any SocketAddress) async throws { | |
| // TODO: implement logic here | |
| } | |
| private func _handle(message: any OSCObject, from address: any SocketAddress) async throws { | |
| switch message { | |
| case let bundle as OSCBundle: | |
| for element in bundle.elements { | |
| try await _handle(message: element, from: address) | |
| } | |
| case let message as OSCMessage: | |
| try await _handle(message: message, from: address) | |
| default: | |
| break | |
| } | |
| } | |
| private func _handle(message: Data, from address: any SocketAddress) async throws { | |
| guard let message = try message.parseOSC() else { | |
| return | |
| } | |
| try await _handle(message: message, from: address) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment