ServerConnection.swift 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright 2024, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import GRPCCore
  17. import NIOCore
  18. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  19. public enum ServerConnection {
  20. public enum Stream {
  21. @_spi(Package)
  22. public struct Outbound: ClosableRPCWriterProtocol {
  23. public typealias Element = RPCResponsePart
  24. private let responseWriter: NIOAsyncChannelOutboundWriter<RPCResponsePart>
  25. private let http2Stream: NIOAsyncChannel<RPCRequestPart, RPCResponsePart>
  26. public init(
  27. responseWriter: NIOAsyncChannelOutboundWriter<RPCResponsePart>,
  28. http2Stream: NIOAsyncChannel<RPCRequestPart, RPCResponsePart>
  29. ) {
  30. self.responseWriter = responseWriter
  31. self.http2Stream = http2Stream
  32. }
  33. public func write(_ element: RPCResponsePart) async throws {
  34. try await self.responseWriter.write(element)
  35. }
  36. public func write(contentsOf elements: some Sequence<Self.Element>) async throws {
  37. try await self.responseWriter.write(contentsOf: elements)
  38. }
  39. public func finish() {
  40. self.responseWriter.finish()
  41. }
  42. public func finish(throwing error: any Error) {
  43. // Fire the error inbound; this fails the inbound writer.
  44. self.http2Stream.channel.pipeline.fireErrorCaught(error)
  45. }
  46. }
  47. }
  48. }