Skip to content
This repository was archived by the owner on May 17, 2018. It is now read-only.

Commit 637f0e0

Browse files
committed
Implement recv() call for datagram sockets
1 parent e1d8575 commit 637f0e0

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,19 @@ impl UnixDatagram {
729729
Ok((count as usize, addr))
730730
}
731731

732+
/// Receives data from the socket.
733+
///
734+
/// On success, returns the number of bytes read
735+
pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
736+
unsafe {
737+
let count = try!(cvt_s(libc::recv(self.inner.0,
738+
buf.as_mut_ptr() as *mut _,
739+
calc_len(buf),
740+
0)));
741+
Ok(count as usize)
742+
}
743+
}
744+
732745
/// Sends data on the socket to the given address.
733746
///
734747
/// On success, returns the number of bytes written.
@@ -1133,4 +1146,21 @@ mod test {
11331146
or_panic!(sock.send(msg));
11341147
or_panic!(bsock2.recv_from(&mut buf));
11351148
}
1149+
1150+
#[test]
1151+
fn test_unix_datagram_recv() {
1152+
let dir = or_panic!(TempDir::new("unix_socket"));
1153+
let path1 = dir.path().join("sock1");
1154+
1155+
let sock1 = or_panic!(UnixDatagram::bind(&path1));
1156+
let mut sock2 = or_panic!(UnixDatagram::new());
1157+
or_panic!(sock2.connect(&path1));
1158+
1159+
let msg = b"hello world";
1160+
or_panic!(sock2.send_to(msg, &path1));
1161+
let mut buf = [0; 11];
1162+
let size = or_panic!(sock1.recv(&mut buf));
1163+
assert_eq!(size, 11);
1164+
assert_eq!(msg, &buf[..]);
1165+
}
11361166
}

0 commit comments

Comments
 (0)