pax_compiler/design_server/websocket/
socket_message_accumulator.rs

1use actix_http::ws::Item;
2use actix_web_actors::ws;
3
4pub struct SocketMessageAccumulator {
5    partial_binary: Vec<u8>,
6}
7
8impl SocketMessageAccumulator {
9    pub fn new() -> Self {
10        Self {
11            partial_binary: Vec::new(),
12        }
13    }
14    pub fn process(&mut self, message: ws::Message) -> Result<Option<Vec<u8>>, String> {
15        match message {
16            ws::Message::Continuation(item) => {
17                match item {
18                    Item::FirstText(_) => return Err("no text support".to_string()),
19                    Item::FirstBinary(first) => self.partial_binary.extend_from_slice(&first),
20                    Item::Continue(b) => self.partial_binary.extend_from_slice(&b),
21                    Item::Last(b) => {
22                        self.partial_binary.extend_from_slice(&b);
23                        let data = std::mem::take(&mut self.partial_binary);
24                        return Ok(Some(data));
25                    }
26                };
27            }
28            ws::Message::Text(_) => return Err("no text support".to_string()),
29            ws::Message::Binary(binary) => return Ok(Some(binary.to_vec())),
30            // TODO
31            ws::Message::Ping(_) => (),
32            ws::Message::Pong(_) => (),
33            ws::Message::Close(_) => (),
34            ws::Message::Nop => (),
35        };
36        Ok(None)
37    }
38}