Skip to content

Commit

Permalink
fix message serializing
Browse files Browse the repository at this point in the history
  • Loading branch information
lsampras committed May 29, 2021
1 parent 53a04d6 commit 20950ca
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
9 changes: 8 additions & 1 deletion src/network/message_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,21 @@ impl Message {
pub fn to_bytes(&self) -> Vec<u8> {
match self {
Message::Request(piece_index, offset, length) => {
let mut data: Vec<u8> = vec![6];
let mut data: Vec<u8> = u32_to_big_endian(&13);
data.push(6);
data.extend(u32_to_big_endian(piece_index));
data.extend(u32_to_big_endian(offset));
data.extend(u32_to_big_endian(length));
data
},
Message::Interested => {
let mut data: Vec<u8> = u32_to_big_endian(&1);
data.push(2);
data
}
_ => panic!("Serialize not implemented for")
}
}

}

15 changes: 3 additions & 12 deletions src/torrent_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,13 @@ impl PeerState {
println!("{:?} is not interested", self.peer);
},
Message::Have(index) => {
println!("Received Have for {}", index);
self.have[index as usize] = true;
},
Message::Bitfield(have) => {
self.have = have;
// TODO: request next piece
if !self.choked {

match torrent_state.get_block_request() {
Some((index, offset, len)) => {

println!("Requesting {} for piece {}, offset {}, len{}", self.peer, index, offset, len);
self.connection.send_data(Message::Request(index, offset as u32, len as u32));
},
None => {stop_flag = true;}
}
}
println!("Received Bit Field");
self.connection.send_data(Message::Interested);
},
Message::Request(piece_index, offset, length) => {
// TODO: implement this
Expand Down

0 comments on commit 20950ca

Please sign in to comment.