From 326855dc3a8e5bf2fbbaa05afbe7ea8c89489d8c Mon Sep 17 00:00:00 2001 From: NikVolf Date: Sun, 3 Apr 2016 23:58:18 +0300 Subject: [PATCH] basic polling --- ipc/nano/src/lib.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/ipc/nano/src/lib.rs b/ipc/nano/src/lib.rs index 166c4ea53..c43cb9b3e 100644 --- a/ipc/nano/src/lib.rs +++ b/ipc/nano/src/lib.rs @@ -22,21 +22,34 @@ extern crate nanomsg; pub use ipc::*; use std::sync::*; +use std::io::Write; use nanomsg::{Socket, Protocol}; pub struct Worker where S: IpcInterface { service: Arc, sockets: Vec, + method_buf: [u8;2], } impl Worker where S: IpcInterface { - pub fn new(service: Arc, socket_addr: &str) -> Worker { + pub fn new(service: Arc) -> Worker { Worker:: { service: service.clone(), sockets: Vec::new(), + method_buf: [0,0] } } - pub fn work_loop(&mut self) { + pub fn poll(&mut self) { + for socket in self.sockets.iter_mut() { + if let Ok(method_sig_len) = socket.nb_read(&mut self.method_buf) { + if method_sig_len == 2 { + let result = self.service.dispatch_buf( + self.method_buf[1] as u16 * 256 + self.method_buf[0] as u16, + socket); + socket.write(&result); + } + } + } } }