Dispatch read one byte of the code, so a code with a non-zero high byte was handled as its low byte. Message::get_message_code now reads the whole little-endian uint32 and handlers register by u32. PeerInit and PierceFirewall, the only frames with a one-byte code, get their own accessor for the listener's first frame.
46 lines
1.3 KiB
Rust
46 lines
1.3 KiB
Rust
use crate::{
|
|
message::{Message, MessageHandler},
|
|
peer::PeerMessage,
|
|
};
|
|
use std::sync::mpsc::Sender;
|
|
|
|
/// `PlaceInQueueRequest` (peer code 51): a peer asking where the file they
|
|
/// queued sits in our upload queue.
|
|
///
|
|
/// Worth answering rather than dropping: a downloader that gets no position
|
|
/// cannot tell a long queue from a client that has forgotten about it.
|
|
pub struct PlaceInQueueRequest;
|
|
|
|
impl MessageHandler<PeerMessage> for PlaceInQueueRequest {
|
|
fn get_code(&self) -> u32 {
|
|
51
|
|
}
|
|
|
|
fn handle(&self, message: &mut Message, sender: Sender<PeerMessage>) {
|
|
let filename = message.read_string();
|
|
let _ = sender.send(PeerMessage::IncomingPlaceInQueueRequest(filename));
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::message::framed;
|
|
|
|
#[test]
|
|
fn the_asked_about_filename_is_forwarded() {
|
|
let (tx, rx) = std::sync::mpsc::channel();
|
|
let mut message = framed(|m| {
|
|
m.write_string("@@share\\track.mp3");
|
|
});
|
|
|
|
PlaceInQueueRequest.handle(&mut message, tx);
|
|
match rx.try_recv() {
|
|
Ok(PeerMessage::IncomingPlaceInQueueRequest(filename)) => {
|
|
assert_eq!(filename, "@@share\\track.mp3");
|
|
}
|
|
other => panic!("unexpected: {other:?}"),
|
|
}
|
|
}
|
|
}
|