/src/swift-nio/Sources/NIOPosix/FileDescriptor.swift
Line | Count | Source |
1 | | //===----------------------------------------------------------------------===// |
2 | | // |
3 | | // This source file is part of the SwiftNIO open source project |
4 | | // |
5 | | // Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors |
6 | | // Licensed under Apache License v2.0 |
7 | | // |
8 | | // See LICENSE.txt for license information |
9 | | // See CONTRIBUTORS.txt for the list of SwiftNIO project authors |
10 | | // |
11 | | // SPDX-License-Identifier: Apache-2.0 |
12 | | // |
13 | | //===----------------------------------------------------------------------===// |
14 | | |
15 | | #if !os(WASI) |
16 | | |
17 | | #if !os(Windows) |
18 | | import NIOCore |
19 | | |
20 | | extension FileDescriptor { |
21 | 0 | internal static func setNonBlocking(fileDescriptor: CInt) throws { |
22 | 0 | let flags = try Posix.fcntl(descriptor: fileDescriptor, command: F_GETFL, value: 0) |
23 | 0 | do { |
24 | 0 | let ret = try Posix.fcntl(descriptor: fileDescriptor, command: F_SETFL, value: flags | O_NONBLOCK) |
25 | 0 | assert(ret == 0, "unexpectedly, fcntl(\(fileDescriptor), F_SETFL, \(flags) | O_NONBLOCK) returned \(ret)") |
26 | 0 | } catch let error as IOError { |
27 | 0 | if error.errnoCode == EINVAL { |
28 | 0 | // Darwin seems to sometimes do this despite the docs claiming it can't happen |
29 | 0 | throw NIOFcntlFailedError() |
30 | 0 | } |
31 | 0 | throw error |
32 | 0 | } |
33 | 0 | } |
34 | | } |
35 | | #endif |
36 | | #endif // !os(WASI) |