swift-nio

所属分类:前端开发
开发工具:Swift
文件大小:1213KB
下载次数:0
上传日期:2023-06-02 10:59:02
上 传 者sh-1993
说明:  用于高性能协议服务器和客户端的事件驱动网络应用程序框架,无阻塞。
(Event-driven network application framework for high performance protocol servers & clients, non-blocking.)

文件列表:
.mailfilter (331, 2023-07-23)
.mailmap (4738, 2023-07-23)
.spi.yml (188, 2023-07-23)
CODE_OF_CONDUCT.md (198, 2023-07-23)
CONTRIBUTING.md (4416, 2023-07-23)
CONTRIBUTORS.txt (7364, 2023-07-23)
IntegrationTests (0, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework (0, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\run-allocation-counter.sh (7484, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template (0, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template\AtomicCounter (0, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template\AtomicCounter\Package.swift (920, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template\AtomicCounter\Sources (0, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template\AtomicCounter\Sources\AtomicCounter (0, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template\AtomicCounter\Sources\AtomicCounter\include (0, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template\AtomicCounter\Sources\AtomicCounter\include\atomic-counter.h (1080, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template\AtomicCounter\Sources\AtomicCounter\src (0, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template\AtomicCounter\Sources\AtomicCounter\src\atomic-counter.c (4993, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template\HookedFunctionsDoHook (0, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template\HookedFunctionsDoHook\Package.swift (999, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template\HookedFunctionsDoHook\Sources (0, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template\HookedFunctionsDoHook\Sources\HookedFunctions (0, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template\HookedFunctionsDoHook\Sources\HookedFunctions\include (0, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template\HookedFunctionsDoHook\Sources\HookedFunctions\include\hooked-functions.h (1800, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template\HookedFunctionsDoHook\Sources\HookedFunctions\src (0, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template\HookedFunctionsDoHook\Sources\HookedFunctions\src\hooked-functions-darwin.c (5727, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template\HookedFunctionsDoHook\Sources\HookedFunctions\src\hooked-functions-unix.c (9425, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template\HookedFunctionsDoNotHook (0, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template\HookedFunctionsDoNotHook\Package.swift (999, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template\HookedFunctionsDoNotHook\Sources (0, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template\HookedFunctionsDoNotHook\Sources\HookedFunctions (0, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template\HookedFunctionsDoNotHook\Sources\HookedFunctions\include (0, 2023-07-23)
IntegrationTests\allocation-counter-tests-framework\template\HookedFunctionsDoNotHook\Sources\HookedFunctions\include\hooked-functions.h (546, 2023-07-23)
... ...

[![sswg:graduated|104x20](https://img.shields.io/badge/sswg-graduated-green.svg)](https://github.com/swift-server/sswg/blob/main/process/incubation.md#graduated-level) # SwiftNIO SwiftNIO is a cross-platform asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients. It's like [Netty](https://netty.io), but written for Swift. ### Repository organization The SwiftNIO project is split across multiple repositories: Repository | NIO 2 (Swift 5.6+) --- | --- [https://github.com/apple/swift-nio][repo-nio]
SwiftNIO core | `from: "2.0.0"` [https://github.com/apple/swift-nio-ssl][repo-nio-ssl]
TLS (SSL) support | `from: "2.0.0"` [https://github.com/apple/swift-nio-http2][repo-nio-http2]
HTTP/2 support | `from: "1.0.0"` [https://github.com/apple/swift-nio-extras][repo-nio-extras]
useful additions around SwiftNIO | `from: "1.0.0"` [https://github.com/apple/swift-nio-transport-services][repo-nio-transport-services]
first-class support for macOS, iOS, tvOS, and watchOS | `from: "1.0.0"` [https://github.com/apple/swift-nio-ssh][repo-nio-ssh]
SSH support | `.upToNextMinor(from: "0.2.0")` NIO 2.29.0 and older support Swift 5.0+, NIO 2.39.0 and older support Swift 5.2+. Within this repository we have a number of products that provide different functionality. This package contains the following products: - `NIO`. This is an umbrella module exporting `NIOCore`, `NIOEmbedded` and `NIOPosix`. - `NIOCore`. This provides the core abstractions and types for using SwiftNIO (see ["Conceptual Overview"](#conceptual-overview) for more details). Most NIO extension projects that provide things like new [`EventLoop`s][el] and [`Channel`s][c] or new protocol implementations should only need to depend on `NIOCore`. - `NIOPosix`. This provides the primary [`EventLoopGroup`], [`EventLoop`][el], and [`Channel`s][c] for use on POSIX-based systems. This is our high performance core I/O layer. In general, this should only be imported by projects that plan to do some actual I/O, such as high-level protocol implementations or applications. - `NIOEmbedded`. This provides [`EmbeddedChannel`][ec] and [`EmbeddedEventLoop`][eel], implementations of the `NIOCore` abstractions that provide fine-grained control over their execution. These are most often used for testing, but can also be used to drive protocol implementations in a way that is decoupled from networking altogether. - `NIOConcurrencyHelpers`. This provides a few low-level concurrency primitives that are used by NIO implementations, such as locks and atomics. - `NIOFoundationCompat`. This extends a number of NIO types for better interoperation with Foundation data types. If you are working with Foundation data types such as `Data`, you should import this. - `NIOTLS`. This provides a few common abstraction types for working with multiple TLS implementations. Note that this doesn't provide TLS itself: please investigate [swift-nio-ssl][repo-nio-ssl] and [swift-nio-transport-services][repo-nio-transport-services] for concrete implementations. - `NIOHTTP1`. This provides a low-level HTTP/1.1 protocol implementation. - `NIOWebSocket`. This provides a low-level WebSocket protocol implementation. - `NIOTestUtils`. This provides a number of helpers for testing projects that use SwiftNIO. ### Protocol Implementations Below you can find a list of a few protocol implementations that are done with SwiftNIO. This is a non-exhaustive list of protocols that are either part of the SwiftNIO project or are accepted into the [SSWG](https://swift.org/server)'s incubation process. All of the libraries listed below do all of their I/O in a non-blocking fashion using SwiftNIO. #### Low-level protocol implementations Low-level protocol implementations are often a collection of [`ChannelHandler`][ch]s that implement a protocol but still require the user to have a good understanding of SwiftNIO. Often, low-level protocol implementations will then be wrapped in high-level libraries with a nicer, more user-friendly API. Protocol | Client | Server | Repository | Module | Comment --- | --- | --- | --- | --- | --- HTTP/1 | | | [apple/swift-nio](https://github.com/apple/swift-nio) | [`NIOHTTP1`][nioh1] | official NIO project HTTP/2 | | | [apple/swift-nio-http2](https://github.com/apple/swift-nio-http2) | [`NIOHTTP2`][nioh2] | official NIO project WebSocket | | | [apple/swift-nio](https://github.com/apple/swift-nio) | [`NIOWebSocket`][niows] | official NIO project TLS | | | [apple/swift-nio-ssl](https://github.com/apple/swift-nio-ssl) | [`NIOSSL`][niossl] | official NIO project SSH | | | [apple/swift-nio-ssh][repo-nio-ssh] | [`NIOSSH`][niossh] | official NIO project #### High-level implementations High-level implementations are usually libraries that come with an API that doesn't expose SwiftNIO's [`ChannelPipeline`][cp] and can therefore be used with very little (or no) SwiftNIO-specific knowledge. The implementations listed below do still do all of their I/O in SwiftNIO and integrate really well with the SwiftNIO ecosystem. Protocol | Client | Server | Repository | Module | Comment --- | --- | --- | --- | --- | --- HTTP | | | [swift-server/async-http-client](https://github.com/swift-server/async-http-client) | `AsyncHTTPClient` | SSWG community project gRPC | | | [grpc/grpc-swift](https://github.com/grpc/grpc-swift) | `GRPC` | also offers a low-level API; SSWG community project APNS | | | [kylebrowning/APNSwift](https://github.com/kylebrowning/APNSwift) | `APNSwift` | SSWG community project PostgreSQL | | | [vapor/postgres-nio](https://github.com/vapor/postgres-nio) | `PostgresNIO` | SSWG community project Redis | | | [swift-server/RediStack](https://github.com/swift-server/RediStack) | `RediStack` | SSWG community project ### Supported Versions ### SwiftNIO 2 This is the current version of SwiftNIO and will be supported for the foreseeable future. The most recent versions of SwiftNIO support Swift 5.6 and newer. The minimum Swift version supported by SwiftNIO releases are detailed below: SwiftNIO | Minimum Swift Version --------------------|---------------------- `2.0.0 ..< 2.30.0` | 5.0 `2.30.0 ..< 2.40.0` | 5.2 `2.40.0 ..< 2.43.0` | 5.4 `2.43.0 ..< 2.51.0` | 5.5.2 `2.51.0 ...` | 5.6 ### SwiftNIO 1 SwiftNIO 1 is considered end of life - it is strongly recommended that you move to a newer version. The Core NIO team does not actively work on this version. No new features will be added to this version but PRs which fix bugs or security vulnerabilities will be accepted until the end of May 2022. If you have a SwiftNIO 1 application or library that you would like to migrate to SwiftNIO 2, please check out the [migration guide](docs/migration-guide-NIO1-to-NIO2.md) we prepared for you. The latest released SwiftNIO 1 version supports Swift 4.0, 4.1, 4.2, and 5.0. ### Supported Platforms SwiftNIO aims to support all of the platforms where Swift is supported. Currently, it is developed and tested on macOS and Linux, and is known to support the following operating system versions: * Ubuntu 18.04+ * macOS 10.9+, iOS 7+; (macOS 10.14+, iOS 12+, tvOS 12+ or watchOS 6+ with [swift-nio-transport-services][repo-nio-transport-services]) ### Compatibility SwiftNIO follows [SemVer 2.0.0](https://semver.org/#semantic-versioning-200) with a separate document declaring [SwiftNIO's Public API](docs/public-api.md). What this means for you is that you should depend on SwiftNIO with a version range that covers everything from the minimum SwiftNIO version you require up to the next major version. In SwiftPM that can be easily done specifying for example `from: "2.0.0"` meaning that you support SwiftNIO in every version starting from 2.0.0 up to (excluding) 3.0.0. SemVer and SwiftNIO's Public API guarantees should result in a working program without having to worry about testing every single version for compatibility. ## Conceptual Overview SwiftNIO is fundamentally a low-level tool for building high-performance networking applications in Swift. It particularly targets those use-cases where using a "thread-per-connection" model of concurrency is inefficient or untenable. This is a common limitation when building servers that use a large number of relatively low-utilization connections, such as HTTP servers. To achieve its goals SwiftNIO extensively uses "non-blocking I/O": hence the name! Non-blocking I/O differs from the more common blocking I/O model because the application does not wait for data to be sent to or received from the network: instead, SwiftNIO asks for the kernel to notify it when I/O operations can be performed without waiting. SwiftNIO does not aim to provide high-level solutions like, for example, web frameworks do. Instead, SwiftNIO is focused on providing the low-level building blocks for these higher-level applications. When it comes to building a web application, most users will not want to use SwiftNIO directly: instead, they'll want to use one of the many great web frameworks available in the Swift ecosystem. Those web frameworks, however, may choose to use SwiftNIO under the covers to provide their networking support. The following sections will describe the low-level tools that SwiftNIO provides, and provide a quick overview of how to work with them. If you feel comfortable with these concepts, then you can skip right ahead to the other sections of this README. ### Basic Architecture The basic building blocks of SwiftNIO are the following 8 types of objects: - [`EventLoopGroup`][elg], a protocol, provided by `NIOCore`. - [`EventLoop`][el], a protocol, provided by `NIOCore`. - [`Channel`][c], a protocol, provided by `NIOCore`. - [`ChannelHandler`][ch], a protocol, provided by `NIOCore`. - `Bootstrap`, several related structures, provided by `NIOCore`. - [`ByteBuffer`][bb], a struct, provided by `NIOCore`. - [`EventLoopFuture`][elf], a generic class, provided by `NIOCore`. - [`EventLoopPromise`][elp], a generic struct, provided by `NIOCore`. All SwiftNIO applications are ultimately constructed of these various components. #### EventLoops and EventLoopGroups The basic I/O primitive of SwiftNIO is the event loop. The event loop is an object that waits for events (usually I/O related events, such as "data received") to happen and then fires some kind of callback when they do. In almost all SwiftNIO applications there will be relatively few event loops: usually only one or two per CPU core the application wants to use. Generally speaking event loops run for the entire lifetime of your application, spinning in an endless loop dispatching events. Event loops are gathered together into event loop *groups*. These groups provide a mechanism to distribute work around the event loops. For example, when listening for inbound connections the listening socket will be registered on one event loop. However, we don't want all connections that are accepted on that listening socket to be registered with the same event loop, as that would potentially overload one event loop while leaving the others empty. For that reason, the event loop group provides the ability to spread load across multiple event loops. In SwiftNIO today there is one [`EventLoopGroup`][elg] implementation, and two [`EventLoop`][el] implementations. For production applications there is the [`MultiThreadedEventLoopGroup`][mtelg], an [`EventLoopGroup`][elg] that creates a number of threads (using the POSIX [`pthreads`][pthreads] library) and places one `SelectableEventLoop` on each one. The `SelectableEventLoop` is an event loop that uses a selector (either [`kqueue`][kqueue] or [`epoll`][epoll] depending on the target system) to manage I/O events from file descriptors and to dispatch work. These [`EventLoop`s][el] and [`EventLoopGroup`s][elg] are provided by the `NIOPosix` module. Additionally, there is the [`EmbeddedEventLoop`][eel], which is a dummy event loop that is used primarily for testing purposes, provided by the `NIOEmbedded` module. [`EventLoop`][el]s have a number of important properties. Most vitally, they are the way all work gets done in SwiftNIO applications. In order to ensure thread-safety, any work that wants to be done on almost any of the other objects in SwiftNIO must be dispatched via an [`EventLoop`][el]. [`EventLoop`][el] objects own almost all the other objects in a SwiftNIO application, and understanding their execution model is critical for building high-performance SwiftNIO applications. #### Channels, Channel Handlers, Channel Pipelines, and Channel Contexts While [`EventLoop`][el]s are critical to the way SwiftNIO works, most users will not interact with them substantially beyond asking them to create [`EventLoopPromise`][elp]s and to schedule work. The parts of a SwiftNIO application most users will spend the most time interacting with are [`Channel`][c]s and [`ChannelHandler`][ch]s. Almost every file descriptor that a user interacts with in a SwiftNIO program is associated with a single [`Channel`][c]. The [`Channel`][c] owns this file descriptor, and is responsible for managing its lifetime. It is also responsible for processing inbound and outbound events on that file descriptor: whenever the event loop has an event that corresponds to a file descriptor, it will notify the [`Channel`][c] that owns that file descriptor. [`Channel`][c]s by themselves, however, are not useful. After all, it is a rare application that doesn't want to do anything with the data it sends or receives on a socket! So the other important part of the [`Channel`][c] is the [`ChannelPipeline`][cp]. A [`ChannelPipeline`][cp] is a sequence of objects, called [`ChannelHandler`][ch]s, that process events on a [`Channel`][c]. The [`ChannelHandler`][ch]s process these events one after another, in order, mutating and transforming events as they go. This can be thought of as a data processing pipeline; hence the name [`ChannelPipeline`][cp]. All [`ChannelHandler`][ch]s are either Inbound or Outbound handlers, or both. Inbound handlers process "inbound" events: events like reading data from a socket, reading socket close, or other kinds of events initiated by remote peers. Outbound handlers process "outbound" events, such as writes, connection attempts, and local socket closes. Each handler processes the events in order. For example, read events are passed from the front of the pipeline to the back, one handler at a time, while write events are passed from the back of the pipeline to the front. Each handler may, at any time, generate either inbound or outbound events that will be sent to the next handler in whichever direction is appropriate. This allows handlers to split up reads, coalesce writes, delay connection attempts, and generally perform arbitrary transformations of events. In general, [`ChannelHandler`][ch]s are designed to be highly re-usable components. This means they tend to be designed to be as small as possible, performing one specific data transformation. This allows handlers to be composed together in novel and flexible ways, which helps with code reuse and encapsulation. [`ChannelHandler`][ch]s are able to keep track of where they are in a [`ChannelPipeline`][cp] by using a [`ChannelHandlerContext`][chc]. These objects contain references to the previous and next channel handler in the pipeline, ensuring that it is always possible for a [`ChannelHandler`][ch] to emit events while it remains in a pipeline. SwiftNIO ships with many [`ChannelHandler`][ch]s built in that provide useful functionality, such as HTTP parsing. In addition, high-performance applications will want to provide as much of their logic as possible in [`ChannelHandler`][ch]s, as it helps avoid problems with context switching. Additionally, SwiftNIO ships with a few [`Channel`][c] implementations. In particular, it ships with `ServerSocketChannel`, a [`Channel`][c] for sockets that accept inbound connections; `SocketChannel`, a [`Channel`][c] for TCP connections; and `DatagramChannel`, a [`Channel`][c] for UDP sockets. All of these are provided by the `NIOPosix` module. It also provides[`EmbeddedChannel`][ec], a [`Channel`][c] primarily used for testing, provided by the `NIOEmbedded` module. ##### A Note on Blocking One of the important notes about [`ChannelPipeline`][cp]s is that they are thread-safe. This is very important for writing SwiftNIO applications, as it allows you to write much simpler [`ChannelHandler`][ch]s in the knowledge that they will not require synchronization. However, this is achieved by dispatching all code on the [`ChannelPipeline`][cp] on the same thread as the [`EventLoop`][el]. This means that, as a general rule, [`ChannelHandler`][ch]s **must not** call blocking code without dispatching it to a background thread. If a [`ChannelHandler`][ch] blocks for any reason, all [`Channel`][c]s attached to the parent [`EventLoop`][el] will be unable to progress until the blocking call completes. This is a common concern while writing SwiftNIO applications. If it is useful to write code in a blocking style, it is highly recommended that you dispatch work to a different thread when you're done with it in your pipeline. #### Bootstrap While it is possible to configure and register [`Channel`][c]s with [`EventLoop`][el]s directly, it is generally more useful to have a higher-level abstraction to handle this work. For this reason, SwiftNIO ships a number of `Bootstrap` objects whose purpose is to streamline the creation of channels. Some `Bootstrap` objects also provide other functionality, such as support for Happy Eyeballs for making TCP connection attempts. Currently SwiftNIO ships with three `Bootstrap` objects in the `NIOPosix` module: [`ServerBootstrap`][sbootstrap], for bootstrapping listening channels; [`ClientBootstrap`][cbootstrap], for bootstrapping client TCP channels; and [`DatagramBootstrap`][dbootstrap] for bootstrapping UDP channels. #### ByteBuffer The majority of the work in a SwiftNIO application involves shuffling buffers of bytes around. At the very least, data is sent and received to and from the network in the form of buffers of bytes. For this reason it's very important to have a high-performance data structure that is optimized for the kind of work SwiftNIO applications perform. For this reason, SwiftNIO provides [`ByteBuffer`][bb], a fast copy-on-write byte buffer that forms a key building block of most SwiftNIO applications. This type is provided by the `NIOCore` module. [`ByteBuffer`][bb] provides a number of useful features, and in addition provides a number of hooks to use it in an "unsafe" mode. This turns off bounds checking for improved performance, at the cost of potentially opening your application up to memory correctness problems. In general, it is highly recommended that you use the [`ByteBuffer`][bb] in its safe mode at all times. For more details on the API of [`ByteBuffer`][bb], please see our API documentation, linked below. #### Promises and Futures One major difference between writing concurrent code and writing synchronous code is that not all actions will complete immediately. For example, when you write data on a channel, it is possible that the event loop will not be able to immediately flush that write out to the network. For this reason, SwiftNIO provides [`EventLoopPromise`][elp] and [`EventLoopFuture`][elf] to manage operations that complete *asynchronously*. These types are provided by the `NIOCore` module. An [`EventLoopFuture`][elf] is essentially a container for the return value of a function that will be populated *at some time in the future*. Each [`EventLoopFuture`][elf] has a corresponding [`EventLoopPromise`][ ... ...

近期下载者

相关文件


收藏者