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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::sync::{
atomic::{AtomicBool, Ordering::SeqCst},
Arc, Mutex,
};
use puffin::{FrameData, FrameView};
pub struct Client {
addr: String,
connected: Arc<AtomicBool>,
alive: Arc<AtomicBool>,
frame_view: Arc<Mutex<FrameView>>,
}
impl Drop for Client {
fn drop(&mut self) {
self.alive.store(false, SeqCst);
}
}
impl Client {
pub fn new(addr: String) -> Self {
let alive = Arc::new(AtomicBool::new(true));
let connected = Arc::new(AtomicBool::new(false));
let frame_view = Arc::new(Mutex::new(FrameView::default()));
let client = Self {
addr: addr.clone(),
connected: connected.clone(),
alive: alive.clone(),
frame_view: frame_view.clone(),
};
std::thread::spawn(move || {
log::info!("Connecting to {}…", addr);
while alive.load(SeqCst) {
match std::net::TcpStream::connect(&addr) {
Ok(mut stream) => {
log::info!("Connected to {}", addr);
connected.store(true, SeqCst);
while alive.load(SeqCst) {
match consume_message(&mut stream) {
Ok(frame_data) => {
frame_view
.lock()
.unwrap()
.add_frame(std::sync::Arc::new(frame_data));
}
Err(err) => {
log::warn!(
"Connection to puffin server closed: {}",
error_display_chain(err.as_ref())
);
connected.store(false, SeqCst);
break;
}
}
}
}
Err(err) => {
log::debug!("Failed to connect to {}: {}", addr, err);
std::thread::sleep(std::time::Duration::from_secs(1));
}
}
}
});
client
}
pub fn addr(&self) -> &str {
&self.addr
}
pub fn connected(&self) -> bool {
self.connected.load(SeqCst)
}
pub fn frame_view(&self) -> std::sync::MutexGuard<'_, FrameView> {
self.frame_view.lock().unwrap()
}
}
pub fn consume_message(stream: &mut impl std::io::Read) -> anyhow::Result<puffin::FrameData> {
let mut server_version = [0_u8; 2];
stream.read_exact(&mut server_version)?;
let server_version = u16::from_le_bytes(server_version);
match server_version.cmp(&crate::PROTOCOL_VERSION) {
std::cmp::Ordering::Less => {
anyhow::bail!(
"puffin server is using an older protocol version ({}) than the client ({}).",
server_version,
crate::PROTOCOL_VERSION
);
}
std::cmp::Ordering::Equal => {}
std::cmp::Ordering::Greater => {
anyhow::bail!(
"puffin server is using a newer protocol version ({}) than the client ({}). Update puffin_viewer with 'cargo install puffin_viewer'.",
server_version,
crate::PROTOCOL_VERSION
);
}
}
use anyhow::Context as _;
FrameData::read_next(stream)
.context("Failed to parse FrameData")?
.ok_or_else(|| anyhow::format_err!("End of stream"))
}
fn error_display_chain(error: &dyn std::error::Error) -> String {
let mut s = error.to_string();
if let Some(source) = error.source() {
s.push_str(" -> ");
s.push_str(&error_display_chain(source));
}
s
}