pub struct UdpSocketConnection { /* private fields */ }
Implementations§
Source§impl UdpSocketConnection
impl UdpSocketConnection
Sourcepub fn new(udp_socket: UdpSocket) -> Self
pub fn new(udp_socket: UdpSocket) -> Self
Create new UdpSocketConnection
Examples found in repository?
examples/listen_to_socket.rs (line 20)
11fn main() {
12 let ip = "127.0.0.1".to_string();
13 let port = 2306;
14 let password = "password".to_string();
15 let udp_socket =
16 UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 0)).expect("Unable to bind an IP address");
17 udp_socket.connect(ip.to_string() + ":" + &port.to_string());
18
19 let be_remote_console: Arc<BERemoteConsole> =
20 Arc::new(BERemoteConsole::new(UdpSocketConnection::new(udp_socket)));
21
22 be_remote_console.authenticate(password);
23
24 let keep_alive_socket = be_remote_console.clone();
25
26 thread::spawn(move || loop {
27 sleep(Duration::from_secs(35)); // BE recommends sending a keep alive packet before 45 seconds.
28 keep_alive_socket.keep_alive();
29 });
30
31 thread::spawn(move || loop {
32 sleep(Duration::from_millis(50)); // Reduce CPU workload
33 let response = be_remote_console
34 .receive_data()
35 .expect("Failed to receive socket data");
36
37 if response.is_empty() {
38 continue;
39 }
40
41 // println!("{:#04X?}", response);
42
43 match response[1] {
44 0x00 => {
45 if response[2] == 0x01 {
46 println!("Authentication accepted");
47 } else {
48 println!("Password does not match with BattlEye config file");
49 }
50 }
51 0x01 => {
52 if response[2] == 0x00 {
53 println!(
54 "{}",
55 String::from_utf8(response[3..response.len()].to_owned()).unwrap()
56 );
57 }
58 }
59 0x02 => {
60 println!(
61 "{}",
62 String::from_utf8(response[3..response.len()].to_owned()).unwrap()
63 );
64 }
65 _ => {
66 println!("Unknown packet identifier");
67 }
68 }
69 })
70 .join();
71}
More examples
examples/read_write_to_socket.rs (line 21)
12fn main() {
13 let ip = "127.0.0.1".to_string();
14 let port = 2306;
15 let password = "password".to_string();
16 let udp_socket =
17 UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 0)).expect("Unable to bind an IP address");
18 udp_socket.connect(ip.to_string() + ":" + &port.to_string());
19
20 let be_remote_console: Arc<BERemoteConsole> =
21 Arc::new(BERemoteConsole::new(UdpSocketConnection::new(udp_socket)));
22
23 be_remote_console.authenticate(password);
24
25 let socket_commands = be_remote_console.clone();
26 let socket_keep_alive = be_remote_console.clone();
27
28 thread::spawn(move || loop {
29 sleep(Duration::from_secs(35));
30 socket_keep_alive.keep_alive();
31 });
32
33 // Thread for terminal input
34 thread::spawn(move || loop {
35 let mut input_string = String::new();
36 stdin()
37 .read_line(&mut input_string)
38 .expect("Did not enter a correct string");
39 socket_commands.send_command(input_string.trim());
40 });
41
42 thread::spawn(move || loop {
43 let response = be_remote_console
44 .receive_data()
45 .expect("Failed to receive socket data");
46
47 if response.is_empty() {
48 continue;
49 }
50
51 // println!("{:#04X?}", response);
52
53 match response[1] {
54 0x00 => {
55 if response[2] == 0x01 {
56 println!("Authentication accepted.");
57 } else {
58 println!("Password does not match with BattlEye config file.");
59 }
60 }
61 0x01 => {
62 if response[2] == 0x00 && response.len() > 3 {
63 println!(
64 "{}",
65 String::from_utf8(response[3..response.len()].to_owned()).unwrap()
66 );
67 }
68 }
69 0x02 => {
70 println!(
71 "{}",
72 String::from_utf8(response[3..response.len()].to_owned()).unwrap()
73 );
74 }
75 _ => {
76 println!("Unknown packet identifier.");
77 }
78 }
79 })
80 .join();
81}
Auto Trait Implementations§
impl Freeze for UdpSocketConnection
impl RefUnwindSafe for UdpSocketConnection
impl Send for UdpSocketConnection
impl Sync for UdpSocketConnection
impl Unpin for UdpSocketConnection
impl UnwindSafe for UdpSocketConnection
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more