2021-09-18 14:32:00 +00:00
|
|
|
use bytes::Bytes;
|
2021-09-23 09:50:28 +00:00
|
|
|
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
|
2021-09-30 09:24:02 +00:00
|
|
|
use std::convert::TryInto;
|
2021-09-25 13:47:02 +00:00
|
|
|
use tokio_tungstenite::tungstenite::Message as TokioMessage;
|
2021-09-18 14:32:00 +00:00
|
|
|
|
2021-09-23 11:59:58 +00:00
|
|
|
// Opti: using four bytes of the data to represent the source
|
2021-09-18 14:32:00 +00:00
|
|
|
#[derive(ProtoBuf, Debug, Clone, Default)]
|
|
|
|
pub struct WsMessage {
|
|
|
|
#[pb(index = 1)]
|
2021-09-23 11:59:58 +00:00
|
|
|
pub module: WsModule,
|
2021-09-18 14:32:00 +00:00
|
|
|
|
|
|
|
#[pb(index = 2)]
|
|
|
|
pub data: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
2021-09-23 09:50:28 +00:00
|
|
|
#[derive(ProtoBuf_Enum, Debug, Clone, Eq, PartialEq, Hash)]
|
2021-09-23 11:59:58 +00:00
|
|
|
pub enum WsModule {
|
2021-09-23 09:50:28 +00:00
|
|
|
Doc = 0,
|
|
|
|
}
|
|
|
|
|
2021-09-23 11:59:58 +00:00
|
|
|
impl std::default::Default for WsModule {
|
|
|
|
fn default() -> Self { WsModule::Doc }
|
2021-09-23 09:50:28 +00:00
|
|
|
}
|
|
|
|
|
2021-09-23 11:59:58 +00:00
|
|
|
impl ToString for WsModule {
|
2021-09-23 09:50:28 +00:00
|
|
|
fn to_string(&self) -> String {
|
|
|
|
match self {
|
2021-09-23 11:59:58 +00:00
|
|
|
WsModule::Doc => "0".to_string(),
|
2021-09-23 09:50:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-27 11:19:41 +00:00
|
|
|
impl std::convert::From<WsMessage> for TokioMessage {
|
|
|
|
fn from(msg: WsMessage) -> Self {
|
|
|
|
let result: Result<Bytes, ::protobuf::ProtobufError> = msg.try_into();
|
2021-09-18 14:32:00 +00:00
|
|
|
match result {
|
2021-09-23 09:50:28 +00:00
|
|
|
Ok(bytes) => TokioMessage::Binary(bytes.to_vec()),
|
2021-09-18 14:32:00 +00:00
|
|
|
Err(e) => {
|
|
|
|
log::error!("WsMessage serialize error: {:?}", e);
|
2021-09-23 09:50:28 +00:00
|
|
|
TokioMessage::Binary(vec![])
|
2021-09-18 14:32:00 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|