NIOSocketAddress+GRPCSocketAddress.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. private import GRPCCore
  17. package import NIOCore
  18. extension GRPCHTTP2Core.SocketAddress {
  19. package init(_ nioSocketAddress: NIOCore.SocketAddress) {
  20. switch nioSocketAddress {
  21. case .v4(let address):
  22. self = .ipv4(
  23. host: address.host,
  24. port: nioSocketAddress.port ?? 0
  25. )
  26. case .v6(let address):
  27. self = .ipv6(
  28. host: address.host,
  29. port: nioSocketAddress.port ?? 0
  30. )
  31. case .unixDomainSocket:
  32. self = .unixDomainSocket(path: nioSocketAddress.pathname ?? "")
  33. }
  34. }
  35. }
  36. extension NIOCore.SocketAddress {
  37. package init(_ socketAddress: GRPCHTTP2Core.SocketAddress) throws {
  38. if let ipv4 = socketAddress.ipv4 {
  39. self = try Self(ipv4)
  40. } else if let ipv6 = socketAddress.ipv6 {
  41. self = try Self(ipv6)
  42. } else if let unixDomainSocket = socketAddress.unixDomainSocket {
  43. self = try Self(unixDomainSocket)
  44. } else {
  45. throw RPCError(
  46. code: .internalError,
  47. message:
  48. "Unsupported mapping to NIOCore/SocketAddress for GRPCHTTP2Core/SocketAddress: \(socketAddress)."
  49. )
  50. }
  51. }
  52. package init(_ address: GRPCHTTP2Core.SocketAddress.IPv4) throws {
  53. try self.init(ipAddress: address.host, port: address.port)
  54. }
  55. package init(_ address: GRPCHTTP2Core.SocketAddress.IPv6) throws {
  56. try self.init(ipAddress: address.host, port: address.port)
  57. }
  58. package init(_ address: GRPCHTTP2Core.SocketAddress.UnixDomainSocket) throws {
  59. try self.init(unixDomainSocketPath: address.path)
  60. }
  61. }