diff --git a/server/stat/src/upstream/upstreammgr.rs b/server/stat/src/upstream/upstreammgr.rs index 78fc044..6de4795 100644 --- a/server/stat/src/upstream/upstreammgr.rs +++ b/server/stat/src/upstream/upstreammgr.rs @@ -1,10 +1,12 @@ use std::rc::{Rc, Weak}; use std::cell::RefCell; use std::sync::{Arc, Mutex}; -use std::error::Error; use tokio::net::{TcpStream, ToSocketAddrs}; use tokio::io::AsyncWriteExt; use tokio::runtime::Runtime; +use tokio::io::Interest; +use std::error::Error; +use std::io; use r9_macro::SharedFromSelf; use r9_macro_derive::SharedFromSelf; @@ -17,11 +19,45 @@ pub struct UpStreamMgr { } async fn run_app() -> Result<(), Box> { - // Connect to a peer - let mut stream = TcpStream::connect("127.0.0.1:8080").await?; + let mut stream = TcpStream::connect("192.168.100.39:7617").await?; - // Write some data. - stream.write_all(b"hello world!").await?; + loop { + let ready = stream.ready(Interest::READABLE | Interest::WRITABLE).await?; + + if ready.is_readable() { + let mut data = vec![0; 1024]; + // Try to read data, this may still fail with `WouldBlock` + // if the readiness event is a false positive. + match stream.try_read(&mut data) { + Ok(n) => { + println!("read {} bytes", n); + } + Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => { + continue; + } + Err(e) => { + //return Err(e.into()); + } + } + + } + + if ready.is_writable() { + // Try to write data, this may still fail with `WouldBlock` + // if the readiness event is a false positive. + match stream.try_write(b"hello world") { + Ok(n) => { + println!("write {} bytes", n); + } + Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => { + continue + } + Err(e) => { + //return Err(e.into()); + } + } + } + } Ok(()) } @@ -39,6 +75,7 @@ impl UpStreamMgr { std::thread::spawn(move || { let rt = tokio::runtime::Builder::new_multi_thread() .worker_threads(4) + .enable_all() .thread_name("my-custom-name") .thread_stack_size(3 * 1024 * 1024) .build()