Proper use of NIOThrowingAsyncProducer

Actually there is an easier way to do this. I just did not find the correct class yesterday.

You can use these two handler on both sides and they will do the correct length prefixing and stripping. Important here is adding these handlers on both sides.

try channel.pipeline.syncOperations.addHandlers([
    LengthFieldPrepender(lengthFieldBitLength: .threeBytes),
    ByteToMessageHandler(
        LengthFieldBasedFrameDecoder(lengthFieldBitLength: .threeBytes),
        maximumBufferSize: 1000
    ),
])
1 Like

Thank you @FranzBusch. So it seems to work fine, my large messages now send great, but under a few situations it behaves interestingly.

  1. Sometimes there is too much data in a message(looks like extra binary when I read a string from the buffer)

//An unrelated issue for the Prependers

  1. On the server side the SSL Handler seems to work one time then throws an SSL error.

I am wondering if I am adding the handlers wrong. Both my server and client produce a NIOAsyncChannel and I then use that channel accordingly. Then Server producers a server channel and child channel. The client simply produces 1 NIOAsyncChannel that i read and write from. I limit Buffer size to 16mb.

I then add the handlers like so

                for try await childChannel in serverChannel.inboundStream {
                    
                    taskGroup.addTask {
                        try await childChannel.channel.pipeline.addHandlers([
                            sslServerHandler,
                            LengthFieldPrepender(lengthFieldBitLength: .threeBytes),
                            ByteToMessageHandler(
                                LengthFieldBasedFrameDecoder(lengthFieldBitLength: .threeBytes),
                                maximumBufferSize: 16777216
                            )
                        ], position: .first).get()
                    }
                    strongSelf.childChannel = childChannel
                    taskGroup.addTask { [weak self] in
                        guard let strongSelf = self else { return }
                        await strongSelf.handleChildChannel(childChannel, logger: logger)
                    }
                }

The same concept is applied to the client inbound stream expect with out the ssl hander

Any suggestions on how I add these handlers?

Thanks,

Cole

@FranzBusch So I have a question in regards to Backpressure. I am trying to send about 300mb of data from my server to client. I have been able to send that amount of data from my client to server no problem. But when sending server to client the NIOAsyncChannelInboundStream seams to skip channelRead and goes straight to firing channelReadComplete. Which in turn seams to cause my device to loop continuously until the device crashes and never delivers the data. I assume this is due to the amount of data being fed into NIOTS and then passed to NIOAsyncChannelInboundStream. Would behavior like this be due to backpressure water mark?

Thanks,

Cole