Skip to content

Commit

Permalink
Add UnixDatagram::from_std
Browse files Browse the repository at this point in the history
Convertion std lib -> Heph.
  • Loading branch information
Thomasdezeeuw committed Dec 30, 2023
1 parent 3e35fcb commit e381953
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
19 changes: 19 additions & 0 deletions rt/src/net/uds/datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,25 @@ impl<M> UnixDatagram<M> {
})
}

/// Converts a [`std::os::unix::net::UnixDatagram`] to a
/// [`heph_rt::net::UnixDatagram`].
///
/// [`heph_rt::net::UnixDatagram`]: UnixDatagram
///
/// # 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::os::unix::net::UnixDatagram) -> UnixDatagram
where
RT: Access,
{
UnixDatagram {
fd: AsyncFd::new(socket.into(), rt.submission_queue()),
mode: PhantomData,
}
}

/// Returns the socket address of the remote peer of this socket.
pub fn peer_addr(&self) -> io::Result<UnixAddr> {
self.with_ref(|socket| socket.peer_addr().map(|a| UnixAddr { inner: a }))
Expand Down
31 changes: 30 additions & 1 deletion rt/tests/functional/uds/datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ use std::time::Duration;

use heph::actor::{self, actor_fn};
use heph_rt::net::uds::{UnixAddr, UnixDatagram};
use heph_rt::net::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 heph_rt::{self as rt};

use crate::util::temp_file;
Expand Down Expand Up @@ -101,3 +103,30 @@ fn bound() {
let actor_ref = try_spawn_local(PanicSupervisor, actor, (), ActorOptions::default()).unwrap();
join(&actor_ref, Duration::from_secs(1)).unwrap();
}

#[test]
fn socket_from_std() {
async fn actor(ctx: actor::Context<!, ThreadLocal>) -> io::Result<()> {
let path1 = temp_file("uds.socket_from_std1");
let path2 = temp_file("uds.socket_from_std2");
let socket = std::os::unix::net::UnixDatagram::bind(path1)?;
let socket = UnixDatagram::<Unconnected>::from_std(ctx.runtime_ref(), socket);
let local_address = socket.local_addr()?;

let peer = std::os::unix::net::UnixDatagram::bind(path2)?;
let peer_address = UnixAddr::from_pathname(peer.local_addr()?.as_pathname().unwrap())?;

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.as_pathname(), local_address.as_pathname());

Ok(())
}

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

0 comments on commit e381953

Please sign in to comment.