swift-nio | driven network application framework for high performance | Networking library
kandi X-RAY | swift-nio Summary
kandi X-RAY | swift-nio Summary
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, but written for Swift.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of swift-nio
swift-nio Key Features
swift-nio Examples and Code Snippets
Community Discussions
Trending Discussions on swift-nio
QUESTION
I have an XCTest which works with UI components. I tried to open a server socket within the xctext function using SwiftNIO.
I took the echo server example from here. and I simplified, removed the args with hardcoded values for the sake of a dirty test.
...ANSWER
Answered 2021-Dec-22 at 10:17Yes, that is possible, you can find many examples of this in the AsyncHTTPClient and SwiftNIO test suites.
The reason that yours doesn't work is because you shut down the MultiThreadedEventLoopGroup
right after binding the socket. So essentially you're starting everything up and then you shut it down again.
Also, for unit tests, I'd recommend binding to 127.0.0.1
only because you probably don't want connections from elsewhere. Another good idea is to use an ephemeral port, ie. have the system pick a free, random port automatically. You can achieve this by specifying port 0
. After you bind
the server Channel
you can then interrogate the server channel by using serverChannel.localAddress?.port!
about the port it picked.
Here's a full example with a client and a server in a test case.
QUESTION
I'm trying to design a SwiftNIO server where multiple clients (like 2 or 3) can connect to the server, and when connected, they can all receive information from the server.
To do this, I create a ServerHandler
class which is shared & added to each pipeline of connected clients.
ANSWER
Answered 2021-Dec-19 at 20:16Your questions aren't stupid at all!
Yeah, sharing a
ChannelHandler
probably counts as "not recommended". But not because it doesn't work, it's more that it's unusual and probably not something other NIO programmers would expect. But if you're comfortable with it, it's fine. If you're high-performance enough that you worry about the exact number of allocations perChannel
then you may be able to save some by sharing handlers. But I really wouldn't optimise the prematurely.If you didn't want to share handlers, then you could use multiple handlers that share a reference to some kind of coordinator object. Don't get me wrong, it's really still the same thing: One shared reference across multiple network connections. The only real difference is that testing that may be a little easier and it would possibly feel more natural to other NIO programmers. (In any case be careful to either make sure that all those
Channel
s are on the sameEventLoop
or to use external synchronisation (with say a lock, which might not be ideal from a performance point of view).write
just enqueues some data to be written.flush
makes SwiftNIO attempt to send all the previously written data.writeAndFlush
simply callswrite
and thenflush
.Why does NIO distinguish between
write
andflush
at all? In high-performance networking applications, the biggest overhead might be the syscall overhead. And to send data over TCP, SwiftNIO has to do a syscall (write
,writev
,send
, ...).Any SwiftNIO program will work if you just ignore
write
andflush
and always usewriteAndFlush
. But, if the network is keeping up, this will cost you one syscall perwriteAndFlush
call. In many cases however, a library/app that's using SwiftNIO already knows that it wants to enqueue multiple bits of data to be sent over the network. And in that case doing say threewriteAndFlush
in a row would be wasteful. If would be much better to accumulate the three bits of data and then send them all in one syscall using a "vector write" (e.g.writev
syscall). And that's exactly what SwiftNIO would do if you did saywrite
,write
,write
,flush
. So the three writes will all be sent using onewritev
system call. SwiftNIO will simply get the three pointers to the bits of data and hand them to the kernel which then attempts to send them over the network.You can take this even a little further. Let's assume you're a high-performance server and you want to respond to a flood of incoming requests. You'll get your requests from the client over
channelRead
. If you're now able to reply synchronously, you could justwrite
them responses (which will enqueue) them. And once you getchannelReadComplete
(which marks the end of a "read burst") you canflush
. That would allow you to respond to as many requests as you can get in a single read burst using just onewritev
syscall. This can be quite an important optimisation in certain scenarios.
QUESTION
I can't get my Swift Vapor project to compile (which was running fine before), because nio produces strange errors all of a sudden:
Also, swift package update
produces this:
ANSWER
Answered 2021-Oct-15 at 11:31Besides cleaning & re-building the project, you can try deleting the following files / directories:
/.build
/.swiftpm
/Package.resolved
Then, run swift package update
and try to build the project again.
QUESTION
I'm using Swift-NIO to create a Http/2 proxy with TLS, for iOS/tvOS. My proxy startup:
...ANSWER
Answered 2021-Sep-14 at 12:17This question was handled on the swift Forums https://forums.swift.org/t/swift-nio-based-proxy-reconfigures-pipeline-on-every-incoming-request/52043
Answer by Lukasa:
This is expected behaviour. HTTP/2 is multiplexed: this means you can run multiple request/response sequences over the same TCP connection. This manifests in SwiftNIO HTTP/2 in the form of the "stream channel initializer": this is called once per stream creation. The stream channel initializer here is the trailing closure being passed to configureCommonHTTPServerPipeline.
If you would like to create the handlers only once, you can do that. But now your handlers need to support being involved in multiple concurrent requests and responses.
QUESTION
I'm fairly new to Swift and very new to NIO.
I'm adding Swift code to a large project that needs to up/down load a lot of data (GBs) to AWS. To that end, I've imported the GitHub project Soto, which relies heavily on NIO.
Most methods that send/receive data do so through ByteBuffer
structs. My application already has the data to upload in Foundation Data
objects. I'm having trouble figuring out the best way to get these Data
objects into NIO.
In the documentation for NIO's ByteBuffer
(2.26.0) it states
Supported types: A variety of types can be read/written from/to a ByteBuffer. ... Out of the box, ByteBuffer supports for example the following types (non-exhaustive list):
- String/StaticString
- Swift’s various (unsigned) integer types
- Foundation‘s Data
- [UInt8] and generally any Collection of UInt8
However, the latest swift-nil package has no ByteBuffer
support for Foundation Data
objects. Instead, it supports DispatchData
objects, which in turn seem to have no interoperability with Data
objects.
What I want to avoid is making a copy of every block of data (100's of MB at a time), just to convert between Data
and DispatchData
types.
So...
Right now my thinking is one of
I'm completely lost, and there's a simple solution I haven't found
The solution is to create a subclass of
DispatchData
backed by aData
objectInitialize the
ByteBuffer
structure using aDispatchData
created using the no-copy initializer pointing to the raw byte array in theData
object, along with a custom deallocator that simply retains theData
object until theByteBuffer
andDispatchData
objects are destroyed.
I would appreciate any thoughts, experience, or suggestions (particularly if it's option #1).
...ANSWER
Answered 2021-Mar-08 at 18:37You'll need to import NIOFoundationCompat
to get any of NIO's method that work with Foundation
data types such as Data
(or JSONDecoder
/JSONEncoder
). NIOFoundationCompat
is just another module of the swift-nio
package so you won't need another dependency.
But just to be clear, under the hood, there will always be copies but probably you don't need to worry about them, copies are extremely fast on today's CPUs. If you absolutely want to avoid copies, you'll need to create ByteBuffer
s straight away. To help you with that, you may want to add where you get your data from that you want to send over the network.
QUESTION
I have been trying to use SWIFT-NIO-SSL, to connect to server using the CA certificate and Server certificate.
After numeral attempts, and trying out different approaches, I could not get a solution. Is there any tutorial or any help in connecting to TLS using ca certificate with swift-nio-ssl would be helpful.
...ANSWER
Answered 2020-Apr-05 at 20:52I'm not 100% sure if that's what you're asking but are you trying to connect to a server using a custom CA, you probably want the following TLSConfiguraion
:
QUESTION
I write a TCP Client in Swift-NIO to connect Netty TCP Server. I want tcp client can auto reconnect when needed.
...ANSWER
Answered 2020-Jan-15 at 12:13The solution for SwiftNIO will require your handler to attach callbacks to the future returned from connect
. These callbacks can close over the repeating task, and so can cancel it when a connection completes. For example:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install swift-nio
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page