read_write_to_socket/
read_write_to_socket.rs1use std::io::stdin;
2use std::net::{Ipv4Addr, UdpSocket};
3use std::sync::Arc;
4use std::thread;
5use std::thread::sleep;
6use std::time::Duration;
7
8use battleye_rust::remote_console::BERemoteConsole;
9use battleye_rust::socket::udp::UdpSocketConnection;
10
11#[allow(unused_must_use)]
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::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 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}