42CBEYZTGKPTDFNBTNCBUWWCABZOMBY3FCWWHUILQIGO2M54OJXQC
2DG7PVTZAJ4NMGY2BNODJRLYTOV3MPTH3V6EZMK3WSIUZESLPQXQC
SMM42WXFAYFMOMUR7DCVCZCK7QMXIE5KUH4GUOIZPVFDDYB4ZMZAC
AWPTZQQYEYW2BMXCWYAWSZAVUE55YOMO7IE4VCXP5J4UOKUMKQFAC
M2FGQSGQITI7FYU3RWY3IGIA4ZLS6JAZMHIO64WNHKS4JIL53OWQC
7VYQARQVDMISCO5566F55TG5FIDSOO3JQEIKVOUKEFEU4X5HZNEAC
AGPU6SD7V5FQNI6W54HDRJQCVHWYCZIHDHL6IL5LVVMJ7SVJL7UAC
KWR2TS2D7YN4LCGMQZ35V4WBLQDHAVSZJV3SZAIR5HQRSJAWLWCQC
K3I54HXNNCTEA7L6UGWIVBFB5TPZVZZXDBRSR2YZDIKREXQVWRIQC
YYUB6HZYSVQSCO7DGF6ETPAXTMG6SG2RQQXLP73NKPLDMZEC3PGAC
LSQ7OPL7Z5WJDO7ZZS3D4UPMEEWUUF2LNQ3FZUWWFUVZEXA7CIOAC
7RMAZDLQ6FEEQGUUAIVEM4X2VME7IUUZMEJMMV3H46U3UKO4BODQC
NRKM62AKJZID7WAF57OJZY2AB5IOJ2OQ4GGIFTDRWYWEDHMS6H3AC
6WLPW4L4PN6ANYYW3I6AOCN3SXQP37RQTUOM52IVXL3SRZD7UHAAC
2ZDRU4SSJJ4LJVQRQUZMD662TPJQTHQKDPZFMERPKTLNCHHDIUTQC
RPECDMZ4O7MMU32H6Z7EW75GRVEQ6IJYNPWSV54HUPTCX5H7I26QC
[[package]]
name = "bincode"
version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f30d3a39baa26f9651f17b375061f3233dde33424a8b72b0dbe93a68a0bc896d"
dependencies = [
"byteorder",
"serde",
]
[[package]]
name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
name = "byteorder"
version = "1.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de"
[[package]]
name = "bytes"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38"
[[package]]
name = "fuchsia-zircon"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
dependencies = [
"bitflags",
"fuchsia-zircon-sys",
]
[[package]]
name = "fuchsia-zircon-sys"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
[[package]]
name = "futures-core"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59f5fff90fd5d971f936ad674802482ba441b6f09ba5e15fd8b39145582ca399"
[[package]]
[[package]]
name = "mio"
version = "0.6.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fce347092656428bc8eaf6201042cb551b8d67855af7374542a92a0fbfcac430"
dependencies = [
"cfg-if",
"fuchsia-zircon",
"fuchsia-zircon-sys",
"iovec",
"kernel32-sys",
"libc",
"log",
"miow",
"net2",
"slab",
"winapi 0.2.8",
]
[[package]]
name = "miow"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919"
dependencies = [
"kernel32-sys",
"net2",
"winapi 0.2.8",
"ws2_32-sys",
]
name = "net2"
version = "0.2.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ba7c918ac76704fb42afcbbb43891e72731f3dcca3bef2a19786297baf14af7"
dependencies = [
"cfg-if",
"libc",
"winapi 0.3.9",
]
[[package]]
name = "pin-project-lite"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282adbf10f2698a7a77f8e983a74b2d18176c19a7fd32a45446139ae7b02b715"
[[package]]
fn send_to(&self, event: &NetEvent, addr: &SocketAddr) -> io::Result<Option<()>>;
fn send_to_all(&self, event: &NetEvent) -> io::Result<Option<()>>;
fn recv_from(&self) -> io::Result<Option<(NetEvent, SocketAddr)>>;
type Addr;
async fn send_to(&mut self, event: &NetEvent, addr: &Self::Addr) -> io::Result<()>;
async fn send_to_all(&mut self, event: &NetEvent) -> io::Result<()>;
async fn recv_from(&mut self) -> io::Result<(NetEvent, Self::Addr)>;
pub fn open(config: Config) -> io::Result<Self> {
let socket = try!(UdpSocket::v4());
try!(socket.set_multicast_loop(false));
try!(socket.join_multicast(&config.multicast_addr));
try!(socket.bind(&match config.server_addr {
IpAddr::V4(addr) => SocketAddr::V4((SocketAddrV4::new(addr, config.port))),
IpAddr::V6(addr) => SocketAddr::V6((SocketAddrV6::new(addr, config.port, 0, 0))),
}));
pub async fn open(config: Config) -> io::Result<Self> {
let addr = SocketAddr::new(config.server_addr, config.port);
let socket = UdpSocket::bind(addr).await?;
match config.multicast_addr {
IpAddr::V4(addr) => {
socket.set_multicast_loop_v4(false)?;
socket.join_multicast_v4(addr, Ipv4Addr::UNSPECIFIED)?;
},
IpAddr::V6(addr) => {
socket.set_multicast_loop_v6(false)?;
socket.join_multicast_v6(&addr, 0)?;
}
}
fn send_to(&self, event: &NetEvent, addr: &SocketAddr) -> io::Result<Option<()>> {
let packet = bincode_serde::serialize(event, SizeLimit::Bounded(1024)).unwrap();
type Addr = SocketAddr;
async fn send_to(&mut self, event: &NetEvent, addr: &Self::Addr) -> io::Result<()> {
let packet = bincode::serialize(event).unwrap();
match self.socket.send_to(&packet, addr) {
Ok(Some(_)) => Ok(Some(())),
Ok(None) => Err(io::Error::new(
io::ErrorKind::WouldBlock,
"The OS socket buffer is probably full"
)),
Err(err) => Err(err),
}
self.socket.send_to(&packet, addr).await?;
Ok(())
fn send_to_all(&self, event: &NetEvent) -> io::Result<Option<()>> {
let addr = match self.config.multicast_addr {
IpAddr::V4(addr) => SocketAddr::V4((SocketAddrV4::new(addr, self.config.port))),
IpAddr::V6(addr) => SocketAddr::V6((SocketAddrV6::new(addr, self.config.port, 0, 0))),
};
self.send_to(event, &addr)
async fn send_to_all(&mut self, event: &NetEvent) -> io::Result<()> {
let addr = SocketAddr::new(self.config.multicast_addr, self.config.port);
self.send_to(event, &addr).await?;
Ok(())
self.socket.recv_from(&mut buf).map(|result| {
result.map(|(len, addr)| {
let event = bincode_serde::deserialize::<NetEvent>(&buf[..len]).unwrap();
debug!("<= {} => ({} bytes) {:#?}", addr, len, event);
(event, addr)
})
})
}
}
/*
* FIXME(Future):
* Method delegation: https://github.com/rust-lang/rfcs/pull/1406
*/
impl Evented for IpInterface {
fn register(&self, selector: &mut Selector, token: Token, interest: EventSet, opts: PollOpt) -> io::Result<()> {
self.socket.register(selector, token, interest, opts)
}
fn reregister(&self, selector: &mut Selector, token: Token, interest: EventSet, opts: PollOpt) -> io::Result<()> {
self.socket.reregister(selector, token, interest, opts)
}
fn deregister(&self, selector: &mut Selector) -> io::Result<()> {
self.socket.deregister(selector)
let (len, addr) = self.socket.recv_from(&mut buf).await?;
let event = bincode::deserialize(&buf[..len]).unwrap();
debug!("<= {} => ({} bytes) {:#?}", addr, len, event);
Ok((event, addr))