|
| 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 | + |
| 17 | +/// A namespace for request message types used by servers. |
| 18 | +public enum ServerRequest {} |
| 19 | + |
| 20 | +extension ServerRequest { |
| 21 | + /// A request received at the server containing a single message. |
| 22 | + public struct Single<Message: Sendable>: Sendable { |
| 23 | + /// Metadata received from the client at the start of the RPC. |
| 24 | + /// |
| 25 | + /// The metadata contains gRPC and transport specific entries in addition to user-specified |
| 26 | + /// metadata. |
| 27 | + public var metadata: Metadata |
| 28 | + |
| 29 | + /// The message received from the client. |
| 30 | + public var message: Message |
| 31 | + |
| 32 | + /// Create a new single server request. |
| 33 | + /// |
| 34 | + /// - Parameters: |
| 35 | + /// - metadata: Metadata received from the client. |
| 36 | + /// - messages: The message received from the client. |
| 37 | + public init(metadata: Metadata, message: Message) { |
| 38 | + self.metadata = metadata |
| 39 | + self.message = message |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) |
| 45 | +extension ServerRequest { |
| 46 | + /// A request received at the server containing a stream of messages. |
| 47 | + public struct Stream<Message: Sendable>: Sendable { |
| 48 | + /// Metadata received from the client at the start of the RPC. |
| 49 | + /// |
| 50 | + /// The metadata contains gRPC and transport specific entries in addition to user-specified |
| 51 | + /// metadata. |
| 52 | + public var metadata: Metadata |
| 53 | + |
| 54 | + /// A sequence of messages received from the client. |
| 55 | + /// |
| 56 | + /// The sequence may be iterated at most once. |
| 57 | + public var messages: RPCAsyncSequence<Message> |
| 58 | + |
| 59 | + /// Create a new streaming request. |
| 60 | + /// |
| 61 | + /// - Parameters: |
| 62 | + /// - metadata: Metadata received from the client. |
| 63 | + /// - messages: A sequence of messages received from the client. |
| 64 | + public init(metadata: Metadata, messages: RPCAsyncSequence<Message>) { |
| 65 | + self.metadata = metadata |
| 66 | + self.messages = messages |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// MARK: - Conversion |
| 72 | + |
| 73 | +@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) |
| 74 | +extension ServerRequest.Stream { |
| 75 | + @_spi(Testing) |
| 76 | + public init(single request: ServerRequest.Single<Message>) { |
| 77 | + self.init(metadata: request.metadata, messages: .one(request.message)) |
| 78 | + } |
| 79 | +} |
0 commit comments