|
| 1 | +#if compiler(>=5.4) |
| 2 | +extension EventLoopFuture { |
| 3 | + public func get() async throws -> Value { |
| 4 | + return await try Task.withUnsafeThrowingContinuation { cont in |
| 5 | + self.whenComplete { result in |
| 6 | + switch result { |
| 7 | + case .success(let value): |
| 8 | + cont.resume(returning: value) |
| 9 | + case .failure(let error): |
| 10 | + cont.resume(throwing: error) |
| 11 | + } |
| 12 | + } |
| 13 | + } |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +extension Channel { |
| 18 | + public func writeAndFlush<T>(_ any: T) async throws { |
| 19 | + await try self.writeAndFlush(any).get() |
| 20 | + } |
| 21 | + |
| 22 | + /// Set `option` to `value` on this `Channel`. |
| 23 | + public func setOption<Option: ChannelOption>(_ option: Option, value: Option.Value) async throws { |
| 24 | + await try self.setOption(option, value: value).get() |
| 25 | + } |
| 26 | + |
| 27 | + /// Get the value of `option` for this `Channel`. |
| 28 | + public func getOption<Option: ChannelOption>(_ option: Option) async throws -> Option.Value { |
| 29 | + return await try self.getOption(option).get() |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +extension ChannelOutboundInvoker { |
| 34 | + /// Register on an `EventLoop` and so have all its IO handled. |
| 35 | + /// |
| 36 | + /// - returns: the future which will be notified once the operation completes. |
| 37 | + public func register(file: StaticString = #file, line: UInt = #line) async throws { |
| 38 | + await try self.register(file: file, line: line) |
| 39 | + } |
| 40 | + |
| 41 | + /// Bind to a `SocketAddress`. |
| 42 | + /// - parameters: |
| 43 | + /// - to: the `SocketAddress` to which we should bind the `Channel`. |
| 44 | + /// - returns: the future which will be notified once the operation completes. |
| 45 | + public func bind(to address: SocketAddress, file: StaticString = #file, line: UInt = #line) async throws { |
| 46 | + await try self.bind(to: address, file: file, line: line) |
| 47 | + } |
| 48 | + |
| 49 | + /// Connect to a `SocketAddress`. |
| 50 | + /// - parameters: |
| 51 | + /// - to: the `SocketAddress` to which we should connect the `Channel`. |
| 52 | + /// - returns: the future which will be notified once the operation completes. |
| 53 | + public func connect(to address: SocketAddress, file: StaticString = #file, line: UInt = #line) async throws { |
| 54 | + await try self.connect(to: address, file: file, line: line) |
| 55 | + } |
| 56 | + |
| 57 | + /// Shortcut for calling `write` and `flush`. |
| 58 | + /// |
| 59 | + /// - parameters: |
| 60 | + /// - data: the data to write |
| 61 | + /// - returns: the future which will be notified once the `write` operation completes. |
| 62 | + public func writeAndFlush(_ data: NIOAny, file: StaticString = #file, line: UInt = #line) async throws { |
| 63 | + await try self.writeAndFlush(data, file: file, line: line) |
| 64 | + } |
| 65 | + |
| 66 | + /// Close the `Channel` and so the connection if one exists. |
| 67 | + /// |
| 68 | + /// - parameters: |
| 69 | + /// - mode: the `CloseMode` that is used |
| 70 | + /// - returns: the future which will be notified once the operation completes. |
| 71 | + public func close(mode: CloseMode = .all, file: StaticString = #file, line: UInt = #line) async throws { |
| 72 | + await try self.close(mode: mode, file: file, line: line) |
| 73 | + } |
| 74 | + |
| 75 | + /// Trigger a custom user outbound event which will flow through the `ChannelPipeline`. |
| 76 | + /// |
| 77 | + /// - parameters: |
| 78 | + /// - event: the event itself. |
| 79 | + /// - returns: the future which will be notified once the operation completes. |
| 80 | + public func triggerUserOutboundEvent(_ event: Any, file: StaticString = #file, line: UInt = #line) async throws { |
| 81 | + await try self.triggerUserOutboundEvent(event, file: file, line: line).get() |
| 82 | + } |
| 83 | +} |
| 84 | +#endif |
0 commit comments