From caebbc52c4aa5ca5b244b6bbc887e7d9bbddee6a Mon Sep 17 00:00:00 2001 From: Titouan Rigoudy Date: Sat, 18 May 2019 20:48:41 +0000 Subject: [PATCH] Use crossbeam channel instead of std::sync::mpsc. --- Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + src/client.rs | 10 +++++----- src/control/ws.rs | 6 +++--- src/main.rs | 6 +++--- src/proto/handler.rs | 16 ++++++++-------- 6 files changed, 31 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 90f3b1a..d0ff298 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -66,6 +66,15 @@ dependencies = [ "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "crossbeam-channel" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", + "smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "crossbeam-deque" version = "0.7.1" @@ -651,6 +660,7 @@ version = "0.1.0" dependencies = [ "byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "encoding 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", @@ -987,6 +997,7 @@ dependencies = [ "checksum bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c" "checksum cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" +"checksum crossbeam-channel 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "0f0ed1a4de2235cabda8558ff5840bffb97fcb64c97827f354a451307df5f72b" "checksum crossbeam-deque 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b18cd2e169ad86297e6bc0ad9aa679aee9daa4f19e8163860faf7c164e4f5a71" "checksum crossbeam-epoch 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "04c9e3102cc2d69cd681412141b390abd55a362afc1540965dad0ad4d34280b4" "checksum crossbeam-queue 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7c979cd6cfe72335896575c6b5688da489e420d36a27a0b9eb0c73db574b4a4b" diff --git a/Cargo.toml b/Cargo.toml index 5b3b74b..c67ddf9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" [dependencies] byteorder = "^0.5.1" bytes = "^0.4" +crossbeam-channel = "^0.3" encoding = "^0.2" env_logger = "^0.3.2" futures = "^0.1" diff --git a/src/client.rs b/src/client.rs index 53b3418..6993724 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,6 +1,6 @@ use std::net; -use std::sync::mpsc; +use crossbeam_channel; use mio; use slab; @@ -49,10 +49,10 @@ struct Peer { pub struct Client { proto_tx: mio::deprecated::Sender, - proto_rx: mpsc::Receiver, + proto_rx: crossbeam_channel::Receiver, control_tx: Option, - control_rx: mpsc::Receiver, + control_rx: crossbeam_channel::Receiver, login_status: LoginStatus, @@ -68,8 +68,8 @@ impl Client { /// through `control_rx`. pub fn new( proto_tx: mio::deprecated::Sender, - proto_rx: mpsc::Receiver, - control_rx: mpsc::Receiver, + proto_rx: crossbeam_channel::Receiver, + control_rx: crossbeam_channel::Receiver, ) -> Self { Client { proto_tx: proto_tx, diff --git a/src/control/ws.rs b/src/control/ws.rs index 4bda16a..8eda242 100644 --- a/src/control/ws.rs +++ b/src/control/ws.rs @@ -1,7 +1,7 @@ use std::error; use std::fmt; -use std::sync::mpsc; +use crossbeam_channel; use rustc_serialize::json; use ws; @@ -92,7 +92,7 @@ impl Sender { #[derive(Debug)] struct Handler { /// The channel on which to send notifications to the client. - client_tx: mpsc::Sender, + client_tx: crossbeam_channel::Sender, /// The channel on which to send messages to the controller. socket_tx: ws::Sender, } @@ -154,7 +154,7 @@ impl ws::Handler for Handler { /// Start listening on the socket address stored in configuration, and send /// control notifications to the client through the given channel. -pub fn listen(client_tx: mpsc::Sender) { +pub fn listen(client_tx: crossbeam_channel::Sender) { let websocket_result = ws::Builder::new() .with_settings(ws::Settings { max_connections: 1, diff --git a/src/main.rs b/src/main.rs index d9a6f90..3c20c69 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,9 +9,9 @@ mod user; #[macro_use] extern crate log; -use std::sync::mpsc; use std::thread; +use crossbeam_channel; use env_logger; fn main() { @@ -23,7 +23,7 @@ fn main() { } }; - let (proto_to_client_tx, proto_to_client_rx) = mpsc::channel(); + let (proto_to_client_tx, proto_to_client_rx) = crossbeam_channel::unbounded(); let mut proto_agent = match proto::Agent::new(proto_to_client_tx) { Ok(agent) => agent, @@ -34,7 +34,7 @@ fn main() { }; let client_to_proto_tx = proto_agent.channel(); - let (control_to_client_tx, control_to_client_rx) = mpsc::channel(); + let (control_to_client_tx, control_to_client_rx) = crossbeam_channel::unbounded(); let mut client = client::Client::new(client_to_proto_tx, proto_to_client_rx, control_to_client_rx); diff --git a/src/proto/handler.rs b/src/proto/handler.rs index 6f2fa77..1ce203a 100644 --- a/src/proto/handler.rs +++ b/src/proto/handler.rs @@ -2,10 +2,10 @@ use std::fmt; use std::io; use std::net; use std::net::ToSocketAddrs; -use std::sync::mpsc; use mio; use slab; +use crossbeam_channel; use crate::config; @@ -47,11 +47,11 @@ pub enum Response { * SERVER RESPONSE SENDER * *========================*/ -pub struct ServerResponseSender(mpsc::Sender); +pub struct ServerResponseSender(crossbeam_channel::Sender); impl SendPacket for ServerResponseSender { type Value = ServerResponse; - type Error = mpsc::SendError; + type Error = crossbeam_channel::SendError; fn send_packet(&mut self, value: Self::Value) -> Result<(), Self::Error> { self.0.send(Response::ServerResponse(value)) @@ -67,13 +67,13 @@ impl SendPacket for ServerResponseSender { *======================*/ pub struct PeerResponseSender { - sender: mpsc::Sender, + sender: crossbeam_channel::Sender, peer_id: usize, } impl SendPacket for PeerResponseSender { type Value = peer::Message; - type Error = mpsc::SendError; + type Error = crossbeam_channel::SendError; fn send_packet(&mut self, value: Self::Value) -> Result<(), Self::Error> { self.sender.send(Response::PeerMessage(self.peer_id, value)) @@ -97,7 +97,7 @@ struct Handler { listener: mio::tcp::TcpListener, - client_tx: mpsc::Sender, + client_tx: crossbeam_channel::Sender, } fn listener_bind(addr_spec: U) -> io::Result @@ -117,7 +117,7 @@ where impl Handler { fn new( - client_tx: mpsc::Sender, + client_tx: crossbeam_channel::Sender, event_loop: &mut mio::deprecated::EventLoop, ) -> io::Result { let host = config::SERVER_HOST; @@ -349,7 +349,7 @@ pub struct Agent { } impl Agent { - pub fn new(client_tx: mpsc::Sender) -> io::Result { + pub fn new(client_tx: crossbeam_channel::Sender) -> io::Result { // Create the event loop. let mut event_loop = mio::deprecated::EventLoop::new()?; // Create the handler for the event loop and register the handler's