Skip to content

Commit 6ebed8f

Browse files
committed
WIP: listen
1 parent 525ff2e commit 6ebed8f

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

Sources/System/Internals/Syscalls.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,10 @@ internal func system_shutdown(_ socket: CInt, _ how: CInt) -> CInt {
129129
#endif
130130
return shutdown(socket, how)
131131
}
132+
133+
internal func system_listen(_ socket: CInt, _ backlog: CInt) -> CInt {
134+
#if ENABLE_MOCKING
135+
if mockingEnabled { return _mock(socket, backlog) }
136+
#endif
137+
return listen(socket, backlog)
138+
}

Sources/System/Sockets/SocketOperations.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,23 @@ extension SocketDescriptor {
6767
nothingOrErrno(system_shutdown(self.rawValue, how.rawValue))
6868
}
6969

70+
/// Listen for connections on a socket.
71+
///
72+
/// Only applies to sockets of connection type `.stream`.
73+
///
74+
/// - Parameters:
75+
/// - backlog: the maximum length for the queue of pending connections
76+
///
77+
/// The corresponding C function is `listen`.
78+
@_alwaysEmitIntoClient
79+
public func listen(backlog: Int) throws {
80+
try _listen(backlog: backlog).get()
81+
}
82+
83+
@usableFromInline
84+
internal func _listen(backlog: Int) -> Result<(), Errno> {
85+
nothingOrErrno(system_listen(self.rawValue, CInt(backlog)))
86+
}
87+
88+
7089
}

Tests/SystemTests/SocketTest.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ final class SocketTest: XCTestCase {
3232
retryOnInterrupt in
3333
_ = try socket.shutdown(.read)
3434
},
35+
MockTestCase(name: "listen", rawSocket, 999, interruptable: false) {
36+
retryOnInterrupt in
37+
_ = try socket.listen(backlog: 999)
38+
},
3539
]
3640

3741
syscallTestCases.forEach { $0.runAllTests() }

0 commit comments

Comments
 (0)