mirror of
https://github.com/apple/swift-nio
synced 2026-11-22 04:30:41 +00:00
Document when channel.remoteAddress field can be nil (#3456)
Added extra documentation to `channel.remoteAddress` field to capture a scenario where this field might be `nil` ### Motivation: It is somewhat common for users to have code like `channel.remoteAddress!` in their implementation, as it is a reasonable assumption to think a socket connection will have an associated remote address. However, in at least one known situation this might not be the case. When that happens, user's code might crash due to the force unwrap of the optional field. ### Modifications: Introduced more documentation to make it clear that users should be prepared to handle the `nil` scenario. ### Result: Less frequent mishandling of `channel.remoteAddress`.
This commit is contained in:
1 parent
2bc71a6aff
commit
b60d141920
4 files changed
+14
No files matched your search
@@ -82,6 +82,8 @@ let channel = try { () -> Channel in
|
||||
}
|
||||
}()
|
||||
|
||||
// In production code, you should check if `channel.remoteAddress` is actually
|
||||
// present, as in rare situations it can be `nil`.
|
||||
print("ChatClient connected to ChatServer: \(channel.remoteAddress!), happy chatting\n. Press ^D to exit.")
|
||||
|
||||
while let line = readLine(strippingNewline: false) {
|
||||
|
||||
@@ -54,6 +54,8 @@ final class ChatHandler: ChannelInboundHandler {
|
||||
private var channels: [ObjectIdentifier: Channel] = [:]
|
||||
|
||||
public func channelActive(context: ChannelHandlerContext) {
|
||||
// In production code, you should check if `context.remoteAddress` is actually
|
||||
// present, as in rare situations it can be `nil`.
|
||||
let remoteAddress = context.remoteAddress!
|
||||
let channel = context.channel
|
||||
self.channelsSyncQueue.async {
|
||||
@@ -92,6 +94,9 @@ final class ChatHandler: ChannelInboundHandler {
|
||||
|
||||
// 64 should be good enough for the ipaddress
|
||||
var buffer = context.channel.allocator.buffer(capacity: read.readableBytes + 64)
|
||||
|
||||
// In production code, you should check if `context.remoteAddress` is actually
|
||||
// present, as in rare situations it can be `nil`.
|
||||
buffer.writeString("(\(context.remoteAddress!)) - ")
|
||||
buffer.writeBuffer(&read)
|
||||
self.channelsSyncQueue.async { [buffer] in
|
||||
|
||||
@@ -122,6 +122,11 @@ public protocol Channel: AnyObject, ChannelOutboundInvoker, _NIOPreconcurrencySe
|
||||
var localAddress: SocketAddress? { get }
|
||||
|
||||
/// The remote peer's `SocketAddress`.
|
||||
///
|
||||
/// If we end up accepting an already-closed connection, the kernel can end up in a place
|
||||
/// where it has no remote address to give us. In this situation, `remoteAddress` will be
|
||||
/// `nil`. It can also be `nil` in cases where it isn't representable in SocketAddress, e.g. if
|
||||
/// we're talking over a vsock.
|
||||
var remoteAddress: SocketAddress? { get }
|
||||
|
||||
/// `Channel`s are hierarchical and might have a parent `Channel`. `Channel` hierarchies are in use for certain
|
||||
|
||||
@@ -23,6 +23,8 @@ private final class HTTPEchoHandler: ChannelInboundHandler {
|
||||
public typealias OutboundOut = HTTPClientRequestPart
|
||||
|
||||
public func channelActive(context: ChannelHandlerContext) {
|
||||
// In production code, you should check if `context.remoteAddress` is actually
|
||||
// present, as in rare situations it can be `nil`.
|
||||
print("Client connected to \(context.remoteAddress!)")
|
||||
|
||||
// We are connected. It's time to send the message to the server to initialize the ping-pong sequence.
|
||||
|
||||
Reference in new issue
Block a user