2021-08-18 19:12:27 +00:00
|
|
|
use super::{ConnectionId, PeerId, TypedEnvelope};
|
2021-08-19 01:06:46 +00:00
|
|
|
use anyhow::Result;
|
2021-07-07 16:23:18 +00:00
|
|
|
use async_tungstenite::tungstenite::{Error as WebSocketError, Message as WebSocketMessage};
|
|
|
|
use futures::{SinkExt as _, StreamExt as _};
|
2021-06-09 19:15:25 +00:00
|
|
|
use prost::Message;
|
2021-08-19 17:38:17 +00:00
|
|
|
use std::any::{Any, TypeId};
|
2021-06-22 00:07:56 +00:00
|
|
|
use std::{
|
|
|
|
io,
|
|
|
|
time::{Duration, SystemTime, UNIX_EPOCH},
|
|
|
|
};
|
2021-06-09 19:15:25 +00:00
|
|
|
|
|
|
|
include!(concat!(env!("OUT_DIR"), "/zed.messages.rs"));
|
|
|
|
|
2021-08-18 17:45:29 +00:00
|
|
|
pub trait EnvelopedMessage: Clone + Sized + Send + Sync + 'static {
|
2021-06-18 03:48:26 +00:00
|
|
|
const NAME: &'static str;
|
2021-06-18 23:26:12 +00:00
|
|
|
fn into_envelope(
|
|
|
|
self,
|
|
|
|
id: u32,
|
|
|
|
responding_to: Option<u32>,
|
|
|
|
original_sender_id: Option<u32>,
|
|
|
|
) -> Envelope;
|
2021-06-15 20:06:50 +00:00
|
|
|
fn from_envelope(envelope: Envelope) -> Option<Self>;
|
2021-06-11 21:30:12 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 01:06:46 +00:00
|
|
|
pub trait EntityMessage: EnvelopedMessage {
|
|
|
|
fn remote_entity_id(&self) -> u64;
|
|
|
|
}
|
|
|
|
|
2021-06-15 20:06:50 +00:00
|
|
|
pub trait RequestMessage: EnvelopedMessage {
|
|
|
|
type Response: EnvelopedMessage;
|
2021-06-11 21:30:12 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 17:38:17 +00:00
|
|
|
pub trait AnyTypedEnvelope: 'static + Send + Sync {
|
|
|
|
fn payload_type_id(&self) -> TypeId;
|
|
|
|
fn payload_type_name(&self) -> &'static str;
|
|
|
|
fn as_any(&self) -> &dyn Any;
|
|
|
|
fn into_any(self: Box<Self>) -> Box<dyn Any + Send + Sync>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: EnvelopedMessage> AnyTypedEnvelope for TypedEnvelope<T> {
|
|
|
|
fn payload_type_id(&self) -> TypeId {
|
|
|
|
TypeId::of::<T>()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn payload_type_name(&self) -> &'static str {
|
|
|
|
T::NAME
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_any(&self) -> &dyn Any {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn into_any(self: Box<Self>) -> Box<dyn Any + Send + Sync> {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-18 19:12:27 +00:00
|
|
|
macro_rules! messages {
|
2021-08-19 01:06:46 +00:00
|
|
|
($($name:ident),* $(,)?) => {
|
2021-08-19 17:38:17 +00:00
|
|
|
pub fn build_typed_envelope(sender_id: ConnectionId, envelope: Envelope) -> Option<Box<dyn AnyTypedEnvelope>> {
|
2021-08-19 01:06:46 +00:00
|
|
|
match envelope.payload {
|
2021-08-19 03:59:13 +00:00
|
|
|
$(Some(envelope::Payload::$name(payload)) => {
|
|
|
|
Some(Box::new(TypedEnvelope {
|
|
|
|
sender_id,
|
|
|
|
original_sender_id: envelope.original_sender_id.map(PeerId),
|
|
|
|
message_id: envelope.id,
|
|
|
|
payload,
|
|
|
|
}))
|
|
|
|
}, )*
|
2021-08-18 19:12:27 +00:00
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$(
|
2021-08-19 01:06:46 +00:00
|
|
|
impl EnvelopedMessage for $name {
|
|
|
|
const NAME: &'static str = std::stringify!($name);
|
|
|
|
|
|
|
|
fn into_envelope(
|
|
|
|
self,
|
|
|
|
id: u32,
|
|
|
|
responding_to: Option<u32>,
|
|
|
|
original_sender_id: Option<u32>,
|
|
|
|
) -> Envelope {
|
|
|
|
Envelope {
|
|
|
|
id,
|
|
|
|
responding_to,
|
|
|
|
original_sender_id,
|
|
|
|
payload: Some(envelope::Payload::$name(self)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_envelope(envelope: Envelope) -> Option<Self> {
|
|
|
|
if let Some(envelope::Payload::$name(msg)) = envelope.payload {
|
|
|
|
Some(msg)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-18 19:12:27 +00:00
|
|
|
)*
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! request_messages {
|
2021-08-19 01:06:46 +00:00
|
|
|
($(($request_name:ident, $response_name:ident)),* $(,)?) => {
|
2021-08-18 19:12:27 +00:00
|
|
|
$(impl RequestMessage for $request_name {
|
|
|
|
type Response = $response_name;
|
|
|
|
})*
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-08-19 01:06:46 +00:00
|
|
|
macro_rules! entity_messages {
|
|
|
|
($id_field:ident, $($name:ident),* $(,)?) => {
|
|
|
|
$(impl EntityMessage for $name {
|
|
|
|
fn remote_entity_id(&self) -> u64 {
|
|
|
|
self.$id_field
|
2021-06-11 21:30:12 +00:00
|
|
|
}
|
2021-08-19 01:06:46 +00:00
|
|
|
})*
|
2021-06-10 01:14:30 +00:00
|
|
|
};
|
2021-06-09 19:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 19:12:27 +00:00
|
|
|
messages!(
|
2021-09-09 18:24:16 +00:00
|
|
|
Ack,
|
2021-08-18 19:12:27 +00:00
|
|
|
AddPeer,
|
2021-08-19 01:06:46 +00:00
|
|
|
BufferSaved,
|
|
|
|
ChannelMessageSent,
|
|
|
|
CloseBuffer,
|
|
|
|
CloseWorktree,
|
2021-08-27 23:53:23 +00:00
|
|
|
Error,
|
2021-08-27 21:58:28 +00:00
|
|
|
GetChannelMessages,
|
|
|
|
GetChannelMessagesResponse,
|
2021-08-19 01:06:46 +00:00
|
|
|
GetChannels,
|
|
|
|
GetChannelsResponse,
|
2021-09-17 16:37:00 +00:00
|
|
|
UpdateCollaborators,
|
2021-08-19 01:06:46 +00:00
|
|
|
GetUsers,
|
|
|
|
GetUsersResponse,
|
|
|
|
JoinChannel,
|
|
|
|
JoinChannelResponse,
|
2021-09-17 01:39:29 +00:00
|
|
|
JoinWorktree,
|
|
|
|
JoinWorktreeResponse,
|
2021-08-19 21:25:20 +00:00
|
|
|
LeaveChannel,
|
2021-09-21 01:05:46 +00:00
|
|
|
LeaveWorktree,
|
2021-08-19 01:06:46 +00:00
|
|
|
OpenBuffer,
|
|
|
|
OpenBufferResponse,
|
2021-09-17 01:39:29 +00:00
|
|
|
OpenWorktree,
|
|
|
|
OpenWorktreeResponse,
|
2021-08-19 03:59:13 +00:00
|
|
|
Ping,
|
2021-08-18 19:12:27 +00:00
|
|
|
RemovePeer,
|
2021-08-19 01:06:46 +00:00
|
|
|
SaveBuffer,
|
2021-08-18 19:12:27 +00:00
|
|
|
SendChannelMessage,
|
2021-08-20 00:24:54 +00:00
|
|
|
SendChannelMessageResponse,
|
2021-08-19 01:06:46 +00:00
|
|
|
ShareWorktree,
|
|
|
|
ShareWorktreeResponse,
|
2021-09-17 01:39:29 +00:00
|
|
|
UnshareWorktree,
|
2021-08-19 01:06:46 +00:00
|
|
|
UpdateBuffer,
|
|
|
|
UpdateWorktree,
|
2021-08-18 19:12:27 +00:00
|
|
|
);
|
2021-06-15 08:42:06 +00:00
|
|
|
|
2021-08-18 19:12:27 +00:00
|
|
|
request_messages!(
|
|
|
|
(GetChannels, GetChannelsResponse),
|
2021-08-19 01:06:46 +00:00
|
|
|
(GetUsers, GetUsersResponse),
|
2021-08-18 19:12:27 +00:00
|
|
|
(JoinChannel, JoinChannelResponse),
|
2021-08-19 01:06:46 +00:00
|
|
|
(OpenBuffer, OpenBufferResponse),
|
2021-09-16 18:25:31 +00:00
|
|
|
(JoinWorktree, JoinWorktreeResponse),
|
2021-09-17 01:39:29 +00:00
|
|
|
(OpenWorktree, OpenWorktreeResponse),
|
2021-09-09 18:24:16 +00:00
|
|
|
(Ping, Ack),
|
2021-08-19 01:06:46 +00:00
|
|
|
(SaveBuffer, BufferSaved),
|
2021-09-09 18:26:06 +00:00
|
|
|
(UpdateBuffer, Ack),
|
2021-08-19 01:06:46 +00:00
|
|
|
(ShareWorktree, ShareWorktreeResponse),
|
2021-09-17 01:39:29 +00:00
|
|
|
(UnshareWorktree, Ack),
|
2021-08-20 00:24:54 +00:00
|
|
|
(SendChannelMessage, SendChannelMessageResponse),
|
2021-08-27 21:58:28 +00:00
|
|
|
(GetChannelMessages, GetChannelMessagesResponse),
|
2021-08-18 19:12:27 +00:00
|
|
|
);
|
|
|
|
|
2021-08-19 01:06:46 +00:00
|
|
|
entity_messages!(
|
|
|
|
worktree_id,
|
|
|
|
AddPeer,
|
|
|
|
BufferSaved,
|
|
|
|
CloseBuffer,
|
|
|
|
CloseWorktree,
|
|
|
|
OpenBuffer,
|
2021-09-16 18:25:31 +00:00
|
|
|
JoinWorktree,
|
2021-08-19 01:06:46 +00:00
|
|
|
RemovePeer,
|
|
|
|
SaveBuffer,
|
2021-09-17 01:39:29 +00:00
|
|
|
UnshareWorktree,
|
2021-08-19 01:06:46 +00:00
|
|
|
UpdateBuffer,
|
|
|
|
UpdateWorktree,
|
|
|
|
);
|
|
|
|
|
|
|
|
entity_messages!(channel_id, ChannelMessageSent);
|
2021-06-10 01:14:30 +00:00
|
|
|
|
2021-06-09 19:15:25 +00:00
|
|
|
/// A stream of protobuf messages.
|
2021-07-07 16:23:18 +00:00
|
|
|
pub struct MessageStream<S> {
|
|
|
|
stream: S,
|
2021-09-22 18:58:23 +00:00
|
|
|
encoding_buffer: Vec<u8>,
|
2021-06-09 19:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 16:23:18 +00:00
|
|
|
impl<S> MessageStream<S> {
|
|
|
|
pub fn new(stream: S) -> Self {
|
2021-09-22 18:58:23 +00:00
|
|
|
Self {
|
|
|
|
stream,
|
|
|
|
encoding_buffer: Vec::new(),
|
|
|
|
}
|
2021-06-09 19:15:25 +00:00
|
|
|
}
|
2021-06-11 21:30:12 +00:00
|
|
|
|
2021-07-07 16:23:18 +00:00
|
|
|
pub fn inner_mut(&mut self) -> &mut S {
|
|
|
|
&mut self.stream
|
2021-06-11 21:30:12 +00:00
|
|
|
}
|
2021-06-09 19:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 16:23:18 +00:00
|
|
|
impl<S> MessageStream<S>
|
2021-06-09 19:15:25 +00:00
|
|
|
where
|
2021-07-07 16:23:18 +00:00
|
|
|
S: futures::Sink<WebSocketMessage, Error = WebSocketError> + Unpin,
|
2021-06-09 19:15:25 +00:00
|
|
|
{
|
|
|
|
/// Write a given protobuf message to the stream.
|
2021-07-07 16:23:18 +00:00
|
|
|
pub async fn write_message(&mut self, message: &Envelope) -> Result<(), WebSocketError> {
|
2021-09-22 18:58:23 +00:00
|
|
|
self.encoding_buffer.resize(message.encoded_len(), 0);
|
|
|
|
self.encoding_buffer.clear();
|
2021-07-07 16:23:18 +00:00
|
|
|
message
|
2021-09-22 18:58:23 +00:00
|
|
|
.encode(&mut self.encoding_buffer)
|
2021-07-07 16:23:18 +00:00
|
|
|
.map_err(|err| io::Error::from(err))?;
|
2021-09-22 18:58:23 +00:00
|
|
|
let buffer = zstd::stream::encode_all(self.encoding_buffer.as_slice(), 4).unwrap();
|
2021-07-07 16:23:18 +00:00
|
|
|
self.stream.send(WebSocketMessage::Binary(buffer)).await?;
|
|
|
|
Ok(())
|
2021-06-09 19:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 16:23:18 +00:00
|
|
|
impl<S> MessageStream<S>
|
2021-06-09 19:15:25 +00:00
|
|
|
where
|
2021-07-07 16:23:18 +00:00
|
|
|
S: futures::Stream<Item = Result<WebSocketMessage, WebSocketError>> + Unpin,
|
2021-06-09 19:15:25 +00:00
|
|
|
{
|
|
|
|
/// Read a protobuf message of the given type from the stream.
|
2021-07-07 16:23:18 +00:00
|
|
|
pub async fn read_message(&mut self) -> Result<Envelope, WebSocketError> {
|
|
|
|
while let Some(bytes) = self.stream.next().await {
|
|
|
|
match bytes? {
|
|
|
|
WebSocketMessage::Binary(bytes) => {
|
2021-09-22 18:58:23 +00:00
|
|
|
self.encoding_buffer.clear();
|
|
|
|
zstd::stream::copy_decode(bytes.as_slice(), &mut self.encoding_buffer).unwrap();
|
|
|
|
let envelope = Envelope::decode(self.encoding_buffer.as_slice())
|
|
|
|
.map_err(io::Error::from)?;
|
2021-07-07 16:23:18 +00:00
|
|
|
return Ok(envelope);
|
|
|
|
}
|
|
|
|
WebSocketMessage::Close(_) => break,
|
|
|
|
_ => {}
|
2021-07-02 01:12:46 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-07 16:23:18 +00:00
|
|
|
Err(WebSocketError::ConnectionClosed)
|
2021-06-09 19:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 00:07:56 +00:00
|
|
|
impl Into<SystemTime> for Timestamp {
|
|
|
|
fn into(self) -> SystemTime {
|
|
|
|
UNIX_EPOCH
|
|
|
|
.checked_add(Duration::new(self.seconds, self.nanos))
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<SystemTime> for Timestamp {
|
|
|
|
fn from(time: SystemTime) -> Self {
|
|
|
|
let duration = time.duration_since(UNIX_EPOCH).unwrap();
|
|
|
|
Self {
|
|
|
|
seconds: duration.as_secs(),
|
|
|
|
nanos: duration.subsec_nanos(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-16 14:23:20 +00:00
|
|
|
|
|
|
|
impl From<u128> for Nonce {
|
|
|
|
fn from(nonce: u128) -> Self {
|
|
|
|
let upper_half = (nonce >> 64) as u64;
|
|
|
|
let lower_half = nonce as u64;
|
|
|
|
Self {
|
|
|
|
upper_half,
|
|
|
|
lower_half,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Nonce> for u128 {
|
|
|
|
fn from(nonce: Nonce) -> Self {
|
|
|
|
let upper_half = (nonce.upper_half as u128) << 64;
|
|
|
|
let lower_half = nonce.lower_half as u128;
|
|
|
|
upper_half | lower_half
|
|
|
|
}
|
|
|
|
}
|