Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any problem with my code and why it doesn't work #186

Open
pysrc opened this issue Mar 23, 2024 · 5 comments
Open

Is there any problem with my code and why it doesn't work #186

pysrc opened this issue Mar 23, 2024 · 5 comments

Comments

@pysrc
Copy link

pysrc commented Mar 23, 2024

use futures::{future, stream, AsyncReadExt, AsyncWriteExt, StreamExt};
use tokio::net::{TcpListener, TcpStream};
use tokio_util::compat::TokioAsyncReadCompatExt;

#[tokio::main]
async fn main() {
    
    tokio::spawn(async move {
        let lis = TcpListener::bind("0.0.0.0:8000").await.unwrap();
        let (conn, _) = lis.accept().await.unwrap();
        let muxcfg = yamux::Config::default();
        let mut c = yamux::Connection::new(conn.compat(), muxcfg, yamux::Mode::Server);
        println!("111");
        let mut server = stream::poll_fn(move |cx| c.poll_next_inbound(cx));
        let mut stream = server.next().await.unwrap().unwrap();
        println!("xxxx");
        let mut buf = [0u8; 1024];
        let n = stream.read(&mut buf).await.unwrap();
        let s = String::from_utf8_lossy(&buf[..n]).to_string();
        println!("{}", s);
        stream.close().await.unwrap();
        println!("s2");
        let mut stream = server.next().await.unwrap().unwrap();
        let mut buf = [0u8; 1024];
        let n = stream.read(&mut buf).await.unwrap();
        let s = String::from_utf8_lossy(&buf[..n]).to_string();
        println!("{}", s);
        stream.close().await.unwrap();
        println!("999");
    });
    tokio::spawn(async move {
        let s = TcpStream::connect("127.0.0.1:8000").await.unwrap();
        let muxcfg = yamux::Config::default();
        let mut c = yamux::Connection::new(s.compat(), muxcfg, yamux::Mode::Client);
        let mut stream = future::poll_fn(|cx| c.poll_new_outbound(cx)).await.unwrap();
        stream
            .write_all("Hello World! 1\n".as_bytes())
            .await
            .unwrap();
        stream.flush().await.unwrap();
        stream.close().await.unwrap();
        println!("send 2");
        let mut stream = future::poll_fn(|cx| c.poll_new_outbound(cx)).await.unwrap();
        stream
            .write_all("Hello World! 2\n".as_bytes())
            .await
            .unwrap();
        stream.flush().await.unwrap();
        stream.close().await.unwrap();
        tokio::time::sleep(tokio::time::Duration::from_secs(100)).await;
    });
    tokio::time::sleep(tokio::time::Duration::from_secs(100)).await;
}
@thomaseizinger
Copy link
Contributor

Yes. You need to continously call poll_next_inbound for streams to make progress.

This yamux implementation does not have any implicit background tasks but the streams are just connected to the Connection via channels. If you don't poll the connection, those channels are never read from and thus no data is sent to the network.

@thomaseizinger
Copy link
Contributor

You probably want to write yourself your own eventloop that houses the Connection and sends Streams to your app via channels.

@pysrc
Copy link
Author

pysrc commented Mar 24, 2024

You probably want to write yourself your own eventloop that houses the Connection and sends Streams to your app via channels.

Can you make some changes based on my code? I have tried many ways, but it still doesn't work.

@thomaseizinger
Copy link
Contributor

You probably want to write yourself your own eventloop that houses the Connection and sends Streams to your app via channels.

Can you make some changes based on my code? I have tried many ways, but it still doesn't work.

Sorry, don't have the capacity for that! Have you had a look at the tests in this repository?

@caelansar
Copy link

Please refer to this code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants