Skip to content

Commit

Permalink
introduce SocketType enum for public API
Browse files Browse the repository at this point in the history
  • Loading branch information
lhoward committed Nov 12, 2024
1 parent a20cc6e commit a5a9cad
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 24 deletions.
2 changes: 1 addition & 1 deletion FlyingFox/Tests/AsyncSocketTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ extension AsyncSocket {
}

static func make(pool: some AsyncSocketPool) throws -> AsyncSocket {
let socket = try Socket(domain: AF_UNIX, type: Socket.stream)
let socket = try Socket(domain: AF_UNIX, type: .stream)
return try AsyncSocket(socket: socket, pool: pool)
}

Expand Down
2 changes: 1 addition & 1 deletion FlyingSocks/Sources/AsyncSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public struct AsyncSocket: Sendable {
pool: some AsyncSocketPool,
timeout: TimeInterval = 5) async throws -> Self {
try await withThrowingTimeout(seconds: timeout) {
let socket = try Socket(domain: Int32(type(of: address).family), type: Socket.stream)
let socket = try Socket(domain: Int32(type(of: address).family), type: .stream)
let asyncSocket = try AsyncSocket(socket: socket, pool: pool)
try await asyncSocket.connect(to: address)
return asyncSocket
Expand Down
22 changes: 19 additions & 3 deletions FlyingSocks/Sources/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ import WinSDK.WinSock2
#endif
import Foundation

public enum SocketType: Sendable {
case stream
case datagram
}

extension SocketType {
var rawValue: Int32 {
switch self {
case .stream:
Socket.stream
case .datagram:
Socket.datagram

Check warning on line 50 in FlyingSocks/Sources/Socket.swift

View check run for this annotation

Codecov / codecov/patch

FlyingSocks/Sources/Socket.swift#L50

Added line #L50 was not covered by tests
}
}
}

public struct Socket: Sendable, Hashable {

public let file: FileDescriptor
Expand All @@ -53,11 +69,11 @@ public struct Socket: Sendable, Hashable {
}

public init(domain: Int32) throws {
try self.init(domain: domain, type: Socket.stream)
try self.init(domain: domain, type: .stream)
}

public init(domain: Int32, type: Int32) throws {
let descriptor = FileDescriptor(rawValue: Socket.socket(domain, type, 0))
public init(domain: Int32, type: SocketType) throws {
let descriptor = FileDescriptor(rawValue: Socket.socket(domain, type.rawValue, 0))
guard descriptor != .invalid else {
throw SocketError.makeFailed("CreateSocket")
}
Expand Down
4 changes: 2 additions & 2 deletions FlyingSocks/Tests/AsyncSocketTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,15 @@ extension AsyncSocket {
static func makeListening(pool: some AsyncSocketPool) throws -> AsyncSocket {
let address = sockaddr_un.unix(path: #function)
try? Socket.unlink(address)
let socket = try Socket(domain: AF_UNIX, type: Socket.stream)
let socket = try Socket(domain: AF_UNIX, type: .stream)
try socket.setValue(true, for: .localAddressReuse)
try socket.bind(to: address)
try socket.listen()
return try AsyncSocket(socket: socket, pool: pool)
}

static func make(pool: some AsyncSocketPool) throws -> AsyncSocket {
let socket = try Socket(domain: AF_UNIX, type: Socket.stream)
let socket = try Socket(domain: AF_UNIX, type: .stream)
return try AsyncSocket(socket: socket, pool: pool)
}

Expand Down
8 changes: 4 additions & 4 deletions FlyingSocks/Tests/SocketPoolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ struct SocketPoolTests {
try await pool.prepare()

let task = Task {
let socket = try Socket(domain: AF_UNIX, type: Socket.stream)
let socket = try Socket(domain: AF_UNIX, type: .stream)
try await pool.suspendSocket(socket, untilReadyFor: .read)
}

Expand Down Expand Up @@ -125,7 +125,7 @@ struct SocketPoolTests {

try? await task.value

let socket = try Socket(domain: AF_UNIX, type: Socket.stream)
let socket = try Socket(domain: AF_UNIX, type: .stream)
await #expect(throws: (any Error).self) {
try await pool.suspendSocket(socket, untilReadyFor: .read)
}
Expand All @@ -138,7 +138,7 @@ struct SocketPoolTests {
let task = Task { try await pool.run() }
defer { task.cancel() }

let socket = try Socket(domain: AF_UNIX, type: Socket.stream)
let socket = try Socket(domain: AF_UNIX, type: .stream)
let suspension = Task {
try await pool.suspendSocket(socket, untilReadyFor: .read)
}
Expand All @@ -160,7 +160,7 @@ struct SocketPoolTests {
let task = Task { try await pool.run() }
defer { task.cancel() }

let socket = try Socket(domain: AF_UNIX, type: Socket.stream)
let socket = try Socket(domain: AF_UNIX, type: .stream)
let suspension = Task {
try await pool.suspendSocket(socket, untilReadyFor: .read)
}
Expand Down
19 changes: 6 additions & 13 deletions FlyingSocks/Tests/SocketTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ struct SocketTests {

@Test
func socketWrite_Throws_WhenSocketIsNotConnected() async throws {
let s1 = try Socket(domain: AF_UNIX, type: Socket.stream)
let s1 = try Socket(domain: AF_UNIX, type: .stream)
let data = Data(repeating: 0x01, count: 100)
#expect(throws: SocketError.self) {
try s1.write(data, from: data.startIndex)
Expand All @@ -108,7 +108,7 @@ struct SocketTests {

@Test
func socket_Sets_And_Gets_ReceiveBufferSize() throws {
let socket = try Socket(domain: AF_UNIX, type: Socket.stream)
let socket = try Socket(domain: AF_UNIX, type: .stream)

try socket.setValue(2048, for: .receiveBufferSize)
#if canImport(Darwin)
Expand All @@ -121,7 +121,7 @@ struct SocketTests {

@Test
func socket_Sets_And_Gets_SendBufferSizeOption() throws {
let socket = try Socket(domain: AF_UNIX, type: Socket.stream)
let socket = try Socket(domain: AF_UNIX, type: .stream)

try socket.setValue(2048, for: .sendBufferSize)
#if canImport(Darwin)
Expand All @@ -134,7 +134,7 @@ struct SocketTests {

@Test
func socket_Sets_And_Gets_BoolOption() throws {
let socket = try Socket(domain: AF_UNIX, type: Socket.stream)
let socket = try Socket(domain: AF_UNIX, type: .stream)

try socket.setValue(true, for: .localAddressReuse)
#expect(try socket.getValue(for: .localAddressReuse))
Expand All @@ -145,20 +145,13 @@ struct SocketTests {

@Test
func socket_Sets_And_Gets_Flags() throws {
let socket = try Socket(domain: AF_UNIX, type: Socket.stream)
let socket = try Socket(domain: AF_UNIX, type: .stream)
#expect(try socket.flags.contains(.append) == false)

try socket.setFlags(.append)
#expect(try socket.flags.contains(.append))
}

@Test
func socketInit_ThrowsError_WhenInvalid() {
#expect(throws: SocketError.self) {
_ = try Socket(domain: -1, type: -1)
}
}

@Test
func socketAccept_ThrowsError_WhenInvalid() {
let socket = Socket(file: -1)
Expand Down Expand Up @@ -193,7 +186,7 @@ struct SocketTests {

@Test
func socketBind_ToINET() throws {
let socket = try Socket(domain: AF_INET, type: Socket.stream)
let socket = try Socket(domain: AF_INET, type: .stream)
try socket.setValue(true, for: .localAddressReuse)
let address = Socket.makeAddressINET(port:5050)
#expect(throws: Never.self) {
Expand Down

0 comments on commit a5a9cad

Please sign in to comment.