Skip to content

Commit

Permalink
Update socket2 version and Rust version
Browse files Browse the repository at this point in the history
  • Loading branch information
irvingoujAtDevolution committed Mar 20, 2024
2 parents 90fbc17 + bcce2a4 commit 0737495
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 117 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ default = ["async", "logging"]
flume = { version = "0.11", default-features = false } # channel between threads
if-addrs = { version = "0.10", features = ["link-local"] } # get local IP addresses
log = { version = "0.4", optional = true } # logging
polling = "2.1" # select/poll sockets
polling = "3.4.0" # select/poll sockets
socket2 = { version = "0.5.5", features = ["all"] } # socket APIs

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion examples/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn print_usage() {
println!("Usage: cargo run --example query <service_type_without_domain_postfix>");
println!("Example: ");
println!("cargo run --example query _my-service._udp");
println!("");
println!();
println!("You can also do a meta-query per RFC 6763 to find which services are available:");
println!("cargo run --example query _services._dns-sd._udp");
}
20 changes: 8 additions & 12 deletions examples/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ fn main() {
let args: Vec<String> = env::args().collect();
let mut should_unreg = false;
for arg in args.iter() {
match arg.as_str() {
"--unregister" => should_unreg = true,
_ => {}
if arg.as_str() == "--unregister" {
should_unreg = true
}
}

Expand Down Expand Up @@ -50,12 +49,12 @@ fn main() {

// The key string in TXT properties is case insensitive. Only the first
// (key, val) pair will take effect.
let properties = vec![("PATH", "one"), ("Path", "two"), ("PaTh", "three")];
let properties = [("PATH", "one"), ("Path", "two"), ("PaTh", "three")];

// Register a service.
let service_info = ServiceInfo::new(
&service_type,
&instance_name,
instance_name,
service_hostname,
my_addrs,
port,
Expand Down Expand Up @@ -85,12 +84,9 @@ fn main() {
// Monitor the daemon events.
while let Ok(event) = monitor.recv() {
println!("Daemon event: {:?}", &event);
match event {
DaemonEvent::Error(e) => {
println!("Failed: {}", e);
break;
}
_ => {}
if let DaemonEvent::Error(e) = event {
println!("Failed: {}", e);
break;
}
}
}
Expand All @@ -101,7 +97,7 @@ fn print_usage() {
println!("cargo run --example register <service_type> <instance_name> [--unregister]");
println!("Options:");
println!("--unregister: automatically unregister after 2 seconds");
println!("");
println!();
println!("For example:");
println!("cargo run --example register _my-hello._udp test1");
}
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@
//! - Only support multicast, not unicast send/recv.
//! - Only support 32-bit or bigger platforms, not 16-bit platforms.
#![forbid(unsafe_code)]
#![allow(clippy::single_component_path_imports)]

// log for logging (optional).
Expand Down
42 changes: 24 additions & 18 deletions src/service_daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use crate::{
};
use flume::{bounded, Sender, TrySendError};
use if_addrs::Interface;
use polling::Poller;
use polling::{Events, Poller};
use socket2::{SockAddr, Socket};
use std::{
cmp,
Expand Down Expand Up @@ -85,7 +85,7 @@ pub enum UnregisterStatus {
}

/// Status code for the service daemon.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Eq)]
#[non_exhaustive]
pub enum DaemonStatus {
/// The daemon is running as normal.
Expand Down Expand Up @@ -384,20 +384,24 @@ impl ServiceDaemon {
fn run(mut zc: Zeroconf, receiver: Receiver<Command>) -> Option<Command> {
// Add the daemon's signal socket to the poller.
let signal_event_key = usize::MAX - 1; // avoid to overlap with zc.poll_ids
if let Err(e) = zc
.poller
.add(&zc.signal_sock, polling::Event::readable(signal_event_key))
{
error!("failed to add signal socket to the poller: {}", e);
return None;
unsafe {
if let Err(e) = zc
.poller
.add(&zc.signal_sock, polling::Event::readable(signal_event_key))
{
error!("failed to add signal socket to the poller: {}", e);
return None;
}
}

// Add mDNS sockets to the poller.
for (ip, if_sock) in zc.intf_socks.iter() {
let key = Zeroconf::add_poll_impl(&mut zc.poll_ids, &mut zc.poll_id_count, *ip);
if let Err(e) = zc.poller.add(&if_sock.sock, polling::Event::readable(key)) {
error!("add socket of {:?} to poller: {}", ip, e);
return None;
unsafe {
if let Err(e) = zc.poller.add(&if_sock.sock, polling::Event::readable(key)) {
error!("add socket of {:?} to poller: {}", ip, e);
return None;
}
}
}

Expand All @@ -408,7 +412,7 @@ impl ServiceDaemon {

// Start the run loop.

let mut events = Vec::new();
let mut events = Events::new();
loop {
let now = current_time_millis();

Expand Down Expand Up @@ -1135,9 +1139,11 @@ impl Zeroconf {

// Add the new interface into the poller.
let key = self.add_poll(new_ip);
if let Err(e) = self.poller.add(&sock, polling::Event::readable(key)) {
error!("check_ip_changes: poller add ip {}: {}", new_ip, e);
return;
unsafe {
if let Err(e) = self.poller.add(&sock, polling::Event::readable(key)) {
error!("check_ip_changes: poller add ip {}: {}", new_ip, e);
return;
}
}

self.intf_socks.insert(new_ip, IntfSock { intf, sock });
Expand Down Expand Up @@ -2301,9 +2307,9 @@ mod tests {

#[test]
fn test_instance_name() {
assert_eq!(valid_instance_name("my-laser._printer._tcp.local."), true);
assert_eq!(valid_instance_name("my-laser.._printer._tcp.local."), true);
assert_eq!(valid_instance_name("_printer._tcp.local."), false);
assert!(valid_instance_name("my-laser._printer._tcp.local."));
assert!(valid_instance_name("my-laser.._printer._tcp.local."));
assert!(!valid_instance_name("_printer._tcp.local."));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/service_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ mod tests {

// test decode
let decoded = decode_txt(&encoded);
assert!(&properties[..] == &decoded[..]);
assert!(properties[..] == decoded[..]);

// test empty value
let properties = vec![TxtProperty::from(&("key3", ""))];
Expand Down
4 changes: 2 additions & 2 deletions tests/addr_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn test_addr_str() {

// verify that `&String` also works.
assert_eq!(
(&addr).as_ip_addrs(),
addr.as_ip_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv4Addr::new(127, 0, 0, 1).into());
Expand Down Expand Up @@ -134,7 +134,7 @@ fn test_addr_ip() {
);

assert_eq!(
(&ip).as_ip_addrs(),
ip.as_ip_addrs(),
Ok({
let mut set = HashSet::new();
set.insert(Ipv4Addr::new(127, 0, 0, 1).into());
Expand Down
Loading

0 comments on commit 0737495

Please sign in to comment.