_ClientRequestChannelHandler.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. ///
  22. /// - Important: This is **NOT** part of the public API.
  23. public class _ClientRequestChannelHandler<RequestMessage: Message>: ChannelInboundHandler {
  24. public typealias InboundIn = Never
  25. public typealias OutboundOut = _GRPCClientRequestPart<RequestMessage>
  26. /// The request head to send.
  27. internal let requestHead: _GRPCRequestHead
  28. init(requestHead: _GRPCRequestHead) {
  29. self.requestHead = requestHead
  30. }
  31. public func channelActive(context: ChannelHandlerContext) {
  32. // If we don't provide a method here the default implementation on protocol (i.e. no-op) will be
  33. // used in subclasses, even if they implement channelActive(context:).
  34. }
  35. }
  36. /// A channel handler for unary client requests.
  37. ///
  38. /// Sends the request head, message and end on `channelActive(context:)`.
  39. ///
  40. /// - Important: This is **NOT** part of the public API.
  41. public final class _UnaryRequestChannelHandler<RequestMessage: Message>: _ClientRequestChannelHandler<RequestMessage> {
  42. /// The request to send.
  43. internal let request: _Box<RequestMessage>
  44. public init(requestHead: _GRPCRequestHead, request: _Box<RequestMessage>) {
  45. self.request = request
  46. super.init(requestHead: requestHead)
  47. }
  48. override public func channelActive(context: ChannelHandlerContext) {
  49. context.write(self.wrapOutboundOut(.head(self.requestHead)), promise: nil)
  50. context.write(self.wrapOutboundOut(.message(self.request)), promise: nil)
  51. context.writeAndFlush(self.wrapOutboundOut(.end), promise: nil)
  52. context.fireChannelActive()
  53. }
  54. }
  55. /// A channel handler for client calls which stream requests.
  56. ///
  57. /// Sends the request head on `channelActive(context:)`.
  58. ///
  59. /// - Important: This is **NOT** part of the public API.
  60. public final class _StreamingRequestChannelHandler<RequestMessage: Message>: _ClientRequestChannelHandler<RequestMessage> {
  61. override public func channelActive(context: ChannelHandlerContext) {
  62. context.writeAndFlush(self.wrapOutboundOut(.head(self.requestHead)), promise: nil)
  63. context.fireChannelActive()
  64. }
  65. }