Browse Source

Use crossbeam channel instead of std::sync::mpsc.

wip
Titouan Rigoudy 6 years ago
parent
commit
caebbc52c4
6 changed files with 31 additions and 19 deletions
  1. +11
    -0
      Cargo.lock
  2. +1
    -0
      Cargo.toml
  3. +5
    -5
      src/client.rs
  4. +3
    -3
      src/control/ws.rs
  5. +3
    -3
      src/main.rs
  6. +8
    -8
      src/proto/handler.rs

+ 11
- 0
Cargo.lock View File

@ -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"


+ 1
- 0
Cargo.toml View File

@ -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"


+ 5
- 5
src/client.rs View File

@ -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::Request>,
proto_rx: mpsc::Receiver<proto::Response>,
proto_rx: crossbeam_channel::Receiver<proto::Response>,
control_tx: Option<control::Sender>,
control_rx: mpsc::Receiver<control::Notification>,
control_rx: crossbeam_channel::Receiver<control::Notification>,
login_status: LoginStatus,
@ -68,8 +68,8 @@ impl Client {
/// through `control_rx`.
pub fn new(
proto_tx: mio::deprecated::Sender<proto::Request>,
proto_rx: mpsc::Receiver<proto::Response>,
control_rx: mpsc::Receiver<control::Notification>,
proto_rx: crossbeam_channel::Receiver<proto::Response>,
control_rx: crossbeam_channel::Receiver<control::Notification>,
) -> Self {
Client {
proto_tx: proto_tx,


+ 3
- 3
src/control/ws.rs View File

@ -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<Notification>,
client_tx: crossbeam_channel::Sender<Notification>,
/// 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<Notification>) {
pub fn listen(client_tx: crossbeam_channel::Sender<Notification>) {
let websocket_result = ws::Builder::new()
.with_settings(ws::Settings {
max_connections: 1,


+ 3
- 3
src/main.rs View File

@ -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);


+ 8
- 8
src/proto/handler.rs View File

@ -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<Response>);
pub struct ServerResponseSender(crossbeam_channel::Sender<Response>);
impl SendPacket for ServerResponseSender {
type Value = ServerResponse;
type Error = mpsc::SendError<Response>;
type Error = crossbeam_channel::SendError<Response>;
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<Response>,
sender: crossbeam_channel::Sender<Response>,
peer_id: usize,
}
impl SendPacket for PeerResponseSender {
type Value = peer::Message;
type Error = mpsc::SendError<Response>;
type Error = crossbeam_channel::SendError<Response>;
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<Response>,
client_tx: crossbeam_channel::Sender<Response>,
}
fn listener_bind<U>(addr_spec: U) -> io::Result<mio::tcp::TcpListener>
@ -117,7 +117,7 @@ where
impl Handler {
fn new(
client_tx: mpsc::Sender<Response>,
client_tx: crossbeam_channel::Sender<Response>,
event_loop: &mut mio::deprecated::EventLoop<Self>,
) -> io::Result<Self> {
let host = config::SERVER_HOST;
@ -349,7 +349,7 @@ pub struct Agent {
}
impl Agent {
pub fn new(client_tx: mpsc::Sender<Response>) -> io::Result<Self> {
pub fn new(client_tx: crossbeam_channel::Sender<Response>) -> io::Result<Self> {
// Create the event loop.
let mut event_loop = mio::deprecated::EventLoop::new()?;
// Create the handler for the event loop and register the handler's


Loading…
Cancel
Save