-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
529 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,118 +1,119 @@ | ||
use std::{net::SocketAddr, pin::Pin, sync::Arc}; | ||
use std::{net::SocketAddr, pin::Pin}; | ||
|
||
use futures::{AsyncRead, AsyncWrite}; | ||
use smol::net::TcpStream; | ||
|
||
use crate::{Buffer, Rollback, RollbackEx, UdpStream}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct FusoStream<Inner> { | ||
inner: Arc<std::sync::Mutex<Rollback<Inner, Buffer<u8>>>>, | ||
pub struct SafeStream<Inner> { | ||
inner: Rollback<Inner, Buffer<u8>>, | ||
} | ||
|
||
pub trait FusoStreamEx<T> { | ||
fn as_fuso_stream(self) -> FusoStream<T>; | ||
|
||
pub trait SafeStreamEx<T> { | ||
fn as_safe_stream(self) -> SafeStream<T>; | ||
} | ||
|
||
impl FusoStreamEx<Self> for TcpStream { | ||
impl SafeStreamEx<Self> for TcpStream { | ||
#[inline] | ||
fn as_fuso_stream(self) -> FusoStream<Self> { | ||
FusoStream { | ||
inner: Arc::new(std::sync::Mutex::new(self.roll())), | ||
} | ||
fn as_safe_stream(self) -> SafeStream<Self> { | ||
SafeStream { inner: self.roll() } | ||
} | ||
} | ||
|
||
impl FusoStreamEx<Self> for UdpStream { | ||
impl SafeStreamEx<Self> for UdpStream { | ||
#[inline] | ||
fn as_fuso_stream(self) -> FusoStream<Self> { | ||
FusoStream { | ||
inner: Arc::new(std::sync::Mutex::new(self.roll())), | ||
} | ||
fn as_safe_stream(self) -> SafeStream<Self> { | ||
SafeStream { inner: self.roll() } | ||
} | ||
} | ||
|
||
impl FusoStream<TcpStream> { | ||
impl SafeStream<TcpStream> { | ||
#[inline] | ||
pub fn local_addr(&self) -> std::io::Result<SocketAddr> { | ||
self.inner.lock().unwrap().local_addr() | ||
self.inner.local_addr() | ||
} | ||
|
||
#[inline] | ||
pub fn peer_addr(&self) -> std::io::Result<SocketAddr> { | ||
self.inner.lock().unwrap().peer_addr() | ||
self.inner.peer_addr() | ||
} | ||
} | ||
|
||
impl FusoStream<UdpStream> { | ||
impl SafeStream<UdpStream> { | ||
#[inline] | ||
pub fn local_addr(&self) -> std::io::Result<SocketAddr> { | ||
self.inner.lock().unwrap().local_addr() | ||
self.inner.local_addr() | ||
} | ||
|
||
#[inline] | ||
pub fn peer_addr(&self) -> std::io::Result<SocketAddr> { | ||
self.inner.lock().unwrap().peer_addr() | ||
self.inner.peer_addr() | ||
} | ||
} | ||
|
||
impl<Inner> FusoStream<Inner> { | ||
impl<Inner> SafeStream<Inner> | ||
where | ||
Inner: Send + Sync + 'static, | ||
{ | ||
#[inline] | ||
pub async fn begin(&self) -> crate::Result<()> { | ||
self.inner.lock().unwrap().begin().await | ||
self.inner.begin().await | ||
} | ||
|
||
#[inline] | ||
pub async fn back(&self) -> crate::Result<()> { | ||
self.inner.lock().unwrap().back().await | ||
self.inner.back().await | ||
} | ||
|
||
#[inline] | ||
pub async fn release(&self) -> crate::Result<()>{ | ||
self.inner.release().await | ||
} | ||
|
||
} | ||
|
||
impl<Inner> AsyncRead for FusoStream<Inner> | ||
impl<Inner> AsyncRead for SafeStream<Inner> | ||
where | ||
Inner: AsyncRead + Unpin + Send + Sync + 'static, | ||
Inner: Clone + AsyncRead + Unpin + Send + Sync + 'static, | ||
{ | ||
#[inline] | ||
fn poll_read( | ||
self: std::pin::Pin<&mut Self>, | ||
mut self: std::pin::Pin<&mut Self>, | ||
cx: &mut std::task::Context<'_>, | ||
buf: &mut [u8], | ||
) -> std::task::Poll<std::io::Result<usize>> { | ||
let mut io = self.inner.lock().unwrap(); | ||
Pin::new(&mut *io).poll_read(cx, buf) | ||
Pin::new(&mut self.inner).poll_read(cx, buf) | ||
} | ||
} | ||
|
||
|
||
impl<Inner> AsyncWrite for FusoStream<Inner> | ||
impl<Inner> AsyncWrite for SafeStream<Inner> | ||
where | ||
Inner: AsyncWrite + Unpin + Send + Sync + 'static, | ||
Inner: Clone + AsyncWrite + Unpin + Send + Sync + 'static, | ||
{ | ||
#[inline] | ||
fn poll_write( | ||
self: std::pin::Pin<&mut Self>, | ||
mut self: std::pin::Pin<&mut Self>, | ||
cx: &mut std::task::Context<'_>, | ||
buf: &[u8], | ||
) -> std::task::Poll<std::io::Result<usize>> { | ||
let mut io = self.inner.lock().unwrap(); | ||
Pin::new(&mut *io).poll_write(cx, buf) | ||
Pin::new(&mut self.inner).poll_write(cx, buf) | ||
} | ||
|
||
#[inline] | ||
fn poll_flush( | ||
self: std::pin::Pin<&mut Self>, | ||
mut self: std::pin::Pin<&mut Self>, | ||
cx: &mut std::task::Context<'_>, | ||
) -> std::task::Poll<std::io::Result<()>> { | ||
let mut io = self.inner.lock().unwrap(); | ||
Pin::new(&mut *io).poll_flush(cx) | ||
Pin::new(&mut self.inner).poll_flush(cx) | ||
} | ||
|
||
#[inline] | ||
fn poll_close( | ||
self: std::pin::Pin<&mut Self>, | ||
mut self: std::pin::Pin<&mut Self>, | ||
cx: &mut std::task::Context<'_>, | ||
) -> std::task::Poll<std::io::Result<()>> { | ||
let mut io = self.inner.lock().unwrap(); | ||
Pin::new(&mut *io).poll_close(cx) | ||
Pin::new(&mut self.inner).poll_close(cx) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.