GRPCServerCodec.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. public enum GRPCServerRequestPart<RequestMessage: Message> {
  23. case head(HTTPRequestHead)
  24. case message(RequestMessage)
  25. case end
  26. }
  27. /// Outgoing gRPC package with a fixed message type.
  28. public enum GRPCServerResponsePart<ResponseMessage: Message> {
  29. case headers(HTTPHeaders)
  30. case message(ResponseMessage)
  31. case status(GRPCStatus)
  32. }
  33. /// A simple channel handler that translates raw gRPC packets into decoded protobuf messages, and vice versa.
  34. public final class GRPCServerCodec<RequestMessage: Message, ResponseMessage: Message> {}
  35. extension GRPCServerCodec: ChannelInboundHandler {
  36. public typealias InboundIn = RawGRPCServerRequestPart
  37. public typealias InboundOut = GRPCServerRequestPart<RequestMessage>
  38. public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
  39. switch self.unwrapInboundIn(data) {
  40. case .head(let requestHead):
  41. context.fireChannelRead(self.wrapInboundOut(.head(requestHead)))
  42. case .message(var message):
  43. let messageAsData = message.readData(length: message.readableBytes)!
  44. do {
  45. context.fireChannelRead(self.wrapInboundOut(.message(try RequestMessage(serializedData: messageAsData))))
  46. } catch {
  47. context.fireErrorCaught(GRPCError.server(.requestProtoDeserializationFailure))
  48. }
  49. case .end:
  50. context.fireChannelRead(self.wrapInboundOut(.end))
  51. }
  52. }
  53. }
  54. extension GRPCServerCodec: ChannelOutboundHandler {
  55. public typealias OutboundIn = GRPCServerResponsePart<ResponseMessage>
  56. public typealias OutboundOut = RawGRPCServerResponsePart
  57. public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
  58. let responsePart = self.unwrapOutboundIn(data)
  59. switch responsePart {
  60. case .headers(let headers):
  61. context.write(self.wrapOutboundOut(.headers(headers)), promise: promise)
  62. case .message(let message):
  63. do {
  64. let messageData = try message.serializedData()
  65. context.write(self.wrapOutboundOut(.message(messageData)), promise: promise)
  66. } catch {
  67. let error = GRPCError.server(.responseProtoSerializationFailure)
  68. promise?.fail(error)
  69. context.fireErrorCaught(error)
  70. }
  71. case .status(let status):
  72. context.writeAndFlush(self.wrapOutboundOut(.status(status)), promise: promise)
  73. }
  74. }
  75. }