refact: android ios, lan discovery (#9207)
* refact: android ios, lan discovery Signed-off-by: fufesou <linlong1266@gmail.com> * fix: build and runtime error Signed-off-by: fufesou <linlong1266@gmail.com> --------- Signed-off-by: fufesou <linlong1266@gmail.com>
This commit is contained in:
parent
832002a10f
commit
e3f6829d02
38
src/lan.rs
38
src/lan.rs
@ -1,4 +1,3 @@
|
|||||||
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
|
||||||
use hbb_common::config::Config;
|
use hbb_common::config::Config;
|
||||||
use hbb_common::{
|
use hbb_common::{
|
||||||
allow_err,
|
allow_err,
|
||||||
@ -22,7 +21,7 @@ use std::{
|
|||||||
|
|
||||||
type Message = RendezvousMessage;
|
type Message = RendezvousMessage;
|
||||||
|
|
||||||
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
#[cfg(not(target_os = "ios"))]
|
||||||
pub(super) fn start_listening() -> ResultType<()> {
|
pub(super) fn start_listening() -> ResultType<()> {
|
||||||
let addr = SocketAddr::from(([0, 0, 0, 0], get_broadcast_port()));
|
let addr = SocketAddr::from(([0, 0, 0, 0], get_broadcast_port()));
|
||||||
let socket = std::net::UdpSocket::bind(addr)?;
|
let socket = std::net::UdpSocket::bind(addr)?;
|
||||||
@ -40,13 +39,22 @@ pub(super) fn start_listening() -> ResultType<()> {
|
|||||||
&Config::get_option("enable-lan-discovery"),
|
&Config::get_option("enable-lan-discovery"),
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
let id = Config::get_id();
|
||||||
|
if p.id == id {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if let Some(self_addr) = get_ipaddr_by_peer(&addr) {
|
if let Some(self_addr) = get_ipaddr_by_peer(&addr) {
|
||||||
let mut msg_out = Message::new();
|
let mut msg_out = Message::new();
|
||||||
|
let mut hostname = whoami::hostname();
|
||||||
|
// The default hostname is "localhost" which is a bit confusing
|
||||||
|
if hostname == "localhost" {
|
||||||
|
hostname = "unknown".to_owned();
|
||||||
|
}
|
||||||
let peer = PeerDiscovery {
|
let peer = PeerDiscovery {
|
||||||
cmd: "pong".to_owned(),
|
cmd: "pong".to_owned(),
|
||||||
mac: get_mac(&self_addr),
|
mac: get_mac(&self_addr),
|
||||||
id: Config::get_id(),
|
id,
|
||||||
hostname: whoami::hostname(),
|
hostname,
|
||||||
username: crate::platform::get_active_username(),
|
username: crate::platform::get_active_username(),
|
||||||
platform: whoami::platform().to_string(),
|
platform: whoami::platform().to_string(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
@ -100,17 +108,17 @@ fn get_broadcast_port() -> u16 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_mac(_ip: &IpAddr) -> String {
|
fn get_mac(_ip: &IpAddr) -> String {
|
||||||
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
#[cfg(not(target_os = "ios"))]
|
||||||
if let Ok(mac) = get_mac_by_ip(_ip) {
|
if let Ok(mac) = get_mac_by_ip(_ip) {
|
||||||
mac.to_string()
|
mac.to_string()
|
||||||
} else {
|
} else {
|
||||||
"".to_owned()
|
"".to_owned()
|
||||||
}
|
}
|
||||||
#[cfg(any(target_os = "android", target_os = "ios"))]
|
#[cfg(target_os = "ios")]
|
||||||
"".to_owned()
|
"".to_owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
#[cfg(not(target_os = "ios"))]
|
||||||
fn get_mac_by_ip(ip: &IpAddr) -> ResultType<String> {
|
fn get_mac_by_ip(ip: &IpAddr) -> ResultType<String> {
|
||||||
for interface in default_net::get_interfaces() {
|
for interface in default_net::get_interfaces() {
|
||||||
match ip {
|
match ip {
|
||||||
@ -153,6 +161,10 @@ fn get_ipaddr_by_peer<A: ToSocketAddrs>(peer: A) -> Option<IpAddr> {
|
|||||||
|
|
||||||
fn create_broadcast_sockets() -> Vec<UdpSocket> {
|
fn create_broadcast_sockets() -> Vec<UdpSocket> {
|
||||||
let mut ipv4s = Vec::new();
|
let mut ipv4s = Vec::new();
|
||||||
|
// TODO: maybe we should use a better way to get ipv4 addresses.
|
||||||
|
// But currently, it's ok to use `[Ipv4Addr::UNSPECIFIED]` for discovery.
|
||||||
|
// `default_net::get_interfaces()` causes undefined symbols error when `flutter build` on iOS simulator x86_64
|
||||||
|
#[cfg(not(any(target_os = "ios")))]
|
||||||
for interface in default_net::get_interfaces() {
|
for interface in default_net::get_interfaces() {
|
||||||
for ipv4 in &interface.ipv4 {
|
for ipv4 in &interface.ipv4 {
|
||||||
ipv4s.push(ipv4.addr.clone());
|
ipv4s.push(ipv4.addr.clone());
|
||||||
@ -178,8 +190,20 @@ fn send_query() -> ResultType<Vec<UdpSocket>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut msg_out = Message::new();
|
let mut msg_out = Message::new();
|
||||||
|
// We may not be able to get the mac address on mobile platforms.
|
||||||
|
// So we need to use the id to avoid discovering ourselves.
|
||||||
|
#[cfg(any(target_os = "android", target_os = "ios"))]
|
||||||
|
let id = crate::ui_interface::get_id();
|
||||||
|
// `crate::ui_interface::get_id()` will cause error:
|
||||||
|
// `get_id()` uses async code with `current_thread`, which is not allowed in this context.
|
||||||
|
//
|
||||||
|
// No need to get id for desktop platforms.
|
||||||
|
// We can use the mac address to identify the device.
|
||||||
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
||||||
|
let id = "".to_owned();
|
||||||
let peer = PeerDiscovery {
|
let peer = PeerDiscovery {
|
||||||
cmd: "ping".to_owned(),
|
cmd: "ping".to_owned(),
|
||||||
|
id,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
msg_out.set_peer_discovery(peer);
|
msg_out.set_peer_discovery(peer);
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
mod keyboard;
|
mod keyboard;
|
||||||
#[cfg(not(any(target_os = "ios")))]
|
|
||||||
/// cbindgen:ignore
|
/// cbindgen:ignore
|
||||||
pub mod platform;
|
pub mod platform;
|
||||||
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
||||||
@ -12,7 +11,6 @@ mod server;
|
|||||||
#[cfg(not(any(target_os = "ios")))]
|
#[cfg(not(any(target_os = "ios")))]
|
||||||
pub use self::server::*;
|
pub use self::server::*;
|
||||||
mod client;
|
mod client;
|
||||||
#[cfg(not(any(target_os = "ios")))]
|
|
||||||
mod lan;
|
mod lan;
|
||||||
#[cfg(not(any(target_os = "ios")))]
|
#[cfg(not(any(target_os = "ios")))]
|
||||||
mod rendezvous_mediator;
|
mod rendezvous_mediator;
|
||||||
|
@ -100,6 +100,7 @@ impl WakeLock {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(target_os = "ios"))]
|
||||||
pub fn get_wakelock(_display: bool) -> WakeLock {
|
pub fn get_wakelock(_display: bool) -> WakeLock {
|
||||||
hbb_common::log::info!("new wakelock, require display on: {_display}");
|
hbb_common::log::info!("new wakelock, require display on: {_display}");
|
||||||
#[cfg(target_os = "android")]
|
#[cfg(target_os = "android")]
|
||||||
|
@ -76,8 +76,11 @@ impl RendezvousMediator {
|
|||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
direct_server(server_cloned).await;
|
direct_server(server_cloned).await;
|
||||||
});
|
});
|
||||||
|
#[cfg(target_os = "android")]
|
||||||
|
let start_lan_listening = true;
|
||||||
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
||||||
if crate::platform::is_installed() {
|
let start_lan_listening = crate::platform::is_installed();
|
||||||
|
if start_lan_listening {
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
allow_err!(super::lan::start_listening());
|
allow_err!(super::lan::start_listening());
|
||||||
});
|
});
|
||||||
|
@ -692,7 +692,6 @@ pub fn create_shortcut(_id: String) {
|
|||||||
#[cfg(any(target_os = "android", target_os = "ios", feature = "flutter"))]
|
#[cfg(any(target_os = "android", target_os = "ios", feature = "flutter"))]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn discover() {
|
pub fn discover() {
|
||||||
#[cfg(not(any(target_os = "ios")))]
|
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
allow_err!(crate::lan::discover());
|
allow_err!(crate::lan::discover());
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user