mirror of
https://github.com/apple/swift-nio
synced 2026-11-25 04:47:49 +00:00
Motivation: When writing programs that use networks, particularly if you want to perform multicast joins, it's extremely important to be able to query network interfaces and get results. Modifications: Add support for querying interfaces using getifaddrs and wrap those query results in a Swift class. Result: Users will be able to enumerate the interfaces on their machine.
83 lines
2.9 KiB
Swift
83 lines
2.9 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#if os(Linux)
|
|
import func CNIOLinux.get_nprocs
|
|
|
|
private func linuxCoreCount() -> Int {
|
|
return Int(get_nprocs())
|
|
}
|
|
#elseif os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
|
private func darwinCoreCount() -> Int {
|
|
var cores: CInt = 0
|
|
var coresLen: size_t = MemoryLayout.size(ofValue: cores)
|
|
let rc = sysctlbyname("hw.logicalcpu", &cores, &coresLen, nil, 0)
|
|
precondition(rc == 0)
|
|
return Int(cores)
|
|
}
|
|
#endif
|
|
|
|
/// Allows to "box" another value.
|
|
final class Box<T> {
|
|
let value: T
|
|
init(_ value: T) { self.value = value }
|
|
}
|
|
|
|
public enum System {
|
|
/// A utility function that returns an estimate of the number of *logical* cores
|
|
/// on the system.
|
|
///
|
|
/// This value can be used to help provide an estimate of how many threads to use with
|
|
/// the `MultiThreadedEventLoopGroup`. The exact ratio between this number and the number
|
|
/// of threads to use is a matter for the programmer, and can be determined based on the
|
|
/// specific execution behaviour of the program.
|
|
///
|
|
/// - returns: The logical core count on the system.
|
|
public static var coreCount: Int {
|
|
#if os(Linux)
|
|
return linuxCoreCount()
|
|
#elseif os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
|
return darwinCoreCount()
|
|
#endif
|
|
}
|
|
|
|
/// A utility function that enumerates the available network interfaces on this machine.
|
|
///
|
|
/// This function returns values that are true for a brief snapshot in time. These results can
|
|
/// change, and the returned values will not change to reflect them. This function must be called
|
|
/// again to get new results.
|
|
///
|
|
/// - returns: An array of network interfaces available on this machine.
|
|
/// - throws: If an error is encountered while enumerating interfaces.
|
|
public static func enumerateInterfaces() throws -> [NIONetworkInterface] {
|
|
var interface: UnsafeMutablePointer<ifaddrs>? = nil
|
|
try Posix.getifaddrs(&interface)
|
|
let originalInterface = interface
|
|
defer {
|
|
freeifaddrs(originalInterface)
|
|
}
|
|
|
|
var results: [NIONetworkInterface] = []
|
|
results.reserveCapacity(12) // Arbitrary choice.
|
|
while let concreteInterface = interface {
|
|
if let nioInterface = NIONetworkInterface(concreteInterface.pointee) {
|
|
results.append(nioInterface)
|
|
}
|
|
interface = concreteInterface.pointee.ifa_next
|
|
}
|
|
|
|
return results
|
|
}
|
|
}
|