summaryrefslogtreecommitdiffhomepage
path: root/talpid_ipc/src
diff options
context:
space:
mode:
authorErik Larkö <erik@mullvad.net>2017-03-01 12:11:50 +0800
committerErik Larkö <erik@mullvad.net>2017-03-02 21:29:23 +0800
commit9dacaac457282cb26311a1e030be73fe94d6bc76 (patch)
treefe67cec91cd420f4c3e6c67d7fd21f1a95591cc9 /talpid_ipc/src
parente8bdd0b098a5851973f7a4a6caf42827fd55efc5 (diff)
downloadmullvadvpn-9dacaac457282cb26311a1e030be73fe94d6bc76.tar.xz
mullvadvpn-9dacaac457282cb26311a1e030be73fe94d6bc76.zip
Removed the parsing of messages from the server
Diffstat (limited to 'talpid_ipc/src')
-rw-r--r--talpid_ipc/src/zmq_ipc.rs40
1 files changed, 9 insertions, 31 deletions
diff --git a/talpid_ipc/src/zmq_ipc.rs b/talpid_ipc/src/zmq_ipc.rs
index ff4c4372a3..0afab082b2 100644
--- a/talpid_ipc/src/zmq_ipc.rs
+++ b/talpid_ipc/src/zmq_ipc.rs
@@ -3,46 +3,30 @@ extern crate zmq;
use super::{OnMessage, ErrorKind, Result, ResultExt};
use std::thread;
-/// The signature of functions that can be used to parse the incoming data
-/// This is very very similar to `TryFrom` on purpose because I wanted `TryFrom`,
-/// but I couldn't get it to work.
-type MessageParser<MessageType> = Fn(Vec<u8>) -> Result<MessageType> + Send + 'static;
-
type IpcServerId = String;
/// The server end of our Inter-Process Communcation implementation.
-/// It can parse any kind of message, so when you create
-/// it you supply it with a `parser` that converts
-/// the read bytes into the type you want.
-pub struct Server<T>
- where T: 'static
-{
- parser: Box<MessageParser<T>>,
+pub struct Server {
port: u16,
}
-impl<T> Server<T>
- where T: 'static
-{
- pub fn new(port: u16, parser: Box<MessageParser<T>>) -> Self {
- Server {
- port: port,
- parser: parser,
- }
+impl Server {
+ pub fn new(port: u16) -> Self {
+ Server { port: port }
}
/// Starts listening to incoming IPC connections on the specified port.
/// Messages are sent to the `on_message` callback. If anything went wrong
- /// when reading or parsing the message, the message will be an `Err`.
+ /// when reading the message, the message will be an `Err`.
/// NOTE that this does not apply to errors regarding whether the server
/// could start or not, those are returned directly by this function.
///
/// This function is non-blocking and thus spawns a thread where it
/// listens to messages.
- pub fn start(self, on_message: Box<OnMessage<T>>) -> Result<IpcServerId> {
+ pub fn start(self, on_message: Box<OnMessage<Vec<u8>>>) -> Result<IpcServerId> {
let socket =
Self::start_zmq_server(self.port).chain_err(|| ErrorKind::CouldNotStartServer)?;
- let _ = Self::start_receive_loop(socket, on_message, self.parser);
+ let _ = Self::start_receive_loop(socket, on_message);
Ok(format!("localhost:{}", self.port))
}
@@ -57,18 +41,12 @@ impl<T> Server<T>
}
fn start_receive_loop(socket: zmq::Socket,
- mut on_message: Box<OnMessage<T>>,
- parser: Box<MessageParser<T>>)
+ mut on_message: Box<OnMessage<Vec<u8>>>)
-> thread::JoinHandle<()> {
thread::spawn(move || loop {
- let read_res = Self::read(&socket, &parser);
+ let read_res = socket.recv_bytes(0).chain_err(|| ErrorKind::ReadFailure);
on_message(read_res)
})
}
-
- fn read(socket: &zmq::Socket, parser: &Box<MessageParser<T>>) -> Result<T> {
- let bytes = socket.recv_bytes(0).chain_err(|| ErrorKind::ReadFailure)?;
- parser(bytes)
- }
}