2
0

GRPCServerCodecHandler.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 .head(let head):
  33. context.fireChannelRead(self.wrapInboundOut(.head(head)))
  34. case .message(let 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(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
  50. switch self.unwrapOutboundIn(data) {
  51. case .headers(let headers):
  52. context.write(self.wrapOutboundOut(.headers(headers)), promise: promise)
  53. case .message(let messageContext):
  54. do {
  55. let buffer = try self.serializer.serialize(messageContext.message, allocator: context.channel.allocator)
  56. context.write(self.wrapOutboundOut(.message(.init(buffer, compressed: messageContext.compressed))), promise: promise)
  57. } catch {
  58. let error = GRPCError.SerializationFailure().captureContext()
  59. promise?.fail(error)
  60. context.fireErrorCaught(error)
  61. }
  62. case .statusAndTrailers(let status, let trailers):
  63. context.write(self.wrapOutboundOut(.statusAndTrailers(status, trailers)), promise: promise)
  64. }
  65. }
  66. }