mirror of
https://github.com/apple/swift-nio-http2
synced 2026-11-25 04:47:50 +00:00
60 lines
2.4 KiB
Swift
60 lines
2.4 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import NIO
|
|
import CNIONghttp2
|
|
|
|
public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboundHandler {
|
|
public typealias InboundIn = HTTP2Frame
|
|
public typealias OutboundIn = HTTP2Frame
|
|
public typealias OutboundOut = HTTP2Frame
|
|
|
|
private var streams: [Int32: Channel] = [:]
|
|
private let streamStateInitializer: ((Channel, Int) -> EventLoopFuture<Void>)?
|
|
|
|
public func channelActive(ctx: ChannelHandlerContext) {
|
|
let frame = HTTP2Frame(header: .init(streamID: 0), payload: .settings([]))
|
|
ctx.write(self.wrapOutboundOut(frame), promise: nil)
|
|
}
|
|
|
|
public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
|
|
let frame = self.unwrapInboundIn(data)
|
|
let streamID = frame.header.streamID
|
|
if let channel = streams[streamID] {
|
|
channel.pipeline.fireChannelRead(data)
|
|
} else {
|
|
switch (streamID, frame.payload) {
|
|
case (0, _), (_, .headers(_)):
|
|
/* this is legal, new stream starts with a header frame */
|
|
self.streams[streamID] = HTTP2StreamChannel(allocator: ctx.channel.allocator,
|
|
parent: ctx.channel,
|
|
streamID: streamID,
|
|
initializer: self.streamStateInitializer)
|
|
self.channelRead(ctx: ctx, data: data)
|
|
default:
|
|
fatalError("unknown stream \(frame.header.streamID) with \(frame.payload)")
|
|
}
|
|
}
|
|
}
|
|
|
|
public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
|
|
/* for now just forward */
|
|
ctx.write(data, promise: promise)
|
|
}
|
|
|
|
public init(streamStateInitializer: ((Channel, Int) -> EventLoopFuture<Void>)? = nil) {
|
|
self.streamStateInitializer = streamStateInitializer
|
|
}
|
|
}
|