Skip to content

Commit

Permalink
fix(protocols/mdns): sockets pinned on poll
Browse files Browse the repository at this point in the history
  • Loading branch information
gallegogt committed Jul 27, 2022
1 parent ebd0c79 commit 0754a06
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
6 changes: 4 additions & 2 deletions protocols/mdns/src/behaviour/iface.rs
Expand Up @@ -201,7 +201,9 @@ where
params: &impl PollParameters,
) -> Option<(PeerId, Multiaddr, Instant)> {
// Poll receive socket.
while let Poll::Ready(data) = self.recv_socket.poll_read(cx, &mut self.recv_buffer) {
while let Poll::Ready(data) =
Pin::new(&mut self.recv_socket).poll_read(cx, &mut self.recv_buffer)
{
match data {
Ok((len, from)) => {
if let Some(packet) = MdnsPacket::new_from_bytes(&self.recv_buffer[..len], from)
Expand All @@ -221,7 +223,7 @@ where

// Send responses.
while let Some(packet) = self.send_buffer.pop_front() {
match self.send_socket.poll_write(
match Pin::new(&mut self.send_socket).poll_write(
cx,
&packet,
SocketAddr::new(self.multicast_addr, 5353),
Expand Down
21 changes: 13 additions & 8 deletions protocols/mdns/src/behaviour/socket.rs
Expand Up @@ -20,12 +20,13 @@

use std::{
io::Error,
marker::Unpin,
net::{SocketAddr, UdpSocket},
task::{Context, Poll},
};

/// Interface that must be implemented by the different runtimes to use the [`UdpSocket`] in async mode
pub trait AsyncSocket: Send + 'static {
pub trait AsyncSocket: Unpin + Send + 'static {
/// Create the async socket from the [`std::net::UdpSocket`]
fn from_std(socket: UdpSocket) -> std::io::Result<Self>
where
Expand Down Expand Up @@ -93,7 +94,7 @@ pub mod asio {
#[cfg(feature = "tokio")]
pub mod tokio {
use super::*;
use ::tokio::net::UdpSocket as TkUdpSocket;
use ::tokio::{io::ReadBuf, net::UdpSocket as TkUdpSocket};

/// Tokio ASync Socket`
pub type TokioUdpSocket = TkUdpSocket;
Expand All @@ -109,8 +110,12 @@ pub mod tokio {
cx: &mut Context,
buf: &mut [u8],
) -> Poll<Result<(usize, SocketAddr), Error>> {
futures::ready!(self.poll_recv_ready(cx))?;
Poll::Ready(self.try_recv_from(buf))
let mut rbuf = ReadBuf::new(buf);
match self.poll_recv_from(cx, &mut rbuf) {
Poll::Pending => Poll::Pending,
Poll::Ready(Err(err)) => Poll::Ready(Err(err)),
Poll::Ready(Ok(addr)) => Poll::Ready(Ok((rbuf.filled().len(), addr))),
}
}

fn poll_write(
Expand All @@ -119,10 +124,10 @@ pub mod tokio {
packet: &[u8],
to: SocketAddr,
) -> Poll<Result<(), Error>> {
futures::ready!(self.poll_send_ready(cx))?;
match self.try_send_to(packet, to) {
Ok(_len) => Poll::Ready(Ok(())),
Err(err) => Poll::Ready(Err(err)),
match self.poll_send_to(cx, packet, to) {
Poll::Pending => Poll::Pending,
Poll::Ready(Err(err)) => Poll::Ready(Err(err)),
Poll::Ready(Ok(_len)) => Poll::Ready(Ok(())),
}
}
}
Expand Down

0 comments on commit 0754a06

Please sign in to comment.