GRPCClientCallHandler.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * Copyright 2020, 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 NIO
  17. /// An inbound channel handler which forwards events and messages to a client call.
  18. internal class GRPCClientCallHandler<Request: GRPCPayload, Response: GRPCPayload>: ChannelInboundHandler {
  19. typealias InboundIn = _GRPCClientResponsePart<Response>
  20. private var call: ChannelTransport<Request, Response>
  21. init(call: ChannelTransport<Request, Response>) {
  22. self.call = call
  23. }
  24. func errorCaught(context: ChannelHandlerContext, error: Error) {
  25. self.call.receiveError(error)
  26. context.fireErrorCaught(error)
  27. }
  28. func channelActive(context: ChannelHandlerContext) {
  29. self.call.activate(stream: context.channel)
  30. context.fireChannelActive()
  31. }
  32. func channelInactive(context: ChannelHandlerContext) {
  33. self.errorCaught(context: context, error: GRPCStatus(code: .unavailable, message: nil))
  34. context.fireChannelInactive()
  35. }
  36. func channelRead(context: ChannelHandlerContext, data: NIOAny) {
  37. let part = self.unwrapInboundIn(data)
  38. self.call.receiveResponse(part)
  39. }
  40. }