Skip to content

Commit 828fdae

Browse files
committed
move registry implementations to poll files
1 parent d0063aa commit 828fdae

File tree

4 files changed

+50
-61
lines changed

4 files changed

+50
-61
lines changed

forwarder/src/poll.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
use crate::{
22
peer::{Peer, PeerManager},
3+
socket::NonBlockingSocket,
34
uri::Protocol,
45
};
56
use parking_lot::RwLock;
67
use std::sync::Arc;
78

8-
mod registry;
9-
pub(crate) use registry::Registry;
10-
119
type OnPeerRecvCallback = dyn Fn(&Peer, &mut [u8]);
1210

11+
/// trait to be able to listen on multiple sockets asynchronously
1312
pub trait Poll: Send {
14-
/// blocks the current thread and listens on multiple registered `NonBlockingSocket`s
13+
/// blocks the current thread and listens on multiple registered `NonBlockingSocket`'s
1514
/// at the same time and calls `on_peer_recv` on new packets from peer
1615
fn poll(
1716
&mut self,
@@ -23,6 +22,13 @@ pub trait Poll: Send {
2322
fn get_registry(&self) -> anyhow::Result<Box<dyn Registry>>;
2423
}
2524

25+
/// trait that allows others to register socket to `Poll`
26+
pub trait Registry: Send + Sync {
27+
// need Sync because parking_lot::RwLock needs inner to be Sync
28+
fn register(&self, socket: &NonBlockingSocket) -> anyhow::Result<()>;
29+
fn deregister(&self, socket: &NonBlockingSocket) -> anyhow::Result<()>;
30+
}
31+
2632
mod icmp;
2733
mod udp;
2834

forwarder/src/poll/icmp.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
use super::{
2-
registry::{IcmpRegistry, Registry},
3-
Poll,
4-
};
1+
use super::{Poll, Registry};
52
use crate::{
63
peer::{Peer, PeerManager},
7-
socket::icmp::IcmpSocket,
4+
socket::{icmp::IcmpSocket, NonBlockingSocket},
85
MAX_PACKET_SIZE,
96
};
107
use parking_lot::RwLock;
@@ -49,3 +46,15 @@ impl Poll for IcmpPoll {
4946
}
5047
}
5148
}
49+
50+
#[derive(Debug)]
51+
pub struct IcmpRegistry;
52+
// icmp doesn't need a registry because we manage it's poll ourself
53+
impl Registry for IcmpRegistry {
54+
fn register(&self, _socket: &NonBlockingSocket) -> anyhow::Result<()> {
55+
Ok(())
56+
}
57+
fn deregister(&self, _socket: &NonBlockingSocket) -> anyhow::Result<()> {
58+
Ok(())
59+
}
60+
}

forwarder/src/poll/registry.rs

Lines changed: 0 additions & 47 deletions
This file was deleted.

forwarder/src/poll/udp.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
use super::{
2-
registry::{Registry, UdpRegistry},
3-
Poll,
4-
};
1+
use super::{Poll, Registry};
52
use crate::{
63
peer::{Peer, PeerManager},
4+
socket::{NonBlockingSocket, NonBlockingSocketTrait},
75
MAX_PACKET_SIZE,
86
};
9-
use mio::Events;
7+
use mio::{unix::SourceFd, Events, Interest, Token};
108
use parking_lot::RwLock;
119
use std::sync::Arc;
1210

@@ -48,3 +46,26 @@ impl Poll for UdpPoll {
4846
}
4947
}
5048
}
49+
50+
#[derive(Debug)]
51+
pub struct UdpRegistry(pub mio::Registry);
52+
impl Registry for UdpRegistry {
53+
fn register(&self, socket: &NonBlockingSocket) -> anyhow::Result<()> {
54+
let socket = socket.as_udp().unwrap();
55+
let local_port = socket.local_addr()?.port();
56+
self.0.register(
57+
&mut SourceFd(&socket.as_raw_fd()),
58+
Token(local_port.into()),
59+
Interest::READABLE,
60+
)?;
61+
Ok(())
62+
}
63+
64+
fn deregister(&self, socket: &NonBlockingSocket) -> anyhow::Result<()> {
65+
let socket = socket.as_udp().unwrap();
66+
let raw_fd = socket.as_raw_fd();
67+
let source = &mut SourceFd(&raw_fd);
68+
self.0.deregister(source)?;
69+
Ok(())
70+
}
71+
}

0 commit comments

Comments
 (0)