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

Update to ewebsock 0.4.0 #3729

Merged
merged 3 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 4 additions & 45 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ emath = "0.23.0"
enumset = "1.0.12"
env_logger = "0.10"
epaint = "0.23.0"
ewebsock = "0.2"
ewebsock = "0.4.0"
futures-channel = "0.3"
futures-util = { version = "0.3", default-features = false }
getrandom = "0.2"
Expand Down
3 changes: 1 addition & 2 deletions crates/re_data_source/src/web_sockets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ pub fn connect_to_ws_url(
}
};

let connection = re_ws_comms::Connection::viewer_to_server(url.to_owned(), callback)?;
std::mem::drop(connection); // Never close the connection. TODO(emilk): is this wise?
re_ws_comms::viewer_to_server(url.to_owned(), callback)?;
Ok(rx)
}
2 changes: 1 addition & 1 deletion crates/re_sdk_comms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ crossbeam.workspace = true
document-features.workspace = true
rand = { workspace = true, features = ["std", "std_rng", "small_rng"] }
thiserror.workspace = true
tokio.workspace = true
tokio = { workspace = true, features = ["io-util", "net"] }
2 changes: 1 addition & 1 deletion crates/re_sdk_comms/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{io::ErrorKind, time::Instant};

use rand::{Rng as _, SeedableRng};
use tokio::net::{TcpListener, TcpStream};

use re_log_types::{LogMsg, TimePoint, TimeType, TimelineName};
use re_smart_channel::{Receiver, Sender};
use tokio::net::{TcpListener, TcpStream};

#[derive(thiserror::Error, Debug)]
pub enum ServerError {
Expand Down
89 changes: 40 additions & 49 deletions crates/re_ws_comms/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,51 @@
use std::ops::ControlFlow;

use ewebsock::{WsEvent, WsMessage, WsSender};
use ewebsock::{WsEvent, WsMessage};

// TODO(jleibs): use thiserror
pub type Result<T> = anyhow::Result<T>;

/// Represents a connection to the server.
/// Disconnects on drop.
#[must_use]
pub struct Connection(WsSender);

impl Connection {
/// Connect viewer to server
pub fn viewer_to_server(
url: String,
on_binary_msg: impl Fn(Vec<u8>) -> ControlFlow<()> + Send + 'static,
) -> Result<Self> {
re_log::info!("Connecting to {url:?}…");
let sender = ewebsock::ws_connect(
url,
Box::new(move |event: WsEvent| match event {
WsEvent::Opened => {
re_log::info!("Connection established");
/// Connect viewer to server
pub fn viewer_to_server(
url: String,
on_binary_msg: impl Fn(Vec<u8>) -> ControlFlow<()> + Send + 'static,
) -> Result<()> {
re_log::info!("Connecting to {url:?}…");
ewebsock::ws_receive(
url,
Box::new(move |event: WsEvent| match event {
WsEvent::Opened => {
re_log::info!("Connection established");
ControlFlow::Continue(())
}
WsEvent::Message(message) => match message {
WsMessage::Binary(binary) => on_binary_msg(binary),
WsMessage::Text(text) => {
re_log::warn!("Unexpected text message: {text:?}");
ControlFlow::Continue(())
}
WsMessage::Unknown(text) => {
re_log::warn!("Unknown message: {text:?}");
ControlFlow::Continue(())
}
WsEvent::Message(message) => match message {
WsMessage::Binary(binary) => on_binary_msg(binary),
WsMessage::Text(text) => {
re_log::warn!("Unexpected text message: {text:?}");
ControlFlow::Continue(())
}
WsMessage::Unknown(text) => {
re_log::warn!("Unknown message: {text:?}");
ControlFlow::Continue(())
}
WsMessage::Ping(_data) => {
re_log::warn!("Unexpected PING");
ControlFlow::Continue(())
}
WsMessage::Pong(_data) => {
re_log::warn!("Unexpected PONG");
ControlFlow::Continue(())
}
},
WsEvent::Error(error) => {
re_log::error!("Connection error: {error}");
ControlFlow::Break(())
WsMessage::Ping(_data) => {
re_log::warn!("Unexpected PING");
ControlFlow::Continue(())
}
WsEvent::Closed => {
re_log::info!("Connection to server closed.");
ControlFlow::Break(())
WsMessage::Pong(_data) => {
re_log::warn!("Unexpected PONG");
ControlFlow::Continue(())
}
}),
)
.map_err(|err| anyhow::format_err!("ewebsock: {err}"))?;

Ok(Self(sender))
}
},
WsEvent::Error(error) => {
re_log::error!("Connection error: {error}");
ControlFlow::Break(())
}
WsEvent::Closed => {
re_log::info!("Connection to server closed.");
ControlFlow::Break(())
}
}),
)
.map_err(|err| anyhow::format_err!("ewebsock: {err}"))
}
2 changes: 1 addition & 1 deletion crates/re_ws_comms/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod client;
use std::{fmt::Display, str::FromStr};

#[cfg(feature = "client")]
pub use client::Connection;
pub use client::viewer_to_server;

#[cfg(feature = "server")]
mod server;
Expand Down
1 change: 1 addition & 0 deletions crates/re_ws_comms/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ impl RerunServer {
let (tcp_stream, _) = tokio::select! {
res = self.listener.accept() => res?,
_ = shutdown_rx.recv() => {
re_log::debug!("Shutting down WebSocket server");
return Ok(());
}
};
Expand Down
Loading