diff --git a/Examples/IORingTCPEcho/IORingTCPEcho.swift b/Examples/IORingTCPEcho/IORingTCPEcho.swift index 7a75887..a5757b6 100644 --- a/Examples/IORingTCPEcho/IORingTCPEcho.swift +++ b/Examples/IORingTCPEcho/IORingTCPEcho.swift @@ -34,23 +34,23 @@ public struct IORingTCPEcho { exit(1) } - let echo = try IORingTCPEcho(port: port) + let echo = try await IORingTCPEcho(port: port) try await echo.runMultishot() } - init(port: UInt16, bufferSize: Int = 32, backlog: Int = 5) throws { + init(port: UInt16, bufferSize: Int = 32, backlog: Int = 5) async throws { self.bufferSize = bufferSize ring = try IORing() - socket = try Socket( + socket = try await Socket( ring: ring, domain: sa_family_t(AF_INET), type: SOCK_STREAM, protocol: 0 ) - try socket.setReuseAddr() - try socket.setTcpNoDelay() - try socket.bind(port: port) - try socket.listen(backlog: backlog) + try await socket.setReuseAddr() + try await socket.setTcpNoDelay() + try await socket.bind(port: port) + try await socket.listen(backlog: backlog) } func readWriteEcho(client: Socket) async { diff --git a/Examples/IORingUDPClient/IORingUDPClient.swift b/Examples/IORingUDPClient/IORingUDPClient.swift index c48f0ad..da44ab9 100644 --- a/Examples/IORingUDPClient/IORingUDPClient.swift +++ b/Examples/IORingUDPClient/IORingUDPClient.swift @@ -37,14 +37,19 @@ public struct IORingUDPClient { } let message = CommandLine.arguments[2] - let client = try IORingUDPClient() + let client = try await IORingUDPClient() try await client.connect(to: address) try await client.send(message: message) } - init() throws { + init() async throws { ring = IORing.shared - socket = try Socket(ring: ring, domain: sa_family_t(AF_INET), type: SOCK_DGRAM, protocol: 0) + socket = try await Socket( + ring: ring, + domain: sa_family_t(AF_INET), + type: SOCK_DGRAM, + protocol: 0 + ) } func connect(to address: any SocketAddress) async throws { diff --git a/Examples/IORingUDPServer/IORingUDPServer.swift b/Examples/IORingUDPServer/IORingUDPServer.swift index 613c379..f9e0723 100644 --- a/Examples/IORingUDPServer/IORingUDPServer.swift +++ b/Examples/IORingUDPServer/IORingUDPServer.swift @@ -33,18 +33,23 @@ public struct IORingUDPServer { exit(1) } - let server = try IORingUDPServer() + let server = try await IORingUDPServer() try await server.bind(port: port) try await server.run() } - init() throws { + init() async throws { ring = IORing.shared - socket = try Socket(ring: ring, domain: sa_family_t(AF_INET), type: SOCK_DGRAM, protocol: 0) + socket = try await Socket( + ring: ring, + domain: sa_family_t(AF_INET), + type: SOCK_DGRAM, + protocol: 0 + ) } func bind(port: UInt16) async throws { - try socket.bind(port: port) + try await socket.bind(port: port) } func run() async throws { diff --git a/Sources/IORingUtils/Socket.swift b/Sources/IORingUtils/Socket.swift index 48c5ec1..08a44d6 100644 --- a/Sources/IORingUtils/Socket.swift +++ b/Sources/IORingUtils/Socket.swift @@ -22,6 +22,7 @@ import Glibc import IORing import SystemPackage +@IORingActor public struct Socket: CustomStringConvertible, Equatable, Hashable, Sendable { private let fileHandle: FileHandle! private let domain: sa_family_t @@ -168,7 +169,7 @@ public struct Socket: CustomStringConvertible, Equatable, Hashable, Sendable { public func accept() async throws -> AnyAsyncSequence { guard let fileHandle else { throw Errno.badFileDescriptor } - return try await ring.accept(from: fileHandle).map { Socket( + return try await ring.accept(from: fileHandle).map { await Socket( ring: ring, fileHandle: $0 as! FileHandle ) }