2
0

MessageParts.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2020, 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 NIOHPACK
  17. public enum GRPCClientRequestPart<Request> {
  18. /// User provided metadata sent at the start of the request stream.
  19. case metadata(HPACKHeaders)
  20. /// A message to send to the server.
  21. case message(Request, MessageMetadata)
  22. /// The end the request stream.
  23. case end
  24. }
  25. public enum GRPCClientResponsePart<Response> {
  26. /// The metadata returned by the server at the start of the RPC.
  27. case metadata(HPACKHeaders)
  28. /// A response message from the server.
  29. case message(Response)
  30. /// The end of response stream sent by the server.
  31. case end(GRPCStatus, HPACKHeaders)
  32. }
  33. public enum GRPCServerRequestPart<Request> {
  34. /// Metadata received from the client at the start of the RPC.
  35. case metadata(HPACKHeaders)
  36. /// A request message sent by the client.
  37. case message(Request)
  38. /// The end the request stream.
  39. case end
  40. }
  41. public enum GRPCServerResponsePart<Response> {
  42. /// The metadata to send to the client at the start of the response stream.
  43. case metadata(HPACKHeaders)
  44. /// A response message sent by the server.
  45. case message(Response, MessageMetadata)
  46. /// The end of response stream sent by the server.
  47. case end(GRPCStatus, HPACKHeaders)
  48. }
  49. /// Metadata associated with a request or response message.
  50. public struct MessageMetadata: Equatable {
  51. /// Whether the message should be compressed. If compression has not been enabled on the RPC
  52. /// then this setting is ignored.
  53. public var compress: Bool
  54. /// Whether the underlying transported should be 'flushed' after writing this message. If a batch
  55. /// of messages is to be sent then flushing only after the last message may improve
  56. /// performance.
  57. public var flush: Bool
  58. public init(compress: Bool, flush: Bool) {
  59. self.compress = compress
  60. self.flush = flush
  61. }
  62. }
  63. extension GRPCClientResponsePart {
  64. @inlinable
  65. internal var isEnd: Bool {
  66. switch self {
  67. case .end:
  68. return true
  69. case .metadata, .message:
  70. return false
  71. }
  72. }
  73. }
  74. extension GRPCServerResponsePart {
  75. @inlinable
  76. internal var isEnd: Bool {
  77. switch self {
  78. case .end:
  79. return true
  80. case .metadata, .message:
  81. return false
  82. }
  83. }
  84. }