diff --git a/Sources/NIO/Channel.swift b/Sources/NIO/Channel.swift index 57829391f..b76f44711 100644 --- a/Sources/NIO/Channel.swift +++ b/Sources/NIO/Channel.swift @@ -66,7 +66,7 @@ public protocol Channel : class, ChannelOutboundInvoker { func setOption(option: T, value: T.OptionType) -> EventLoopFuture /// Get the value of `option` for this `Channel`. - func getOption(option: T) throws -> T.OptionType + func getOption(option: T) -> EventLoopFuture /// Returns if this `Channel` is currently writable. var isWritable: Bool { get } diff --git a/Sources/NIO/DeadChannel.swift b/Sources/NIO/DeadChannel.swift index 22092d016..7ef6e8201 100644 --- a/Sources/NIO/DeadChannel.swift +++ b/Sources/NIO/DeadChannel.swift @@ -103,8 +103,8 @@ internal final class DeadChannel: Channel { return EventLoopFuture(eventLoop: self.pipeline.eventLoop, error: ChannelError.ioOnClosedChannel, file: #file, line: #line) } - func getOption(option: T) throws -> T.OptionType where T : ChannelOption { - throw ChannelError.ioOnClosedChannel + func getOption(option: T) -> EventLoopFuture where T : ChannelOption { + return eventLoop.newFailedFuture(error: ChannelError.ioOnClosedChannel) } let isWritable = false diff --git a/Sources/NIO/Embedded.swift b/Sources/NIO/Embedded.swift index d9496668b..aeee3824e 100644 --- a/Sources/NIO/Embedded.swift +++ b/Sources/NIO/Embedded.swift @@ -336,9 +336,9 @@ public class EmbeddedChannel : Channel { fatalError("no options supported") } - public func getOption(option: T) throws -> T.OptionType where T : ChannelOption { + public func getOption(option: T) -> EventLoopFuture where T : ChannelOption { if option is AutoReadOption { - return true as! T.OptionType + return self.eventLoop.newSucceededFuture(result: true as! T.OptionType) } fatalError("option \(option) not supported") } diff --git a/Sources/NIO/SocketChannel.swift b/Sources/NIO/SocketChannel.swift index e84f67838..bf1b178cc 100644 --- a/Sources/NIO/SocketChannel.swift +++ b/Sources/NIO/SocketChannel.swift @@ -305,11 +305,15 @@ class BaseSocketChannel : SelectableChannel, ChannelCore { } } - public final func getOption(option: T) throws -> T.OptionType { + public func getOption(option: T) -> EventLoopFuture where T : ChannelOption { if eventLoop.inEventLoop { - return try getOption0(option: option) + do { + return eventLoop.newSucceededFuture(result: try getOption0(option: option)) + } catch { + return eventLoop.newFailedFuture(error: error) + } } else { - return try eventLoop.submit{ try self.getOption0(option: option) }.wait() + return eventLoop.submit { try self.getOption0(option: option) } } } @@ -575,7 +579,8 @@ class BaseSocketChannel : SelectableChannel, ChannelCore { // ChannelError.eof is not something we want to fire through the pipeline as it just means the remote // peer closed / shutdown the connection. if let channelErr = err as? ChannelError, channelErr == ChannelError.eof { - if try! getOption(option: ChannelOptions.allowRemoteHalfClosure) { + // Directly call getOption0 as we are already on the EventLoop and so not need to create an extra future. + if try! getOption0(option: ChannelOptions.allowRemoteHalfClosure) { // If we want to allow half closure we will just mark the input side of the Channel // as closed. pipeline.fireChannelReadComplete0()