1
0

removing several unnecessary async marks

This commit is contained in:
EAimTY 2023-01-27 13:41:57 +09:00
parent 8b985b1586
commit 8d4c3dc8da
2 changed files with 26 additions and 17 deletions

View File

@ -8,4 +8,4 @@ bytes = { version = "1.3.0", default-features = false, features = ["std"] }
futures-util = { version = "0.3.25", default-features = false, features = ["io", "std"] } futures-util = { version = "0.3.25", default-features = false, features = ["io", "std"] }
quinn = { version = "0.9.3", default-features = false, features = ["futures-io"] } quinn = { version = "0.9.3", default-features = false, features = ["futures-io"] }
thiserror = { version = "1.0.38", default-features = false } thiserror = { version = "1.0.38", default-features = false }
tuic = { path = "../tuic", default-features = false, features = ["async_marshal", "model"] } tuic = { path = "../tuic", default-features = false, features = ["async_marshal", "marshal", "model"] }

View File

@ -1,11 +1,11 @@
use self::side::Side; use self::side::Side;
use bytes::Bytes; use bytes::{BufMut, Bytes};
use futures_util::{io::Cursor, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}; use futures_util::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use quinn::{ use quinn::{
Connection as QuinnConnection, ConnectionError, RecvStream, SendDatagramError, SendStream, Connection as QuinnConnection, ConnectionError, RecvStream, SendDatagramError, SendStream,
}; };
use std::{ use std::{
io::Error as IoError, io::{Cursor, Error as IoError},
pin::Pin, pin::Pin,
task::{Context, Poll}, task::{Context, Poll},
time::Duration, time::Duration,
@ -37,7 +37,7 @@ pub struct Connection<'conn, Side> {
} }
impl<'conn, Side> Connection<'conn, Side> { impl<'conn, Side> Connection<'conn, Side> {
pub async fn packet_native( pub fn packet_native(
&self, &self,
pkt: impl AsRef<[u8]>, pkt: impl AsRef<[u8]>,
addr: Address, addr: Address,
@ -50,10 +50,10 @@ impl<'conn, Side> Connection<'conn, Side> {
let model = self.model.send_packet(assoc_id, addr, max_pkt_size); let model = self.model.send_packet(assoc_id, addr, max_pkt_size);
for (header, frag) in model.into_fragments(pkt) { for (header, frag) in model.into_fragments(pkt) {
let mut buf = Cursor::new(vec![0; header.len() + frag.len()]); let mut buf = vec![0; header.len() + frag.len()];
header.async_marshal(&mut buf).await?; header.write(&mut buf);
buf.write_all(frag).await.unwrap(); buf.put_slice(frag);
self.conn.send_datagram(Bytes::from(buf.into_inner()))?; self.conn.send_datagram(Bytes::from(buf))?;
} }
Ok(()) Ok(())
@ -97,7 +97,7 @@ impl<'conn, Side> Connection<'conn, Side> {
.map(|(addr, assoc_id)| (Bytes::from(asm), addr, assoc_id))) .map(|(addr, assoc_id)| (Bytes::from(asm), addr, assoc_id)))
} }
async fn accept_packet_native( fn accept_packet_native(
&self, &self,
model: PacketModel<Rx, Bytes>, model: PacketModel<Rx, Bytes>,
data: Bytes, data: Bytes,
@ -174,17 +174,17 @@ impl<'conn> Connection<'conn, side::Client> {
} }
} }
pub async fn accept_datagram(&self, dg: Bytes) -> Result<Task, Error> { pub fn accept_datagram(&self, dg: Bytes) -> Result<Task, Error> {
let mut dg = Cursor::new(dg); let mut dg = Cursor::new(dg);
match Header::async_unmarshal(&mut dg).await? { match Header::unmarshal(&mut dg)? {
Header::Authenticate(_) => Err(Error::BadCommand("authenticate")), Header::Authenticate(_) => Err(Error::BadCommand("authenticate")),
Header::Connect(_) => Err(Error::BadCommand("connect")), Header::Connect(_) => Err(Error::BadCommand("connect")),
Header::Packet(pkt) => { Header::Packet(pkt) => {
let model = self.model.recv_packet(pkt); let model = self.model.recv_packet(pkt);
let pos = dg.position() as usize; let pos = dg.position() as usize;
let buf = dg.into_inner().slice(pos..pos + model.size() as usize); let buf = dg.into_inner().slice(pos..pos + model.size() as usize);
Ok(Task::Packet(self.accept_packet_native(model, buf).await?)) Ok(Task::Packet(self.accept_packet_native(model, buf)?))
} }
Header::Dissociate(_) => Err(Error::BadCommand("dissociate")), Header::Dissociate(_) => Err(Error::BadCommand("dissociate")),
Header::Heartbeat(_) => Err(Error::BadCommand("heartbeat")), Header::Heartbeat(_) => Err(Error::BadCommand("heartbeat")),
@ -242,17 +242,17 @@ impl<'conn> Connection<'conn, side::Server> {
} }
} }
pub async fn accept_datagram(&self, dg: Bytes) -> Result<Task, Error> { pub fn accept_datagram(&self, dg: Bytes) -> Result<Task, Error> {
let mut dg = Cursor::new(dg); let mut dg = Cursor::new(dg);
match Header::async_unmarshal(&mut dg).await? { match Header::unmarshal(&mut dg)? {
Header::Authenticate(_) => Err(Error::BadCommand("authenticate")), Header::Authenticate(_) => Err(Error::BadCommand("authenticate")),
Header::Connect(_) => Err(Error::BadCommand("connect")), Header::Connect(_) => Err(Error::BadCommand("connect")),
Header::Packet(pkt) => { Header::Packet(pkt) => {
let model = self.model.recv_packet(pkt); let model = self.model.recv_packet(pkt);
let pos = dg.position() as usize; let pos = dg.position() as usize;
let buf = dg.into_inner().slice(pos..pos + model.size() as usize); let buf = dg.into_inner().slice(pos..pos + model.size() as usize);
Ok(Task::Packet(self.accept_packet_native(model, buf).await?)) Ok(Task::Packet(self.accept_packet_native(model, buf)?))
} }
Header::Dissociate(_) => Err(Error::BadCommand("dissociate")), Header::Dissociate(_) => Err(Error::BadCommand("dissociate")),
Header::Heartbeat(hb) => { Header::Heartbeat(hb) => {
@ -336,9 +336,18 @@ pub enum Error {
#[error(transparent)] #[error(transparent)]
SendDatagram(#[from] SendDatagramError), SendDatagram(#[from] SendDatagramError),
#[error(transparent)] #[error(transparent)]
Unmarshal(#[from] UnmarshalError), Unmarshal(UnmarshalError),
#[error(transparent)] #[error(transparent)]
Assemble(#[from] AssembleError), Assemble(#[from] AssembleError),
#[error("{0}")] #[error("{0}")]
BadCommand(&'static str), BadCommand(&'static str),
} }
impl From<UnmarshalError> for Error {
fn from(err: UnmarshalError) -> Self {
match err {
UnmarshalError::Io(err) => Self::Io(err),
err => Self::Unmarshal(err),
}
}
}