Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace rust-websocket with ws-rs in the debugger server. #14110

Merged
merged 1 commit into from Nov 7, 2016
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -15,6 +15,7 @@ bluetooth_traits = { path = "../bluetooth_traits" }
canvas = {path = "../canvas"}
canvas_traits = {path = "../canvas_traits"}
compositing = {path = "../compositing"}
debugger = {path = "../debugger"}
devtools_traits = {path = "../devtools_traits"}
euclid = "0.10.1"
gfx = {path = "../gfx"}
@@ -17,6 +17,7 @@ use canvas_traits::CanvasMsg;
use compositing::SendableFrameTree;
use compositing::compositor_thread::CompositorProxy;
use compositing::compositor_thread::Msg as ToCompositorMsg;
use debugger;
use devtools_traits::{ChromeToDevtoolsControlMsg, DevtoolsControlMsg};
use euclid::scale_factor::ScaleFactor;
use euclid::size::{Size2D, TypedSize2D};
@@ -113,6 +114,9 @@ pub struct Constellation<Message, LTF, STF> {
/// A channel through which messages can be sent to the image cache thread.
image_cache_thread: ImageCacheThread,

/// A channel through which messages can be sent to the debugger.
debugger_chan: Option<debugger::Sender>,

/// A channel through which messages can be sent to the developer tools.
devtools_chan: Option<Sender<DevtoolsControlMsg>>,

@@ -190,6 +194,8 @@ pub struct Constellation<Message, LTF, STF> {
pub struct InitialConstellationState {
/// A channel through which messages can be sent to the compositor.
pub compositor_proxy: Box<CompositorProxy + Send>,
/// A channel to the debugger, if applicable.
pub debugger_chan: Option<debugger::Sender>,
/// A channel to the developer tools, if applicable.
pub devtools_chan: Option<Sender<DevtoolsControlMsg>>,
/// A channel to the bluetooth thread.
@@ -482,6 +488,7 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
compositor_receiver: compositor_receiver,
layout_receiver: layout_receiver,
compositor_proxy: state.compositor_proxy,
debugger_chan: state.debugger_chan,
devtools_chan: state.devtools_chan,
bluetooth_thread: state.bluetooth_thread,
public_resource_threads: state.public_resource_threads,
@@ -1070,6 +1077,10 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
warn!("Exit resource thread failed ({})", e);
}

if let Some(ref chan) = self.debugger_chan {
debugger::shutdown_server(chan);
}

if let Some(ref chan) = self.devtools_chan {
debug!("Exiting devtools.");
let msg = DevtoolsControlMsg::FromChrome(ChromeToDevtoolsControlMsg::ServerExitMsg);
@@ -15,6 +15,7 @@ extern crate bluetooth_traits;
extern crate canvas;
extern crate canvas_traits;
extern crate compositing;
extern crate debugger;
extern crate devtools_traits;
extern crate euclid;
#[cfg(not(target_os = "windows"))]
@@ -11,5 +11,6 @@ path = "lib.rs"
crate_type = ["rlib"]

[dependencies]
util = { path = "../util" }
websocket = "0.17.1"
log = "0.3.5"
util = {path = "../util"}
ws = "0.5.3"
@@ -2,49 +2,68 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#[macro_use]
extern crate log;
extern crate util;
extern crate websocket;
extern crate ws;

use std::sync::mpsc;
use std::sync::mpsc::channel;
use util::thread::spawn_named;
use websocket::{Message, Receiver, Sender, Server};
use websocket::message::Type;
use ws::{Builder, CloseCode, Handler, Handshake};

pub fn start_server(port: u16) {
println!("Starting debugger server.");
spawn_named("debugger-server".to_owned(), move || {
run_server(port)
});
enum Message {
ShutdownServer,
}

pub struct Sender(mpsc::Sender<Message>);

struct Connection {
sender: ws::Sender
}

impl Handler for Connection {
fn on_open(&mut self, _: Handshake) -> ws::Result<()> {
debug!("Connection opened.");
Ok(())
}

fn on_close(&mut self, _: CloseCode, _: &str) {
debug!("Connection closed.");
}

fn on_message(&mut self, message: ws::Message) -> ws::Result<()> {
self.sender.send(message)
}
}

fn run_server(port: u16) {
let server = Server::bind(("127.0.0.1", port)).unwrap();
for connection in server {
spawn_named("debugger-connection".to_owned(), move || {
let connection = connection.unwrap();
let request = connection.read_request().unwrap();
let response = request.accept();
let client = response.send().unwrap();
let (mut sender, mut receiver) = client.split();
for message in receiver.incoming_messages() {
let message: Message = message.unwrap();
match message.opcode {
Type::Close => {
let message = Message::close();
sender.send_message(&message).unwrap();
break;
}
Type::Ping => {
let message = Message::pong(message.payload);
sender.send_message(&message).unwrap();
}
Type::Text => {
sender.send_message(&message).unwrap();
}
_ => {
panic!("Unexpected message type.");
}
pub fn start_server(port: u16) -> Sender {
debug!("Starting server.");
let (sender, receiver) = channel();
spawn_named("debugger".to_owned(), move || {
let socket = Builder::new().build(|sender: ws::Sender| {
Connection { sender: sender }
}).unwrap();
let sender = socket.broadcaster();
spawn_named("debugger-websocket".to_owned(), move || {
socket.listen(("127.0.0.1", port)).unwrap();
});
while let Ok(message) = receiver.recv() {
match message {
Message::ShutdownServer => {
break;
}
}
});
}
sender.shutdown().unwrap();
});
Sender(sender)
}

pub fn shutdown_server(sender: &Sender) {
debug!("Shutting down server.");
let &Sender(ref sender) = sender;
if let Err(_) = sender.send(Message::ShutdownServer) {
warn!("Failed to shut down server.");
}
}

Some generated files are not rendered by default. Learn more.

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.