Skip to content

Commit

Permalink
Rechannel: error when trying to send empty messages
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaspoffo committed Sep 3, 2022
1 parent 4ff9153 commit 210c752
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion rechannel/src/channel/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ impl SendBlockChannel {

info!(
"Generated SliceMessage {} from chunk_id {}. ({}/{})",
message.slice_id, self.chunk_id, message.slice_id, num_slices
message.slice_id, self.chunk_id, message.slice_id + 1, *num_slices
);

slice_messages.push(message);
Expand Down Expand Up @@ -294,6 +294,12 @@ impl SendChannel for SendBlockChannel {
return;
}

if payload.is_empty() {
log::error!("Tried to send empty block message");
self.error = Some(ChannelError::SentEmptyMessage);
return;
}

if payload.len() as u64 > self.max_message_size {
log::error!(
"Tried to send block message with size above the limit, got {} bytes, expected less than {}",
Expand Down
6 changes: 6 additions & 0 deletions rechannel/src/channel/reliable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ impl SendChannel for SendReliableChannel {
return;
}

if payload.is_empty() {
log::error!("Tried to send empty reliable message");
self.error = Some(ChannelError::SentEmptyMessage);
return;
}

self.send_message_id = self.send_message_id.wrapping_add(1);

let reliable_message = ReliableMessage::new(message_id, payload);
Expand Down
6 changes: 6 additions & 0 deletions rechannel/src/channel/unreliable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ impl SendChannel for SendUnreliableChannel {
return;
}

if payload.is_empty() {
log::error!("Tried to send empty unreliable message");
self.error = Some(ChannelError::SentEmptyMessage);
return;
}

if self.messages_to_send.len() >= self.message_send_queue_size {
self.error = Some(ChannelError::SendQueueFull);
log::warn!("Unreliable channel {} has reached the maximum queue size", self.channel_id);
Expand Down
3 changes: 3 additions & 0 deletions rechannel/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub enum ChannelError {
FailedToSerialize,
/// Tried to send a message that is above the channel max message size.
SentMessageAboveMaxSize,
/// Tried to send an empty message
SentEmptyMessage,
/// Received a message above above the channel max message size.
ReceivedMessageAboveMaxSize,
/// Received an invalid slice message in a block channel.
Expand All @@ -46,6 +48,7 @@ impl fmt::Display for ChannelError {
SendQueueFull => write!(fmt, "send queue was full"),
FailedToSerialize => write!(fmt, "failed to serialize or deserialize"),
SentMessageAboveMaxSize => write!(fmt, "sent message above the channel max message size"),
SentEmptyMessage => write!(fmt, "sent empty message"),
ReceivedMessageAboveMaxSize => write!(fmt, "received message above the channel max message size"),
InvalidSliceMessage => write!(fmt, "received an invalid slice message in a block channel"),
}
Expand Down

0 comments on commit 210c752

Please sign in to comment.