Description
The Decoder
is used by the FramedTcp
transport to transform a stream-based protocol (TCP) into a packet-based protocol that fits really well with the concept of message.
The Decoder
collects data from the stream until it can be considered a message. In that process, each chunk of data received from the network is written in a temporal buffer. If that data is not yet a message, then, de Decoder
copies from that buffer to its internal buffer in order to wait for more chunks.
This last copy can be avoided if we are able to read directly into the decoder. To get this, the decoder could expose its buffer in order to allow the stream.read()
dumping its data directly into the decoder, or even better, the Decoder
can receive a Read
trait object (that would be the socket) from which extract the data. Something similar to:
Decoder::decode_from(&self mut, reader: &dyn Read, impl decoded_callback: impl FnMut(&[u8]) -> Result<()>
Note that since it works in a non-blocking way, several calls to read must be performed inside this function until receiving a WouldBlock
io error.