Skip to content

Instantly share code, notes, and snippets.

Created May 7, 2016 01:26
Show Gist options
  • Save anonymous/64343cf0a73ef2f8e86da4be73f4f58c to your computer and use it in GitHub Desktop.
Save anonymous/64343cf0a73ef2f8e86da4be73f4f58c to your computer and use it in GitHub Desktop.
Shared via Rust Playground
// incoming packet handler and reconnection thread
thread::spawn(move || {
// get underlying tcpstream clone from connection object
let mut _stream_pkt_hndlr_thrd = {
let ref connection = *_client_pkt_hndlr_thrd.connection.lock().unwrap();
let stream = match connection.stream {
Some(ref s) => s,
None => panic!("No stream found in the connectino"),
};
stream.try_clone().unwrap()
};
loop {
// blocking
let packet = match VariablePacket::decode(&mut _stream_pkt_hndlr_thrd) {
Ok(pk) => pk,
Err(err) => {
println!("error in receiving packet {:?}", err);
let mut connection = _client_pkt_hndlr_thrd.connection.lock().unwrap();
if connection.retry(3) {
continue;
} else {
// do an on weird callback here so that user can handle disconnection
unimplemented!();
continue; //remove this later
}
}
};
_client_pkt_hndlr_thrd.handle_packet(&packet);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment