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

Fix WS protocol for trycp client #214

Merged
merged 4 commits into from
Jun 14, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Removed
### Changed
### Fixed
- The `trycp_client` now handles ping/pong messages from the TryCP server to keep the connection alive.
- The `trycp_client` now handles the `close` event from the TryCP server to close the connection.

## 2024-05-03: v0.17.0-dev.0
### Changed
Expand Down
24 changes: 21 additions & 3 deletions crates/trycp_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,30 @@ impl TrycpClient {

let (recv_send, recv_recv) = tokio::sync::mpsc::channel(32);

let ws = Arc::new(tokio::sync::Mutex::new(sink));
let ws2 = ws.clone();
let pend2 = pend.clone();
let recv_task = tokio::task::spawn(async move {
while let Some(Ok(msg)) = stream.next().await {
let msg = msg.into_data();
let msg = match msg {
Message::Close(close_msg) => {
eprintln!("Received websocket close from TryCP server: {}", close_msg.map(|f| f.reason).unwrap_or("No reason".into()));
break;
}
Message::Ping(p) => {
ws2.lock().await.send(Message::Pong(p)).await.unwrap();
continue;
}
Message::Pong(_) => {
continue;
}
Message::Binary(msg) => {
msg
}
_ => {
panic!("Unexpected message from TryCP server: {:?}", msg);
}
};
let msg: MessageToClient = rmp_serde::from_slice(&msg).unwrap();

match msg {
Expand All @@ -91,8 +111,6 @@ impl TrycpClient {
}
});

let ws = Arc::new(tokio::sync::Mutex::new(sink));

Ok((
Self {
ws,
Expand Down
2 changes: 1 addition & 1 deletion crates/trycp_server/src/app_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub(crate) async fn connect(
port: u16,
response_writer: Arc<futures::lock::Mutex<WsResponseWriter>>,
) -> Result<(), ConnectError> {
let connection_lock = Arc::clone(&CONNECTIONS.lock().await.entry(port).or_default());
let connection_lock = Arc::clone(CONNECTIONS.lock().await.entry(port).or_default());

let mut connection = connection_lock.lock().await;
if connection.is_some() {
Expand Down
2 changes: 1 addition & 1 deletion crates/trycp_server/src/download_dna.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub(crate) async fn download_dna(url_str: String) -> Result<String, DownloadDnaE
fn get_downloaded_dna_path(url: &url::Url) -> PathBuf {
Path::new(DNA_DIR_PATH)
.join(url.scheme())
.join(url.path().replace("/", "").replace("%", "_"))
.join(url.path().replace('/', "").replace('%', "_"))
}

/// Tries to create a file, returning Ok(None) if a file already exists at path
Expand Down
Loading