fix(client): 修复双实例镜像同步并调整底部工具布局
This commit is contained in:
36 files changed
+274
-88
No files matched your search
@@ -30,6 +30,7 @@ pub mod armor;
|
||||
pub mod armor_reset;
|
||||
pub mod config;
|
||||
pub mod camera_mirror;
|
||||
pub mod mirror_transport;
|
||||
pub mod match_clock;
|
||||
pub mod operator;
|
||||
pub mod password;
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
//! 镜像使用独立的组播监听。同机多实例均可收到消息,不争用装甲板单播回执端口。
|
||||
use std::net::{Ipv4Addr, SocketAddrV4, UdpSocket};
|
||||
use std::time::Duration;
|
||||
pub const GROUP: Ipv4Addr = Ipv4Addr::new(239, 255, 56, 2);
|
||||
pub const PORT: u16 = 5602;
|
||||
pub fn bind() -> std::io::Result<UdpSocket> {
|
||||
let socket = socket2::Socket::new(
|
||||
socket2::Domain::IPV4,
|
||||
socket2::Type::DGRAM,
|
||||
Some(socket2::Protocol::UDP),
|
||||
)?;
|
||||
socket.set_reuse_address(true)?;
|
||||
socket.bind(&SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, PORT).into())?;
|
||||
let socket: UdpSocket = socket.into();
|
||||
socket.join_multicast_v4(&GROUP, &Ipv4Addr::UNSPECIFIED)?;
|
||||
socket.set_multicast_loop_v4(true)?;
|
||||
socket.set_multicast_ttl_v4(1)?;
|
||||
socket.set_read_timeout(Some(Duration::from_millis(200)))?;
|
||||
socket.set_write_timeout(Some(Duration::from_millis(10)))?;
|
||||
Ok(socket)
|
||||
}
|
||||
pub fn spawn(
|
||||
socket: UdpSocket,
|
||||
sink: impl Fn(protocol::CameraMirror) + Send + 'static,
|
||||
) -> std::io::Result<()> {
|
||||
std::thread::Builder::new()
|
||||
.name("mirror-reader".into())
|
||||
.spawn(move || {
|
||||
let mut bytes = [0u8; 128];
|
||||
loop {
|
||||
match socket.recv_from(&mut bytes) {
|
||||
Ok((len, _)) => {
|
||||
if let Some(packet) = protocol::CameraMirror::decode(&bytes[..len]) {
|
||||
sink(packet);
|
||||
}
|
||||
}
|
||||
Err(e)
|
||||
if matches!(
|
||||
e.kind(),
|
||||
std::io::ErrorKind::TimedOut | std::io::ErrorKind::WouldBlock
|
||||
) => {}
|
||||
Err(_) => std::thread::sleep(Duration::from_millis(200)),
|
||||
}
|
||||
}
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user