ControlMessages.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 Foundation
  17. import GRPCCore
  18. @available(gRPCSwiftNIOTransport 2.0, *)
  19. struct ControlInput: Codable {
  20. /// Whether metadata should be echo'd back in the initial metadata.
  21. ///
  22. /// Ignored if the initial metadata has already been sent back to the
  23. /// client.
  24. ///
  25. /// Each header field name in the request headers will be prefixed with
  26. /// "echo-". For example the header field name "foo" will be returned
  27. /// as "echo-foo. Note that semicolons aren't valid in HTTP header field
  28. /// names (apart from pseudo headers). As such all semicolons should be
  29. /// removed (":path" should become "echo-path").
  30. var echoMetadataInHeaders: Bool = false
  31. /// Parameters for response messages.
  32. var payloadParameters: PayloadParameters = PayloadParameters(size: 0, content: 0)
  33. /// The number of response messages.
  34. var numberOfMessages: Int = 0
  35. /// The status code and message to use at the end of the RPC.
  36. ///
  37. /// If this is set then the RPC will be ended after `numberOfMessages`
  38. /// messages have been sent back to the client.
  39. var status: Status? = nil
  40. /// Whether the response should be trailers only.
  41. ///
  42. /// Ignored unless it's set on the first message on the stream. When set
  43. /// the RPC will be completed with a trailers-only response using the
  44. /// status code and message from 'status'. The request metadata will be
  45. /// included if 'echo_metadata_in_trailers' is set.
  46. ///
  47. /// If this is set then `numberOfMessages', 'messageParams', and
  48. /// `echoMetadataInHeaders` are ignored.
  49. var isTrailersOnly: Bool = false
  50. /// Whether metadata should be echo'd back in the trailing metadata.
  51. ///
  52. /// Ignored unless 'status' is set.
  53. ///
  54. /// Each header field name in the request headers will be prefixed with
  55. /// "echo-". For example the header field name "foo" will be returned
  56. /// as "echo-foo. Note that semicolons aren't valid in HTTP header field
  57. /// names (apart from pseudo headers). As such all semicolons should be
  58. /// removed (":path" should become "echo-path").
  59. var echoMetadataInTrailers: Bool = false
  60. /// Key-value pairs to add to the initial metadata.
  61. var initialMetadataToAdd: [String: String] = [:]
  62. /// Key-value pairs to add to the trailing metadata.
  63. var trailingMetadataToAdd: [String: String] = [:]
  64. struct Status: Codable {
  65. var code: GRPCCore.Status.Code
  66. var message: String
  67. init() {
  68. self.code = .ok
  69. self.message = ""
  70. }
  71. static func with(_ populate: (inout Self) -> Void) -> Self {
  72. var defaults = Self()
  73. populate(&defaults)
  74. return defaults
  75. }
  76. enum CodingKeys: CodingKey {
  77. case code
  78. case message
  79. }
  80. init(from decoder: any Decoder) throws {
  81. let container = try decoder.container(keyedBy: CodingKeys.self)
  82. let rawValue = try container.decode(Int.self, forKey: .code)
  83. self.code = GRPCCore.Status.Code(rawValue: rawValue) ?? .unknown
  84. self.message = try container.decode(String.self, forKey: .message)
  85. }
  86. func encode(to encoder: any Encoder) throws {
  87. var container = encoder.container(keyedBy: CodingKeys.self)
  88. try container.encode(self.code.rawValue, forKey: .code)
  89. try container.encode(self.message, forKey: .message)
  90. }
  91. }
  92. struct PayloadParameters: Codable {
  93. var size: Int
  94. var content: UInt8
  95. static func with(_ populate: (inout Self) -> Void) -> Self {
  96. var defaults = Self(size: 0, content: 0)
  97. populate(&defaults)
  98. return defaults
  99. }
  100. }
  101. static func with(_ populate: (inout Self) -> Void) -> Self {
  102. var defaults = Self()
  103. populate(&defaults)
  104. return defaults
  105. }
  106. }
  107. struct ControlOutput: Codable {
  108. var payload: Data
  109. }
  110. enum CancellationKind: Codable {
  111. case awaitCancelled
  112. case withCancellationHandler
  113. }
  114. struct Empty: Codable {
  115. }