mirror of
https://github.com/apple/swift-nio
synced 2026-11-29 05:24:21 +00:00
Motivation: We missed to check if the file descriptor is still open in ServerSocket before we act on it. We can remove a lot of complexity by just providing a withUnsafeFileDescriptor which will do this for us and use it through out the code-base. Modifications: Introduce withUnsafeFileDescriptor and use it to ensure we always only act on open Selectables. Result: More correct code.
34 lines
1.3 KiB
Swift
34 lines
1.3 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2017-2018 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// Represents a selectable resource which can be registered to a `Selector` to be notified once there are some events ready for it.
|
|
///
|
|
/// - warning: `Selectable`s are not thread-safe, only to be used on the appropriate `EventLoop`.
|
|
protocol Selectable {
|
|
|
|
/// Will be called with the file descriptor of the `Selectable` if still open, if not it will
|
|
/// throw an `IOError`.
|
|
///
|
|
/// - parameters:
|
|
/// - fn: The closure to execute if the `Selectable` is still open.
|
|
/// - throws: If either the `Selectable` was closed before or the closure throws by itself.
|
|
func withUnsafeFileDescriptor<T>(_ fn: (Int32) throws -> T) throws -> T
|
|
|
|
/// `true` if this `Selectable` is open (which means it was not closed yet).
|
|
var open: Bool { get }
|
|
|
|
/// Close this `Selectable` (and so its file descriptor).
|
|
func close() throws
|
|
}
|