-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.rs
45 lines (39 loc) · 1.06 KB
/
debug.rs
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
use std::net::{TcpStream, IpAddr};
use std::io::Write;
use std::thread::sleep;
use std::time::Duration;
pub struct DebugServer {
ip: IpAddr,
port: u16,
con: TcpStream,
}
impl DebugServer {
pub fn new(ip: IpAddr, port: u16) -> Self {
// Loop until we connect to the debug server
let con;
loop {
if let Ok(c) = TcpStream::connect(format!("{}:{}", ip, port)) {
con = c;
break;
}
println!("Failed to connect to debug server, trying again in 5 secs...");
sleep(Duration::from_secs(5))
}
Self {
ip,
port,
con,
}
}
pub fn send_message<T: AsRef<str>>(&mut self, msg: T) {
// Try 5 times and then return
for _ in 0..5 {
let res = self.con.write_all(msg.as_ref().as_bytes());
if let Ok(_) = res {
return
}
println!("Failed to send debug message, trying again");
sleep(Duration::from_secs(5))
}
}
}