NIOClientBootstrap+SocketAddress.swift 2.0 KB

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