Made the irc-stream not block if it has messages in the buffer
and added some cleaning for the incoming messages.
This commit is contained in:
parent
5d134f716d
commit
bf53e62f17
1 changed files with 11 additions and 9 deletions
20
src/lib.rs
20
src/lib.rs
|
@ -37,22 +37,24 @@ pub struct IrcStream<S: Read + Write> {
|
||||||
|
|
||||||
impl<S: Read + Write> IrcRead for IrcStream<S> {
|
impl<S: Read + Write> IrcRead for IrcStream<S> {
|
||||||
fn read(&mut self) -> Result<Message> {
|
fn read(&mut self) -> Result<Message> {
|
||||||
let mut message: Option<Message> = None;
|
loop {
|
||||||
while message == None {
|
match self.buffer.find("\n") {
|
||||||
let mut temp_buf = [0; 512];
|
|
||||||
self.stream.read(&mut temp_buf)?;
|
|
||||||
self.buffer.push_str(std::str::from_utf8(&temp_buf)?);
|
|
||||||
match self.buffer.find("\r\n") {
|
|
||||||
Some(i) => {
|
Some(i) => {
|
||||||
let mut raw_message = self.buffer.clone();
|
let mut raw_message = self.buffer.clone();
|
||||||
|
|
||||||
// This returns the part after the line break to the buffer.
|
// This returns the part after the line break to the buffer.
|
||||||
self.buffer = raw_message.split_off(i+2);
|
self.buffer = raw_message.split_off(i+1);
|
||||||
message = Some(Message::from(raw_message.as_str()));
|
|
||||||
|
// Trim null bytes for good measure.
|
||||||
|
raw_message = String::from(raw_message.trim_matches('\u{0}'));
|
||||||
|
return Ok(Message::from(raw_message.as_str()));
|
||||||
},
|
},
|
||||||
None => (),
|
None => (),
|
||||||
}
|
}
|
||||||
|
let mut temp_buf = [0; 512];
|
||||||
|
self.stream.read(&mut temp_buf)?;
|
||||||
|
self.buffer.push_str(std::str::from_utf8(&temp_buf)?);
|
||||||
}
|
}
|
||||||
Ok(message.expect("Got no message."))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue