Skip to content

Commit

Permalink
Preliminary fix for broken connections (see #61)
Browse files Browse the repository at this point in the history
  • Loading branch information
7h0ma5 committed Mar 23, 2017
1 parent ad87c89 commit cadaf63
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 11 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
15 changes: 7 additions & 8 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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()?),
Expand All @@ -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 {
Expand All @@ -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);
Expand All @@ -97,18 +96,18 @@ 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();

while self.reader.read_line(&mut buffer)? > 0 {
self.handle(&*buffer)?;
buffer.clear();
}

Ok(())
}

Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit cadaf63

Please sign in to comment.