Commit Graph
37 Commits
Author SHA1 Message Date
Johannes Weiss 8c5eee85fc remove unnecessary UInt8 pointer binds (#524)
Motivation:

The bytes that a Unsafe(Mutable)RawBufferPointers points to can be
accessed directly as `UInt8`s. We didn't know that so we bind
`ByteBuffer`s memory to `UInt8` so that we can access it as
`UnsafePointer<UInt8>` using `assumingMemoryBound(to: UInt8.self)` in
many places. Most of the time that is unnecessarily awkward and
unnecessary.

Modifications:

- remove some easy instances where we access an
  `UnsafeMutableBufferPointer` as `UnsafeBufferPointer<UInt8>` just to
  read some `UInt8`s.

Result:

less awkward looking code
2018-07-23 19:11:19 +01:00
Cory Benfield 799060071a Cleanup wrapErrorIsNullReturnCall. (#511)
Motivation:

Given that wrapErrorIsNullReturnCall exists purely to wrap syscalls that
return NULL pointers on errors, there is no point allowing the return
value to be nil. Additionally, it's currently weirdly specific about the
return pointer type, so we can make the function generic.

Modifications:

- Make the return value of wrapErrorIsNullReturnCall generic.
- Make the return value of wrapErrorIsNullReturnCall non-optional.

Result:

Easier to use wrapErrorIsNullReturnCall and any matching function
2018-07-12 12:21:19 +01:00
Alex Blewitt 1fa9f15aca Explicitly define constants through Posix and import errno.h (#400)
Explicitly define constants through Posix and import errno.h

Motivation:

We implicitly depend on errno's ENOENT in Linux.swift, but don't explicitly import errno.h.

Similarly, in some cases the compiler can be confused as to where SHUT_* come from, so make them an exported value from Posix instead.

Result:

Compiles successfully on Linux and Darwin.
2018-05-10 20:12:33 +02:00
Norman Maurer b1f948a115 Set hints for getaddrinfo to restrict results (#221)
Motivation:

We only use our GetaddrinfoResolver for SocketChannels (TCP stream channels), so we should set some hints for getaddrinfo.

Modifications:

Set proto and socktype hints.

Result:

More correct usage of getaddrinfo. Fixes https://github.com/apple/swift-nio/issues/201.
2018-03-28 10:49:05 +02:00
Tim a0b7f646fb Fixes build issue on 4.1 (#229)
Motivation:

Resolves #228

Modifications:

Remove type information in empty closure fixed build warning with compiler suggestion

Result:

No API changes, no issues building on 4.1!
2018-03-23 16:28:43 +00:00
Gopal Sharma 61f03a7a53 Annotate global constants that refer to C functions with @convention(c) (#223)
Motivation:

This fixes https://github.com/apple/swift-nio/issues/206.

Also see: https://bugs.swift.org/browse/SR-7242.

Modifications:

Annotated global constants that refer to C functions with @convention(c), and added explcit types.

Results:

https://bugs.swift.org/browse/SR-7242 is worked around.
2018-03-23 09:37:51 +00:00
Norman Maurer 91d7058a36 Not retry calling close(...) when EINTR is reported. (#217)
Motivation:

Usually we retry the syscall when EINTR is reported. This is not safe in the case of close(...) as the file descriptor may already be closed. The problem arise if this was the case and we retry it and the file descriptor is already reused again. In this case we would close a wrong file descriptor.

See:
  - https://bugs.chromium.org/p/chromium/issues/detail?id=269623
  - https://lwn.net/Articles/576478/

Modifications:

Ignore EINTR on close(...) and just assume the FD was closed.

Result:

Not possible to close the wrong file descriptor when close(...) reports EINTR.
2018-03-22 18:16:00 +01:00
Bas Broek a095decbc2 Prefer if case over switch
Motivation:

There are quite a few `switch`es that cover just two cases. Explicitly state this via an `if case` + `else` structure.

Modifications:

Changes something like:

switch a {
case .b:
  return c
default:
  return d
}

to

if case .b = a {
  return c
} else {
  return d
}

Result:

This should prevent misinterpretation and thus potential bugs in the future.
2018-03-19 14:45:32 +01:00
Bas Broek b180113d81 Improve switch readability
Motivation:
This changes two things:
- it removes the negation of non-used associated types (the removal of `(_)`).
- it prefers exhausting `switch`es over `default`ing.

This change does not impact bahavior; it is pure refactoring.

Modifications:

Omitting needless `(_)`'s and explicitly exhausting switches.

Result:

This change is pure refactoring.
2018-03-19 14:43:38 +01:00
Helge Heß b06b712085 No need to wrap fatalError within code
Motivation:

No need for the "self-invoking function" here.

Modifications:

Call fatalError directly.

Result:

18 bytes are saved in the source code. No functional
difference.
2018-03-19 09:52:44 +01:00
Helge Heß 94718bcaa8 Wrap sendfile() in #if's that match the import
Motivation:

NIO doesn't compile on iOS. Which I'd like to do.

Modifications:

The #if's used to protect the syscall do not match
the #if's used to import.
Specifically the Darwin branch only applied to
macOS, not watchOS or homePodOS.

Result:

Hopefully will compile on iOS.
2018-03-19 09:52:44 +01:00
Norman Maurer 95a6e44e93 Remove return when possible. (#122)
Motivation:

We can cleanup the code by removing return keywords in closures sometimes.

Modifications:

Remove return when possible.

Result:

Cleaner code.
2018-03-11 22:47:29 +09:00
Cory Benfield f3f37af4ae Support enumerating network interfaces. (#47)
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.
2018-03-03 09:04:57 +09:00
adamnemecek 0bbed6d72f * removed trailing whitespace (#52) 2018-03-02 15:40:52 +09:00
Johannes Weiß 73a805c7f6 minor stylistic improvements (#36)
Motivation:

Sometimes we deviated from the style the Swift stdlib sets out for no
reason.

Modifications:

Fixed some stylistic deviations.

Result:

Looks more Swift-like.
2018-02-26 15:52:49 +00:00
Johannes Weiss 68724be6e6 fix Channel thread-safety (local/remoteAddress)
Motivation:

There were a couple of places in the Channel implementations that just
weren't thread-safe at all, namely:

- localAddress
- remoetAddress
- parent

those are now fixed. I also re-ordered the code so it should be easier
to maintain in the future (the `// MARK` markers were totally
incorrect).

Modifications:

- made Channel.{local,remote}Address return a future
- made Channel.parent a `let`
- unified more of `SocketChannel` and `DatagramSocketChannel`
- fixed the `// MARK`s by reordering code
- annotated methods/properties that are in the Channel API and need to
be thread-safe

Result:

slightly more thread-safety :)
2018-02-20 15:18:51 +00:00
Cory Benfield ab9427bdd4 Initial UDP support 2018-01-16 17:00:09 +00:00
Johannes Weiß 5cb13b21e6 make FileRegion a value type and behave much more like ByteBuffer
Motivation:

Previously `FileRegion` was a special snow flake, a bit like
`ByteBuffer` but also totally different. That caused surprise and made
`PendingWritesManager` even harder. It was also used to manage the
lifetime of a file descriptor but only sort of.

Modifications:

We now have a `FileHandle` which is a one-to-one mapping with a file
descriptor and its lifetime must be managed appropriately.

Result:

hopefully less bugs and fd leaks.
2018-02-06 16:48:35 +00:00
Cory Benfield 67bb71e44b Add a pending datagram writes manager
This change adds support for doing datagram-style writes. It's not currently
hooked up to anything, but it will be sometime soon.
2018-02-14 15:32:31 +00:00
Johannes Weiß e3ea0d7f5d small cleanups 2018-02-05 17:47:04 +00:00
Cory Benfield 9b0b0f9b51 Add support for SOCK_DGRAM 2018-02-08 14:58:10 +00:00
Norman Maurer 17b5fd58e4 Use aliases to reduce duplication and make it easier to maintain.
* Use aliases to reduce duplication and make it easier to maintain.

Motivation:

We used many #if #else blocks which made the code very clumby.

Modifications:

Use aliases and define these in one place.

Result:

Easier to maintain code.

* Comments

* Comments
2018-02-09 16:56:52 +01:00
Johannes Weiss 755ea5aedc non-blocking file IO 2018-02-07 17:25:45 +00:00
Norman Maurer 4d8cf1ff22 Add support for closing only the output / input side of the Channel. 2017-12-28 13:40:31 +01:00
Norman Maurer 2e2a82b27e Try to drain inbound data on write error.
Motivation:

To ensure we not leave any unread data on the socket we should try to read one last time (if autoread is enabled).

Modifications:

- Depending on if autoread is enabled or not we will try to read one more time
- Guard against re-entry of flushNow0()
- Correctly handle SIGPIPE in all cases
- Add testcase

Result:

Drain data from socket on write error if needed.
2017-12-27 08:10:53 +01:00
Cory Benfield a2bb047224 Add bindings for {send,recv}mmsg. 2018-01-17 09:18:28 +00:00
Johannes Weiss 8b74d21562 more robust tempfiles for SSL trust bundle 2018-01-10 17:19:02 +00:00
Johannes Weiss 7996d12bed make NIO cross-compilable 2018-01-10 11:16:07 +00:00
Johannes Weiß c79c5ba364 re-export Darwin/Glibc and reduce number of #if os(...) 2018-01-09 15:08:17 +00:00
Johannes Weiss 215620d7a6 syscall wrapper: don't do an allocation for every failed syscall 2017-12-07 15:37:12 +00:00
Norman Maurer 5475ba069a Add docs for IO and Selectable and also rename error parameter name to errorCode to ensure we not capture errno by mistake.
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.
2017-12-05 15:44:50 +01:00
Norman Maurer 68a475b37a Rename IOError.errno to IOError.errnoCode as errno is always in scope 2017-11-17 15:45:12 +01:00
Norman Maurer 854aea060e Not creating IOError for EWOULDBLOCK
Motivation:

Creating an IOError is not "cheap" as we use String formatting to do so. In cases of EWOULDBLOCK we will never use the IOError at the end so we should not even create one.

Modifications:

Handle EWOUDLBOCK without creating and IOError

Result:

Less overhead when a syscall will fail because it would block
2017-11-17 13:06:29 +01:00
Norman Maurer f6d9bcc701 Make check for blacklisted errno an assert
Motivation:

Checking for blacklisted errno values should be an assert and not a precondition as this would signal a bug in our code itself and not something bad the user was doing. Also this will make the code smaller when compiling in release mode.

Modifications:

Change the blacklist check to be an assert.

Result:

Only check for blacklisted errno when asserts are enabled.
2017-11-17 08:20:50 +01:00
Johannes Weiß 936e812b32 make system call wrapper fast & test it 2017-11-16 15:42:03 +00:00
Norman Maurer e67fc995cf Remove external depencies
Motivation:

We had a few external dependencies that were mainly used to include header files.

Modifications:

Add extra modules that will be used to export these headers.

Result:

Less external dependencies.
2017-11-16 15:20:13 +01:00
Norman Maurer ba35fcb5a5 Move all syscalls that also need to handle errno in an extra file and ensure we not get any problems with invalid errno handling
* Move all syscalls that also need to handle errno in an extra file and ensure we not get any problems with invalid errno handling

Motivation:

Because it is possible ARC cause an syscall and so set the errno value we need to ensure no ARC can ever happen before we were able to read the errno value that was caused by an syscall we made.

Modifications:

Move all syscalls to an extra file (System.swift) and ensure all exposed methods are marked with `@inline(never)` and not capture any classes. This ensures no ARC can ever happen.

Result:

Be able to safely read errno if needed.

* Comments
2017-11-16 16:41:19 +01:00