Skip to content

Commit ef213fa

Browse files
committed
socket: remove SocketRef.
The intent was to run custom code after the user is done modifying the socket, for example to update a (not yet existing) port->socket map in SocketSet. However this wouldn't work, since the SocketRef would have to borrow the SocketSet at the same time as the Socket to be able to notify the SocketSet. I believe such indexing can be achieved by setting a "dirty" bit *before* giving the socket to the user, then on poll() reindexing all dirty sockets. This could even be faster: if user gets a socket multiple times between polls, it'd be reindexed only once.
1 parent bde881d commit ef213fa

File tree

11 files changed

+44
-140
lines changed

11 files changed

+44
-140
lines changed

examples/benchmark.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ fn main() {
120120

121121
// tcp:1234: emit data
122122
{
123-
let mut socket = iface.get_socket::<TcpSocket>(tcp1_handle);
123+
let socket = iface.get_socket::<TcpSocket>(tcp1_handle);
124124
if !socket.is_open() {
125125
socket.listen(1234).unwrap();
126126
}
@@ -140,7 +140,7 @@ fn main() {
140140

141141
// tcp:1235: sink data
142142
{
143-
let mut socket = iface.get_socket::<TcpSocket>(tcp2_handle);
143+
let socket = iface.get_socket::<TcpSocket>(tcp2_handle);
144144
if !socket.is_open() {
145145
socket.listen(1235).unwrap();
146146
}

examples/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn main() {
5555
let tcp_handle = iface.add_socket(tcp_socket);
5656

5757
{
58-
let mut socket = iface.get_socket::<TcpSocket>(tcp_handle);
58+
let socket = iface.get_socket::<TcpSocket>(tcp_handle);
5959
socket.connect((address, port), 49500).unwrap();
6060
}
6161

@@ -70,7 +70,7 @@ fn main() {
7070
}
7171

7272
{
73-
let mut socket = iface.get_socket::<TcpSocket>(tcp_handle);
73+
let socket = iface.get_socket::<TcpSocket>(tcp_handle);
7474
if socket.is_active() && !tcp_active {
7575
debug!("connected");
7676
} else if !socket.is_active() && tcp_active {

examples/httpclient.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ fn main() {
7777
}
7878

7979
{
80-
let mut socket = iface.get_socket::<TcpSocket>(tcp_handle);
80+
let socket = iface.get_socket::<TcpSocket>(tcp_handle);
8181

8282
state = match state {
8383
State::Connect if !socket.is_active() => {

examples/multicast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ fn main() {
7878
}
7979

8080
{
81-
let mut socket = iface.get_socket::<RawSocket>(raw_handle);
81+
let socket = iface.get_socket::<RawSocket>(raw_handle);
8282

8383
if socket.can_recv() {
8484
// For display purposes only - normally we wouldn't process incoming IGMP packets
@@ -93,7 +93,7 @@ fn main() {
9393
}
9494
}
9595
{
96-
let mut socket = iface.get_socket::<UdpSocket>(udp_handle);
96+
let socket = iface.get_socket::<UdpSocket>(udp_handle);
9797
if !socket.is_open() {
9898
socket.bind(MDNS_PORT).unwrap()
9999
}

examples/ping.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ fn main() {
157157

158158
{
159159
let timestamp = Instant::now();
160-
let mut socket = iface.get_socket::<IcmpSocket>(icmp_handle);
160+
let socket = iface.get_socket::<IcmpSocket>(icmp_handle);
161161
if !socket.is_open() {
162162
socket.bind(IcmpEndpoint::Ident(ident)).unwrap();
163163
send_at = timestamp;

examples/server.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fn main() {
8181

8282
// udp:6969: respond "hello"
8383
{
84-
let mut socket = iface.get_socket::<UdpSocket>(udp_handle);
84+
let socket = iface.get_socket::<UdpSocket>(udp_handle);
8585
if !socket.is_open() {
8686
socket.bind(6969).unwrap()
8787
}
@@ -109,7 +109,7 @@ fn main() {
109109

110110
// tcp:6969: respond "hello"
111111
{
112-
let mut socket = iface.get_socket::<TcpSocket>(tcp1_handle);
112+
let socket = iface.get_socket::<TcpSocket>(tcp1_handle);
113113
if !socket.is_open() {
114114
socket.listen(6969).unwrap();
115115
}
@@ -124,7 +124,7 @@ fn main() {
124124

125125
// tcp:6970: echo with reverse
126126
{
127-
let mut socket = iface.get_socket::<TcpSocket>(tcp2_handle);
127+
let socket = iface.get_socket::<TcpSocket>(tcp2_handle);
128128
if !socket.is_open() {
129129
socket.listen(6970).unwrap()
130130
}
@@ -168,7 +168,7 @@ fn main() {
168168

169169
// tcp:6971: sinkhole
170170
{
171-
let mut socket = iface.get_socket::<TcpSocket>(tcp3_handle);
171+
let socket = iface.get_socket::<TcpSocket>(tcp3_handle);
172172
if !socket.is_open() {
173173
socket.listen(6971).unwrap();
174174
socket.set_keep_alive(Some(Duration::from_millis(1000)));
@@ -191,7 +191,7 @@ fn main() {
191191

192192
// tcp:6972: fountain
193193
{
194-
let mut socket = iface.get_socket::<TcpSocket>(tcp4_handle);
194+
let socket = iface.get_socket::<TcpSocket>(tcp4_handle);
195195
if !socket.is_open() {
196196
socket.listen(6972).unwrap()
197197
}

examples/sixlowpan.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ fn main() {
101101

102102
// udp:6969: respond "hello"
103103
{
104-
let mut socket = iface.get_socket::<UdpSocket>(udp_handle);
104+
let socket = iface.get_socket::<UdpSocket>(udp_handle);
105105
if !socket.is_open() {
106106
socket.bind(6969).unwrap()
107107
}

src/iface/interface.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ where
485485
/// # Panics
486486
/// This function may panic if the handle does not belong to this socket set
487487
/// or the socket has the wrong type.
488-
pub fn get_socket<T: AnySocket<'a>>(&mut self, handle: SocketHandle) -> SocketRef<T> {
488+
pub fn get_socket<T: AnySocket<'a>>(&mut self, handle: SocketHandle) -> &mut T {
489489
self.sockets.get(handle)
490490
}
491491

@@ -830,7 +830,7 @@ where
830830
let _caps = device.capabilities();
831831

832832
let mut emitted_any = false;
833-
for mut socket in sockets.iter_mut() {
833+
for socket in sockets.iter_mut() {
834834
if !socket
835835
.meta_mut()
836836
.egress_permitted(cx.now, |ip_addr| inner.has_neighbor(cx, &ip_addr))
@@ -1202,7 +1202,7 @@ impl<'a> InterfaceInner<'a> {
12021202

12031203
// Look for UDP sockets that will accept the UDP packet.
12041204
// If it does not accept the packet, then send an ICMP message.
1205-
for mut udp_socket in sockets.iter_mut().filter_map(UdpSocket::downcast) {
1205+
for udp_socket in sockets.iter_mut().filter_map(UdpSocket::downcast) {
12061206
if !udp_socket.accepts(&IpRepr::Ipv6(ipv6_repr), &udp_repr) {
12071207
continue;
12081208
}
@@ -1328,7 +1328,7 @@ impl<'a> InterfaceInner<'a> {
13281328
let mut handled_by_raw_socket = false;
13291329

13301330
// Pass every IP packet to all raw sockets we have registered.
1331-
for mut raw_socket in sockets.iter_mut().filter_map(RawSocket::downcast) {
1331+
for raw_socket in sockets.iter_mut().filter_map(RawSocket::downcast) {
13321332
if !raw_socket.accepts(ip_repr) {
13331333
continue;
13341334
}
@@ -1460,7 +1460,7 @@ impl<'a> InterfaceInner<'a> {
14601460
if udp_packet.src_port() == DHCP_SERVER_PORT
14611461
&& udp_packet.dst_port() == DHCP_CLIENT_PORT
14621462
{
1463-
if let Some(mut dhcp_socket) =
1463+
if let Some(dhcp_socket) =
14641464
sockets.iter_mut().filter_map(Dhcpv4Socket::downcast).next()
14651465
{
14661466
let (src_addr, dst_addr) = (ip_repr.src_addr(), ip_repr.dst_addr());
@@ -1639,7 +1639,7 @@ impl<'a> InterfaceInner<'a> {
16391639
let mut handled_by_icmp_socket = false;
16401640

16411641
#[cfg(all(feature = "socket-icmp", feature = "proto-ipv6"))]
1642-
for mut icmp_socket in _sockets.iter_mut().filter_map(IcmpSocket::downcast) {
1642+
for icmp_socket in _sockets.iter_mut().filter_map(IcmpSocket::downcast) {
16431643
if !icmp_socket.accepts(cx, &ip_repr, &icmp_repr.into()) {
16441644
continue;
16451645
}
@@ -1825,7 +1825,7 @@ impl<'a> InterfaceInner<'a> {
18251825
let mut handled_by_icmp_socket = false;
18261826

18271827
#[cfg(all(feature = "socket-icmp", feature = "proto-ipv4"))]
1828-
for mut icmp_socket in _sockets.iter_mut().filter_map(IcmpSocket::downcast) {
1828+
for icmp_socket in _sockets.iter_mut().filter_map(IcmpSocket::downcast) {
18291829
if !icmp_socket.accepts(cx, &ip_repr, &icmp_repr.into()) {
18301830
continue;
18311831
}
@@ -1949,7 +1949,7 @@ impl<'a> InterfaceInner<'a> {
19491949
let udp_repr = UdpRepr::parse(&udp_packet, &src_addr, &dst_addr, &cx.caps.checksum)?;
19501950
let udp_payload = udp_packet.payload();
19511951

1952-
for mut udp_socket in sockets.iter_mut().filter_map(UdpSocket::downcast) {
1952+
for udp_socket in sockets.iter_mut().filter_map(UdpSocket::downcast) {
19531953
if !udp_socket.accepts(&ip_repr, &udp_repr) {
19541954
continue;
19551955
}
@@ -2006,7 +2006,7 @@ impl<'a> InterfaceInner<'a> {
20062006
let tcp_packet = TcpPacket::new_checked(ip_payload)?;
20072007
let tcp_repr = TcpRepr::parse(&tcp_packet, &src_addr, &dst_addr, &cx.caps.checksum)?;
20082008

2009-
for mut tcp_socket in sockets.iter_mut().filter_map(TcpSocket::downcast) {
2009+
for tcp_socket in sockets.iter_mut().filter_map(TcpSocket::downcast) {
20102010
if !tcp_socket.accepts(&ip_repr, &tcp_repr) {
20112011
continue;
20122012
}
@@ -2944,7 +2944,7 @@ mod test {
29442944

29452945
{
29462946
// Bind the socket to port 68
2947-
let mut socket = iface.get_socket::<UdpSocket>(socket_handle);
2947+
let socket = iface.get_socket::<UdpSocket>(socket_handle);
29482948
assert_eq!(socket.bind(68), Ok(()));
29492949
assert!(!socket.can_recv());
29502950
assert!(socket.can_send());
@@ -2971,7 +2971,7 @@ mod test {
29712971
{
29722972
// Make sure the payload to the UDP packet processed by process_udp is
29732973
// appended to the bound sockets rx_buffer
2974-
let mut socket = iface.get_socket::<UdpSocket>(socket_handle);
2974+
let socket = iface.get_socket::<UdpSocket>(socket_handle);
29752975
assert!(socket.can_recv());
29762976
assert_eq!(
29772977
socket.recv(),
@@ -3443,7 +3443,7 @@ mod test {
34433443
let echo_data = &[0xff; 16];
34443444

34453445
{
3446-
let mut socket = iface.get_socket::<IcmpSocket>(socket_handle);
3446+
let socket = iface.get_socket::<IcmpSocket>(socket_handle);
34473447
// Bind to the ID 0x1234
34483448
assert_eq!(socket.bind(IcmpEndpoint::Ident(ident)), Ok(()));
34493449
}
@@ -3494,7 +3494,7 @@ mod test {
34943494
);
34953495

34963496
{
3497-
let mut socket = iface.get_socket::<IcmpSocket>(socket_handle);
3497+
let socket = iface.get_socket::<IcmpSocket>(socket_handle);
34983498
assert!(socket.can_recv());
34993499
assert_eq!(
35003500
socket.recv(),
@@ -3856,7 +3856,7 @@ mod test {
38563856
let udp_socket_handle = iface.add_socket(udp_socket);
38573857
{
38583858
// Bind the socket to port 68
3859-
let mut socket = iface.get_socket::<UdpSocket>(udp_socket_handle);
3859+
let socket = iface.get_socket::<UdpSocket>(udp_socket_handle);
38603860
assert_eq!(socket.bind(68), Ok(()));
38613861
assert!(!socket.can_recv());
38623862
assert!(socket.can_send());
@@ -3929,7 +3929,7 @@ mod test {
39293929

39303930
{
39313931
// Make sure the UDP socket can still receive in presence of a Raw socket that handles UDP
3932-
let mut socket = iface.get_socket::<UdpSocket>(udp_socket_handle);
3932+
let socket = iface.get_socket::<UdpSocket>(udp_socket_handle);
39333933
assert!(socket.can_recv());
39343934
assert_eq!(
39353935
socket.recv(),

src/socket/mod.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ mod icmp;
2424
mod meta;
2525
#[cfg(feature = "socket-raw")]
2626
mod raw;
27-
mod ref_;
2827
mod set;
2928
#[cfg(feature = "socket-tcp")]
3029
mod tcp;
@@ -59,9 +58,6 @@ pub use self::dhcpv4::{Config as Dhcpv4Config, Dhcpv4Socket, Event as Dhcpv4Even
5958
pub use self::set::{Handle as SocketHandle, Item as SocketSetItem, Set as SocketSet};
6059
pub use self::set::{Iter as SocketSetIter, IterMut as SocketSetIterMut};
6160

62-
pub use self::ref_::Ref as SocketRef;
63-
pub(crate) use self::ref_::Session as SocketSession;
64-
6561
/// Gives an indication on the next time the socket should be polled.
6662
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Copy)]
6763
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
@@ -144,25 +140,19 @@ impl<'a> Socket<'a> {
144140
}
145141
}
146142

147-
impl<'a> SocketSession for Socket<'a> {
148-
fn finish(&mut self) {
149-
dispatch_socket!(mut self, |socket| socket.finish())
150-
}
151-
}
152-
153143
/// A conversion trait for network sockets.
154-
pub trait AnySocket<'a>: SocketSession + Sized {
155-
fn downcast<'c>(socket_ref: SocketRef<'c, Socket<'a>>) -> Option<SocketRef<'c, Self>>;
144+
pub trait AnySocket<'a>: Sized {
145+
fn downcast<'c>(socket: &'c mut Socket<'a>) -> Option<&'c mut Self>;
156146
}
157147

158148
macro_rules! from_socket {
159149
($socket:ty, $variant:ident) => {
160150
impl<'a> AnySocket<'a> for $socket {
161-
fn downcast<'c>(ref_: SocketRef<'c, Socket<'a>>) -> Option<SocketRef<'c, Self>> {
162-
if let Socket::$variant(ref mut socket) = SocketRef::into_inner(ref_) {
163-
Some(SocketRef::new(socket))
164-
} else {
165-
None
151+
fn downcast<'c>(socket: &'c mut Socket<'a>) -> Option<&'c mut Self> {
152+
#[allow(unreachable_patterns)]
153+
match socket {
154+
Socket::$variant(socket) => Some(socket),
155+
_ => None,
166156
}
167157
}
168158
}

src/socket/ref_.rs

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

0 commit comments

Comments
 (0)