1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use {io, sys, Evented, EventSet, IpAddr, PollOpt, Selector, Token};
use std::net::SocketAddr;
#[derive(Debug)]
pub struct UdpSocket {
sys: sys::UdpSocket,
}
impl UdpSocket {
pub fn v4() -> io::Result<UdpSocket> {
sys::UdpSocket::v4()
.map(From::from)
}
pub fn v6() -> io::Result<UdpSocket> {
sys::UdpSocket::v6()
.map(From::from)
}
pub fn bound(addr: &SocketAddr) -> io::Result<UdpSocket> {
let sock = try!(match *addr {
SocketAddr::V4(..) => UdpSocket::v4(),
SocketAddr::V6(..) => UdpSocket::v6(),
});
try!(sock.bind(addr));
Ok(sock)
}
pub fn bind(&self, addr: &SocketAddr) -> io::Result<()> {
self.sys.bind(addr)
}
pub fn local_addr(&self) -> io::Result<SocketAddr> {
self.sys.local_addr()
}
pub fn try_clone(&self) -> io::Result<UdpSocket> {
self.sys.try_clone()
.map(From::from)
}
pub fn send_to(&self, buf: &[u8], target: &SocketAddr)
-> io::Result<Option<usize>> {
self.sys.send_to(buf, target)
}
pub fn recv_from(&self, buf: &mut [u8])
-> io::Result<Option<(usize, SocketAddr)>> {
self.sys.recv_from(buf)
}
pub fn set_broadcast(&self, on: bool) -> io::Result<()> {
self.sys.set_broadcast(on)
}
pub fn set_multicast_loop(&self, on: bool) -> io::Result<()> {
self.sys.set_multicast_loop(on)
}
pub fn join_multicast(&self, multi: &IpAddr) -> io::Result<()> {
self.sys.join_multicast(multi)
}
pub fn leave_multicast(&self, multi: &IpAddr) -> io::Result<()> {
self.sys.leave_multicast(multi)
}
pub fn set_multicast_time_to_live(&self, ttl: i32) -> io::Result<()> {
self.sys.set_multicast_time_to_live(ttl)
}
}
impl Evented for UdpSocket {
fn register(&self, selector: &mut Selector, token: Token, interest: EventSet, opts: PollOpt) -> io::Result<()> {
self.sys.register(selector, token, interest, opts)
}
fn reregister(&self, selector: &mut Selector, token: Token, interest: EventSet, opts: PollOpt) -> io::Result<()> {
self.sys.reregister(selector, token, interest, opts)
}
fn deregister(&self, selector: &mut Selector) -> io::Result<()> {
self.sys.deregister(selector)
}
}
impl From<sys::UdpSocket> for UdpSocket {
fn from(sys: sys::UdpSocket) -> UdpSocket {
UdpSocket { sys: sys }
}
}
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
#[cfg(unix)]
impl AsRawFd for UdpSocket {
fn as_raw_fd(&self) -> RawFd {
self.sys.as_raw_fd()
}
}
#[cfg(unix)]
impl FromRawFd for UdpSocket {
unsafe fn from_raw_fd(fd: RawFd) -> UdpSocket {
UdpSocket { sys: FromRawFd::from_raw_fd(fd) }
}
}