aboutsummaryrefslogtreecommitdiffstats
path: root/src/timer.rs
diff options
context:
space:
mode:
authorJake McGinty <me@jake.su>2018-02-14 13:36:41 +0000
committerJake McGinty <me@jake.su>2018-02-14 13:36:41 +0000
commit59687b82503f30ccfc169a83dc699177b0baba85 (patch)
tree12a4886c59e7b00c4df857655610bd3b8230e108 /src/timer.rs
parentuse constant time comparison for mac (diff)
downloadwireguard-rs-59687b82503f30ccfc169a83dc699177b0baba85.tar.xz
wireguard-rs-59687b82503f30ccfc169a83dc699177b0baba85.zip
timer module
Diffstat (limited to 'src/timer.rs')
-rw-r--r--src/timer.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/timer.rs b/src/timer.rs
new file mode 100644
index 0000000..f4d826d
--- /dev/null
+++ b/src/timer.rs
@@ -0,0 +1,46 @@
+use failure::Error;
+use futures::{self, Async, Future, Stream, Sink, Poll, future, unsync, sync, stream};
+use std::time::Duration;
+use tokio_core::reactor::Handle;
+use tokio_timer::{self, Interval};
+use interface::SharedPeer;
+
+#[derive(Debug)]
+pub enum TimerMessage {
+ KeepAlive(SharedPeer, u32),
+ Rekey(SharedPeer, u32),
+}
+
+pub struct Timer {
+ timer: tokio_timer::Timer,
+ tx: unsync::mpsc::Sender<TimerMessage>,
+ rx: unsync::mpsc::Receiver<TimerMessage>,
+}
+
+impl Timer {
+ pub fn new() -> Self {
+ let (tx, rx) = unsync::mpsc::channel::<TimerMessage>(1024);
+ let timer = tokio_timer::Timer::default();
+ Self { timer, tx, rx }
+ }
+
+ pub fn spawn_delayed(&mut self, handle: &Handle, delay_secs: u64, message: TimerMessage) {
+ let timer = self.timer.sleep(Duration::from_secs(delay_secs)).map_err(|_|());
+ let future = timer.and_then({
+ let tx = self.tx.clone();
+ move |_| {
+ tx.clone().send(message).then(|_| Ok(()))
+ }
+ }).then(|_| Ok(()));
+ handle.spawn(future);
+ }
+}
+
+impl Stream for Timer {
+ type Item = TimerMessage;
+ type Error = ();
+
+ fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
+ self.rx.poll()
+ }
+}