GRPCServerCodec.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 NIOFoundationCompat
  20. import NIOHTTP1
  21. /// Incoming gRPC package with a fixed message type.
  22. ///
  23. /// - Important: This is **NOT** part of the public API.
  24. public enum _GRPCServerRequestPart<RequestMessage: Message> {
  25. case head(HTTPRequestHead)
  26. case message(RequestMessage)
  27. case end
  28. }
  29. /// Outgoing gRPC package with a fixed message type.
  30. ///
  31. /// - Important: This is **NOT** part of the public API.
  32. public enum _GRPCServerResponsePart<ResponseMessage: Message> {
  33. case headers(HTTPHeaders)
  34. case message(ResponseMessage)
  35. case statusAndTrailers(GRPCStatus, HTTPHeaders)
  36. }
  37. /// A simple channel handler that translates raw gRPC packets into decoded protobuf messages, and vice versa.
  38. internal final class GRPCServerCodec<RequestMessage: Message, ResponseMessage: Message> {}
  39. extension GRPCServerCodec: ChannelInboundHandler {
  40. typealias InboundIn = _RawGRPCServerRequestPart
  41. typealias InboundOut = _GRPCServerRequestPart<RequestMessage>
  42. func channelRead(context: ChannelHandlerContext, data: NIOAny) {
  43. switch self.unwrapInboundIn(data) {
  44. case .head(let requestHead):
  45. context.fireChannelRead(self.wrapInboundOut(.head(requestHead)))
  46. case .message(var message):
  47. let messageAsData = message.readData(length: message.readableBytes)!
  48. do {
  49. context.fireChannelRead(self.wrapInboundOut(.message(try RequestMessage(serializedData: messageAsData))))
  50. } catch {
  51. context.fireErrorCaught(GRPCError.DeserializationFailure().captureContext())
  52. }
  53. case .end:
  54. context.fireChannelRead(self.wrapInboundOut(.end))
  55. }
  56. }
  57. }
  58. extension GRPCServerCodec: ChannelOutboundHandler {
  59. typealias OutboundIn = _GRPCServerResponsePart<ResponseMessage>
  60. typealias OutboundOut = _RawGRPCServerResponsePart
  61. func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
  62. let responsePart = self.unwrapOutboundIn(data)
  63. switch responsePart {
  64. case .headers(let headers):
  65. context.write(self.wrapOutboundOut(.headers(headers)), promise: promise)
  66. case .message(let message):
  67. do {
  68. let messageData = try message.serializedData()
  69. context.write(self.wrapOutboundOut(.message(messageData)), promise: promise)
  70. } catch {
  71. let error = GRPCError.SerializationFailure().captureContext()
  72. promise?.fail(error)
  73. context.fireErrorCaught(error)
  74. }
  75. case let .statusAndTrailers(status, trailers):
  76. context.writeAndFlush(self.wrapOutboundOut(.statusAndTrailers(status, trailers)), promise: promise)
  77. }
  78. }
  79. }