func channelRead()

in Sources/HTTPServerWithQuiescingDemo/main.swift [26:55]


    func channelRead(context: ChannelHandlerContext, data: NIOAny) {
        let req = self.unwrapInboundIn(data)
        switch req {
        case .head(let head):
            guard head.version == HTTPVersion(major: 1, minor: 1) else {
                context.write(self.wrapOutboundOut(.head(HTTPResponseHead(version: head.version, status: .badRequest))), promise: nil)
                context.writeAndFlush(self.wrapOutboundOut(.end(nil))).whenComplete { (_: Result<(), Error>) in
                    context.close(promise: nil)
                }
                return
            }
        case .body:
            () // ignore
        case .end:
            var buffer = context.channel.allocator.buffer(capacity: 128)
            buffer.writeStaticString("received request; waiting 30s then finishing up request\n")
            buffer.writeStaticString("press Ctrl+C in the server's terminal or run the following command to initiate server shutdown\n")
            buffer.writeString("    kill -INT \(getpid())\n")
            context.write(self.wrapOutboundOut(.head(HTTPResponseHead(version: HTTPVersion(major: 1, minor: 1),
                                                                  status: .ok))), promise: nil)
            context.writeAndFlush(self.wrapOutboundOut(.body(.byteBuffer(buffer))), promise: nil)
            buffer.clear()
            buffer.writeStaticString("done with the request now\n")
            _ = context.eventLoop.scheduleTask(in: .seconds(30)) { [buffer] in
                context.write(self.wrapOutboundOut(.body(.byteBuffer(buffer))), promise: nil)
                context.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil)

            }
        }
    }