From cadaf63114c9ad271a4fa4672fa184ededa61832 Mon Sep 17 00:00:00 2001 From: Thomas Gatzweiler Date: Fri, 24 Mar 2017 00:57:30 +0100 Subject: [PATCH] Preliminary fix for broken connections (see #61) --- Cargo.lock | 1 - Cargo.toml | 1 - src/connection.rs | 15 +++++++-------- src/main.rs | 1 - 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 33891d8..17b38f6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,6 @@ version = "0.5.0" dependencies = [ "lazy_static 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", - "net2 0.2.26 (registry+https://github.com/rust-lang/crates.io-index)", "raspi 0.1.0", "serde 0.9.10 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 0.9.10 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index e9d8dd8..1721ce3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,6 @@ serde = "^0.9" serde_derive = "^0.9" serde_json = "^0.9" lazy_static = "^0.2.2" -net2 = "^0.2.26" [dependencies.raspi] path = "lib/raspi" diff --git a/src/connection.rs b/src/connection.rs index 8cc9da9..cff0ddd 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -4,7 +4,6 @@ use std::time::Duration; use std::thread::{self, JoinHandle}; use std::sync::mpsc::{Sender, channel}; use std::str::{FromStr}; -use net2::TcpStreamExt; use config::Config; use pocsag::{Scheduler, TimeSlots}; @@ -33,7 +32,7 @@ impl Connection { let addr = (&*config.master.server, config.master.port); let stream = TcpStream::connect(addr)?; stream.set_write_timeout(Some(Duration::from_millis(10000)))?; - stream.set_keepalive(Some(Duration::from_millis(5000)))?; + stream.set_read_timeout(Some(Duration::from_millis(125000)))?; Ok(Connection { reader: BufReader::new(stream.try_clone()?), @@ -49,7 +48,7 @@ impl Connection { pub fn start(config: Config, scheduler: Scheduler) -> (Sender<()>, JoinHandle<()>) { let (stop_tx, stop_rx) = channel(); let mut reconnect = true; - let mut delay = Duration::from_millis(1000); + let mut delay = Duration::from_millis(5000); let handle = thread::spawn(move || { while reconnect { @@ -72,12 +71,12 @@ impl Connection { _ = stop_rx.recv() => reconnect = false } - stream.shutdown(Shutdown::Both).unwrap(); + stream.shutdown(Shutdown::Both).ok(); handle.join().unwrap(); status!(connected: false); warn!("Disconnected from master."); - delay = Duration::from_millis(1000); + delay = Duration::from_millis(2500); } else { status!(connected: false); @@ -97,11 +96,10 @@ impl Connection { pub fn run(&mut self) -> Result<()> { let version = env!("CARGO_PKG_VERSION"); - let id = format!("[UniPager-{} v{} {} {}]\r\n", + let id = format!("[UniPager-{} v{} {} {}]", self.id, version, self.call, self.auth); - self.writer.write(id.as_bytes())?; - self.writer.flush()?; + self.send(&*id)?; let mut buffer = String::new(); @@ -109,6 +107,7 @@ impl Connection { self.handle(&*buffer)?; buffer.clear(); } + Ok(()) } diff --git a/src/main.rs b/src/main.rs index c444b48..bf568c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,6 @@ extern crate serial; extern crate raspi; extern crate ws; extern crate tiny_http; -extern crate net2; extern crate serde; #[macro_use] extern crate serde_derive;