From 542897ce2ed7fb8a8627ec8ba8b3490acb29149f Mon Sep 17 00:00:00 2001 From: hozan23 Date: Tue, 21 Nov 2023 15:38:45 +0300 Subject: jsonrpc: switch to LF delimiter for reading json, replacing the dummy delimiter --- jsonrpc/examples/client.py | 8 ++++---- jsonrpc/src/client.rs | 3 ++- jsonrpc/src/codec.rs | 8 ++++---- jsonrpc/src/server.rs | 3 ++- 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) -> Result { - 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}"); } -- cgit v1.2.3