| 1 |
use std::io; |
| 2 |
use std::net::{TcpListener, UdpSocket}; |
| 3 |
|
| 4 |
#[cfg(not(windows))] |
| 5 |
use std::os::unix::net::{UnixDatagram, UnixListener}; |
| 6 |
|
| 7 |
#[cfg(not(windows))] |
| 8 |
use crate::unix as imp; |
| 9 |
|
| 10 |
#[cfg(windows)] |
| 11 |
use crate::windows as imp; |
| 12 |
|
| 13 |
|
| 14 |
pub struct ListenFd { |
| 15 |
fds: Vec<Option<imp::FdType>>, |
| 16 |
} |
| 17 |
|
| 18 |
impl ListenFd { |
| 19 |
|
| 20 |
pub fn from_env() -> ListenFd { |
| 21 |
match imp::get_fds() { |
| 22 |
Some(fds) => ListenFd { |
| 23 |
fds: fds.into_iter().map(Some).collect(), |
| 24 |
}, |
| 25 |
None => ListenFd::empty(), |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
pub fn empty() -> ListenFd { |
| 36 |
ListenFd { fds: vec![] } |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
#[allow(clippy::len_without_is_empty)] |
| 44 |
pub fn len(&self) -> usize { |
| 45 |
self.fds.len() |
| 46 |
} |
| 47 |
|
| 48 |
fn with_fd<R, F: FnOnce(imp::FdType) -> io::Result<R>>( |
| 49 |
&mut self, |
| 50 |
idx: usize, |
| 51 |
f: F, |
| 52 |
) -> io::Result<Option<R>> { |
| 53 |
let bucket = match self.fds.get_mut(idx) { |
| 54 |
Some(None) | None => return Ok(None), |
| 55 |
Some(bucket) => bucket, |
| 56 |
}; |
| 57 |
f(*bucket.as_ref().unwrap()).map(|rv| { |
| 58 |
bucket.take(); |
| 59 |
Some(rv) |
| 60 |
}) |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
pub fn take_tcp_listener(&mut self, idx: usize) -> io::Result<Option<TcpListener>> { |
| 70 |
self.with_fd(idx, imp::make_tcp_listener) |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
#[cfg(not(windows))] |
| 82 |
pub fn take_unix_listener(&mut self, idx: usize) -> io::Result<Option<UnixListener>> { |
| 83 |
self.with_fd(idx, imp::make_unix_listener) |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
pub fn take_udp_socket(&mut self, idx: usize) -> io::Result<Option<UdpSocket>> { |
| 93 |
let _idx = idx; |
| 94 |
self.with_fd(idx, imp::make_udp_socket) |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
#[cfg(not(windows))] |
| 106 |
pub fn take_unix_datagram(&mut self, idx: usize) -> io::Result<Option<UnixDatagram>> { |
| 107 |
self.with_fd(idx, imp::make_unix_datagram) |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
#[cfg(not(windows))] |
| 122 |
pub fn take_custom<T: std::os::unix::prelude::FromRawFd>( |
| 123 |
&mut self, |
| 124 |
idx: usize, |
| 125 |
sock_fam: libc::c_int, |
| 126 |
sock_type: libc::c_int, |
| 127 |
hint: &str, |
| 128 |
) -> io::Result<Option<T>> { |
| 129 |
self.with_fd(idx, |fd| imp::make_custom(fd, sock_fam, sock_type, hint)) |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
#[cfg(not(windows))] |
| 134 |
pub fn take_raw_fd(&mut self, idx: usize) -> io::Result<Option<imp::FdType>> { |
| 135 |
let _idx = idx; |
| 136 |
self.with_fd(idx, Ok) |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
#[cfg(windows)] |
| 143 |
pub fn take_raw_socket(&mut self, idx: usize) -> io::Result<Option<imp::RawSocket>> { |
| 144 |
let _idx = idx; |
| 145 |
Ok(None) |
| 146 |
} |
| 147 |
} |