I looked into this SO page but it seems that it's outdated.
How should it be done with latest tokio?
Well you certainly don't want to be using tokio_io
. That's crate is very very old. The only official Tokio crates that are new are:
tokio
tokio-util
tokio-stream
As for how to modify data while using tokio::io::copy
, well, the main way is to not use tokio::io::copy
. For example, you can replace it with a loop that alternates between reading and writing.
loop {
let data = read_message(&mut read).await?;
let output = message_to_output(data);
write.write_all(&output).await?;
}
This loop uses some example functions to do the reading and writing.
One option is to use the tokio_util::codec
module. It can be used to more easily implement the read_message
and message_to_output
parts of the above loop, but you still need a loop that forwards messages (or a call to forward
) rather than tokio::io::copy
.
Thank you very much for your kind response!
I wonder is it possible to do your loop inside tokio::spawn
?
Because I'm actually trying to modify this code to fit my use case.
I could really use some mentoring on how to arrange my code here, I'm trying it myself already with my clumsy baysteps
There should be no issue with placing it inside a tokio::spawn
.
I checked code from tokio and realised I cannot do it in a loop since I don't know how to resume the io.
Now I'm checking out why there's a ready
macro in this code base
Okay now I finished this job.
I copied the whole tokio::io::copy code and altered it, although I'm still not sure if it has to be this complicated.
In case anyone is intrested my code is here.
I might make a library to do this job later.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.