2
0

SettingsObservingHandler.swift 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 NIOHTTP2
  19. import Logging
  20. /// The purpose of this channel handler is to observe the initial settings frame on the root stream.
  21. /// This is an indication that the connection has become `.ready`. When this happens this handler
  22. /// will remove itself from the pipeline.
  23. class InitialSettingsObservingHandler: ChannelInboundHandler, RemovableChannelHandler {
  24. typealias InboundIn = HTTP2Frame
  25. typealias InboundOut = HTTP2Frame
  26. private let connectivityStateMonitor: ConnectivityStateMonitor
  27. private let logger = Logger(subsystem: .clientChannel)
  28. init(connectivityStateMonitor: ConnectivityStateMonitor) {
  29. self.connectivityStateMonitor = connectivityStateMonitor
  30. }
  31. func channelRead(context: ChannelHandlerContext, data: NIOAny) {
  32. let frame = self.unwrapInboundIn(data)
  33. if frame.streamID == .rootStream, case .settings(.settings) = frame.payload {
  34. self.logger.info("observed initial settings frame on the root stream")
  35. self.connectivityStateMonitor.state = .ready
  36. // We're no longer needed at this point, remove ourselves from the pipeline.
  37. self.logger.debug("removing 'InitialSettingsObservingHandler' from the channel")
  38. context.pipeline.removeHandler(self, promise: nil)
  39. }
  40. // We should always forward the frame.
  41. context.fireChannelRead(data)
  42. }
  43. }