Skip to content

Commit

Permalink
simplify and log more of message received
Browse files Browse the repository at this point in the history
  • Loading branch information
rgbkrk committed Dec 13, 2024
1 parent 9bb27d2 commit 1a1a5d9
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/pub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,31 @@ pub(crate) struct PubSocketBackend {

impl PubSocketBackend {
fn message_received(&self, peer_id: &PeerIdentity, message: Message) {
let message = match message {
Message::Message(m) => m,
let data = match message {
Message::Message(m) => {
if m.len() != 1 {
log::warn!("Received message with unexpected length: {}", m.len());
return;
}
m.into_vec().pop().unwrap_or_default()
}
_ => return,
};
assert_eq!(message.len(), 1);
let data: Vec<u8> = message.into_vec().pop().unwrap().to_vec();

if data.is_empty() {
return;
}
match data[0] {
1 => {

match data.first() {
Some(1) => {
// Subscribe
self.subscribers
.get_mut(peer_id)
.unwrap()
.subscriptions
.push(Vec::from(&data[1..]));
}
0 => {
Some(0) => {
// Unsubscribe
let mut del_index = None;
let sub = Vec::from(&data[1..]);
Expand All @@ -77,7 +83,10 @@ impl PubSocketBackend {
.remove(index);
}
}
_ => (),
_ => log::warn!(
"Received message with unexpected first byte: {:?}",
data.first()
),
}
}
}
Expand Down

0 comments on commit 1a1a5d9

Please sign in to comment.