NIOClientBootstrap+SocketAddress.swift 2.0 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. internal import GRPCCore
  17. internal import GRPCNIOTransportCore
  18. internal import NIOCore
  19. internal import NIOPosix
  20. @available(gRPCSwiftNIOTransport 2.0, *)
  21. extension ClientBootstrap {
  22. func connect<Result: Sendable>(
  23. to address: GRPCNIOTransportCore.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. @available(gRPCSwiftNIOTransport 2.0, *)
  46. extension NIOPosix.VsockAddress {
  47. init(_ address: GRPCNIOTransportCore.SocketAddress.VirtualSocket) {
  48. self.init(
  49. cid: ContextID(rawValue: address.contextID.rawValue),
  50. port: Port(rawValue: address.port.rawValue)
  51. )
  52. }
  53. }