ServerHandlerProtocol.swift 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright 2021, 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 NIOCore
  17. import NIOHPACK
  18. /// This protocol lays out the inbound interface between the gRPC module and generated server code.
  19. /// On receiving a new RPC, gRPC will ask all available service providers for an instance of this
  20. /// protocol in order to handle the RPC.
  21. ///
  22. /// See also: ``CallHandlerProvider/handle(method:context:)``.
  23. public protocol GRPCServerHandlerProtocol {
  24. /// Called when request headers have been received at the start of an RPC.
  25. /// - Parameter metadata: The request headers.
  26. func receiveMetadata(_ metadata: HPACKHeaders)
  27. /// Called when request message has been received.
  28. /// - Parameter bytes: The bytes of the serialized request.
  29. func receiveMessage(_ bytes: ByteBuffer)
  30. /// Called at the end of the request stream.
  31. func receiveEnd()
  32. /// Called when an error has been encountered. The handler should be torn down on receiving an
  33. /// error.
  34. /// - Parameter error: The error which has been encountered.
  35. func receiveError(_ error: Error)
  36. /// Called when the RPC handler should be torn down.
  37. func finish()
  38. }
  39. /// This protocol defines the outbound interface between the gRPC module and generated server code.
  40. /// It is used by server handlers in order to send responses back to gRPC.
  41. @usableFromInline
  42. internal protocol GRPCServerResponseWriter {
  43. /// Send the initial response metadata.
  44. /// - Parameters:
  45. /// - metadata: The user-provided metadata to send to the client.
  46. /// - flush: Whether a flush should be emitted after writing the metadata.
  47. /// - promise: A promise to complete once the metadata has been handled.
  48. func sendMetadata(_ metadata: HPACKHeaders, flush: Bool, promise: EventLoopPromise<Void>?)
  49. /// Send the serialized bytes of a response message.
  50. /// - Parameters:
  51. /// - bytes: The serialized bytes to send to the client.
  52. /// - metadata: Metadata associated with sending the response, such as whether it should be
  53. /// compressed.
  54. /// - promise: A promise to complete once the message as been handled.
  55. func sendMessage(_ bytes: ByteBuffer, metadata: MessageMetadata, promise: EventLoopPromise<Void>?)
  56. /// Ends the response stream.
  57. /// - Parameters:
  58. /// - status: The final status of the RPC.
  59. /// - trailers: Any user-provided trailers to send back to the client with the status.
  60. /// - promise: A promise to complete once the status and trailers have been handled.
  61. func sendEnd(status: GRPCStatus, trailers: HPACKHeaders, promise: EventLoopPromise<Void>?)
  62. }