GRPCClientRequestChannelHandler.swift 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright 2019, 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 SwiftProtobuf
  18. import NIO
  19. import NIOHTTP1
  20. /// A base channel handler for client requests.
  21. internal class GRPCClientRequestChannelHandler<RequestMessage: Message>: ChannelInboundHandler {
  22. typealias InboundIn = Never
  23. typealias OutboundOut = GRPCClientRequestPart<RequestMessage>
  24. /// The request head to send.
  25. internal let requestHead: HTTPRequestHead
  26. init(requestHead: HTTPRequestHead) {
  27. self.requestHead = requestHead
  28. }
  29. public func channelActive(context: ChannelHandlerContext) {
  30. // If we don't provide a method here the default implementation on protocol (i.e. no-op) will be
  31. // used in subclasses, even if they implement channelActive(context:).
  32. }
  33. }
  34. /// A channel handler for unary client requests.
  35. ///
  36. /// Sends the request head, message and end on `channelActive(context:)`.
  37. internal final class GRPCClientUnaryRequestChannelHandler<RequestMessage: Message>: GRPCClientRequestChannelHandler<RequestMessage> {
  38. /// The request to send.
  39. internal let request: _Box<RequestMessage>
  40. init(requestHead: HTTPRequestHead, request: _Box<RequestMessage>) {
  41. self.request = request
  42. super.init(requestHead: requestHead)
  43. }
  44. override public func channelActive(context: ChannelHandlerContext) {
  45. context.write(self.wrapOutboundOut(.head(self.requestHead)), promise: nil)
  46. context.write(self.wrapOutboundOut(.message(self.request)), promise: nil)
  47. context.writeAndFlush(self.wrapOutboundOut(.end), promise: nil)
  48. context.fireChannelActive()
  49. }
  50. }
  51. /// A channel handler for client calls which stream requests.
  52. ///
  53. /// Sends the request head on `channelActive(context:)`.
  54. internal final class GRPCClientStreamingRequestChannelHandler<RequestMessage: Message>: GRPCClientRequestChannelHandler<RequestMessage> {
  55. override public func channelActive(context: ChannelHandlerContext) {
  56. context.writeAndFlush(self.wrapOutboundOut(.head(self.requestHead)), promise: nil)
  57. context.fireChannelActive()
  58. }
  59. }