diff options
| author | hozan23 <hozan23@proton.me> | 2023-11-20 23:15:10 +0300 | 
|---|---|---|
| committer | hozan23 <hozan23@proton.me> | 2023-11-20 23:15:10 +0300 | 
| commit | 598f9e2d47da80f2bec2ead9c2fe215eff157936 (patch) | |
| tree | 121f1a391a01d3b7856d948aaaa3c154317f0cd6 /jsonrpc/src/utils.rs | |
| parent | 2ee34b432e7652a34ee64a706b5ebc1bce867dce (diff) | |
jsonrpc: add Codec struct for reading from and writing to the connection
Diffstat (limited to 'jsonrpc/src/utils.rs')
| -rw-r--r-- | jsonrpc/src/utils.rs | 63 | 
1 files changed, 0 insertions, 63 deletions
| diff --git a/jsonrpc/src/utils.rs b/jsonrpc/src/utils.rs deleted file mode 100644 index 1f21b7a..0000000 --- a/jsonrpc/src/utils.rs +++ /dev/null @@ -1,63 +0,0 @@ -use memchr::memchr; - -use karyons_net::Conn; - -use crate::{Error, Result}; - -const DEFAULT_MSG_SIZE: usize = 1024; -const MAX_ALLOWED_MSG_SIZE: usize = 1024 * 1024; // 1MB - -// TODO: Add unit tests for these functions. - -/// Read all bytes into `buffer` until the `0x0` byte or EOF is -/// reached. -/// -/// If successful, this function will return the total number of bytes read. -pub async fn read_until(conn: &Conn, buffer: &mut Vec<u8>) -> Result<usize> { -    let delim = b'\0'; - -    let mut read = 0; - -    loop { -        let mut tmp_buf = [0; DEFAULT_MSG_SIZE]; -        let n = conn.read(&mut tmp_buf).await?; -        if n == 0 { -            return Err(Error::IO(std::io::ErrorKind::UnexpectedEof.into())); -        } - -        match memchr(delim, &tmp_buf) { -            Some(i) => { -                buffer.extend_from_slice(&tmp_buf[..i]); -                read += i; -                break; -            } -            None => { -                buffer.extend_from_slice(&tmp_buf); -                read += tmp_buf.len(); -            } -        } - -        if buffer.len() == MAX_ALLOWED_MSG_SIZE { -            return Err(Error::InvalidMsg( -                "Message exceeds the maximum allowed size", -            )); -        } -    } - -    Ok(read) -} - -/// Writes an entire buffer into the given connection. -pub async fn write_all(conn: &Conn, mut buf: &[u8]) -> Result<()> { -    while !buf.is_empty() { -        let n = conn.write(buf).await?; -        let (_, rest) = std::mem::take(&mut buf).split_at(n); -        buf = rest; - -        if n == 0 { -            return Err(Error::IO(std::io::ErrorKind::UnexpectedEof.into())); -        } -    } - -    Ok(()) -} | 
