NIOClientBootstrap+SocketAddress.swift 2.0 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. internal import GRPCCore
  17. internal import GRPCHTTP2Core
  18. internal import NIOCore
  19. internal import NIOPosix
  20. extension ClientBootstrap {
  21. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  22. func connect<Result: Sendable>(
  23. to address: GRPCHTTP2Core.SocketAddress,
  24. _ configure: @Sendable @escaping (any Channel) -> EventLoopFuture<Result>
  25. ) async throws -> Result {
  26. if let ipv4 = address.ipv4 {
  27. return try await self.connect(to: NIOCore.SocketAddress(ipv4), channelInitializer: configure)
  28. } else if let ipv6 = address.ipv6 {
  29. return try await self.connect(to: NIOCore.SocketAddress(ipv6), channelInitializer: configure)
  30. } else if let uds = address.unixDomainSocket {
  31. return try await self.connect(to: NIOCore.SocketAddress(uds), channelInitializer: configure)
  32. } else if let vsock = address.virtualSocket {
  33. return try await self.connect(to: VsockAddress(vsock), channelInitializer: configure)
  34. } else {
  35. throw RuntimeError(
  36. code: .transportError,
  37. message: """
  38. Unhandled socket address '\(address)', this is a gRPC Swift bug. Please file an issue \
  39. against the project.
  40. """
  41. )
  42. }
  43. }
  44. }
  45. extension NIOPosix.VsockAddress {
  46. init(_ address: GRPCHTTP2Core.SocketAddress.VirtualSocket) {
  47. self.init(
  48. cid: ContextID(rawValue: address.contextID.rawValue),
  49. port: Port(rawValue: address.port.rawValue)
  50. )
  51. }
  52. }