GRPCClientCodec.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 NIO
  18. import NIOHTTP1
  19. import SwiftProtobuf
  20. import Logging
  21. /// Outgoing gRPC package with a fixed message type.
  22. public enum GRPCClientRequestPart<RequestMessage: Message> {
  23. case head(HTTPRequestHead)
  24. // We box the message to keep the enum small enough to fit in `NIOAny` and avoid unnecessary
  25. // allocations.
  26. case message(_Box<RequestMessage>)
  27. case end
  28. }
  29. /// Incoming gRPC package with a fixed message type.
  30. public enum GRPCClientResponsePart<ResponseMessage: Message> {
  31. case headers(HTTPHeaders)
  32. // We box the message to keep the enum small enough to fit in `NIOAny` and avoid unnecessary
  33. // allocations.
  34. case message(_Box<ResponseMessage>)
  35. case status(GRPCStatus, HTTPHeaders?)
  36. }
  37. /// This channel handler simply encodes and decodes protobuf messages into typed messages
  38. /// and `Data`.
  39. public final class GRPCClientCodec<RequestMessage: Message, ResponseMessage: Message> {
  40. private let logger: Logger
  41. public init(logger: Logger) {
  42. var loggerWithMetadata = logger
  43. loggerWithMetadata[metadataKey: MetadataKey.channelHandler] = "GRPCClientCodec"
  44. self.logger = loggerWithMetadata
  45. }
  46. }
  47. extension GRPCClientCodec: ChannelInboundHandler {
  48. public typealias InboundIn = RawGRPCClientResponsePart
  49. public typealias InboundOut = GRPCClientResponsePart<ResponseMessage>
  50. public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
  51. let response = self.unwrapInboundIn(data)
  52. switch response {
  53. case .headers(let headers):
  54. self.logger.debug("read response headers: \(headers)")
  55. context.fireChannelRead(self.wrapInboundOut(.headers(headers)))
  56. case .message(var messageBuffer):
  57. self.logger.debug("read message \(messageBuffer)")
  58. // Force unwrapping is okay here; we're reading the readable bytes.
  59. let messageAsData = messageBuffer.readData(length: messageBuffer.readableBytes)!
  60. do {
  61. self.logger.debug("deserializing \(messageAsData.count) bytes as \(ResponseMessage.self)")
  62. let box = _Box(try ResponseMessage(serializedData: messageAsData))
  63. context.fireChannelRead(self.wrapInboundOut(.message(box)))
  64. } catch {
  65. context.fireErrorCaught(GRPCError.client(.responseProtoDeserializationFailure))
  66. }
  67. case let .statusAndTrailers(status, trailers):
  68. self.logger.debug("read status \(status)")
  69. context.fireChannelRead(self.wrapInboundOut(.status(status, trailers)))
  70. }
  71. }
  72. }
  73. extension GRPCClientCodec: ChannelOutboundHandler {
  74. public typealias OutboundIn = GRPCClientRequestPart<RequestMessage>
  75. public typealias OutboundOut = RawGRPCClientRequestPart
  76. public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
  77. let request = self.unwrapOutboundIn(data)
  78. switch request {
  79. case .head(let head):
  80. self.logger.debug("writing request head: \(head)")
  81. context.write(self.wrapOutboundOut(.head(head)), promise: promise)
  82. case .message(let box):
  83. do {
  84. self.logger.debug("serializing and writing \(RequestMessage.self) protobuf")
  85. context.write(self.wrapOutboundOut(.message(try box.value.serializedData())), promise: promise)
  86. } catch {
  87. self.logger.error("failed to serialize message: \(box.value)")
  88. let error = GRPCError.client(.requestProtoSerializationFailure)
  89. promise?.fail(error)
  90. context.fireErrorCaught(error)
  91. }
  92. case .end:
  93. self.logger.debug("writing end")
  94. context.write(self.wrapOutboundOut(.end), promise: promise)
  95. }
  96. }
  97. }