Skip to content

Commit

Permalink
Merge pull request #118 from yrashk/tcp-frame-size
Browse files Browse the repository at this point in the history
Problem: server's frame size is too large
  • Loading branch information
yrashk committed Feb 24, 2017
2 parents 3dd6fc0 + 7268e9a commit 13fccaf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
10 changes: 5 additions & 5 deletions src/bin/pumpkindb-term.rs
Expand Up @@ -53,22 +53,22 @@ fn main() {
text, uuid.hyphenated().to_string()).as_str()) {
Ok(compiled) => {
let msg = compiled;
let mut buf = [0u8; 8];
let mut buf = [0u8; 4];

BigEndian::write_u64(&mut buf, msg.len() as u64);
BigEndian::write_u32(&mut buf, msg.len() as u32);
stream.write_all(buf.as_ref()).unwrap();
stream.write_all(msg.as_ref()).unwrap();

let mut buf = [0u8; 8];
let mut buf = [0u8; 4];
stream.read(&mut buf).unwrap();

let msg_len = BigEndian::read_u64(&mut buf);
let msg_len = BigEndian::read_u32(&mut buf);

let s_ref = <TcpStream as Read>::by_ref(&mut stream);

r.clear();

match s_ref.take(msg_len).read_to_end(&mut r) {
match s_ref.take(msg_len as u64).read_to_end(&mut r) {
Ok(0) => {
},
Ok(_) => {
Expand Down
16 changes: 8 additions & 8 deletions src/server/connection.rs
Expand Up @@ -37,7 +37,7 @@ pub struct Connection {

// track whether a read received `WouldBlock` and store the number of
// byte we are supposed to read
read_continuation: Option<u64>,
read_continuation: Option<u32>,

// track whether a write received `WouldBlock`
write_continuation: bool,
Expand Down Expand Up @@ -106,7 +106,7 @@ impl Connection {
}
Err(e) => {
if e.kind() == ErrorKind::WouldBlock {
self.read_continuation = Some(msg_len as u64);
self.read_continuation = Some(msg_len as u32);
return Ok(None);
} else {
return Err(e);
Expand All @@ -117,12 +117,12 @@ impl Connection {
Ok(Some(recv_buf.to_vec()))
}

fn read_message_length(&mut self) -> io::Result<Option<u64>> {
fn read_message_length(&mut self) -> io::Result<Option<u32>> {
if let Some(n) = self.read_continuation {
return Ok(Some(n));
}

let mut buf = [0u8; 8];
let mut buf = [0u8; 4];

let bytes = match self.sock.read(&mut buf) {
Ok(n) => n,
Expand All @@ -135,11 +135,11 @@ impl Connection {
}
};

if bytes < 8 {
if bytes < 4 {
return Err(Error::new(ErrorKind::InvalidData, "Invalid message length"));
}

let msg_len = BigEndian::read_u64(buf.as_ref());
let msg_len = BigEndian::read_u32(buf.as_ref());

Ok(Some(msg_len))
}
Expand Down Expand Up @@ -192,8 +192,8 @@ impl Connection {
}

let len = buf.len();
let mut send_buf = [0u8; 8];
BigEndian::write_u64(&mut send_buf, len as u64);
let mut send_buf = [0u8; 4];
BigEndian::write_u32(&mut send_buf, len as u32);

match self.sock.write(&send_buf) {
Ok(_) => {
Expand Down

0 comments on commit 13fccaf

Please sign in to comment.