ServerConnection.swift 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. package import GRPCCore
  17. package import NIOCore
  18. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  19. public enum ServerConnection {
  20. public enum Stream {
  21. package struct Outbound: ClosableRPCWriterProtocol {
  22. package typealias Element = RPCResponsePart
  23. private let responseWriter: NIOAsyncChannelOutboundWriter<RPCResponsePart>
  24. private let http2Stream: NIOAsyncChannel<RPCRequestPart, RPCResponsePart>
  25. package init(
  26. responseWriter: NIOAsyncChannelOutboundWriter<RPCResponsePart>,
  27. http2Stream: NIOAsyncChannel<RPCRequestPart, RPCResponsePart>
  28. ) {
  29. self.responseWriter = responseWriter
  30. self.http2Stream = http2Stream
  31. }
  32. package func write(_ element: RPCResponsePart) async throws {
  33. try await self.responseWriter.write(element)
  34. }
  35. package func write(contentsOf elements: some Sequence<Self.Element>) async throws {
  36. try await self.responseWriter.write(contentsOf: elements)
  37. }
  38. package func finish() {
  39. self.responseWriter.finish()
  40. }
  41. package func finish(throwing error: any Error) {
  42. // Fire the error inbound; this fails the inbound writer.
  43. self.http2Stream.channel.pipeline.fireErrorCaught(error)
  44. }
  45. }
  46. }
  47. }