Skip to content
This repository has been archived by the owner on Sep 7, 2024. It is now read-only.

Commit

Permalink
Trying reading TcpStream with BufRead
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Jan 27, 2019
1 parent 6abbd15 commit e382886
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/network/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
//!

use std::iter;
use std::io::Cursor;
use std::io::Read;
use std::io::{Cursor, Read, BufReader};
use std::net::TcpStream;
use std::sync::mpsc::Sender;

Expand Down Expand Up @@ -150,21 +149,17 @@ impl RawNetworkMessage {

/// Reads stream from a TCP socket and parses first message from it, returing
/// the rest of the unparsed buffer for later usage.
pub fn from_tcpstream(stream: &mut TcpStream, buffer: &mut [u8]) -> Result<Self, encode::Error> {
let mut spliced = buffer.to_vec();
let mut buffer2: [u8; 1024] = [0; 1024];
pub fn from_tcpstream(stream: &mut TcpStream, reader: &mut BufReader<TcpStream>) -> Result<Self, encode::Error> {
loop {
let count = stream.read(&mut buffer2)?;
let buffer = reader.fill_buf()?;
let count = buffer.len();
if count == 0 {
continue;
}
let mut new_data = buffer2.to_vec();
spliced.append(&mut new_data);

let mut consumed: u64 = 0;
let result = encode::deserialize_partial::<RawNetworkMessage>(&spliced.to_vec(), &mut consumed);
let index = consumed as usize;
buffer.copy_from_slice(&spliced[index..]);
let result = encode::deserialize_partial::<RawNetworkMessage>(buffer, &mut consumed);
reader.consume(consumed as usize);

match result {
Err(encode::Error::InvalidChecksum { .. }) => continue,
Expand Down

0 comments on commit e382886

Please sign in to comment.