GRPCClientCodec.swift 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. /// Outgoing gRPC package with a fixed message type.
  21. public enum GRPCClientRequestPart<RequestMessage: Message> {
  22. case head(HTTPRequestHead)
  23. case message(RequestMessage)
  24. case end
  25. }
  26. /// Incoming gRPC package with a fixed message type.
  27. public enum GRPCClientResponsePart<ResponseMessage: Message> {
  28. case headers(HTTPHeaders)
  29. case message(ResponseMessage)
  30. case status(GRPCStatus)
  31. }
  32. /// This channel handler simply encodes and decodes protobuf messages into typed messages
  33. /// and `Data`.
  34. public final class GRPCClientCodec<RequestMessage: Message, ResponseMessage: Message> {
  35. public init() {}
  36. }
  37. extension GRPCClientCodec: ChannelInboundHandler {
  38. public typealias InboundIn = RawGRPCClientResponsePart
  39. public typealias InboundOut = GRPCClientResponsePart<ResponseMessage>
  40. public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
  41. let response = self.unwrapInboundIn(data)
  42. switch response {
  43. case .headers(let headers):
  44. ctx.fireChannelRead(self.wrapInboundOut(.headers(headers)))
  45. case .message(var messageBuffer):
  46. // Force unwrapping is okay here; we're reading the readable bytes.
  47. let messageAsData = messageBuffer.readData(length: messageBuffer.readableBytes)!
  48. do {
  49. ctx.fireChannelRead(self.wrapInboundOut(.message(try ResponseMessage(serializedData: messageAsData))))
  50. } catch {
  51. ctx.fireErrorCaught(GRPCError.client(.responseProtoDeserializationFailure))
  52. }
  53. case .status(let status):
  54. ctx.fireChannelRead(self.wrapInboundOut(.status(status)))
  55. }
  56. }
  57. }
  58. extension GRPCClientCodec: ChannelOutboundHandler {
  59. public typealias OutboundIn = GRPCClientRequestPart<RequestMessage>
  60. public typealias OutboundOut = RawGRPCClientRequestPart
  61. public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
  62. let request = self.unwrapOutboundIn(data)
  63. switch request {
  64. case .head(let head):
  65. ctx.write(self.wrapOutboundOut(.head(head)), promise: promise)
  66. case .message(let message):
  67. do {
  68. ctx.write(self.wrapOutboundOut(.message(try message.serializedData())), promise: promise)
  69. } catch {
  70. let error = GRPCError.client(.requestProtoSerializationFailure)
  71. promise?.fail(error: error)
  72. ctx.fireErrorCaught(error)
  73. }
  74. case .end:
  75. ctx.write(self.wrapOutboundOut(.end), promise: promise)
  76. }
  77. }
  78. }