Skip to content

Commit 061c8cc

Browse files
committed
consolidate socketpair
1 parent 3c81443 commit 061c8cc

File tree

10 files changed

+49
-109
lines changed

10 files changed

+49
-109
lines changed

FlyingFox/Tests/AsyncSocketTests.swift

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,6 @@ extension AsyncSocket {
4848
try await makePair(pool: .client)
4949
}
5050

51-
static func makePair(pool: some AsyncSocketPool) throws -> (AsyncSocket, AsyncSocket) {
52-
let (file1, file2) = Socket.socketpair(AF_UNIX, Socket.stream, 0)
53-
guard file1.rawValue > -1, file2.rawValue > -1 else {
54-
throw SocketError.makeFailed("SocketPair")
55-
}
56-
57-
let s1 = try AsyncSocket(socket: Socket(file: file1), pool: pool)
58-
let s2 = try AsyncSocket(socket: Socket(file: file2), pool: pool)
59-
return (s1, s2)
60-
}
61-
6251
func writeString(_ string: String) async throws {
6352
try await write(string.data(using: .utf8)!)
6453
}

FlyingFox/XCTests/AsyncSocketTests.swift

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,6 @@ extension AsyncSocket {
4848
try await makePair(pool: .client)
4949
}
5050

51-
static func makePair(pool: some AsyncSocketPool) throws -> (AsyncSocket, AsyncSocket) {
52-
let (file1, file2) = Socket.socketpair(AF_UNIX, Socket.stream, 0)
53-
guard file1.rawValue > -1, file2.rawValue > -1 else {
54-
throw SocketError.makeFailed("SocketPair")
55-
}
56-
57-
let s1 = try AsyncSocket(socket: Socket(file: file1), pool: pool)
58-
let s2 = try AsyncSocket(socket: Socket(file: file2), pool: pool)
59-
return (s1, s2)
60-
}
61-
6251
func writeString(_ string: String) async throws {
6352
try await write(string.data(using: .utf8)!)
6453
}

FlyingSocks/Sources/AsyncSocket.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,16 @@ public struct AsyncSocket: Sendable {
176176
}
177177
}
178178

