aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhozan23 <hozan23@proton.me>2023-11-21 15:38:45 +0300
committerhozan23 <hozan23@proton.me>2023-11-21 15:42:28 +0300
commit542897ce2ed7fb8a8627ec8ba8b3490acb29149f (patch)
treecf4cd602688ebbf6407248fad92eafe048005223
parentd1ab7d52abc833bb5c65d1e9545e250d589054ee (diff)
jsonrpc: switch to LF delimiter for reading json, replacing the dummy delimiter
-rw-r--r--jsonrpc/examples/client.py8
-rw-r--r--jsonrpc/src/client.rs3
-rw-r--r--jsonrpc/src/codec.rs8
-rw-r--r--jsonrpc/src/server.rs3
4 files changed, 12 insertions, 10 deletions
diff --git a/jsonrpc/examples/client.py b/jsonrpc/examples/client.py
index 14b3cf9..2066e82 100644
--- a/jsonrpc/examples/client.py
+++ b/jsonrpc/examples/client.py
@@ -15,7 +15,7 @@ req = {
"params": {"x": 4, "y": 3},
}
print("Send: ", req)
-s.sendall(json.dumps(req).encode())
+s.sendall((json.dumps(req) + '\n').encode())
res = s.recv(1024)
res = json.loads(res)
print("Received: ", res)
@@ -27,7 +27,7 @@ req = {
"params": {"x": 4, "y": 3},
}
print("Send: ", req)
-s.sendall(json.dumps(req).encode())
+s.sendall((json.dumps(req) + '\n').encode())
res = s.recv(1024)
res = json.loads(res)
print("Received: ", res)
@@ -39,7 +39,7 @@ req = {
"params": None,
}
print("Send: ", req)
-s.sendall(json.dumps(req).encode())
+s.sendall((json.dumps(req) + '\n').encode())
res = s.recv(1024)
res = json.loads(res)
print("Received: ", res)
@@ -51,7 +51,7 @@ req = {
"params": None,
}
print("Send: ", req)
-s.sendall(json.dumps(req).encode())
+s.sendall((json.dumps(req) + '\n').encode())
res = s.recv(1024)
res = json.loads(res)
print("Received: ", res)
diff --git a/jsonrpc/src/client.rs b/jsonrpc/src/client.rs
index 1ce9ecb..f5277aa 100644
--- a/jsonrpc/src/client.rs
+++ b/jsonrpc/src/client.rs
@@ -58,7 +58,8 @@ impl Client {
params: serde_json::json!(params),
};
- let payload = serde_json::to_vec(&request)?;
+ let mut payload = serde_json::to_vec(&request)?;
+ payload.push(b'\n');
self.codec.write_all(&payload).await?;
debug!("--> {request}");
diff --git a/jsonrpc/src/codec.rs b/jsonrpc/src/codec.rs
index 41fed06..e198a6e 100644
--- a/jsonrpc/src/codec.rs
+++ b/jsonrpc/src/codec.rs
@@ -39,12 +39,12 @@ impl Codec {
Self { conn, config }
}
- /// Read all bytes into `buffer` until the `0x0` byte or EOF is
+ /// Read all bytes into `buffer` until the `0x0A` byte or EOF is
/// reached.
///
/// If successful, this function will return the total number of bytes read.
pub async fn read_until(&self, buffer: &mut Vec<u8>) -> Result<usize> {
- let delim = b'\0';
+ let delim = b'\n';
let mut read = 0;
@@ -57,8 +57,8 @@ impl Codec {
match memchr(delim, &tmp_buf) {
Some(i) => {
- buffer.extend_from_slice(&tmp_buf[..i]);
- read += i;
+ buffer.extend_from_slice(&tmp_buf[..=i]);
+ read += i + 1;
break;
}
None => {
diff --git a/jsonrpc/src/server.rs b/jsonrpc/src/server.rs
index 133f261..f6006cc 100644
--- a/jsonrpc/src/server.rs
+++ b/jsonrpc/src/server.rs
@@ -99,7 +99,8 @@ impl<'a> Server<'a> {
let mut buffer = vec![];
codec.read_until(&mut buffer).await?;
let response = selfc.handle_request(&buffer).await;
- let payload = serde_json::to_vec(&response)?;
+ let mut payload = serde_json::to_vec(&response)?;
+ payload.push(b'\n');
codec.write_all(&payload).await?;
debug!("--> {response}");
}