Skip to content

Commit c0a099e

Browse files
refactor: simplify processing incoming data frames
1 parent e7e060a commit c0a099e

2 files changed

Lines changed: 29 additions & 40 deletions

File tree

src/protocol/message.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,11 @@ enum IncompleteMessageCollector {
9393

9494
impl IncompleteMessage {
9595
/// Create new.
96-
pub fn new(message_type: IncompleteMessageType) -> Self {
96+
pub fn new(message_type: MessageType) -> Self {
9797
IncompleteMessage {
9898
collector: match message_type {
99-
IncompleteMessageType::Binary => IncompleteMessageCollector::Binary(Vec::new()),
100-
IncompleteMessageType::Text => {
101-
IncompleteMessageCollector::Text(StringCollector::new())
102-
}
99+
MessageType::Binary => IncompleteMessageCollector::Binary(Vec::new()),
100+
MessageType::Text => IncompleteMessageCollector::Text(StringCollector::new()),
103101
},
104102
}
105103
}
@@ -149,7 +147,7 @@ impl IncompleteMessage {
149147
}
150148

151149
/// The type of incomplete message.
152-
pub enum IncompleteMessageType {
150+
pub enum MessageType {
153151
Text,
154152
Binary,
155153
}

src/protocol/mod.rs

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use self::{
1111
coding::{CloseCode, Control as OpCtl, Data as OpData, OpCode},
1212
Frame, FrameCodec,
1313
},
14-
message::{IncompleteMessage, IncompleteMessageType},
14+
message::{IncompleteMessage, MessageType},
1515
};
1616
use crate::{
1717
error::{CapacityError, Error, ProtocolError, Result},
@@ -679,44 +679,35 @@ impl WebSocketContext {
679679

680680
OpCode::Data(data) => {
681681
let fin = frame.header().is_final;
682-
match data {
683-
OpData::Continue => {
684-
let msg = self
685-
.incomplete
686-
.as_mut()
687-
.ok_or(Error::Protocol(ProtocolError::UnexpectedContinueFrame))?;
688-
msg.extend(frame.into_payload(), self.config.max_message_size)?;
689-
if fin {
690-
Ok(Some(self.incomplete.take().unwrap().complete()?))
691-
} else {
692-
Ok(None)
693-
}
694-
}
695-
c if self.incomplete.is_some() => {
696-
Err(Error::Protocol(ProtocolError::ExpectedFragment(c)))
697-
}
698-
OpData::Text if fin => {
699-
check_max_size(frame.payload().len(), self.config.max_message_size)?;
700-
Ok(Some(Message::Text(frame.into_text()?)))
682+
683+
let payload = match (data, self.incomplete.as_mut()) {
684+
(OpData::Continue, None) => Err(ProtocolError::UnexpectedContinueFrame),
685+
(OpData::Continue, Some(incomplete)) => {
686+
incomplete.extend(frame.into_payload(), self.config.max_message_size)?;
687+
Ok(None)
701688
}
702-
OpData::Binary if fin => {
703-
check_max_size(frame.payload().len(), self.config.max_message_size)?;
704-
Ok(Some(Message::Binary(frame.into_payload())))
689+
(_, Some(_)) => Err(ProtocolError::ExpectedFragment(data)),
690+
(OpData::Text, _) => Ok(Some((frame.into_payload(), MessageType::Text))),
691+
(OpData::Binary, _) => Ok(Some((frame.into_payload(), MessageType::Binary))),
692+
(OpData::Reserved(i), _) => Err(ProtocolError::UnknownDataFrameType(i)),
693+
}?;
694+
695+
match (payload, fin) {
696+
(None, true) => Ok(Some(self.incomplete.take().unwrap().complete()?)),
697+
(None, false) => Ok(None),
698+
(Some((payload, t)), true) => {
699+
check_max_size(payload.len(), self.config.max_message_size)?;
700+
match t {
701+
MessageType::Text => Ok(Some(Message::Text(payload.try_into()?))),
702+
MessageType::Binary => Ok(Some(Message::Binary(payload))),
703+
}
705704
}
706-
OpData::Text | OpData::Binary => {
707-
let message_type = match data {
708-
OpData::Text => IncompleteMessageType::Text,
709-
OpData::Binary => IncompleteMessageType::Binary,
710-
_ => panic!("Bug: message is not text nor binary"),
711-
};
712-
let mut incomplete = IncompleteMessage::new(message_type);
713-
incomplete.extend(frame.into_payload(), self.config.max_message_size)?;
705+
(Some((payload, t)), false) => {
706+
let mut incomplete = IncompleteMessage::new(t);
707+
incomplete.extend(payload, self.config.max_message_size)?;
714708
self.incomplete = Some(incomplete);
715709
Ok(None)
716710
}
717-
OpData::Reserved(i) => {
718-
Err(Error::Protocol(ProtocolError::UnknownDataFrameType(i)))
719-
}
720711
}
721712
}
722713
} // match opcode

0 commit comments

Comments
 (0)