GRPCServerCodecHandler.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. class GRPCServerCodecHandler<Serializer: MessageSerializer, Deserializer: MessageDeserializer> {
  18. /// The response serializer.
  19. private let serializer: Serializer
  20. /// The request 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 GRPCServerCodecHandler: ChannelInboundHandler {
  28. typealias InboundIn = _RawGRPCServerRequestPart
  29. typealias InboundOut = _GRPCServerRequestPart<Deserializer.Output>
  30. internal func channelRead(context: ChannelHandlerContext, data: NIOAny) {
  31. switch self.unwrapInboundIn(data) {
  32. case let .headers(head):
  33. context.fireChannelRead(self.wrapInboundOut(.headers(head)))
  34. case let .message(buffer):
  35. do {
  36. let deserialized = try self.deserializer.deserialize(byteBuffer: buffer)
  37. context.fireChannelRead(self.wrapInboundOut(.message(deserialized)))
  38. } catch {
  39. context.fireErrorCaught(error)
  40. }
  41. case .end:
  42. context.fireChannelRead(self.wrapInboundOut(.end))
  43. }
  44. }
  45. }
  46. extension GRPCServerCodecHandler: ChannelOutboundHandler {
  47. typealias OutboundIn = _GRPCServerResponsePart<Serializer.Input>
  48. typealias OutboundOut = _RawGRPCServerResponsePart
  49. internal func write(
  50. context: ChannelHandlerContext,
  51. data: NIOAny,
  52. promise: EventLoopPromise<Void>?
  53. ) {
  54. switch self.unwrapOutboundIn(data) {
  55. case let .headers(headers):
  56. context.write(self.wrapOutboundOut(.headers(headers)), promise: promise)
  57. case let .message(messageContext):
  58. do {
  59. let buffer = try self.serializer.serialize(
  60. messageContext.message,
  61. allocator: context.channel.allocator
  62. )
  63. context.write(
  64. self.wrapOutboundOut(.message(.init(buffer, compressed: messageContext.compressed))),
  65. promise: promise
  66. )
  67. } catch {
  68. let error = GRPCError.SerializationFailure().captureContext()
  69. promise?.fail(error)
  70. context.fireErrorCaught(error)
  71. }
  72. case let .statusAndTrailers(status, trailers):
  73. context.write(self.wrapOutboundOut(.statusAndTrailers(status, trailers)), promise: promise)
  74. }
  75. }
  76. }