Skip to content

Commit

Permalink
Auto merge of #23461 - TheOriginalAlex:issue-23408, r=jdm
Browse files Browse the repository at this point in the history
Switched from using thread for websocket requests to ipc_channel::router::ROUTER

<!-- Please describe your changes on the following line: -->
Switched from using thread for websocket requests to ipc_channel::router::ROUTER

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #23408  (GitHub issue number if applicable)

<!-- Either: -->
- [ ] There are tests for these changes OR
- [x] These changes do not require tests because the existing websocket tests should cover these changes

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/23461)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed May 28, 2019
2 parents 2ac916b + 1c35c44 commit 96ac540
Showing 1 changed file with 36 additions and 37 deletions.
73 changes: 36 additions & 37 deletions components/script/dom/websocket.rs
Expand Up @@ -27,6 +27,7 @@ use crate::task_source::websocket::WebsocketTaskSource;
use crate::task_source::TaskSource;
use dom_struct::dom_struct;
use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
use ipc_channel::router::ROUTER;
use js::jsapi::{JSAutoRealm, JSObject};
use js::jsval::UndefinedValue;
use js::rust::CustomAutoRooterGuard;
Expand All @@ -40,7 +41,6 @@ use servo_url::{ImmutableOrigin, ServoUrl};
use std::borrow::ToOwned;
use std::cell::Cell;
use std::ptr;
use std::thread;

#[derive(Clone, Copy, Debug, JSTraceable, MallocSizeOf, PartialEq)]
enum WebSocketRequestState {
Expand Down Expand Up @@ -216,42 +216,41 @@ impl WebSocket {

let task_source = global.websocket_task_source();
let canceller = global.task_canceller(WebsocketTaskSource::NAME);
thread::spawn(move || {
while let Ok(event) = dom_event_receiver.recv() {
match event {
WebSocketNetworkEvent::ConnectionEstablished { protocol_in_use } => {
let open_thread = ConnectionEstablishedTask {
address: address.clone(),
protocol_in_use,
};
task_source
.queue_with_canceller(open_thread, &canceller)
.unwrap();
},
WebSocketNetworkEvent::MessageReceived(message) => {
let message_thread = MessageReceivedTask {
address: address.clone(),
message: message,
};
task_source
.queue_with_canceller(message_thread, &canceller)
.unwrap();
},
WebSocketNetworkEvent::Fail => {
fail_the_websocket_connection(address.clone(), &task_source, &canceller);
},
WebSocketNetworkEvent::Close(code, reason) => {
close_the_websocket_connection(
address.clone(),
&task_source,
&canceller,
code,
reason,
);
},
}
}
});
ROUTER.add_route(
dom_event_receiver.to_opaque(),
Box::new(move |message| match message.to().unwrap() {
WebSocketNetworkEvent::ConnectionEstablished { protocol_in_use } => {
let open_thread = ConnectionEstablishedTask {
address: address.clone(),
protocol_in_use,
};
task_source
.queue_with_canceller(open_thread, &canceller)
.unwrap();
},
WebSocketNetworkEvent::MessageReceived(message) => {
let message_thread = MessageReceivedTask {
address: address.clone(),
message: message,
};
task_source
.queue_with_canceller(message_thread, &canceller)
.unwrap();
},
WebSocketNetworkEvent::Fail => {
fail_the_websocket_connection(address.clone(), &task_source, &canceller);
},
WebSocketNetworkEvent::Close(code, reason) => {
close_the_websocket_connection(
address.clone(),
&task_source,
&canceller,
code,
reason,
);
},
}),
);

// Step 7.
Ok(ws)
Expand Down

0 comments on commit 96ac540

Please sign in to comment.