mirror of
https://github.com/apple/swift-nio
synced 2026-11-25 04:47:49 +00:00
### Motivation:
Eight doc comments name an argument that the declaration below them does
not have, so DocC has nothing to bind the description to and quietly
drops it. Nothing warns about this: Swift has no equivalent of
`-Wdocumentation`, and DocC stays silent rather than complaining, so
these sit until somebody reads the generated page and wonders where the
description went
| file | the comment says | the declaration says |
|---|---|---|
| `NIOPosix/ServerSocket.swift` | `descriptor` | `socket` |
| `NIOPosix/Socket.swift` ×2 | `descriptor` | `socket` |
| `NIOHTTP1/ByteCollectionUtils.swift` | `bytes` | `to` |
| `NIOHTTP1/HTTPTypes.swift` | `buffer` | `headers` |
| `NIOFS/BufferedStream.swift` ×2 | `sequence` | `element` |
| `NIOFS/SystemFileHandle.swift` | `executor` | `threadPool` |
The socket ones are worth a word. Each of those files has a pair: a
deprecated `init(descriptor:)` and its replacement `init(socket:)`, and
the rename is recorded right there in `@available(*, deprecated,
renamed: "init(socket:setNonBlocking:)")`. The replacement inherited the
wording of the one it replaced, so the doc still says `descriptor`. The
deprecated ones are correct and I left them alone
And the last two are the same defect in two copies of one module,
`NIOFS` and `_NIOFileSystem`, which is what a verbatim copy of a file
does to a mistake inside it. Fixing one and not the other would leave
them out of sync, so both are in here
### Modifications:
Renamed the documented argument to the one in the declaration, eleven
lines across eight files
One is slightly more than a rename: in `ByteBuffer.write(headers:)` the
entry described the buffer the method is called on rather than its
argument, so it now describes `headers` and keeps the note about the
writer index
### Result:
The parameter descriptions reach the generated documentation. No API
change, no behaviour change, comments only
btw these came out of a checker I wrote for exactly this, run over the
tree and then read by hand one by one. Six of the things it flagged
turned out to be my parser rather than your code, and those are fixed on
my side rather than sent to you 🙌🏼
all of it on a MacBook Air M3, of course
156 lines
5.4 KiB
Swift
156 lines
5.4 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#if !os(WASI)
|
|
|
|
import NIOCore
|
|
|
|
/// A server socket that can accept new connections.
|
|
class ServerSocket: BaseSocket, ServerSocketProtocol {
|
|
typealias SocketType = ServerSocket
|
|
private let cleanupOnClose: Bool
|
|
|
|
public final class func bootstrap(
|
|
protocolFamily: NIOBSDSocket.ProtocolFamily,
|
|
host: String,
|
|
port: Int
|
|
) throws -> ServerSocket {
|
|
let socket = try ServerSocket(protocolFamily: protocolFamily)
|
|
try socket.bind(to: SocketAddress.makeAddressResolvingHost(host, port: port))
|
|
try socket.listen()
|
|
return socket
|
|
}
|
|
|
|
/// Create a new instance.
|
|
///
|
|
/// - Parameters:
|
|
/// - protocolFamily: The protocol family to use (usually `AF_INET6` or `AF_INET`).
|
|
/// - protocolSubtype: The subtype of the protocol, corresponding to the `protocol`
|
|
/// argument to the socket syscall. Defaults to 0.
|
|
/// - setNonBlocking: Set non-blocking mode on the socket.
|
|
/// - Throws: An `IOError` if creation of the socket failed.
|
|
init(
|
|
protocolFamily: NIOBSDSocket.ProtocolFamily,
|
|
protocolSubtype: NIOBSDSocket.ProtocolSubtype = .default,
|
|
setNonBlocking: Bool = false
|
|
) throws {
|
|
let sock = try BaseSocket.makeSocket(
|
|
protocolFamily: protocolFamily,
|
|
type: .stream,
|
|
protocolSubtype: protocolSubtype,
|
|
setNonBlocking: setNonBlocking
|
|
)
|
|
switch protocolFamily {
|
|
case .unix:
|
|
cleanupOnClose = true
|
|
default:
|
|
cleanupOnClose = false
|
|
}
|
|
try super.init(socket: sock)
|
|
}
|
|
|
|
/// Create a new instance.
|
|
///
|
|
/// - Parameters:
|
|
/// - descriptor: The _Unix file descriptor_ representing the bound socket.
|
|
/// - setNonBlocking: Set non-blocking mode on the socket.
|
|
/// - Throws: An `IOError` if socket is invalid.
|
|
#if !os(Windows)
|
|
@available(*, deprecated, renamed: "init(socket:setNonBlocking:)")
|
|
convenience init(descriptor: CInt, setNonBlocking: Bool = false) throws {
|
|
try self.init(socket: descriptor, setNonBlocking: setNonBlocking)
|
|
}
|
|
#endif
|
|
|
|
/// Create a new instance.
|
|
///
|
|
/// - Parameters:
|
|
/// - socket: The _Unix file descriptor_ representing the bound socket.
|
|
/// - setNonBlocking: Set non-blocking mode on the socket.
|
|
/// - Throws: An `IOError` if socket is invalid.
|
|
init(socket: NIOBSDSocket.Handle, setNonBlocking: Bool = false) throws {
|
|
cleanupOnClose = false // socket already bound, owner must clean up
|
|
try super.init(socket: socket)
|
|
if setNonBlocking {
|
|
try self.setNonBlocking()
|
|
}
|
|
}
|
|
|
|
/// Start to listen for new connections.
|
|
///
|
|
/// - Parameters:
|
|
/// - backlog: The backlog to use.
|
|
/// - Throws: An `IOError` if creation of the socket failed.
|
|
func listen(backlog: Int32 = 128) throws {
|
|
try withUnsafeHandle {
|
|
_ = try NIOBSDSocket.listen(socket: $0, backlog: backlog)
|
|
}
|
|
}
|
|
|
|
/// Accept a new connection
|
|
///
|
|
/// - Parameters:
|
|
/// - setNonBlocking: set non-blocking mode on the returned `Socket`. On Linux this will use accept4 with SOCK_NONBLOCK to save a system call.
|
|
/// - Returns: A `Socket` once a new connection was established or `nil` if this `ServerSocket` is in non-blocking mode and there is no new connection that can be accepted when this method is called.
|
|
/// - Throws: An `IOError` if the operation failed.
|
|
func accept(setNonBlocking: Bool = false) throws -> Socket? {
|
|
try withUnsafeHandle { fd in
|
|
#if os(Linux)
|
|
let flags: Int32
|
|
if setNonBlocking {
|
|
flags = Linux.SOCK_NONBLOCK
|
|
} else {
|
|
flags = 0
|
|
}
|
|
let result = try Linux.accept4(descriptor: fd, addr: nil, len: nil, flags: flags)
|
|
#else
|
|
let result = try NIOBSDSocket.accept(socket: fd, address: nil, address_len: nil)
|
|
#endif
|
|
|
|
guard let fd = result else {
|
|
return nil
|
|
}
|
|
|
|
let sock = try Socket(socket: fd)
|
|
|
|
#if !os(Linux)
|
|
if setNonBlocking {
|
|
do {
|
|
try sock.setNonBlocking()
|
|
} catch {
|
|
// best effort
|
|
try? sock.close()
|
|
throw error
|
|
}
|
|
}
|
|
#endif
|
|
return sock
|
|
}
|
|
}
|
|
|
|
/// Close the socket.
|
|
///
|
|
/// After the socket was closed all other methods will throw an `IOError` when called.
|
|
///
|
|
/// - Throws: An `IOError` if the operation failed.
|
|
override func close() throws {
|
|
let maybePathname = self.cleanupOnClose ? (try? self.localAddress().pathname) : nil
|
|
try super.close()
|
|
if let socketPath = maybePathname {
|
|
try BaseSocket.cleanupSocket(unixDomainSocketPath: socketPath)
|
|
}
|
|
}
|
|
}
|
|
#endif // !os(WASI)
|