diff --git a/Sources/NIO/Codec.swift b/Sources/NIO/Codec.swift index 03bf63aa8..7ec20dd60 100644 --- a/Sources/NIO/Codec.swift +++ b/Sources/NIO/Codec.swift @@ -63,9 +63,10 @@ public protocol ByteToMessageDecoder { /// - parameters: /// - ctx: The `ChannelHandlerContext` which this `ByteToMessageDecoder` belongs to. /// - buffer: The `ByteBuffer` from which we decode. + /// - seenEOF: `true` if EOF has been seen. Usually if this is `false` the handler has been removed. /// - returns: `DecodingState.continue` if we should continue calling this method or `DecodingState.needMoreData` if it should be called // again when more data is present in the `ByteBuffer`. - mutating func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState + mutating func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer, seenEOF: Bool) throws -> DecodingState /// Called once this `ByteToMessageDecoder` is removed from the `ChannelPipeline`. /// @@ -108,10 +109,6 @@ extension ByteToMessageDecoder { return buffer.capacity > 1024 && (buffer.capacity - buffer.readerIndex) >= buffer.readerIndex } - public func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState { - return .needMoreData - } - public func wrapInboundOut(_ value: InboundOut) -> NIOAny { return NIOAny(value) } @@ -156,13 +153,19 @@ private struct B2MDBuffer { private var state: State = .ready private var buffers: CircularBuffer = CircularBuffer(initialCapacity: 4) + private let emptyByteBuffer: ByteBuffer + + init(emptyByteBuffer: ByteBuffer) { + assert(emptyByteBuffer.readableBytes == 0) + self.emptyByteBuffer = emptyByteBuffer + } } // MARK: B2MDBuffer Main API extension B2MDBuffer { /// Start processing some bytes if possible, if we receive a returned buffer (through `.available(ByteBuffer)`) /// we _must_ indicate the processing has finished by calling `finishProcessing`. - mutating func startProcessing() -> BufferAvailability { + mutating func startProcessing(allowEmptyBuffer: Bool) -> BufferAvailability { switch self.state { case .processingInProgress: return .bufferAlreadyBeingProcessed @@ -170,7 +173,7 @@ extension B2MDBuffer { var buffer = self.buffers.removeFirst() buffer.writeBuffers(self.buffers) self.buffers.removeAll(keepingCapacity: self.buffers.capacity < 16) // don't grow too much - if buffer.readableBytes > 0 { + if buffer.readableBytes > 0 || allowEmptyBuffer { self.state = .processingInProgress return .available(buffer) } else { @@ -178,6 +181,10 @@ extension B2MDBuffer { } case .ready: assert(self.buffers.count == 0) + if allowEmptyBuffer { + self.state = .processingInProgress + return .available(self.emptyByteBuffer) + } return .nothingAvailable } } @@ -266,7 +273,9 @@ public class ByteToMessageHandler { internal private(set) var decoder: Decoder? // only `nil` if we're already decoding (ie. we're re-entered) private var state: State = .active private var removalState: RemovalState = .notBeingRemoved - private var buffer: B2MDBuffer = B2MDBuffer() + // sadly to construct a B2MDBuffer we need an empty ByteBuffer which we can only get from the allocator, so IUO. + private var buffer: B2MDBuffer! + private var seenEOF: Bool = false public init(_ decoder: Decoder) { self.decoder = decoder @@ -287,8 +296,8 @@ extension ByteToMessageHandler { // MARK: ByteToMessageHandler's Main API extension ByteToMessageHandler { - private func withNextBuffer(_ body: (inout Decoder, inout ByteBuffer) throws -> DecodingState) rethrows -> B2MDBuffer.BufferProcessingResult { - switch self.buffer.startProcessing() { + private func withNextBuffer(allowEmptyBuffer: Bool, _ body: (inout Decoder, inout ByteBuffer) throws -> DecodingState) rethrows -> B2MDBuffer.BufferProcessingResult { + switch self.buffer.startProcessing(allowEmptyBuffer: allowEmptyBuffer) { case .bufferAlreadyBeingProcessed: return .cannotProcessReentrantly case .nothingAvailable: @@ -329,12 +338,14 @@ extension ByteToMessageHandler { } private func decodeLoop(ctx: ChannelHandlerContext, decodeMode: DecodeMode) throws -> B2MDBuffer.BufferProcessingResult { + var allowEmptyBuffer = decodeMode == .last while decodeMode == .last || self.removalState == .notBeingRemoved { - let result = try self.withNextBuffer { decoder, buffer in + let result = try self.withNextBuffer(allowEmptyBuffer: allowEmptyBuffer) { decoder, buffer in if decodeMode == .normal { return try decoder.decode(ctx: ctx, buffer: &buffer) } else { - return try decoder.decodeLast(ctx: ctx, buffer: &buffer) + allowEmptyBuffer = false + return try decoder.decodeLast(ctx: ctx, buffer: &buffer, seenEOF: self.seenEOF) } } switch result { @@ -354,6 +365,7 @@ extension ByteToMessageHandler { extension ByteToMessageHandler: ChannelInboundHandler { public func handlerAdded(ctx: ChannelHandlerContext) { + self.buffer = B2MDBuffer(emptyByteBuffer: ctx.channel.allocator.buffer(capacity: 0)) // here we can force it because we know that the decoder isn't in use if we're just adding this handler self.decoder!.decoderAdded(ctx: ctx) } @@ -398,10 +410,21 @@ extension ByteToMessageHandler: ChannelInboundHandler { /// Call `decodeLast` before forward the event through the pipeline. public func channelInactive(ctx: ChannelHandlerContext) { + self.seenEOF = true + self.processLeftovers(ctx: ctx) ctx.fireChannelInactive() } + + public func userInboundEventTriggered(ctx: ChannelHandlerContext, event: Any) { + if event as? ChannelEvent == .some(.inputClosed) { + self.seenEOF = true + + self.processLeftovers(ctx: ctx) + } + ctx.fireUserInboundEventTriggered(event) + } } /// `ChannelOutboundHandler` which allows users to encode custom messages to a `ByteBuffer` easily. diff --git a/Sources/NIOChatServer/main.swift b/Sources/NIOChatServer/main.swift index 9d9a96e84..9dc5f2bb5 100644 --- a/Sources/NIOChatServer/main.swift +++ b/Sources/NIOChatServer/main.swift @@ -31,6 +31,10 @@ final class LineDelimiterCodec: ByteToMessageDecoder { } return .needMoreData } + + public func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer, seenEOF: Bool) throws -> DecodingState { + return try self.decode(ctx: ctx, buffer: &buffer) + } } /// This `ChannelInboundHandler` demonstrates a few things: diff --git a/Sources/NIOTLS/SNIHandler.swift b/Sources/NIOTLS/SNIHandler.swift index 86f05faa8..092af9df6 100644 --- a/Sources/NIOTLS/SNIHandler.swift +++ b/Sources/NIOTLS/SNIHandler.swift @@ -107,7 +107,7 @@ public class SNIHandler: ByteToMessageDecoder { self.waitingForUser = false } - public func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState { + public func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer, seenEOF: Bool) throws -> DecodingState { ctx.fireChannelRead(NIOAny(buffer)) return .needMoreData } diff --git a/Sources/NIOWebSocket/WebSocketFrameDecoder.swift b/Sources/NIOWebSocket/WebSocketFrameDecoder.swift index a9b6bec9e..1e6569c20 100644 --- a/Sources/NIOWebSocket/WebSocketFrameDecoder.swift +++ b/Sources/NIOWebSocket/WebSocketFrameDecoder.swift @@ -281,7 +281,7 @@ public final class WebSocketFrameDecoder: ByteToMessageDecoder { return .needMoreData } - public func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState { + public func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer, seenEOF: Bool) throws -> DecodingState { // EOF is not semantic in WebSocket, so ignore this. return .needMoreData } diff --git a/Tests/NIOTests/CodecTest+XCTest.swift b/Tests/NIOTests/CodecTest+XCTest.swift index e35ebc3c1..7ac375d0e 100644 --- a/Tests/NIOTests/CodecTest+XCTest.swift +++ b/Tests/NIOTests/CodecTest+XCTest.swift @@ -41,6 +41,11 @@ extension ByteToMessageDecoderTest { ("testHandlerRemoveInChannelRead", testHandlerRemoveInChannelRead), ("testChannelCloseInChannelRead", testChannelCloseInChannelRead), ("testDecodeLoopGetsInterruptedWhenRemovalIsTriggered", testDecodeLoopGetsInterruptedWhenRemovalIsTriggered), + ("testDecodeLastIsInvokedOnceEvenIfNothingEverArrivedOnChannelClosed", testDecodeLastIsInvokedOnceEvenIfNothingEverArrivedOnChannelClosed), + ("testDecodeLastIsInvokedOnceEvenIfNothingEverArrivedOnChannelHalfClosure", testDecodeLastIsInvokedOnceEvenIfNothingEverArrivedOnChannelHalfClosure), + ("testDecodeLastHasSeenEOFFalseOnHandlerRemoved", testDecodeLastHasSeenEOFFalseOnHandlerRemoved), + ("testDecodeLastHasSeenEOFFalseOnHandlerRemovedEvenIfNoData", testDecodeLastHasSeenEOFFalseOnHandlerRemovedEvenIfNoData), + ("testDecodeLastHasSeenEOFTrueOnChannelInactive", testDecodeLastHasSeenEOFTrueOnChannelInactive), ] } } diff --git a/Tests/NIOTests/CodecTest.swift b/Tests/NIOTests/CodecTest.swift index cb7f16cc4..88d3fcf64 100644 --- a/Tests/NIOTests/CodecTest.swift +++ b/Tests/NIOTests/CodecTest.swift @@ -62,6 +62,11 @@ public class ByteToMessageDecoderTest: XCTestCase { ctx.fireChannelRead(self.wrapInboundOut(buffer.readInteger()!)) return .continue } + + func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer, seenEOF: Bool) throws -> DecodingState { + XCTAssertTrue(seenEOF) + return self.decode(ctx: ctx, buffer: &buffer) + } } private final class ForeverDecoder: ByteToMessageDecoder { @@ -71,6 +76,11 @@ public class ByteToMessageDecoderTest: XCTestCase { func decode(ctx: ChannelHandlerContext, buffer: inout ByteBuffer) -> DecodingState { return .needMoreData } + + func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer, seenEOF: Bool) throws -> DecodingState { + XCTAssertTrue(seenEOF) + return self.decode(ctx: ctx, buffer: &buffer) + } } private final class LargeChunkDecoder: ByteToMessageDecoder { @@ -85,6 +95,11 @@ public class ByteToMessageDecoderTest: XCTestCase { ctx.fireChannelRead(self.wrapInboundOut(buffer)) return .continue } + + func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer, seenEOF: Bool) throws -> DecodingState { + XCTAssertTrue(seenEOF) + return self.decode(ctx: ctx, buffer: &buffer) + } } // A special case decoder that decodes only once there is 5,120 bytes in the buffer, @@ -101,6 +116,11 @@ public class ByteToMessageDecoderTest: XCTestCase { ctx.fireChannelRead(self.wrapInboundOut(buffer.readSlice(length: 2048)!)) return .continue } + + func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer, seenEOF: Bool) throws -> DecodingState { + XCTAssertTrue(seenEOF) + return self.decode(ctx: ctx, buffer: &buffer) + } } func testDecoder() throws { @@ -262,6 +282,11 @@ public class ByteToMessageDecoderTest: XCTestCase { } return .continue } + + func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer, seenEOF: Bool) throws -> DecodingState { + XCTAssertTrue(seenEOF) + return .needMoreData + } } let testDecoder = TestDecoder() @@ -308,9 +333,12 @@ public class ByteToMessageDecoderTest: XCTestCase { class HandItThroughDecoder: ByteToMessageDecoder { typealias InboundOut = ByteBuffer + var decodeLastCalls = 0 + func decode(ctx: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState { + let originalBuffer = buffer ctx.fireChannelRead(self.wrapInboundOut(buffer.readSlice(length: buffer.readableBytes)!)) - if buffer.readableBytesView.last == "0".utf8.last { + if originalBuffer.readableBytesView.last == "0".utf8.last { ctx.close().whenFailure { error in XCTFail("unexpected error: \(error)") } @@ -318,13 +346,18 @@ public class ByteToMessageDecoderTest: XCTestCase { return .needMoreData } - func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState { - XCTFail("shouldn't be called") - return .continue + func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer, seenEOF: Bool) throws -> DecodingState { + XCTAssertTrue(seenEOF) + self.decodeLastCalls += 1 + XCTAssertEqual(1, self.decodeLastCalls) + return .needMoreData } } - let channel = EmbeddedChannel(handler: ByteToMessageHandler(HandItThroughDecoder())) + let decoder = HandItThroughDecoder() + let channel = EmbeddedChannel(handler: ByteToMessageHandler(decoder)) + XCTAssertNoThrow(try channel.connect(to: SocketAddress(ipAddress: "1.2.3.4", port: 5678)).wait()) + XCTAssertTrue(channel.isActive) var buffer = channel.allocator.buffer(capacity: 16) buffer.clear() @@ -336,12 +369,14 @@ public class ByteToMessageDecoderTest: XCTestCase { buffer.clear() buffer.writeStaticString("4567890") XCTAssertNoThrow(try channel.writeInbound(buffer)) + (channel.eventLoop as! EmbeddedEventLoop).run() XCTAssertFalse(channel.isActive) XCTAssertEqual("1", channel.readInbound(as: ByteBuffer.self).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) }) XCTAssertEqual("23", channel.readInbound(as: ByteBuffer.self).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) }) XCTAssertEqual("4567890", channel.readInbound(as: ByteBuffer.self).map { String(decoding: $0.readableBytesView, as: Unicode.UTF8.self) }) XCTAssertNil(channel.readInbound()) + XCTAssertEqual(1, decoder.decodeLastCalls) } func testLeftOversMakeDecodeLastCalled() { @@ -428,7 +463,8 @@ public class ByteToMessageDecoderTest: XCTestCase { } } - mutating func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState { + mutating func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer, seenEOF: Bool) throws -> DecodingState { + XCTAssertTrue(seenEOF) ctx.fireChannelRead(self.wrapInboundOut(buffer.readableBytes * -1)) return .needMoreData } @@ -477,6 +513,11 @@ public class ByteToMessageDecoderTest: XCTestCase { return .needMoreData } } + + mutating func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer, seenEOF: Bool) throws -> DecodingState { + XCTAssertTrue(seenEOF) + return try self.decode(ctx: ctx, buffer: &buffer) + } } let channel = EmbeddedChannel(handler: ByteToMessageHandler(ProcessAndReentrantylyProcessExponentiallyLessStuffDecoder())) var buffer = channel.allocator.buffer(capacity: 16) @@ -507,7 +548,8 @@ public class ByteToMessageDecoderTest: XCTestCase { } } - mutating func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState { + mutating func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer, seenEOF: Bool) throws -> DecodingState { + XCTAssertTrue(seenEOF) ctx.fireChannelRead(self.wrapInboundOut(buffer)) return .needMoreData } @@ -538,7 +580,8 @@ public class ByteToMessageDecoderTest: XCTestCase { } } - mutating func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState { + mutating func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer, seenEOF: Bool) throws -> DecodingState { + XCTAssertFalse(seenEOF) ctx.fireChannelRead(self.wrapInboundOut(buffer)) return .needMoreData } @@ -570,7 +613,8 @@ public class ByteToMessageDecoderTest: XCTestCase { } } - mutating func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState { + mutating func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer, seenEOF: Bool) throws -> DecodingState { + XCTAssertTrue(seenEOF) return .needMoreData } } @@ -611,7 +655,8 @@ public class ByteToMessageDecoderTest: XCTestCase { return .continue } - mutating func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState { + mutating func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer, seenEOF: Bool) throws -> DecodingState { + XCTAssertFalse(seenEOF) self.callsToDecodeLast += 1 XCTAssertLessThanOrEqual(self.callsToDecodeLast, 2) ctx.fireChannelRead(self.wrapInboundOut(String(decoding: buffer.readBytes(length: 4) ?? @@ -639,6 +684,169 @@ public class ByteToMessageDecoderTest: XCTestCase { XCTAssertEqual($0, channel.readInbound()) } } + + func testDecodeLastIsInvokedOnceEvenIfNothingEverArrivedOnChannelClosed() { + class Decoder: ByteToMessageDecoder { + typealias InboundOut = () + var decodeLastCalls = 0 + + public func decode(ctx: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState { + XCTFail("did not expect to see decode called") + return .needMoreData + } + + public func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer, seenEOF: Bool) throws -> DecodingState { + XCTAssertTrue(seenEOF) + self.decodeLastCalls += 1 + XCTAssertEqual(1, self.decodeLastCalls) + XCTAssertEqual(0, buffer.readableBytes) + ctx.fireChannelRead(self.wrapInboundOut(())) + return .needMoreData + } + } + + let decoder = Decoder() + let channel = EmbeddedChannel(handler: ByteToMessageHandler(decoder)) + + XCTAssertNoThrow(try channel.connect(to: SocketAddress(ipAddress: "1.2.3.4", port: 5678)).wait()) + XCTAssertNil(channel.readInbound()) + + XCTAssertNoThrow(try channel.close().wait()) + XCTAssertNotNil(channel.readInbound()) + XCTAssertNil(channel.readInbound()) + + XCTAssertEqual(1, decoder.decodeLastCalls) + } + + func testDecodeLastIsInvokedOnceEvenIfNothingEverArrivedOnChannelHalfClosure() { + class Decoder: ByteToMessageDecoder { + typealias InboundOut = () + var decodeLastCalls = 0 + + public func decode(ctx: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState { + XCTFail("did not expect to see decode called") + return .needMoreData + } + + public func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer, seenEOF: Bool) throws -> DecodingState { + XCTAssertTrue(seenEOF) + self.decodeLastCalls += 1 + XCTAssertEqual(1, self.decodeLastCalls) + XCTAssertEqual(0, buffer.readableBytes) + ctx.fireChannelRead(self.wrapInboundOut(())) + return .needMoreData + } + } + + let decoder = Decoder() + let channel = EmbeddedChannel(handler: ByteToMessageHandler(decoder)) + + XCTAssertNoThrow(try channel.connect(to: SocketAddress(ipAddress: "1.2.3.4", port: 5678)).wait()) + XCTAssertNil(channel.readInbound()) + + channel.pipeline.fireUserInboundEventTriggered(ChannelEvent.inputClosed) + XCTAssertNotNil(channel.readInbound()) + XCTAssertNil(channel.readInbound()) + + XCTAssertEqual(1, decoder.decodeLastCalls) + + XCTAssertNoThrow(XCTAssertFalse(try channel.finish())) + + XCTAssertNil(channel.readInbound()) + XCTAssertEqual(1, decoder.decodeLastCalls) + } + + func testDecodeLastHasSeenEOFFalseOnHandlerRemoved() { + class Decoder: ByteToMessageDecoder { + typealias InboundOut = () + var decodeLastCalls = 0 + + public func decode(ctx: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState { + XCTAssertEqual(1, buffer.readableBytes) + return .needMoreData + } + + public func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer, seenEOF: Bool) throws -> DecodingState { + self.decodeLastCalls += 1 + XCTAssertEqual(1, buffer.readableBytes) + XCTAssertEqual(1, self.decodeLastCalls) + XCTAssertFalse(seenEOF) + return .needMoreData + } + } + + let decoder = Decoder() + let channel = EmbeddedChannel(handler: ByteToMessageHandler(decoder)) + XCTAssertNoThrow(try channel.connect(to: SocketAddress(ipAddress: "1.2.3.4", port: 5678)).wait()) + var buffer = channel.allocator.buffer(capacity: 1) + buffer.writeString("x") + XCTAssertNoThrow(try channel.writeInbound(buffer)) + let removalFuture = channel.pipeline.context(handlerType: ByteToMessageHandler.self).flatMap { + channel.pipeline.removeHandler(ctx: $0) + } + (channel.eventLoop as! EmbeddedEventLoop).run() + XCTAssertNoThrow(try removalFuture.wait()) + XCTAssertEqual(1, decoder.decodeLastCalls) + } + + func testDecodeLastHasSeenEOFFalseOnHandlerRemovedEvenIfNoData() { + class Decoder: ByteToMessageDecoder { + typealias InboundOut = () + var decodeLastCalls = 0 + + public func decode(ctx: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState { + XCTFail("shouldn't have been called") + return .needMoreData + } + + public func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer, seenEOF: Bool) throws -> DecodingState { + self.decodeLastCalls += 1 + XCTAssertEqual(0, buffer.readableBytes) + XCTAssertEqual(1, self.decodeLastCalls) + XCTAssertFalse(seenEOF) + return .needMoreData + } + } + + let decoder = Decoder() + let channel = EmbeddedChannel(handler: ByteToMessageHandler(decoder)) + XCTAssertNoThrow(try channel.connect(to: SocketAddress(ipAddress: "1.2.3.4", port: 5678)).wait()) + let removalFuture = channel.pipeline.context(handlerType: ByteToMessageHandler.self).flatMap { + channel.pipeline.removeHandler(ctx: $0) + } + (channel.eventLoop as! EmbeddedEventLoop).run() + XCTAssertNoThrow(try removalFuture.wait()) + XCTAssertEqual(1, decoder.decodeLastCalls) + } + + func testDecodeLastHasSeenEOFTrueOnChannelInactive() { + class Decoder: ByteToMessageDecoder { + typealias InboundOut = () + var decodeLastCalls = 0 + + public func decode(ctx: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState { + XCTAssertEqual(1, buffer.readableBytes) + return .needMoreData + } + + public func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer, seenEOF: Bool) throws -> DecodingState { + self.decodeLastCalls += 1 + XCTAssertEqual(1, buffer.readableBytes) + XCTAssertEqual(1, self.decodeLastCalls) + XCTAssertTrue(seenEOF) + return .needMoreData + } + } + + let decoder = Decoder() + let channel = EmbeddedChannel(handler: ByteToMessageHandler(decoder)) + XCTAssertNoThrow(try channel.connect(to: SocketAddress(ipAddress: "1.2.3.4", port: 5678)).wait()) + var buffer = channel.allocator.buffer(capacity: 1) + buffer.writeString("x") + XCTAssertNoThrow(try channel.writeInbound(buffer)) + XCTAssertNoThrow(XCTAssertFalse(try channel.finish())) + XCTAssertEqual(1, decoder.decodeLastCalls) + } } public class MessageToByteEncoderTest: XCTestCase { @@ -713,7 +921,7 @@ private class PairOfBytesDecoder: ByteToMessageDecoder { } } - func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState { + func decodeLast(ctx: ChannelHandlerContext, buffer: inout ByteBuffer, seenEOF: Bool) throws -> DecodingState { self.decodeLastCalls += 1 XCTAssertEqual(1, self.decodeLastCalls) self.lastPromise.succeed(buffer) diff --git a/docs/public-api-changes-NIO1-to-NIO2.md b/docs/public-api-changes-NIO1-to-NIO2.md index f16eb058c..a2ecad84a 100644 --- a/docs/public-api-changes-NIO1-to-NIO2.md +++ b/docs/public-api-changes-NIO1-to-NIO2.md @@ -10,6 +10,8 @@ - `ByteToMessageDecoder`s now need to be wrapped in `ByteToMessageHandler` before they can be added to the pipeline. before: `pipeline.add(MyDecoder())`, after: `pipeline.add(ByteToMessageHandler(MyDecoder()))` +- `ByteToMessageDecoder` now requires the implementation of `decodeLast` +- `ByteToMessageDecoder.decodeLast` has a new parameter `seenEOF: Bool` - `EventLoop.makePromise`/`makeSucceededFuture`/`makeFailedFuture` instead of `new*`, also `result:`/`error:` labels dropped - `SocketAddress.makeAddressResolvingHost(:port:)` instead of `SocketAddress.newAddressResolving(host:port:)`