ServerRequest.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright 2023, 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. /// A request received at the server containing a single message.
  17. public struct ServerRequest<Message: Sendable>: Sendable {
  18. /// Metadata received from the client at the start of the RPC.
  19. ///
  20. /// The metadata contains gRPC and transport specific entries in addition to user-specified
  21. /// metadata.
  22. public var metadata: Metadata
  23. /// The message received from the client.
  24. public var message: Message
  25. /// Create a new single server request.
  26. ///
  27. /// - Parameters:
  28. /// - metadata: Metadata received from the client.
  29. /// - message: The message received from the client.
  30. public init(metadata: Metadata, message: Message) {
  31. self.metadata = metadata
  32. self.message = message
  33. }
  34. }
  35. /// A request received at the server containing a stream of messages.
  36. public struct StreamingServerRequest<Message: Sendable>: Sendable {
  37. /// Metadata received from the client at the start of the RPC.
  38. ///
  39. /// The metadata contains gRPC and transport specific entries in addition to user-specified
  40. /// metadata.
  41. public var metadata: Metadata
  42. /// A sequence of messages received from the client.
  43. ///
  44. /// The sequence may be iterated at most once.
  45. public var messages: RPCAsyncSequence<Message, any Error>
  46. /// Create a new streaming request.
  47. ///
  48. /// - Parameters:
  49. /// - metadata: Metadata received from the client.
  50. /// - messages: A sequence of messages received from the client.
  51. public init(metadata: Metadata, messages: RPCAsyncSequence<Message, any Error>) {
  52. self.metadata = metadata
  53. self.messages = messages
  54. }
  55. }
  56. // MARK: - Conversion
  57. extension StreamingServerRequest {
  58. public init(single request: ServerRequest<Message>) {
  59. self.init(metadata: request.metadata, messages: .one(request.message))
  60. }
  61. }
  62. extension ServerRequest {
  63. public init(stream request: StreamingServerRequest<Message>) async throws {
  64. var iterator = request.messages.makeAsyncIterator()
  65. guard let message = try await iterator.next() else {
  66. throw RPCError(code: .internalError, message: "Empty stream.")
  67. }
  68. guard try await iterator.next() == nil else {
  69. throw RPCError(code: .internalError, message: "Too many messages.")
  70. }
  71. self = ServerRequest(metadata: request.metadata, message: message)
  72. }
  73. }