ControlMessages.swift 4.6 KB

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