179+
package extension AsyncSocket {
180+
181+
static func makePair(pool: some AsyncSocketPool) throws -> (AsyncSocket, AsyncSocket) {
182+
let (s1, s2) = try Socket.makePair()
183+
let a1 = try AsyncSocket(socket: s1, pool: pool)
184+
let a2 = try AsyncSocket(socket: s2, pool: pool)
185+
return (a1, a2)
186+
}
187+
}
188+
179189
private extension AsyncSocketPool {
180190

181191
func loopUntilReady<T>(for events: Socket.Events, on socket: Socket, body: () throws -> T) async throws -> T {

FlyingSocks/Sources/Socket+Darwin.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ extension Socket {
9494
Darwin.socket(domain, type, `protocol`)
9595
}
9696

97+
static func socketpair(_ domain: Int32, _ type: Int32, _ protocol: Int32) -> (FileDescriptorType, FileDescriptorType) {
98+
var sockets: [Int32] = [-1, -1]
99+
_ = Darwin.socketpair(domain, type, `protocol`, &sockets)
100+
return (sockets[0], sockets[1])
101+
}
102+
97103
static func fcntl(_ fd: FileDescriptorType, _ cmd: Int32) -> Int32 {
98104
Darwin.fcntl(fd, cmd)
99105
}

FlyingSocks/Sources/Socket+Glibc.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,16 @@ extension Socket {
8686
return addr
8787
}
8888

89-
static func socket(_ domain: Int32, _ type: Int32, _ protocol: Int32) -> Int32 {
89+
static func socket(_ domain: Int32, _ type: Int32, _ protocol: Int32) -> FileDescriptorType {
9090
Glibc.socket(domain, type, `protocol`)
9191
}
9292

93+
static func socketpair(_ domain: Int32, _ type: Int32, _ protocol: Int32) -> (FileDescriptorType, FileDescriptorType) {
94+
var sockets: [Int32] = [-1, -1]
95+
_ = Glibc.socketpair(domain, type, `protocol`, &sockets)
96+
return (sockets[0], sockets[1])
97+
}
98+
9399
static func fcntl(_ fd: Int32, _ cmd: Int32) -> Int32 {
94100
Glibc.fcntl(fd, cmd)
95101
}

FlyingSocks/Sources/Socket+Pair.swift

Lines changed: 0 additions & 64 deletions
This file was deleted.

FlyingSocks/Sources/Socket+WinSock2.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ extension Socket {
112112
WinSDK.socket(domain, type, `protocol`)
113113
}
114114

115+
static func socketpair(_ domain: Int32, _ type: Int32, _ protocol: Int32) -> (FileDescriptorType, FileDescriptorType) {
116+
(-1, -1) // no supported
117+
}
118+
115119
static func setsockopt(_ fd: FileDescriptorType, _ level: Int32, _ name: Int32,
116120
_ value: UnsafeRawPointer!, _ len: socklen_t) -> Int32 {
117121
WinSDK.setsockopt(fd, level, name, value.assumingMemoryBound(to: CChar.self), len)

FlyingSocks/Sources/Socket.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,25 @@ public extension SocketOption where Self == Int32SocketOption {
344344
Int32SocketOption(name: SO_RCVBUF)
345345
}
346346
}
347+
348+
package extension Socket {
349+
350+
static func makePair(flags: Flags? = nil) throws -> (Socket, Socket) {
351+
let (file1, file2) = Socket.socketpair(AF_UNIX, Socket.stream, 0)
352+
guard file1 > -1, file2 > -1 else {
353+
throw SocketError.makeFailed("SocketPair")
354+
}
355+
let s1 = Socket(file: .init(rawValue: file1))
356+
let s2 = Socket(file: .init(rawValue: file2))
357+
358+
if let flags {
359+
try s1.setFlags(flags)
360+
try s2.setFlags(flags)
361+
}
362+
return (s1, s2)
363+
}
364+
365+
static func makeNonBlockingPair() throws -> (Socket, Socket) {
366+
try Socket.makePair(flags: .nonBlocking)
367+
}
368+
}

FlyingSocks/Tests/AsyncSocketTests.swift

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,6 @@ extension AsyncSocket {
214214
try await makePair(pool: .client)
215215
}
216216

217-
static func makePair(pool: some AsyncSocketPool) throws -> (AsyncSocket, AsyncSocket) {
218-
let (file1, file2) = Socket.socketpair(AF_UNIX, Socket.stream, 0)
219-
guard file1.rawValue > -1, file2.rawValue > -1 else {
220-
throw SocketError.makeFailed("SocketPair")
221-
}
222-
223-
let s1 = try AsyncSocket(socket: Socket(file: file1), pool: pool)
224-
let s2 = try AsyncSocket(socket: Socket(file: file2), pool: pool)
225-
return (s1, s2)
226-
}
227-
228217
func writeString(_ string: String) async throws {
229218
try await write(string.data(using: .utf8)!)
230219
}

FlyingSocks/XCTests/AsyncSocketTests.swift

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,6 @@ extension AsyncSocket {
183183
try await makePair(pool: .client)
184184
}
185185

186-
static func makePair(pool: some AsyncSocketPool) throws -> (AsyncSocket, AsyncSocket) {
187-
let (file1, file2) = Socket.socketpair(AF_UNIX, Socket.stream, 0)
188-
guard file1.rawValue > -1, file2.rawValue > -1 else {
189-
throw SocketError.makeFailed("SocketPair")
190-
}
191-
192-
let s1 = try AsyncSocket(socket: Socket(file: file1), pool: pool)
193-
let s2 = try AsyncSocket(socket: Socket(file: file2), pool: pool)
194-
return (s1, s2)
195-
}
196-
197186
func writeString(_ string: String) async throws {
198187
try await write(string.data(using: .utf8)!)
199188
}

0 commit comments

Comments
 (0)