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
use crate::{error::Result, server::ServerConfig};
use crossbeam_channel::{Receiver, Sender};
use laminar::{Packet, Socket, SocketEvent};
use std::thread;
#[allow(missing_debug_implementations)]
pub struct Host {
packet_sender: Sender<Packet>,
packet_receiver: Receiver<SocketEvent>,
}
impl Host {
pub fn run(server_config: &ServerConfig) -> Result<Host> {
let (mut socket, packet_sender, packet_receiver) = Socket::bind_with_config(
server_config.udp_socket_addr,
server_config.laminar_config.clone(),
)?;
thread::spawn(move || {
socket.start_polling().unwrap();
});
Ok(Host {
packet_sender,
packet_receiver,
})
}
pub fn udp_receive_handle(&self) -> Receiver<SocketEvent> {
self.packet_receiver.clone()
}
pub fn udp_send_handle(&self) -> Sender<Packet> {
self.packet_sender.clone()
}
pub fn send_udp(&mut self, packet: Packet) -> Result<()> {
self.packet_sender.send(packet)?;
Ok(())
}
}