Skip to content

Commit

Permalink
feat: Try async recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed May 23, 2024
1 parent a28e5cd commit a2922f6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-recursion = "1.1.1"
clap = "4.5.4"
dunce = "1.0.4"
rpassword = "7.3.1"
Expand Down
11 changes: 6 additions & 5 deletions src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
use crate::server;

pub fn handle(connection: &mut server::Connection, json: &str) {
pub async fn handle(connection: &mut server::Connection, json: &str) {
let v = serde_json::from_str(json);
let val: serde_json::Value = v.unwrap();

Expand All @@ -19,7 +19,7 @@ pub fn handle(connection: &mut server::Connection, json: &str) {

match method {
"enter" => {
enter::handle(connection, &val);
enter::handle(connection, &val).await;
}
"exit" => {
// TODO: ..
Expand All @@ -34,10 +34,11 @@ pub fn handle(connection: &mut server::Connection, json: &str) {
mod enter {
use crate::server;

pub fn handle(connection: &mut server::Connection, json: &serde_json::Value) {
pub async fn handle(connection: &mut server::Connection, json: &serde_json::Value) {
tracing::trace!("method: {:?}", json["method"]);
connection.send(serde_json::json!({
"": "",
}));
"jsonrpc": "2.0",
"method": "enter",
})).await;
}
}
11 changes: 7 additions & 4 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use serde_json::json;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;

use async_recursion::async_recursion;

const SEPARATOR_LEN: usize = "\r\n".len();
const BUF_SIZE: usize = 1024 * 1;

Expand Down Expand Up @@ -69,7 +71,7 @@ impl Connection {
self.data.append(&mut new_data.to_vec());
}

self.process();
self.process().await;

n
}
Expand All @@ -80,7 +82,8 @@ impl Connection {
};
}

pub fn process(&mut self) {
#[async_recursion]
pub async fn process(&mut self) {
let data = &self.data.clone();
let decrypted = String::from_utf8_lossy(data);

Expand Down Expand Up @@ -112,7 +115,7 @@ impl Connection {
boundary += content_len;

let data = &line[..content_len];
handler::handle(self, data);
handler::handle(self, data).await;
//println!("{}: {}", "receive all", data);

process = true;
Expand All @@ -133,7 +136,7 @@ impl Connection {
boundary,
String::from_utf8_lossy(&self.data)
);
self.process();
self.process().await;
}
}

Expand Down

0 comments on commit a2922f6

Please sign in to comment.