NIOSocketAddress+GRPCSocketAddress.swift 1.6 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. import NIOCore
  17. @_spi(Package)
  18. extension GRPCHTTP2Core.SocketAddress {
  19. public 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. @_spi(Package)
  37. extension NIOCore.SocketAddress {
  38. public init(_ address: GRPCHTTP2Core.SocketAddress.IPv4) throws {
  39. try self.init(ipAddress: address.host, port: address.port)
  40. }
  41. public init(_ address: GRPCHTTP2Core.SocketAddress.IPv6) throws {
  42. try self.init(ipAddress: address.host, port: address.port)
  43. }
  44. public init(_ address: GRPCHTTP2Core.SocketAddress.UnixDomainSocket) throws {
  45. try self.init(unixDomainSocketPath: address.path)
  46. }
  47. }