GRPCServerCodec.swift 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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<RequestPayload: GRPCPayload> {
  25. case head(HTTPRequestHead)
  26. case message(RequestPayload)
  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<ResponsePayload: GRPCPayload> {
  33. case headers(HTTPHeaders)
  34. case message(ResponsePayload)
  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<RequestPayload: GRPCPayload, ResponsePayload: GRPCPayload> {
  39. var messageWriter = LengthPrefixedMessageWriter(compression: .none)
  40. }
  41. extension GRPCServerCodec: ChannelInboundHandler {
  42. typealias InboundIn = _RawGRPCServerRequestPart
  43. typealias InboundOut = _GRPCServerRequestPart<RequestPayload>
  44. func channelRead(context: ChannelHandlerContext, data: NIOAny) {
  45. switch self.unwrapInboundIn(data) {
  46. case .head(let requestHead):
  47. context.fireChannelRead(self.wrapInboundOut(.head(requestHead)))
  48. case .message(var message):
  49. do {
  50. context.fireChannelRead(self.wrapInboundOut(.message(try RequestPayload(serializedByteBuffer: &message))))
  51. } catch {
  52. context.fireErrorCaught(GRPCError.DeserializationFailure().captureContext())
  53. }
  54. case .end:
  55. context.fireChannelRead(self.wrapInboundOut(.end))
  56. }
  57. }
  58. }
  59. extension GRPCServerCodec: ChannelOutboundHandler {
  60. typealias OutboundIn = _GRPCServerResponsePart<ResponsePayload>
  61. typealias OutboundOut = _RawGRPCServerResponsePart
  62. func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
  63. let responsePart = self.unwrapOutboundIn(data)
  64. switch responsePart {
  65. case .headers(let headers):
  66. context.write(self.wrapOutboundOut(.headers(headers)), promise: promise)
  67. case .message(let message):
  68. do {
  69. var responseBuffer = context.channel.allocator.buffer(capacity: 0)
  70. try self.messageWriter.write(message, into: &responseBuffer)
  71. context.write(self.wrapOutboundOut(.message(responseBuffer)), promise: promise)
  72. } catch {
  73. let error = GRPCError.SerializationFailure().captureContext()
  74. promise?.fail(error)
  75. context.fireErrorCaught(error)
  76. }
  77. case let .statusAndTrailers(status, trailers):
  78. context.writeAndFlush(self.wrapOutboundOut(.statusAndTrailers(status, trailers)), promise: promise)
  79. }
  80. }
  81. }