Commit Graph
2140 Commits
Author SHA1 Message Date
Johannes Weiss f38984d80e remove bogus 'is active' assertion in channelRead0 (#739)
Motivation:

BaseSocketChannel used to assert that `channelRead0` is only called when
the channel is active. That's bogus and it's trivial to hit this issue
when calling `fireChannelRead` in the last `ChannelHandler` when the
channel has gone inactive.

Modifications:

remove assertion, add comment

Result:

fewer crashes
2019-01-07 14:18:48 +00:00
Johannes Weiss fa2ebe4a56 remove backports of newer Swift functionality (#738)
Motivation:

We now depend on Swift 5.0, so we can remove all the backporting of
functionality.

Modifications:

deleted all backported Swift functionality

Result:

less code
2019-01-07 12:32:58 +00:00
Johannes Weiss 95b290a51f use automatic Equatable conformances (#737)
Motivation:

No code is the best code, let's have the compiler generate more
Equatable instances for us.

Modifications:

remove some hand-written Equatable conformances

Result:

less code, possibly fewer bugs
2019-01-07 11:52:22 +00:00
Johannes Weiss 684cad331c EventLoopFuture: use Result type (#734)
Motivation:

Now that the stdlib has introduced the Result type, we can use it in the
implementation (and the whenComplete) function of EventLoopFuture

Modifications:

- replace EventLoopValue with Result
- make whenComplete provide the Result

Result:

use the new shiny stuff
2019-01-05 08:08:47 +00:00
Johannes Weiss 08eb3259ce better DispatchData support (#732)
Motivation:

DispatchData's APIs are less than optimal and the current one we used
did allocate at least on Linux to copy bytes out.

Modifications:

introduce ByteBuffer.write/read/set/getDispatchData

Result:

when Dispatch gets fixed, lower allocations
2019-01-03 17:45:45 +00:00
Johannes Weiss 695b1ebe13 prefer non-deprecated API for Data/Range (#733)
Motivation:

Deprecated API sucks.

Modifications:

Use non-deprecated API for Data/Range stuff

Result:

better code
2019-01-03 16:00:16 +00:00
Cory Benfield 0db974bdbe Fix: BlockingIOThreadPool not releasing the latest WorkItem (#716) (#730)
BUGFIX: BlockingIOThreadPool not releasing the latest WorkItem

Fix is to clear the latest work item just before waiting for
the next one to be submitted.

Added two tests to validate the fix: submitting only one work item
to an IO thread pool, and submitting two consecutive work items.

Thanks to Gwynne Raskind for help troubleshooting & fix

(cherry picked from commit 8df414b716)
2019-01-03 10:58:15 +00:00
Ilya Puchka 0db96bf744 [NIOHTTP1Server] Use fixed default string to write in response and do not hardcode content length (#718)
* Use fixed default string to write in response and do not hardcode content length

Motivation:

Improve example code, resolve #714

Modifications:

Move hardcoded "Hello world" string to a constant property and removed hardcoding of content length header value

Result:

In example code content length will be written correctly when default response changes
2019-01-03 10:09:13 +00:00
Ilya Puchka b878f06b20 override content encoding header value in compressor (#728)
override content encoding header value in compressor
2019-01-03 09:57:57 +00:00
Sergej Jaskiewicz 161eaa760e [Gardening] Use multiline string literals instead of concatenation (#719)
Motivation:

The rest of the code in NIO uses multiline literals.

Modifications:

Converted string concatenations to multiline literals.

Result:

NFC, prettier code.
2019-01-02 10:36:04 +00:00
Johannes Weiss 531745d71a remove ContiguousCollection (#709)
Motivation:

ContiguousCollection is no longer necessary because Sequence gained
withContiguousStorageIfAvailable.

Modifications:

Removed ContiguousCollection and switch to withContiguousStorageIfAvailable.

Result:

less code
2018-12-18 09:20:37 +00:00
Johannes Weiss e415d654a5 internalise things made public by accident (#463)
Motivation:

A few things are public by accident, this makes those internal for NIO
2.0.0.

Modifications:

internalise a few things

Result:

less public API
2018-12-17 19:19:40 +00:00
Norman Maurer bc21c9a7ac Remove method from ChannelOutboundInvoker protocol that can be extens… (#214)
* Remove method from ChannelOutboundInvoker protocol that can be extensions.

Motivation:

We should not have methods in the ChannelOutboundInvoker that can easily implemented as extension and that never need to be overriden.

Modifications:

Remove methods that return EventLoopFuture<Void> from the protocol and just implement it in the extension.

Result:

More narrow protocol.

* Address comment
2018-12-17 18:40:44 +00:00
Johannes Weiss 885ac373ad fix remaining warnings & -warnings-as-errors (#712)
Motivation:

Warnings are bad.

Modifications:

- fixed remaining warnings
- enabled -warnings-as-errors

Result:

happier devs
2018-12-17 17:13:25 +00:00
Johannes Weiss 2262b656ac fix CircularBuffer.replaceSubrange expands (#683)
Motivation:

CircularBuffer.replaceSubrange was crazy eager to double the capacity
which is bad.

Modifications:

Made CircularBuffer.replaceSubrange only double if necessary.

Result:

less memory usage
2018-12-14 18:49:34 +00:00
Johannes Weiss 1a69a97b5e use String.UTF8View.withContiguousStorageIfAvailable (#707)
Motivation:

The new shiny Swift 5 gives us fast access to the UTF8 bytes (if
available, which mostly means not bridged from Cocoa), we should use
that.

Modifications:

use UTF8View.withContiguousStorageIfAvailable

Result:

faster String access
2018-12-14 18:19:55 +00:00
Cory Benfield 15be5243ef Improve ByteBuffer String APIs (#696)
Motivation:

ByteBuffer has pretty good string writing APIs, but they're imperfect.
In particular, the methods in ByteBuffer-aux both return optional Int,
which is strange as they cannot fail. We also only have support for writing
strings in UTF-8.

Modifications:

- ByteBuffer.set(string:at:) and ByteBuffer.write(string:) now both
     return `Int`, and their return value has been made discardable.
- NIOFoundationCompat has been expanded with two new methods for writing
     strings to ByteBuffers in various encodings using Foundation's
     encoding support: ByteBuffer.set(string:encoding:at:) and
     ByteBuffer.write(string:encoding:).
- NIOFoundationCompat has also been expanded with a companion to the
     already existing ByteBuffer.getString(at:length:encoding), namely
     ByteBuffer.readString(length:encoding:).
- New error enum for reporting failures to encode.

Result:

Easier to work with strings in a wider variety of cases with
ByteBuffer

Resolves #253.
2018-12-14 17:25:13 +00:00
Johannes Weiss 1ac199c195 allow EventLoopIterator to be constructed using [EventLoop] (#706)
Motivation:

Modules outside of NIO core must be able to create `EventLoopIterator`s
in order to conform to the `EventLoop` protocol, therefore we need an
initialiser that's available.

Modifications:

add public initialiser to construct EventLoopIterator from [EventLoop]

Result:

others can implement EventLoops again
2018-12-14 16:13:17 +00:00
Johannes Weiss ee6534f627 update tools-version to 5.0 & fix latest warnings (#704)
Motivation:

The latest Swift 5.0s bring us new goodies (hence tools version) and
also more warnings, hence the fixes for them.

Modifications:

- set tools-version to 5.0
- fix warnings
- workaround https://bugs.swift.org/browse/SR-9514

Result:

happier developers
2018-12-14 12:34:08 +00:00
Sergo Beruashvili e91dacbd54 Removed specialisations from Atomics functions and added @inlinable attribute instead. (#698)
Access level of properties used from @inlinable functions are now internal and they are marked as @usableFromInline.
2018-12-12 09:43:46 +00:00
Bas Broek 6772f13c29 Change unnecessary functions to variables (#695)
* Change unnecessary functions to variables

Motivation:

The changed functions do not have any side effects and therefore better convey intent when being a simple computed variable.

Modifications:

Changes:

- `func markedElementIndex() -> Int?` to `var markedElementIndex: Int?`
- `func markedElement() -> E?` to `var markedElement: E?`
- `func hasMark() -> Bool` to `var hasMark: Bool`

Result:

- improved code quality
2018-12-11 17:50:58 +00:00
Cory Benfield f3f6e09ba2 Make WebSocketOpcode a struct. (#694)
Motivation:

WebSocketOpcode was made an enumeration to make it possible to cleanly
switch over the values in switch statements. However, this unfortunately
made it possible to construct contradictory values of WebSocketOpcode,
such as .unknownNonControl(0x1) (which should be spelled .text), or
.unknownControl(0xFF) (which is simply invalid).

This patch removes the ability to construct invalid values of
WebSocketOpcode by turning it into a struct and using static lets for known
values. While we're here it cleans some other stuff up.

Modifications:

- Made WebSocketOpcode a struct
- Added several static lets for easy access and to reduce code churn
- Used synthesised Equatable conformance
- Added synthesised Hashable conformance
- Added CustomStringConvertible conformance to better represent known values
- Added CaseIterable conformance to provide entire range of valid values.

Result:

WebSocketOpcode will be a better-behaved type with more guarantees and
fewer places to trap and explode.

Resolves #617.
2018-12-11 11:56:10 +00:00
Cory Benfield 55e4bc805e Reunify errors into a single enum. (#690)
Motivation:

If we're going to have errors in enumerations, we should aim to have as
few as possible. We wanted to throw away these two, so let's do it.

Modifications:

- Removed `ChannelLifecycleError`, moved case to `ChannelError`.
- Removed `MulticastError`, moved cases to `ChannelError`.
- Removed manual `Equatable` conformance, used synthesised one.
- Pulled `connectFailed` out of `ChannelError`, as it made it impossible to
    write a correct implementation of `Equatable`.
- Conformed `NIOConnectionError` to `Error` instead.

Result:

Better error handling.

Resolves #620
2018-12-11 08:44:45 +00:00
Johannes Weiss ce39ec20c0 all caps for SNI (#693)
Motivation:

In Swift, abbreviations use the same case for all letters, therefore it
should be `SNI` and not `Sni`.

Modifications:

changes `Sni` to `SNI`

Result:

more consistent with naming guidelines
2018-12-10 20:28:10 +00:00
Johannes Weiss f6b4acc133 changed port/HTTP version parts to Ints (#691)
Motivation:

- `Int` is the currency type for integral things and we already sort of
  changed the the type for ports but not everywhere.
- generic parameters that aren't just 'any type' shouldn't be just `T`
  or `S` or something

Modifications:

- make more use of `Int`
- rename generic parameters to something more descriptive like `Bytes`

Result:

easier to read/write NIO code
2018-12-10 20:18:13 +00:00
Johannes Weiss 305ee9818b make factory method names start with make (#692)
Motivation:

Swift naming guidelines mandate that factory methods start with `make`,
like `makeSomething`. We had a few that were `newSomething`.

Modifications:

make all factories start with make

Result:

more compliant to Swift naming rules
2018-12-10 17:59:24 +00:00
Johannes Weiss cbc214938a first cut at B2MD v2 (#675)
Motivation:

Explain here the context, and why you're making that change.
What is the problem you're trying to solve.

Modifications:

Describe the modifications you've done.

Result:

After your change, what will change.
2018-12-10 16:23:12 +00:00
Johannes Weiss 9b78557aff kick off NIO2 (#678)
Motivation:

NIO2 development starts now.

Modifications:

Made NIO Swift 5-only for everything else see docs/public-api-changes-NIO1-to-NIO2.md

Result:

NIO2 development can start.
2018-12-07 21:15:34 +00:00
Johannes Weiss a20e129c22 change newPromise(for: T.Type) to newPromise(of: T.Type) (#672)
Motivation:

making a promise of a type is more grammatical than making a promise for
a type.

Modifications:

s/newPromise(for/newPromise(of/g

Result:

more grammar
2018-11-28 17:32:07 +00:00
Johannes Weiss c1a84a0085 optional type parameter for newPromise() (#671)
Motivation:

When creating new promises I always find it very frustrating to type the
`: EventLoopPromise<Type>` type annotation but it's necessary for the
compiler to know type the promise will be fulfilled with.

Modifications:

allow an optional `for: SomeType.self` parameter for `newPromise` as

    let p = eventLoop.newPromise(for: Int.self)

is much easier to type than

    let p: EventLoopPromise<Int> = eventLoop.newPromise()

Result:

easier to write code
2018-11-28 12:18:03 +00:00
Bouke Haarsma 29398d5f26 Assign channel variable in docs (#662) 2018-11-27 08:09:06 +00:00
Kyle Jessup ac9e5d02ac Added public init for HTTPServerProtocolErrorHandler (#660) 2018-11-23 15:29:09 +00:00
Johannes Weiss c66386eb3a don't capture in the callout closures in BaseSocketChannel (#658)
Motivation:

We were complaining that we can't use `@convention(thin)` in a place
where we really need to make sure we don't get allocations for returning
a closure (that doesn't capture any context), so it should just be a
function pointer.

In reality however, we were actually capturing context...

Modifications:

Don't capture context anymore.

Result:

Closures should not actually not enclose anything.
2018-11-21 09:48:06 +00:00
Johannes Weiss cf958fb5a5 ignore pthread_setname_np result (#656)
Motivation:

We have seen pthread_setname_np fail with EPERM in containers, it's
non-critical so best to ignore.

Modifications:

ignore pthread_setname_np's return code.

Result:

works in containers.
2018-11-16 20:01:11 +00:00
Jim Dovey bf0cd5295e Make the member variables of HTTPHeader and HTTPHeaderIndex visible to swift-nio-http2. (#654) 2018-11-15 10:46:27 +00:00
Johannes Weiss 08ab8b51cd remove ugly and unnecessary ignore result (_ = ...) (#652)
Motivation:

_ = expression() is ugly and in many cases unimportant. In fact we train
ourselves to overread it which makes special cases where you'd actually
expect a result to be used just look normal.

Modifications:

remove _ = from a lot of places. In many cases we already had a better
(and sometimes cheaper way) to not return a value but in some cases
(mostly `remove` functions) I added `@discardableResult`

Result:

NIO source code looks nicer
2018-11-13 14:49:08 +00:00
Cory Benfield a0b3b19cf4 Provide ChannelPipeline.remove methods with promises. (#651)
Motivation:

ChannelPipeline.remove0 contains a comment that indicates that users
were expected to be able to use the ChannelHandlerContext when both
handlerRemoved and the promise for channel removal call out.

This works when invoking remove() from outside the event loop, but if
a handler invokes remove on itself then it will only be able to attach
a callback to the future *after* the callout occurs. This means that a
ChannelHandler removing itself from the pipeline cannot rely on there
being a moment when it can still invoke the ChannelHandlerContext, but
when it is no longer a formal part of the pipeline.

This kind of behaviour is useful in some unusual cases, and it would
be nice to support it.

Modifications:

- Added remove() methods that accept promises as input.
- Rewrote the current remove() methods in terms of the new ones.
- Added tests.

Result:

ChannelHandlers can perform cleanup with a valid ChannelHandlerContext
but outside the Channel.
2018-11-07 11:50:51 +00:00
Johannes Weiss 035962e6b6 EventLoop.assertInEventLoop and preconditionInEventLoop (#644)
Motivation:

Dispatch can only precondition that code is run from a certain queue, it
can't return if code is running on a certain queue.
To support that this introduces new EventLoop.assertInEventLoop and
EventLoop.preconditionInEventLoop methods instead of
assert(eventLoop.inEventLoop).

Modifications:

add EventLoop.assertInEventLoop and EventLoop.preconditionInEventLoop

Result:

better support for Network.framework in NIOTS
2018-11-05 09:50:33 +00:00
Johannes Weiss 26bee21674 fix Swift 5 warnings (mostly redundant modifiers (#642)
Motivation:

Swift 5 complains on redundant modifiers. For example declaring a
`public func` inside of a `public extension` is now a warning. I don't
agree with this but I dislike warnings even more, so...

Modifications:

remove all redundant modifiers that the compiler now warns about such as

    swift-nio/Sources/NIOPriorityQueue/PriorityQueue.swift:90:5: warning: 'public' modifier is redundant for property declared in a public extension

Result:

no warnings in Swift 5
2018-11-01 09:50:55 +00:00
Johannes Weiss 054f65e473 add String blitting benchmarks (#641)
Motivation:

Writing Strings to ByteBuffers is important, let's have some more
benchmarks.

Modifications:

add String to ByteBuffer benchmarks

Result:

more benchmarks
2018-10-30 23:41:20 -07:00
Johannes Weiss ab69133269 ByteBuffer: small & medium sized string copies over 10% faster (#640)
Motivation:

urbp[start ..< end] is quite a bit faster than when done in two
operations urbp.dropFirst(start).prefix(length). This improves small (4
bytes) & medium sized (24 bytes) String copies by over 10%. Good for
HTTP headers etc.

Modifications:

replace urbp.dropFirst(start).prefix(length) by urbp[start ..< start +
length]

Result:

small & medium sized String copies faster
2018-10-30 21:01:19 +00:00
Johannes Weiss ee9a229a33 String decoding: go through URBP directly instead of Slice (#639)
Motivation:

String decoding can work faster when going through URBP directly instead
of a Slice of a URBP. The reason is that if the String has the utf8
represenation stored directly it'll just be a memcpy (+ validation).

Modifications:

go through URBP when decoding a String using String(decoding:as:)

Result:

faster if the String utf8 representation happens to be available
2018-10-30 08:20:21 +00:00
Norio Nomura 287d50c373 CONNECT method should not have request body (#637)
* CONNECT method has no request body

### Motivation:

[rfc7231](https://tools.ietf.org/html/rfc7231#section-4.3.6) said:
> A payload within a `CONNECT` request message has no defined semantics;
   sending a payload body on a `CONNECT` request might cause some existing
   implementations to reject the request.

### Modifications:

Change `HTTPMethod.CONNECT.hasRequestBody` to returns `.no`.

### Result:

`HTTPRequestEncoder` does not generate chunked body for `CONNECT` method.

* Change `HTTPMethod.CONNECT.hasRequestBody` to returns `.unlikely`
2018-10-29 22:54:52 -07:00
Johannes Weiss 6e6c68c1b2 verify that IOThreadPools get shut down (#635)
Motivation:

Not shutting down IOThreadPools leads to thread leaks so we should
verify they're actually shut down.

Modifications:

add a check that we're shutting down

Result:

fewer thread leaks
2018-10-29 09:51:31 -07:00
Johannes Weiss 176dd6e856 fix potential deadlock in BlockingIOThreadPool (#634)
Motivation:

BlockingIOThreadPool used to call out with a lock held and on top of
that on the wrong thread...

Modifications:

Make BlockingIOThreadPool call out on the supplied `queue`

Result:

fewer deadlock and surprises
2018-10-19 16:52:51 +01:00
Johannes Weiss 9bae007a15 use umbrella headers for C modules (#636)
Motivation:

SwiftPM only generates fully usable clang modules for C modules if they
have umbrella headers. Therefore, adding a generated NIO.xcodeproj to
another Xcode project as a sub-project never worked.
To make a header an umbrella header you need to name them _exactly_ like
the module.

Modifications:

give each C module an umbrella header

Result:

generated NIO Xcode projects can be used as sub-projects.
2018-10-19 14:49:43 +01:00
Johannes Weiss ed28803a78 update http_parser.c (#627)
Motivation:

There was an outstanding change in http_parser.c that we did not yet
have in our copy.

Modifications:

run the update http parser script and add changes as well as a test case
designed to hit the change.

Result:

ship the latest and greatest
2018-10-16 10:27:44 +01:00
Gopal Sharma 8d7d4cb9ca Fix minor typo (#626) 2018-09-30 08:19:08 +01:00
Albert Aleksieiev a59293cbcd add Android support (#609)
### Motivation:

make swift-nio compatible with Android, and pass all test in Android.

### Modifications:

* add missed `ifaddrs` implementation
* add missed thread related functions - `CNIOLinux_pthread_getname_np`, `CNIOLinux_pthread_setaffinity_np` and `CNIOLinux_pthread_getaffinity_np`
* fix types incositency between Linux and Android, e.g. `Epoll.swift` class
* make bytes counter explicitely 64bits to avoid overflow on 32bit, in `PendingWritesManager.swift` and `PendingDatagramWritesState.swift`
* fix issue with Unix domain socket, first byte need to be zero in Android
* several incosistency fixes between Linux and Android api.

### Result:

now swift-nio works on Android. All tests passed!
2018-09-27 11:32:43 +01:00
Cory Benfield c73eb57694 Support UDP multicast. (#618)
Motivation:

A large number of very useful protocols are implemented using multicast
with UDP. As a result, it would be helpful to add support for joining and
leaving IP multicast groups using SwiftNIO.

Modifications:

- Defines a MulticastChannel protocol for channels that support joining and
  leaving multicast groups.
- Adds an implementation of MulticastChannel to DatagramChannel.
- Adds a interfaceIndex property to NIONetworkInterface.
- Adds if_nametoindex to the Posix enum.
- Adds a isMulticast computed property to SocketAddress
- Adds a demo multicast chat application.
- Add a number of multicast-related socket options to SocketOptionProvider.

Result:

NIO users will be able to write channels that handle multicast UDP.
2018-09-24 10:24:09 +01:00