Skip to content

Commit 1a2ec2a

Browse files
committed
listener
1 parent 08630ae commit 1a2ec2a

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

library/std/src/os/windows/net/addr.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
use crate::os::raw::{c_char, c_int};
44
use crate::path::Path;
5-
use crate::sys::c;
5+
use crate::sys::c::{self, SOCKADDR};
6+
use crate::sys::cvt;
67
use crate::{io, mem};
78
#[stable(feature = "rust1", since = "1.0.0")]
89
pub fn sockaddr_un(path: &Path) -> io::Result<(c::sockaddr_un, c_int)> {
@@ -52,6 +53,18 @@ pub struct SocketAddr {
5253
len: c_int,
5354
}
5455
impl SocketAddr {
56+
#[stable(feature = "rust1", since = "1.0.0")]
57+
pub fn new<F>(f: F) -> io::Result<SocketAddr>
58+
where
59+
F: FnOnce(*mut SOCKADDR, *mut c_int) -> c_int,
60+
{
61+
unsafe {
62+
let mut addr: c::sockaddr_un = mem::zeroed();
63+
let mut len = mem::size_of::<c::sockaddr_un>() as c_int;
64+
cvt(f(&mut addr as *mut _ as *mut _, &mut len))?;
65+
SocketAddr::from_parts(addr, len)
66+
}
67+
}
5568
fn from_parts(addr: c::sockaddr_un, mut len: c_int) -> io::Result<SocketAddr> {
5669
if len == 0 {
5770
// When there is a datagram from unnamed unix socket

library/std/src/os/windows/net/listener.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::io;
55
use crate::os::raw::c_int;
66
use crate::os::windows::net::{SocketAddr, from_sockaddr_un};
77
use crate::path::Path;
8-
use crate::sys::c::{self, bind, listen};
8+
use crate::sys::c::{self, bind, getsockname, listen};
99
use crate::sys::cvt;
1010
use crate::sys::net::Socket;
1111
#[stable(feature = "rust1", since = "1.0.0")]
@@ -33,4 +33,28 @@ impl UnixListener {
3333
let addr = from_sockaddr_un(storage, len)?;
3434
Ok((UnixStream(sock), addr))
3535
}
36+
#[stable(feature = "rust1", since = "1.0.0")]
37+
pub fn incoming(&self) -> Incoming<'_> {
38+
Incoming { listener: self }
39+
}
40+
#[stable(feature = "rust1", since = "1.0.0")]
41+
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
42+
self.0.take_error()
43+
}
44+
#[stable(feature = "rust1", since = "1.0.0")]
45+
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
46+
self.0.set_nonblocking(nonblocking)
47+
}
48+
#[stable(feature = "rust1", since = "1.0.0")]
49+
pub fn local_addr(&self) -> io::Result<SocketAddr> {
50+
SocketAddr::new(|addr, len| unsafe { getsockname(self.0.as_raw() as _, addr, len) })
51+
}
52+
#[stable(feature = "rust1", since = "1.0.0")]
53+
pub fn try_clone(&self) -> io::Result<UnixListener> {
54+
self.0.duplicate().map(UnixListener)
55+
}
56+
}
57+
#[stable(feature = "rust1", since = "1.0.0")]
58+
pub struct Incoming<'a> {
59+
listener: &'a UnixListener,
3660
}

0 commit comments

Comments
 (0)