Skip to content

Commit a925d71

Browse files
committed
introduce SocketType enum for public API
1 parent a20cc6e commit a925d71

File tree

6 files changed

+36
-22
lines changed

6 files changed

+36
-22
lines changed

FlyingFox/Tests/AsyncSocketTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extension AsyncSocket {
4040
}
4141

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

FlyingSocks/Sources/AsyncSocket.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public struct AsyncSocket: Sendable {
8383
pool: some AsyncSocketPool,
8484
timeout: TimeInterval = 5) async throws -> Self {
8585
try await withThrowingTimeout(seconds: timeout) {
86-
let socket = try Socket(domain: Int32(type(of: address).family), type: Socket.stream)
86+
let socket = try Socket(domain: Int32(type(of: address).family), type: .stream)
8787
let asyncSocket = try AsyncSocket(socket: socket, pool: pool)
8888
try await asyncSocket.connect(to: address)
8989
return asyncSocket

FlyingSocks/Sources/Socket.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ import WinSDK.WinSock2
3636
#endif
3737
import Foundation
3838

39+
public enum SocketType: Sendable {
40+
case stream
41+
case datagram
42+
}
43+
44+
extension SocketType {
45+
var rawValue: Int32 {
46+
switch self {
47+
case .stream:
48+
Socket.stream
49+
case .datagram:
50+
Socket.datagram
51+
}
52+
}
53+
}
54+
3955
public struct Socket: Sendable, Hashable {
4056

4157
public let file: FileDescriptor
@@ -53,9 +69,10 @@ public struct Socket: Sendable, Hashable {
5369
}
5470

5571
public init(domain: Int32) throws {
56-
try self.init(domain: domain, type: Socket.stream)
72+
try self.init(domain: domain, type: .stream)
5773
}
5874

75+
@available(*, deprecated, message: "type is now SocketType")
5976
public init(domain: Int32, type: Int32) throws {
6077
let descriptor = FileDescriptor(rawValue: Socket.socket(domain, type, 0))
6178
guard descriptor != .invalid else {
@@ -64,6 +81,10 @@ public struct Socket: Sendable, Hashable {
6481
self.file = descriptor
6582
}
6683

84+
public init(domain: Int32, type: SocketType) throws {
85+
try self.init(domain: domain, type: type.rawValue)
86+
}
87+
6788
public var flags: Flags {
6889
get throws {
6990
let flags = Socket.fcntl(file.rawValue, F_GETFL)

FlyingSocks/Tests/AsyncSocketTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,15 @@ extension AsyncSocket {
198198
static func makeListening(pool: some AsyncSocketPool) throws -> AsyncSocket {
199199
let address = sockaddr_un.unix(path: #function)
200200
try? Socket.unlink(address)
201-
let socket = try Socket(domain: AF_UNIX, type: Socket.stream)
201+
let socket = try Socket(domain: AF_UNIX, type: .stream)
202202
try socket.setValue(true, for: .localAddressReuse)
203203
try socket.bind(to: address)
204204
try socket.listen()
205205
return try AsyncSocket(socket: socket, pool: pool)
206206
}
207207

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

FlyingSocks/Tests/SocketPoolTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ struct SocketPoolTests {
8585
try await pool.prepare()
8686

8787
let task = Task {
88-
let socket = try Socket(domain: AF_UNIX, type: Socket.stream)
88+
let socket = try Socket(domain: AF_UNIX, type: .stream)
8989
try await pool.suspendSocket(socket, untilReadyFor: .read)
9090
}
9191

@@ -125,7 +125,7 @@ struct SocketPoolTests {
125125

126126
try? await task.value
127127

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

141-
let socket = try Socket(domain: AF_UNIX, type: Socket.stream)
141+
let socket = try Socket(domain: AF_UNIX, type: .stream)
142142
let suspension = Task {
143143
try await pool.suspendSocket(socket, untilReadyFor: .read)
144144
}
@@ -160,7 +160,7 @@ struct SocketPoolTests {
160160
let task = Task { try await pool.run() }
161161
defer { task.cancel() }
162162

163-
let socket = try Socket(domain: AF_UNIX, type: Socket.stream)
163+
let socket = try Socket(domain: AF_UNIX, type: .stream)
164164
let suspension = Task {
165165
try await pool.suspendSocket(socket, untilReadyFor: .read)
166166
}

FlyingSocks/Tests/SocketTests.swift

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ struct SocketTests {
9898

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

109109
@Test
110110
func socket_Sets_And_Gets_ReceiveBufferSize() throws {
111-
let socket = try Socket(domain: AF_UNIX, type: Socket.stream)
111+
let socket = try Socket(domain: AF_UNIX, type: .stream)
112112

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

122122
@Test
123123
func socket_Sets_And_Gets_SendBufferSizeOption() throws {
124-
let socket = try Socket(domain: AF_UNIX, type: Socket.stream)
124+
let socket = try Socket(domain: AF_UNIX, type: .stream)
125125

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

135135
@Test
136136
func socket_Sets_And_Gets_BoolOption() throws {
137-
let socket = try Socket(domain: AF_UNIX, type: Socket.stream)
137+
let socket = try Socket(domain: AF_UNIX, type: .stream)
138138

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

146146
@Test
147147
func socket_Sets_And_Gets_Flags() throws {
148-
let socket = try Socket(domain: AF_UNIX, type: Socket.stream)
148+
let socket = try Socket(domain: AF_UNIX, type: .stream)
149149
#expect(try socket.flags.contains(.append) == false)
150150

151151
try socket.setFlags(.append)
152152
#expect(try socket.flags.contains(.append))
153153
}
154154

155-
@Test
156-
func socketInit_ThrowsError_WhenInvalid() {
157-
#expect(throws: SocketError.self) {
158-
_ = try Socket(domain: -1, type: -1)
159-
}
160-
}
161-
162155
@Test
163156
func socketAccept_ThrowsError_WhenInvalid() {
164157
let socket = Socket(file: -1)
@@ -193,7 +186,7 @@ struct SocketTests {
193186

194187
@Test
195188
func socketBind_ToINET() throws {
196-
let socket = try Socket(domain: AF_INET, type: Socket.stream)
189+
let socket = try Socket(domain: AF_INET, type: .stream)
197190
try socket.setValue(true, for: .localAddressReuse)
198191
let address = Socket.makeAddressINET(port:5050)
199192
#expect(throws: Never.self) {

0 commit comments

Comments
 (0)