don't pretend user events are typed & remove tryUnwrap for in/outbound data
This commit is contained in:
parent
4d8cf1ff22
commit
db28cf4452
|
@ -26,18 +26,10 @@ extension _EmittingChannelHandler {
|
|||
|
||||
public protocol ChannelInboundHandler: _ChannelInboundHandler, _EmittingChannelHandler {
|
||||
associatedtype InboundIn
|
||||
associatedtype InboundUserEventIn = Never
|
||||
|
||||
associatedtype InboundOut = Never
|
||||
associatedtype OutboundUserEventOut = Never
|
||||
associatedtype InboundUserEventOut = Never
|
||||
|
||||
func unwrapInboundIn(_ value: NIOAny) -> InboundIn
|
||||
func tryUnwrapInboundIn(_ value: NIOAny) -> InboundIn?
|
||||
func wrapInboundOut(_ value: InboundOut) -> NIOAny
|
||||
func unwrapInboundUserEventIn(_ value: Any) -> InboundUserEventIn
|
||||
func tryUnwrapInboundUserEventIn(_ value: Any) -> InboundUserEventIn?
|
||||
func wrapInboundUserEventOut(_ value: InboundUserEventOut) -> Any
|
||||
}
|
||||
|
||||
extension ChannelInboundHandler {
|
||||
|
@ -45,61 +37,20 @@ extension ChannelInboundHandler {
|
|||
return value.forceAs()
|
||||
}
|
||||
|
||||
public func tryUnwrapInboundIn(_ value: NIOAny) -> InboundIn? {
|
||||
return value.tryAs()
|
||||
}
|
||||
|
||||
public func wrapInboundOut(_ value: InboundOut) -> NIOAny {
|
||||
return NIOAny(value)
|
||||
}
|
||||
|
||||
public func unwrapInboundUserEventIn(_ value: Any) -> InboundUserEventIn {
|
||||
return value as! InboundUserEventIn
|
||||
}
|
||||
|
||||
public func tryUnwrapInboundUserEventIn(_ value: Any) -> InboundUserEventIn? {
|
||||
return value as? InboundUserEventIn
|
||||
}
|
||||
|
||||
public func wrapInboundUserEventOut(_ value: InboundUserEventOut) -> Any {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
public protocol ChannelOutboundHandler: _ChannelOutboundHandler, _EmittingChannelHandler {
|
||||
associatedtype OutboundIn
|
||||
associatedtype OutboundUserEventIn = Never
|
||||
|
||||
associatedtype InboundOut = Never
|
||||
associatedtype OutboundUserEventOut = Never
|
||||
associatedtype InboundUserEventOut = Never
|
||||
|
||||
func unwrapOutboundIn(_ value: NIOAny) -> OutboundIn
|
||||
func tryUnwrapOutboundIn(_ value: NIOAny) -> OutboundIn?
|
||||
|
||||
func unwrapOutboundUserEventIn(_ value: Any) -> OutboundUserEventIn
|
||||
func tryUnwrapOutboundUserEventIn(_ value: Any) -> OutboundUserEventIn?
|
||||
func wrapOutboundUserEventOut(_ value: OutboundUserEventOut) -> Any
|
||||
}
|
||||
|
||||
extension ChannelOutboundHandler {
|
||||
public func unwrapOutboundIn(_ value: NIOAny) -> OutboundIn {
|
||||
return value.forceAs()
|
||||
}
|
||||
|
||||
public func tryUnwrapOutboundIn(_ value: NIOAny) -> OutboundIn? {
|
||||
return value.tryAs()
|
||||
}
|
||||
|
||||
public func unwrapOutboundUserEventIn(_ value: Any) -> OutboundUserEventIn {
|
||||
return value as! OutboundUserEventIn
|
||||
}
|
||||
|
||||
public func tryUnwrapOutboundUserEventIn(_ value: Any) -> OutboundUserEventIn? {
|
||||
return value as? OutboundUserEventIn
|
||||
}
|
||||
|
||||
public func wrapOutboundUserEventOut(_ value: OutboundUserEventOut) -> Any {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
|
|
@ -118,8 +118,8 @@ public final class HTTPRequestEncoder : ChannelOutboundHandler {
|
|||
public init () { }
|
||||
|
||||
public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
|
||||
switch self.tryUnwrapOutboundIn(data) {
|
||||
case .some(.head(var request)):
|
||||
switch self.unwrapOutboundIn(data) {
|
||||
case .head(var request):
|
||||
sanitizeTransportHeaders(mayHaveBody: request.method.mayHaveRequestBody, headers: &request.headers, version: request.version)
|
||||
|
||||
self.isChunked = isChunkedPart(request.headers)
|
||||
|
@ -132,12 +132,10 @@ public final class HTTPRequestEncoder : ChannelOutboundHandler {
|
|||
request.version.write(buffer: &buffer)
|
||||
buffer.write(staticString: "\r\n")
|
||||
}, ctx: ctx, headers: request.headers, promise: promise)
|
||||
case .some(.body(let bodyPart)):
|
||||
case .body(let bodyPart):
|
||||
writeChunk(wrapOutboundOut: self.wrapOutboundOut, ctx: ctx, isChunked: self.isChunked, chunk: bodyPart, promise: promise)
|
||||
case .some(.end(let trailers)):
|
||||
case .end(let trailers):
|
||||
writeTrailers(wrapOutboundOut: self.wrapOutboundOut, ctx: ctx, isChunked: self.isChunked, trailers: trailers, promise: promise)
|
||||
case .none:
|
||||
ctx.write(data: data, promise: promise)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -155,8 +153,8 @@ public final class HTTPResponseEncoder : ChannelOutboundHandler {
|
|||
public init () { }
|
||||
|
||||
public func write(ctx: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
|
||||
switch self.tryUnwrapOutboundIn(data) {
|
||||
case .some(.head(var response)):
|
||||
switch self.unwrapOutboundIn(data) {
|
||||
case .head(var response):
|
||||
sanitizeTransportHeaders(mayHaveBody: response.status.mayHaveResponseBody, headers: &response.headers, version: response.version)
|
||||
|
||||
self.isChunked = isChunkedPart(response.headers)
|
||||
|
@ -166,12 +164,10 @@ public final class HTTPResponseEncoder : ChannelOutboundHandler {
|
|||
response.status.write(buffer: &buffer)
|
||||
buffer.write(staticString: "\r\n")
|
||||
}, ctx: ctx, headers: response.headers, promise: promise)
|
||||
case .some(.body(let bodyPart)):
|
||||
case .body(let bodyPart):
|
||||
writeChunk(wrapOutboundOut: self.wrapOutboundOut, ctx: ctx, isChunked: self.isChunked, chunk: bodyPart, promise: promise)
|
||||
case .some(.end(let trailers)):
|
||||
case .end(let trailers):
|
||||
writeTrailers(wrapOutboundOut: self.wrapOutboundOut, ctx: ctx, isChunked: self.isChunked, trailers: trailers, promise: promise)
|
||||
case .none:
|
||||
ctx.write(data: data, promise: promise)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,7 +29,6 @@ public class OpenSSLHandler : ChannelInboundHandler, ChannelOutboundHandler {
|
|||
public typealias OutboundOut = ByteBuffer
|
||||
public typealias InboundIn = ByteBuffer
|
||||
public typealias InboundOut = ByteBuffer
|
||||
public typealias InboundUserEventOut = TLSUserEvent
|
||||
|
||||
private enum ConnectionState {
|
||||
case idle
|
||||
|
@ -199,7 +198,7 @@ public class OpenSSLHandler : ChannelInboundHandler, ChannelOutboundHandler {
|
|||
|
||||
// TODO(cory): This event should probably fire out of the OpenSSL info callback.
|
||||
let negotiatedProtocol = connection.getAlpnProtocol()
|
||||
ctx.fireUserInboundEventTriggered(event: wrapInboundUserEventOut(TLSUserEvent.handshakeCompleted(negotiatedProtocol: negotiatedProtocol)))
|
||||
ctx.fireUserInboundEventTriggered(event: TLSUserEvent.handshakeCompleted(negotiatedProtocol: negotiatedProtocol))
|
||||
|
||||
// We need to unbuffer any pending writes. We will have pending writes if the user attempted to write
|
||||
// before we completed the handshake.
|
||||
|
@ -236,7 +235,7 @@ public class OpenSSLHandler : ChannelInboundHandler, ChannelOutboundHandler {
|
|||
writeDataToNetwork(ctx: ctx, promise: nil)
|
||||
|
||||
// TODO(cory): This should probably fire out of the OpenSSL info callback.
|
||||
ctx.fireUserInboundEventTriggered(event: wrapInboundUserEventOut(TLSUserEvent.shutdownCompleted))
|
||||
ctx.fireUserInboundEventTriggered(event: TLSUserEvent.shutdownCompleted)
|
||||
channelClose(ctx: ctx)
|
||||
case .failed(let err):
|
||||
// TODO(cory): This should probably fire out of the OpenSSL info callback.
|
||||
|
|
|
@ -70,7 +70,6 @@ public enum ALPNResult: Equatable {
|
|||
public class ApplicationProtocolNegotiationHandler: ChannelInboundHandler {
|
||||
public typealias InboundIn = Any
|
||||
public typealias InboundOut = Any
|
||||
public typealias InboundUserEventIn = TLSUserEvent
|
||||
|
||||
private let completionHandler: (ALPNResult) -> EventLoopFuture<Void>
|
||||
private var waitingForUser: Bool
|
||||
|
@ -88,7 +87,7 @@ public class ApplicationProtocolNegotiationHandler: ChannelInboundHandler {
|
|||
}
|
||||
|
||||
public func userInboundEventTriggered(ctx: ChannelHandlerContext, event: Any) {
|
||||
guard let tlsEvent = tryUnwrapInboundUserEventIn(event) else {
|
||||
guard let tlsEvent = event as? TLSUserEvent else {
|
||||
ctx.fireUserInboundEventTriggered(event: event)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -143,11 +143,10 @@ private class SuccessfulUpgrader: HTTPProtocolUpgrader {
|
|||
|
||||
private class UserEventSaver<EventType>: ChannelInboundHandler {
|
||||
public typealias InboundIn = Any
|
||||
public typealias InboundUserEventIn = EventType
|
||||
public var events: [EventType] = []
|
||||
|
||||
public func userInboundEventTriggered(ctx: ChannelHandlerContext, event: Any) {
|
||||
events.append(unwrapInboundUserEventIn(event))
|
||||
events.append(event as! EventType)
|
||||
ctx.fireUserInboundEventTriggered(event: event)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,7 +54,6 @@ private func interactInMemory(clientChannel: EmbeddedChannel, serverChannel: Emb
|
|||
private final class SimpleEchoServer: ChannelInboundHandler {
|
||||
public typealias InboundIn = ByteBuffer
|
||||
public typealias OutboundOut = ByteBuffer
|
||||
public typealias InboundUserEventIn = TLSUserEvent
|
||||
|
||||
public func channelRead(ctx: ChannelHandlerContext, data: NIOAny) {
|
||||
ctx.write(data: data, promise: nil)
|
||||
|
@ -70,7 +69,6 @@ private final class SimpleEchoServer: ChannelInboundHandler {
|
|||
internal final class PromiseOnReadHandler: ChannelInboundHandler {
|
||||
public typealias InboundIn = ByteBuffer
|
||||
public typealias OutboundOut = ByteBuffer
|
||||
public typealias InboundUserEventIn = TLSUserEvent
|
||||
|
||||
private let promise: EventLoopPromise<ByteBuffer>
|
||||
private var data: NIOAny? = nil
|
||||
|
@ -104,7 +102,6 @@ private final class WriteCountingHandler: ChannelOutboundHandler {
|
|||
|
||||
public final class EventRecorderHandler<UserEventType>: ChannelInboundHandler where UserEventType: Equatable {
|
||||
public typealias InboundIn = ByteBuffer
|
||||
public typealias InboundUserEventIn = UserEventType
|
||||
|
||||
public enum RecordedEvents: Equatable {
|
||||
case Registered
|
||||
|
@ -175,7 +172,7 @@ public final class EventRecorderHandler<UserEventType>: ChannelInboundHandler wh
|
|||
}
|
||||
|
||||
public func userInboundEventTriggered(ctx: ChannelHandlerContext, event: Any) {
|
||||
guard let ourEvent = tryUnwrapInboundUserEventIn(event) else {
|
||||
guard let ourEvent = event as? UserEventType else {
|
||||
ctx.fireUserInboundEventTriggered(event: event)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -42,7 +42,6 @@ class IdleStateHandlerTest : XCTestCase {
|
|||
class TestWriteHandler: ChannelInboundHandler {
|
||||
typealias InboundIn = ByteBuffer
|
||||
typealias OutboundOut = ByteBuffer
|
||||
typealias InboundUserEventIn = IdleStateHandler.IdleStateEvent
|
||||
|
||||
private var read = false
|
||||
private let writeToChannel: Bool
|
||||
|
@ -62,7 +61,7 @@ class IdleStateHandlerTest : XCTestCase {
|
|||
XCTAssertTrue(self.read)
|
||||
}
|
||||
|
||||
XCTAssertTrue(assertEventFn(self.unwrapInboundUserEventIn(event)))
|
||||
XCTAssertTrue(assertEventFn(event as! IdleStateHandler.IdleStateEvent))
|
||||
ctx.close(promise: nil)
|
||||
}
|
||||
|
||||
|
|
|
@ -26,7 +26,6 @@ extension TypeAssistedChannelHandlerTest {
|
|||
|
||||
static var allTests : [(String, (TypeAssistedChannelHandlerTest) -> () throws -> Void)] {
|
||||
return [
|
||||
("testOptionalInboundIn", testOptionalInboundIn),
|
||||
("testCanDefineBothInboundAndOutbound", testCanDefineBothInboundAndOutbound),
|
||||
]
|
||||
}
|
||||
|
|
|
@ -16,22 +16,6 @@ import XCTest
|
|||
@testable import NIO
|
||||
|
||||
class TypeAssistedChannelHandlerTest: XCTestCase {
|
||||
func testOptionalInboundIn() throws {
|
||||
class TestClass: ChannelInboundHandler {
|
||||
public typealias InboundIn = String
|
||||
|
||||
func canUnwrap(_ data: NIOAny) -> Bool {
|
||||
return tryUnwrapInboundIn(data) != nil
|
||||
}
|
||||
}
|
||||
|
||||
let c = TestClass()
|
||||
let goodAny = NIOAny("Hello, world!")
|
||||
let badAny = NIOAny(42)
|
||||
XCTAssertTrue(c.canUnwrap(goodAny))
|
||||
XCTAssertFalse(c.canUnwrap(badAny))
|
||||
}
|
||||
|
||||
func testCanDefineBothInboundAndOutbound() throws {
|
||||
class TestClass: ChannelInboundHandler, ChannelOutboundHandler {
|
||||
public typealias OutboundIn = ByteBuffer
|
||||
|
|
Loading…
Reference in New Issue