nervosnetwork / tentacle

A multiplexed p2p network framework that supports custom protocols
https://docs.rs/tentacle
MIT License
54 stars 24 forks source link

[yamux] how to use the tokio-yamux when the client stream is created continually? #254

Closed abel-von closed 4 years ago

abel-von commented 4 years ago

Hi, I'm trying to use the tokio-yamux in my project, but i have encountered a problem.

in the example i can find the codes below

tokio::spawn(async move {
            loop {
                match session.next().await {
                    Some(_) => (),
                    None => break,
                }
            }
        });

I know this is like an engine for client to send the message out. but this session is moved to the async block, and we can not use it anymore. so all the streams of this session should be opened before spawn the async block.

but in my senario, the yamux client is a proxy, the stream is opened based on the new connection coming to the proxy, so I can not open all streams before spawn the async block. I tried to workaround by calling other methods in session to make sure the events of client be sent out. but i found that only calling next() can the client send events out.

So is there any way to solve my issue?

thanks very much

driftluo commented 4 years ago

use control to open a new stream on the async way https://github.com/nervosnetwork/tentacle/blob/master/yamux/src/session.rs#L259 https://github.com/nervosnetwork/tentacle/blob/master/yamux/src/control.rs#L23

abel-von commented 4 years ago

I didn't notice that the control can open stream asynchronouly, Thank you very much. @driftluo