Skip to content

Commit

Permalink
Add UdpSocket::from_std
Browse files Browse the repository at this point in the history
Converts a UdpSocket from std lib to one from Heph.
  • Loading branch information
Thomasdezeeuw committed Dec 30, 2023
1 parent 7a163e6 commit 5153c92
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
18 changes: 18 additions & 0 deletions rt/src/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,24 @@ impl<M> UdpSocket<M> {
})
}

/// Converts a [`std::net::UdpSocket`] to a [`heph_rt::net::UdpSocket`].
///
/// [`heph_rt::net::UdpSocket`]: UdpSocket
///
/// # Notes
///
/// It's up to the caller to ensure that the socket's mode is correctly set
/// to [`Connected`] or [`Unconnected`].
pub fn from_std<RT>(rt: &RT, socket: std::net::UdpSocket) -> UdpSocket
where
RT: Access,
{
UdpSocket {
fd: AsyncFd::new(socket.into(), rt.submission_queue()),
mode: PhantomData,
}
}

/// Returns the sockets peer address.
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
self.with_ref(|socket| socket.peer_addr().and_then(convert_address))
Expand Down
29 changes: 27 additions & 2 deletions rt/tests/functional/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use std::net::SocketAddr;
use std::time::Duration;

use heph::actor::{self, actor_fn, Actor, NewActor};
use heph_rt::net::udp::UdpSocket;
use heph_rt::net::udp::{UdpSocket, Unconnected};
use heph_rt::spawn::ActorOptions;
use heph_rt::test::{join, try_spawn_local, PanicSupervisor};
use heph_rt::test::{block_on_local_actor, join, try_spawn_local, PanicSupervisor};
use heph_rt::ThreadLocal;

use crate::util::{any_local_address, any_local_ipv6_address};
Expand Down Expand Up @@ -299,3 +299,28 @@ fn assert_read(mut got: &[u8], expected: &[&[u8]]) {
got = g;
}
}

#[test]
fn socket_from_std() {
async fn actor(ctx: actor::Context<!, ThreadLocal>) -> io::Result<()> {
let socket = std::net::UdpSocket::bind(any_local_address())?;
let socket = UdpSocket::<Unconnected>::from_std(ctx.runtime_ref(), socket);
let local_address = socket.local_addr()?;

let peer = std::net::UdpSocket::bind(any_local_address())?;
let peer_address = peer.local_addr()?;

let (_, bytes_written) = socket.send_to(DATA, peer_address).await?;
assert_eq!(bytes_written, DATA.len());

let mut buf = vec![0; DATA.len() + 2];
let (n, address) = peer.recv_from(&mut buf)?;
assert_eq!(n, DATA.len());
assert_eq!(&buf[..n], DATA);
assert_eq!(address, local_address);

Ok(())
}

block_on_local_actor(actor_fn(actor), ()).unwrap();
}

0 comments on commit 5153c92

Please sign in to comment.