_GRPCClientCodecHandler.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 NIO
  17. internal class GRPCClientCodecHandler<Serializer: MessageSerializer, Deserializer: MessageDeserializer> {
  18. /// The request serializer.
  19. private let serializer: Serializer
  20. /// The response deserializer.
  21. private let deserializer: Deserializer
  22. internal init(serializer: Serializer, deserializer: Deserializer) {
  23. self.serializer = serializer
  24. self.deserializer = deserializer
  25. }
  26. }
  27. extension GRPCClientCodecHandler: ChannelInboundHandler {
  28. typealias InboundIn = _RawGRPCClientResponsePart
  29. typealias InboundOut = _GRPCClientResponsePart<Deserializer.Output>
  30. internal func channelRead(context: ChannelHandlerContext, data: NIOAny) {
  31. switch self.unwrapInboundIn(data) {
  32. case .initialMetadata(let headers):
  33. context.fireChannelRead(self.wrapInboundOut(.initialMetadata(headers)))
  34. case .message(let messageContext):
  35. do {
  36. let response = try self.deserializer.deserialize(byteBuffer: messageContext.message)
  37. context.fireChannelRead(self.wrapInboundOut(.message(.init(response, compressed: messageContext.compressed))))
  38. } catch {
  39. context.fireErrorCaught(error)
  40. }
  41. case .trailingMetadata(let trailers):
  42. context.fireChannelRead(self.wrapInboundOut(.trailingMetadata(trailers)))
  43. case .status(let status):
  44. context.fireChannelRead(self.wrapInboundOut(.status(status)))
  45. }
  46. }
  47. }
  48. extension GRPCClientCodecHandler: ChannelOutboundHandler {
  49. typealias OutboundIn = _GRPCClientRequestPart<Serializer.Input>
  50. typealias OutboundOut = _RawGRPCClientRequestPart
  51. internal func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
  52. switch self.unwrapOutboundIn(data) {
  53. case .head(let head):
  54. context.write(self.wrapOutboundOut(.head(head)), promise: promise)
  55. case .message(let message):
  56. do {
  57. let serialized = try self.serializer.serialize(message.message, allocator: context.channel.allocator)
  58. context.write(self.wrapOutboundOut(.message(.init(serialized, compressed: message.compressed))), promise: promise)
  59. } catch {
  60. promise?.fail(error)
  61. context.fireErrorCaught(error)
  62. }
  63. case .end:
  64. context.write(self.wrapOutboundOut(.end), promise: promise)
  65. }
  66. }
  67. }