NIOSocketAddress+GRPCSocketAddress.swift 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. extension GRPCHTTP2Core.SocketAddress {
  18. package init(_ nioSocketAddress: NIOCore.SocketAddress) {
  19. switch nioSocketAddress {
  20. case .v4(let address):
  21. self = .ipv4(
  22. host: address.host,
  23. port: nioSocketAddress.port ?? 0
  24. )
  25. case .v6(let address):
  26. self = .ipv6(
  27. host: address.host,
  28. port: nioSocketAddress.port ?? 0
  29. )
  30. case .unixDomainSocket:
  31. self = .unixDomainSocket(path: nioSocketAddress.pathname ?? "")
  32. }
  33. }
  34. }
  35. extension NIOCore.SocketAddress {
  36. package init(_ address: GRPCHTTP2Core.SocketAddress.IPv4) throws {
  37. try self.init(ipAddress: address.host, port: address.port)
  38. }
  39. package init(_ address: GRPCHTTP2Core.SocketAddress.IPv6) throws {
  40. try self.init(ipAddress: address.host, port: address.port)
  41. }
  42. package init(_ address: GRPCHTTP2Core.SocketAddress.UnixDomainSocket) throws {
  43. try self.init(unixDomainSocketPath: address.path)
  44. }
  45. }