TransportKind.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright 2025, 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 GRPCNIOTransportHTTP2
  18. enum TransportKind: CaseIterable, Hashable, Sendable {
  19. case posix
  20. #if canImport(Network)
  21. case transportServices
  22. #endif
  23. static var supported: [Self] {
  24. Self.allCases
  25. }
  26. }
  27. enum NIOClientTransport: ClientTransport {
  28. case posix(HTTP2ClientTransport.Posix)
  29. #if canImport(Network)
  30. case transportServices(HTTP2ClientTransport.TransportServices)
  31. #endif
  32. init(_ transport: HTTP2ClientTransport.Posix) {
  33. self = .posix(transport)
  34. }
  35. #if canImport(Network)
  36. init(_ transport: HTTP2ClientTransport.TransportServices) {
  37. self = .transportServices(transport)
  38. }
  39. #endif
  40. typealias Bytes = GRPCNIOTransportBytes
  41. var retryThrottle: GRPCCore.RetryThrottle? {
  42. switch self {
  43. case .posix(let transport):
  44. return transport.retryThrottle
  45. #if canImport(Network)
  46. case .transportServices(let transport):
  47. return transport.retryThrottle
  48. #endif
  49. }
  50. }
  51. func connect() async throws {
  52. switch self {
  53. case .posix(let transport):
  54. try await transport.connect()
  55. #if canImport(Network)
  56. case .transportServices(let transport):
  57. try await transport.connect()
  58. #endif
  59. }
  60. }
  61. func beginGracefulShutdown() {
  62. switch self {
  63. case .posix(let transport):
  64. transport.beginGracefulShutdown()
  65. #if canImport(Network)
  66. case .transportServices(let transport):
  67. transport.beginGracefulShutdown()
  68. #endif
  69. }
  70. }
  71. func withStream<T: Sendable>(
  72. descriptor: MethodDescriptor,
  73. options: CallOptions,
  74. _ closure: (_ stream: RPCStream<Inbound, Outbound>, _ context: ClientContext) async throws -> T
  75. ) async throws -> T {
  76. switch self {
  77. case .posix(let transport):
  78. return try await transport.withStream(descriptor: descriptor, options: options, closure)
  79. #if canImport(Network)
  80. case .transportServices(let transport):
  81. return try await transport.withStream(descriptor: descriptor, options: options, closure)
  82. #endif
  83. }
  84. }
  85. func config(forMethod descriptor: GRPCCore.MethodDescriptor) -> GRPCCore.MethodConfig? {
  86. switch self {
  87. case .posix(let transport):
  88. return transport.config(forMethod: descriptor)
  89. #if canImport(Network)
  90. case .transportServices(let transport):
  91. return transport.config(forMethod: descriptor)
  92. #endif
  93. }
  94. }
  95. }
  96. enum NIOServerTransport: ServerTransport, ListeningServerTransport {
  97. case posix(HTTP2ServerTransport.Posix)
  98. #if canImport(Network)
  99. case transportServices(HTTP2ServerTransport.TransportServices)
  100. #endif
  101. init(_ transport: HTTP2ServerTransport.Posix) {
  102. self = .posix(transport)
  103. }
  104. #if canImport(Network)
  105. init(_ transport: HTTP2ServerTransport.TransportServices) {
  106. self = .transportServices(transport)
  107. }
  108. #endif
  109. typealias Bytes = GRPCNIOTransportBytes
  110. var listeningAddress: GRPCNIOTransportCore.SocketAddress {
  111. get async throws {
  112. switch self {
  113. case .posix(let transport):
  114. try await transport.listeningAddress
  115. #if canImport(Network)
  116. case .transportServices(let transport):
  117. try await transport.listeningAddress
  118. #endif
  119. }
  120. }
  121. }
  122. func listen(
  123. streamHandler: @escaping @Sendable (
  124. _ stream: RPCStream<Inbound, Outbound>,
  125. _ context: ServerContext
  126. ) async -> Void
  127. ) async throws {
  128. switch self {
  129. case .posix(let transport):
  130. try await transport.listen(streamHandler: streamHandler)
  131. #if canImport(Network)
  132. case .transportServices(let transport):
  133. try await transport.listen(streamHandler: streamHandler)
  134. #endif
  135. }
  136. }
  137. func beginGracefulShutdown() {
  138. switch self {
  139. case .posix(let transport):
  140. transport.beginGracefulShutdown()
  141. #if canImport(Network)
  142. case .transportServices(let transport):
  143. transport.beginGracefulShutdown()
  144. #endif
  145. }
  146. }
  147. }