Skip to content

Commit

Permalink
constrain Socket to IORingActor
Browse files Browse the repository at this point in the history
  • Loading branch information
lhoward committed Apr 11, 2024
1 parent 6e43268 commit 1f8ca8d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
14 changes: 7 additions & 7 deletions Examples/IORingTCPEcho/IORingTCPEcho.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 8 additions & 3 deletions Examples/IORingUDPClient/IORingUDPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 9 additions & 4 deletions Examples/IORingUDPServer/IORingUDPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion Sources/IORingUtils/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -168,7 +169,7 @@ public struct Socket: CustomStringConvertible, Equatable, Hashable, Sendable {

public func accept() async throws -> AnyAsyncSequence<Socket> {
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
) }
Expand Down

0 comments on commit 1f8ca8d

Please sign in to comment.