Skip to content

Commit

Permalink
rename streams to sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Apr 18, 2024
1 parent 3db90b1 commit 082b2b6
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use tokio::{

pub(crate) type PacketSender = UnboundedSender<NetworkPacket>;
pub(crate) type PacketReceiver = UnboundedReceiver<NetworkPacket>;
pub(crate) type SessionCollection = AHashMap<NetworkTuple, PacketSender>;

mod error;
mod packet;
Expand Down Expand Up @@ -118,7 +119,7 @@ fn run<D>(
where
D: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
let mut streams: AHashMap<NetworkTuple, PacketSender> = AHashMap::new();
let mut sessions: SessionCollection = AHashMap::new();
let pi = config.packet_information;
let offset = if pi && cfg!(unix) { 4 } else { 0 };
let mut buffer = [0_u8; u16::MAX as usize + 4];
Expand All @@ -130,7 +131,7 @@ where
Ok(n) = device.read(&mut buffer) => {
if let Some(stream) = process_device_read(
&buffer[offset..n],
&mut streams,
&mut sessions,
&pkt_sender,
&config,
) {
Expand All @@ -140,7 +141,7 @@ where
Some(packet) = pkt_receiver.recv() => {
process_upstream_recv(
packet,
&mut streams,
&mut sessions,
&mut device,
#[cfg(unix)]
pi,
Expand All @@ -154,7 +155,7 @@ where

fn process_device_read(
data: &[u8],
streams: &mut AHashMap<NetworkTuple, PacketSender>,
sessions: &mut SessionCollection,
pkt_sender: &PacketSender,
config: &IpStackConfig,
) -> Option<IpStackStream> {
Expand All @@ -175,7 +176,7 @@ fn process_device_read(
));
}

match streams.entry(packet.network_tuple()) {
match sessions.entry(packet.network_tuple()) {
Occupied(mut entry) => {
if let Err(e) = entry.get().send(packet) {
trace!("New stream because: {}", e);
Expand Down Expand Up @@ -239,15 +240,15 @@ fn create_stream(

async fn process_upstream_recv<D>(
packet: NetworkPacket,
streams: &mut AHashMap<NetworkTuple, PacketSender>,
sessions: &mut SessionCollection,
device: &mut D,
#[cfg(unix)] packet_information: bool,
) -> Result<()>
where
D: AsyncWrite + Unpin + 'static,
{
if packet.ttl() == 0 {
streams.remove(&packet.reverse_network_tuple());
sessions.remove(&packet.reverse_network_tuple());
return Ok(());
}
#[allow(unused_mut)]
Expand Down

0 comments on commit 082b2b6

Please sign in to comment.