mirror of
https://github.com/apple/swift-nio
synced 2026-11-23 04:34:23 +00:00
Motivation:
There are multiple sub-optimal ByteBuffer creation patterns that occur
in the wild. Most often they happen when people don't actually have
access to a `Channel` just want to "convert" a `String` into a
`ByteBuffer`. To do this, they are forced to type
var buffer = ByteBufferAllocator().buffer(capacity: string.utf8.count)
buffer.writeString(string)
Sometimes, they don't get the capacity calculation right or just put a
`0`.
Similar problems happen if NIO users want to cache a ByteBuffer in their
`ChannelHandler`. You will then find this code:
```swift
if self.buffer == nil {
self.buffer = receivedBuffer
} else {
var receivedBuffer = receivedBuffer
self.buffer!.writeBuffer(&receivedBuffer)
}
```
And lastly, sometimes people want to append one `ByteBuffer` to another
without mutating the appendee. That's also cumbersome because we only
support a mutable version of `writeBuffer`.
Modifications:
- add `ByteBuffer` convenience initialisers
- add convenience `writeBuffer` methods to `Optional<ByteBuffer>`
- add `writeBufferImmutable` which doesn't mutate the appendee.
Result:
More convenience.
Co-authored-by: Cory Benfield <lukasa@apple.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
NIOEchoClient
This sample application provides a simple echo client that will send a single line to an echo server and wait for a response. Invoke it using one of the following syntaxes:
swift run NIOEchoClient # Connects to a server on ::1, port 9999.
swift run NIOEchoClient 9899 # Connects to a server on ::1, port 9899
swift run NIOEchoClient /path/to/unix/socket # Connects to a server using the given UNIX socket
swift run NIOEchoClient echo.example.com 9899 # Connects to a server on echo.example.com:9899