-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix automatically drop IpstackTcpStream (#32)
* fix automatically drop IpstackTcpStream * IpStackTcpStreamInner is so larger and place it to heap * Code refactor --------- Co-authored-by: SajjadPourali
- Loading branch information
Showing
4 changed files
with
128 additions
and
22 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
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 |
---|---|---|
@@ -0,0 +1,121 @@ | ||
use std::{net::SocketAddr, pin::Pin, time::Duration}; | ||
|
||
use tokio::{ | ||
io::AsyncWriteExt, | ||
sync::mpsc::{self, UnboundedSender}, | ||
time::timeout, | ||
}; | ||
|
||
use crate::{ | ||
packet::{NetworkPacket, TcpPacket}, | ||
IpStackError, | ||
}; | ||
|
||
use super::tcp::IpStackTcpStream as IpStackTcpStreamInner; | ||
|
||
pub struct IpStackTcpStream { | ||
inner: Option<Box<IpStackTcpStreamInner>>, | ||
peer_addr: SocketAddr, | ||
local_addr: SocketAddr, | ||
stream_sender: mpsc::UnboundedSender<NetworkPacket>, | ||
} | ||
|
||
impl IpStackTcpStream { | ||
pub(crate) fn new( | ||
local_addr: SocketAddr, | ||
peer_addr: SocketAddr, | ||
tcp: TcpPacket, | ||
pkt_sender: UnboundedSender<NetworkPacket>, | ||
mtu: u16, | ||
tcp_timeout: Duration, | ||
) -> Result<IpStackTcpStream, IpStackError> { | ||
let (stream_sender, stream_receiver) = mpsc::unbounded_channel::<NetworkPacket>(); | ||
IpStackTcpStreamInner::new( | ||
local_addr, | ||
peer_addr, | ||
tcp, | ||
pkt_sender, | ||
stream_receiver, | ||
mtu, | ||
tcp_timeout, | ||
) | ||
.map(Box::new) | ||
.map(|inner| IpStackTcpStream { | ||
inner: Some(inner), | ||
peer_addr, | ||
local_addr, | ||
stream_sender, | ||
}) | ||
} | ||
pub fn local_addr(&self) -> SocketAddr { | ||
self.local_addr | ||
} | ||
pub fn peer_addr(&self) -> SocketAddr { | ||
self.peer_addr | ||
} | ||
pub fn stream_sender(&self) -> UnboundedSender<NetworkPacket> { | ||
self.stream_sender.clone() | ||
} | ||
} | ||
|
||
impl tokio::io::AsyncRead for IpStackTcpStream { | ||
fn poll_read( | ||
mut self: std::pin::Pin<&mut Self>, | ||
cx: &mut std::task::Context<'_>, | ||
buf: &mut tokio::io::ReadBuf<'_>, | ||
) -> std::task::Poll<std::io::Result<()>> { | ||
match self.inner.as_mut() { | ||
Some(mut inner) => Pin::new(&mut inner).poll_read(cx, buf), | ||
None => { | ||
std::task::Poll::Ready(Err(std::io::Error::from(std::io::ErrorKind::NotConnected))) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl tokio::io::AsyncWrite for IpStackTcpStream { | ||
fn poll_write( | ||
mut self: std::pin::Pin<&mut Self>, | ||
cx: &mut std::task::Context<'_>, | ||
buf: &[u8], | ||
) -> std::task::Poll<Result<usize, std::io::Error>> { | ||
match self.inner.as_mut() { | ||
Some(mut inner) => Pin::new(&mut inner).poll_write(cx, buf), | ||
None => { | ||
std::task::Poll::Ready(Err(std::io::Error::from(std::io::ErrorKind::NotConnected))) | ||
} | ||
} | ||
} | ||
fn poll_flush( | ||
mut self: std::pin::Pin<&mut Self>, | ||
cx: &mut std::task::Context<'_>, | ||
) -> std::task::Poll<Result<(), std::io::Error>> { | ||
match self.inner.as_mut() { | ||
Some(mut inner) => Pin::new(&mut inner).poll_flush(cx), | ||
None => { | ||
std::task::Poll::Ready(Err(std::io::Error::from(std::io::ErrorKind::NotConnected))) | ||
} | ||
} | ||
} | ||
fn poll_shutdown( | ||
mut self: std::pin::Pin<&mut Self>, | ||
cx: &mut std::task::Context<'_>, | ||
) -> std::task::Poll<Result<(), std::io::Error>> { | ||
match self.inner.as_mut() { | ||
Some(mut inner) => Pin::new(&mut inner).poll_shutdown(cx), | ||
None => { | ||
std::task::Poll::Ready(Err(std::io::Error::from(std::io::ErrorKind::NotConnected))) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl Drop for IpStackTcpStream { | ||
fn drop(&mut self) { | ||
if let Some(mut inner) = self.inner.take() { | ||
tokio::spawn(async move { | ||
_ = timeout(Duration::from_secs(2), inner.shutdown()).await; | ||
}); | ||
} | ||
} | ||
} |