This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed a bug where read_one_message would break the flow of reading me…
…ssage past it
- Loading branch information
Showing
5 changed files
with
108 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,33 @@ | ||
use std::io::{BufRead, BufReader}; | ||
use std::{io::Read, net::TcpStream}; | ||
|
||
use crate::Message; | ||
|
||
pub struct Reader; | ||
|
||
impl Reader { | ||
pub fn read_message<R: std::io::Read, T: serde::de::DeserializeOwned>( | ||
inner: R, | ||
) -> Result<T, String> { | ||
let mut reader = BufReader::new(inner); | ||
let mut buf = String::with_capacity(1024); | ||
let message = reader | ||
.read_line(&mut buf) | ||
.map_err(|e| format!("Reader error: {}", e))?; | ||
|
||
if message == 0 { | ||
return Err("Could not read message from given value".to_string()); | ||
pub fn read_one_message(stream: &mut TcpStream) -> Result<Message, String> { | ||
let message = Self::read_until_char(stream, '\n')?; | ||
|
||
serde_json::from_str::<Message>(&message) | ||
.map_err(|e| format!("Error while deserialziing: {}", e)) | ||
} | ||
|
||
fn read_until_char(stream: &mut TcpStream, target_char: char) -> Result<String, String> { | ||
let mut buffer = [0u8; 1]; // Read one byte at a time | ||
let mut result = String::new(); | ||
|
||
loop { | ||
stream.read_exact(&mut buffer).map_err(|e| e.to_string())?; // Read one byte into the buffer | ||
|
||
let byte_read = buffer[0]; | ||
let character = byte_read as char; | ||
result.push(character); | ||
|
||
if character == target_char { | ||
break; | ||
} | ||
} | ||
|
||
serde_json::from_str::<T>(&buf).map_err(|e| format!("Error while deserialziing: {}", e)) | ||
Ok(result) | ||
} | ||
} |