Arg.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright 2016, 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. #if SWIFT_PACKAGE
  17. import CgRPC
  18. #endif
  19. import Foundation // for String.Encoding
  20. public enum Arg {
  21. /// Default authority to pass if none specified on call construction.
  22. case defaultAuthority(String)
  23. /// Primary user agent. Goes at the start of the user-agent metadata sent
  24. /// on each request.
  25. case primaryUserAgent(String)
  26. /// Secondary user agent. Goes at the end of the user-agent metadata sent
  27. /// on each request.
  28. case secondaryUserAgent(String)
  29. /// After a duration of this time, the client/server pings its peer to see
  30. /// if the transport is still alive.
  31. case keepAliveTime(TimeInterval)
  32. /// After waiting for a duration of this time, if the keepalive ping sender does
  33. /// not receive the ping ack, it will close the transport.
  34. case keepAliveTimeout(TimeInterval)
  35. /// Is it permissible to send keepalive pings without any outstanding streams?
  36. case keepAlivePermitWithoutCalls(Bool)
  37. /// The time between the first and second connection attempts.
  38. case reconnectBackoffInitial(TimeInterval)
  39. /// The minimum time between subsequent connection attempts.
  40. case reconnectBackoffMin(TimeInterval)
  41. /// The maximum time between subsequent connection attempts.
  42. case reconnectBackoffMax(TimeInterval)
  43. /// Should we allow receipt of true-binary data on http2 connections?
  44. /// Defaults to on (true)
  45. case http2EnableTrueBinary(Bool)
  46. /// Minimum time between sending successive ping frames without receiving
  47. /// any data frame.
  48. case http2MinSentPingInterval(TimeInterval)
  49. /// Number of pings before needing to send a data frame or header frame.
  50. /// `0` indicates that an infinite number of pings can be sent without
  51. /// sending a data frame or header frame.
  52. case http2MaxPingsWithoutData(UInt)
  53. /// This *should* be used for testing only.
  54. /// Override the target name used for SSL host name checking using this
  55. /// channel argument. If this argument is not specified, the name used
  56. /// for SSL host name checking will be the target parameter (assuming that the
  57. /// secure channel is an SSL channel). If this parameter is specified and the
  58. /// underlying is not an SSL channel, it will just be ignored.
  59. case sslTargetNameOverride(String)
  60. }
  61. extension Arg {
  62. func toCArg() -> grpc_arg {
  63. switch self {
  64. case let .defaultAuthority(value):
  65. return arg("grpc.default_authority", value: value)
  66. case let .primaryUserAgent(value):
  67. return arg("grpc.primary_user_agent", value: value)
  68. case let .secondaryUserAgent(value):
  69. return arg("grpc.secondary_user_agent", value: value)
  70. case let .keepAliveTime(value):
  71. return arg("grpc.keepalive_time_ms", value: value * 1_000)
  72. case let .keepAliveTimeout(value):
  73. return arg("grpc.keepalive_timeout_ms", value: value * 1_000)
  74. case let .keepAlivePermitWithoutCalls(value):
  75. return arg("grpc.keepalive_permit_without_calls", value: value)
  76. case let .reconnectBackoffMin(value):
  77. return arg("grpc.min_reconnect_backoff_ms", value: value * 1_000)
  78. case let .reconnectBackoffMax(value):
  79. return arg("grpc.max_reconnect_backoff_ms", value: value * 1_000)
  80. case let .reconnectBackoffInitial(value):
  81. return arg("grpc.initial_reconnect_backoff_ms", value: value * 1_000)
  82. case let .http2EnableTrueBinary(value):
  83. return arg("grpc.http2.true_binary", value: value)
  84. case let .http2MinSentPingInterval(value):
  85. return arg("grpc.http2.min_time_between_pings_ms", value: value * 1_000)
  86. case let .http2MaxPingsWithoutData(value):
  87. return arg("grpc.http2.max_pings_without_data", value: value)
  88. case let .sslTargetNameOverride(value):
  89. return arg("grpc.ssl_target_name_override", value: value)
  90. }
  91. }
  92. private func arg(_ key: String, value: String) -> grpc_arg {
  93. var arg = grpc_arg()
  94. arg.key = gpr_strdup(key)
  95. arg.type = GRPC_ARG_STRING
  96. arg.value.string = gpr_strdup(value)
  97. return arg
  98. }
  99. private func arg(_ key: String, value: Bool) -> grpc_arg {
  100. return arg(key, value: Int32(value ? 1 : 0))
  101. }
  102. private func arg(_ key: String, value: Double) -> grpc_arg {
  103. return arg(key, value: Int32(value))
  104. }
  105. private func arg(_ key: String, value: UInt) -> grpc_arg {
  106. return arg(key, value: Int32(value))
  107. }
  108. private func arg(_ key: String, value: Int) -> grpc_arg {
  109. return arg(key, value: Int32(value))
  110. }
  111. private func arg(_ key: String, value: Int32) -> grpc_arg {
  112. var arg = grpc_arg()
  113. arg.key = gpr_strdup(key)
  114. arg.type = GRPC_ARG_INTEGER
  115. arg.value.integer = value
  116. return arg
  117. }
  118. }