ChannelArgument.swift 5.3 KB

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