|
| 1 | +#if compiler(>=5.4) && $AsyncAwait |
| 2 | +import RSocketCore |
| 3 | +import _Concurrency |
| 4 | +import NIO |
| 5 | +import _NIOConcurrency |
| 6 | + |
| 7 | +public protocol RSocket { |
| 8 | + func requestResponse(payload: Payload) async throws -> Payload |
| 9 | + func requestStream(payload: Payload) -> AsyncStreamSequence |
| 10 | +} |
| 11 | + |
| 12 | +public struct RequesterAdapter: RSocket { |
| 13 | + private let requester: RSocketCore.RSocket |
| 14 | + private let eventLoop = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
| 15 | + public init(requester: RSocketCore.RSocket) { |
| 16 | + self.requester = requester |
| 17 | + } |
| 18 | + public func requestResponse(payload: Payload) async throws -> Payload { |
| 19 | + struct RequestResponseOperator: UnidirectionalStream { |
| 20 | + var promise: EventLoopPromise<Payload> |
| 21 | + func onNext(_ payload: Payload, isCompletion: Bool) { |
| 22 | + assert(isCompletion) |
| 23 | + promise.succeed(payload) |
| 24 | + } |
| 25 | + |
| 26 | + func onComplete() { |
| 27 | + assertionFailure("request response does not support \(#function)") |
| 28 | + } |
| 29 | + |
| 30 | + func onRequestN(_ requestN: Int32) { |
| 31 | + assertionFailure("request response does not support \(#function)") |
| 32 | + } |
| 33 | + |
| 34 | + func onCancel() { |
| 35 | + promise.fail(Error.canceled(message: "onCancel")) |
| 36 | + } |
| 37 | + |
| 38 | + func onError(_ error: Error) { |
| 39 | + promise.fail(error) |
| 40 | + } |
| 41 | + |
| 42 | + func onExtension(extendedType: Int32, payload: Payload, canBeIgnored: Bool) { |
| 43 | + assertionFailure("request response does not support \(#function)") |
| 44 | + } |
| 45 | + } |
| 46 | + let promise = eventLoop.next().makePromise(of: Payload.self) |
| 47 | + let stream = RequestResponseOperator(promise: promise) |
| 48 | + _ = requester.requestResponse(payload: payload, responderStream: stream) |
| 49 | + return try await promise.futureResult.get() |
| 50 | + } |
| 51 | + |
| 52 | + public func requestStream(payload: Payload) -> AsyncStreamSequence { |
| 53 | + AsyncStreamSequence(payload: payload, requester: requester, eventLoop: eventLoop.next()) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +public struct AsyncStreamSequence: AsyncSequence { |
| 58 | + public typealias AsyncIterator = AsyncStreamIterator |
| 59 | + |
| 60 | + public typealias Element = Payload |
| 61 | + |
| 62 | + fileprivate init(payload: Payload, requester: RSocketCore.RSocket, eventLoop: EventLoop) { |
| 63 | + self.payload = payload |
| 64 | + self.requester = requester |
| 65 | + self.eventLoop = eventLoop |
| 66 | + } |
| 67 | + private var payload: Payload |
| 68 | + private var requester: RSocketCore.RSocket |
| 69 | + private var eventLoop: EventLoop |
| 70 | + public func makeAsyncIterator() -> AsyncStreamIterator { |
| 71 | + let stream = AsyncStreamIterator(eventLoop: eventLoop) |
| 72 | + stream.subscription = requester.stream(payload: payload, initialRequestN: 0, responderStream: stream) |
| 73 | + return stream |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +public final class AsyncStreamIterator: AsyncIteratorProtocol, UnidirectionalStream { |
| 78 | + fileprivate init( |
| 79 | + eventLoop: EventLoop |
| 80 | + ) { |
| 81 | + self.eventLoop = eventLoop |
| 82 | + } |
| 83 | + |
| 84 | + private enum Event { |
| 85 | + case next(Payload, isCompletion: Bool) |
| 86 | + case error(Error) |
| 87 | + case complete |
| 88 | + case cancel |
| 89 | + } |
| 90 | + private var eventLoop: EventLoop |
| 91 | + private var event: EventLoopPromise<Event>? = nil |
| 92 | + private var isCompleted: Bool = false |
| 93 | + fileprivate var subscription: Subscription! = nil |
| 94 | + public func onNext(_ payload: Payload, isCompletion: Bool) { |
| 95 | + eventLoop.execute { [self] in |
| 96 | + assert(event != nil) |
| 97 | + event?.succeed(.next(payload, isCompletion: isCompletion)) |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + public func onComplete() { |
| 103 | + eventLoop.execute { [self] in |
| 104 | + assert(event != nil) |
| 105 | + event?.succeed(.complete) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public func onRequestN(_ requestN: Int32) { |
| 110 | + assertionFailure("request response does not support \(#function)") |
| 111 | + } |
| 112 | + |
| 113 | + public func onCancel() { |
| 114 | + eventLoop.execute { [self] in |
| 115 | + assert(event != nil) |
| 116 | + event?.succeed(.cancel) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public func onError(_ error: Error) { |
| 121 | + eventLoop.execute { [self] in |
| 122 | + assert(event != nil) |
| 123 | + event?.succeed(.error(error)) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public func onExtension(extendedType: Int32, payload: Payload, canBeIgnored: Bool) { |
| 128 | + assertionFailure("request response does not support \(#function)") |
| 129 | + } |
| 130 | + public func next() async throws -> Payload? { |
| 131 | + let p = eventLoop.makePromise(of: Optional<Payload>.self) |
| 132 | + p.completeWithAsync { [self] in |
| 133 | + guard !isCompleted else { return nil } |
| 134 | + assert(event == nil) |
| 135 | + let promise = eventLoop.makePromise(of: Event.self) |
| 136 | + event = promise |
| 137 | + subscription.onRequestN(1) |
| 138 | + let result = try await promise.futureResult.get() |
| 139 | + event = nil |
| 140 | + switch result { |
| 141 | + case let .next(payload, isCompletion): |
| 142 | + self.isCompleted = isCompletion |
| 143 | + return payload |
| 144 | + case .complete: |
| 145 | + self.isCompleted = true |
| 146 | + return nil |
| 147 | + case .cancel: |
| 148 | + self.isCompleted = true |
| 149 | + return nil |
| 150 | + case let .error(error): |
| 151 | + self.isCompleted = true |
| 152 | + throw error |
| 153 | + } |
| 154 | + } |
| 155 | + return try await p.futureResult.get() |
| 156 | + } |
| 157 | + deinit { |
| 158 | + subscription.onCancel() |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +public struct AsyncClient { |
| 163 | + private let coreClient: RSocketCore.CoreClient |
| 164 | + |
| 165 | + public var requester: RSocket { RequesterAdapter(requester: coreClient.requester) } |
| 166 | + |
| 167 | + public init(_ coreClient: RSocketCore.CoreClient) { |
| 168 | + self.coreClient = coreClient |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +extension RSocketCore.ClientBootstrap where Client == CoreClient, Responder == RSocketCore.RSocket { |
| 173 | + public func connect(to endpoint: Transport.Endpoint) async throws -> AsyncClient { |
| 174 | + AsyncClient(try await connect(to: endpoint, responder: nil).get()) |
| 175 | + } |
| 176 | +} |
| 177 | +#endif |
0 commit comments