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

Async tcp #660

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions protocol/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ path = "../std_or_web"
version = "0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { version = "1", features = [ "rt", "net" ]}
reqwest = { version = "0.11.4", features = [ "blocking" ]}
85 changes: 81 additions & 4 deletions protocol/src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ use std::default;
use std::fmt;
use std::io;
use std::io::{Read, Write};
use std::net::TcpStream;
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, AtomicI32, Ordering};
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf};
use tokio::net::TcpStream;

pub const SUPPORTED_PROTOCOLS: [i32; 25] = [
756, 754, 753, 751, 736, 735, 578, 575, 498, 490, 485, 480, 477, 452, 451, 404, 340, 316, 315,
Expand Down Expand Up @@ -1027,7 +1030,8 @@ impl ::std::fmt::Display for Error {
type Aes128Cfb = Cfb8<Aes128>;

pub struct Conn {
stream: TcpStream,
//stream: TcpStream,
stream: std::net::TcpStream,
pub host: String,
pub port: u16,
direction: Direction,
Expand All @@ -1051,7 +1055,8 @@ impl Conn {
} else {
format!("{}:{}", parts[0], parts[1])
};
let stream = TcpStream::connect(&*address)?;
//let mut stream = TcpStream::connect(&*address).await?;
let mut stream = std::net::TcpStream::connect(&*address)?;
Result::Ok(Conn {
stream,
host: parts[0].to_owned(),
Expand Down Expand Up @@ -1097,7 +1102,9 @@ impl Conn {
if self.compression_threshold >= 0 && extra == 1 {
VarInt(0).write_to(self)?;
}
self.write_all(&buf)?;
//self.write_all(&buf)?;
std::io::Write::write_all(self, &buf)?;
//AsyncWriteExt::write_all(&mut self, &buf)?; // TODO

Ok(())
}
Expand Down Expand Up @@ -1465,6 +1472,76 @@ pub struct StatusPlayer {
id: String,
}

// TODO: is this the way to implement async r/w traits? See discussion https://users.rust-lang.org/t/what-to-pin-when-implementing-asyncread/63019/6
// I'm using tokio's Async{Read,Write}, maybe instead for runtime agnostic use https://crates.io/crates/futures?
// Or are these the right traits? Ext? Something else? Just need to wrap enough to let use as async, encryption pipeline
impl AsyncRead for Conn {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
unimplemented!()
/* TODO
match self.cipher.as_mut() {
Option::None => self.stream.read(buf).await,
Option::Some(cipher) => {
let ret = self.stream.read(buf).await?;
cipher.decrypt(&mut buf[..ret]);

Ok(ret)
}
}
*/
}
}

impl AsyncWrite for Conn {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
unimplemented!()
/* TODO
match self.cipher.as_mut() {
Option::None => self.stream.write(buf).await,
Option::Some(cipher) => {
let mut data = vec![0; buf.len()];
data[..buf.len()].clone_from_slice(&buf[..]);

cipher.encrypt(&mut data);

self.stream.write_all(&data).await?;
Ok(buf.len())
}
}
*/
}

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
unimplemented!()
// TODO self.stream.poll_flush().await?
}

fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
unimplemented!()
}

fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[io::IoSlice<'_>],
) -> Poll<Result<usize, io::Error>> {
unimplemented!()
}

fn is_write_vectored(&self) -> bool {
false
}
}

// TODO: remove sync
impl Read for Conn {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match self.cipher.as_mut() {
Expand Down