2021-09-09 00:49:07 +00:00
|
|
|
use async_tungstenite::tungstenite::{Error as WebSocketError, Message as WebSocketMessage};
|
2022-03-04 23:52:02 +00:00
|
|
|
use futures::{SinkExt as _, StreamExt as _};
|
2021-09-09 00:49:07 +00:00
|
|
|
|
2021-09-15 01:19:11 +00:00
|
|
|
pub struct Connection {
|
2021-09-09 00:49:07 +00:00
|
|
|
pub(crate) tx:
|
|
|
|
Box<dyn 'static + Send + Unpin + futures::Sink<WebSocketMessage, Error = WebSocketError>>,
|
|
|
|
pub(crate) rx: Box<
|
|
|
|
dyn 'static
|
|
|
|
+ Send
|
|
|
|
+ Unpin
|
|
|
|
+ futures::Stream<Item = Result<WebSocketMessage, WebSocketError>>,
|
|
|
|
>,
|
|
|
|
}
|
|
|
|
|
2021-09-15 01:19:11 +00:00
|
|
|
impl Connection {
|
2021-09-09 00:49:07 +00:00
|
|
|
pub fn new<S>(stream: S) -> Self
|
|
|
|
where
|
|
|
|
S: 'static
|
|
|
|
+ Send
|
|
|
|
+ Unpin
|
|
|
|
+ futures::Sink<WebSocketMessage, Error = WebSocketError>
|
|
|
|
+ futures::Stream<Item = Result<WebSocketMessage, WebSocketError>>,
|
|
|
|
{
|
|
|
|
let (tx, rx) = stream.split();
|
|
|
|
Self {
|
|
|
|
tx: Box::new(tx),
|
|
|
|
rx: Box::new(rx),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn send(&mut self, message: WebSocketMessage) -> Result<(), WebSocketError> {
|
|
|
|
self.tx.send(message).await
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-01-24 22:38:24 +00:00
|
|
|
pub fn in_memory(
|
|
|
|
executor: std::sync::Arc<gpui::executor::Background>,
|
2022-03-04 23:52:02 +00:00
|
|
|
) -> (Self, Self, postage::barrier::Sender) {
|
|
|
|
use postage::prelude::Stream;
|
2021-09-09 00:49:07 +00:00
|
|
|
|
2022-03-04 23:52:02 +00:00
|
|
|
let (kill_tx, kill_rx) = postage::barrier::channel();
|
|
|
|
let (a_tx, a_rx) = channel(kill_rx.clone(), executor.clone());
|
|
|
|
let (b_tx, b_rx) = channel(kill_rx, executor);
|
|
|
|
return (
|
2021-09-09 11:27:44 +00:00
|
|
|
Self { tx: a_tx, rx: b_rx },
|
|
|
|
Self { tx: b_tx, rx: a_rx },
|
|
|
|
kill_tx,
|
2022-03-04 23:52:02 +00:00
|
|
|
);
|
2021-09-09 11:27:44 +00:00
|
|
|
|
2022-03-04 23:52:02 +00:00
|
|
|
fn channel(
|
|
|
|
kill_rx: postage::barrier::Receiver,
|
|
|
|
executor: std::sync::Arc<gpui::executor::Background>,
|
|
|
|
) -> (
|
|
|
|
Box<dyn Send + Unpin + futures::Sink<WebSocketMessage, Error = WebSocketError>>,
|
|
|
|
Box<
|
|
|
|
dyn Send + Unpin + futures::Stream<Item = Result<WebSocketMessage, WebSocketError>>,
|
|
|
|
>,
|
|
|
|
) {
|
|
|
|
use futures::channel::mpsc;
|
|
|
|
use std::{
|
|
|
|
io::{Error, ErrorKind},
|
|
|
|
sync::Arc,
|
|
|
|
};
|
2021-09-09 11:27:44 +00:00
|
|
|
|
2022-03-04 23:52:02 +00:00
|
|
|
let (tx, rx) = mpsc::unbounded::<WebSocketMessage>();
|
|
|
|
|
|
|
|
let tx = tx
|
|
|
|
.sink_map_err(|e| WebSocketError::from(Error::new(ErrorKind::Other, e)))
|
|
|
|
.with({
|
|
|
|
let kill_rx = kill_rx.clone();
|
|
|
|
let executor = Arc::downgrade(&executor);
|
|
|
|
move |msg| {
|
|
|
|
let mut kill_rx = kill_rx.clone();
|
|
|
|
let executor = executor.clone();
|
|
|
|
Box::pin(async move {
|
|
|
|
if let Some(executor) = executor.upgrade() {
|
|
|
|
executor.simulate_random_delay().await;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Writes to a half-open TCP connection will error.
|
|
|
|
if kill_rx.try_recv().is_ok() {
|
|
|
|
std::io::Result::Err(
|
|
|
|
Error::new(ErrorKind::Other, "connection lost").into(),
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(msg)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let rx = rx.then({
|
2021-09-09 11:27:44 +00:00
|
|
|
let kill_rx = kill_rx.clone();
|
2022-03-04 23:52:02 +00:00
|
|
|
let executor = Arc::downgrade(&executor);
|
2021-09-09 11:27:44 +00:00
|
|
|
move |msg| {
|
2022-03-04 23:52:02 +00:00
|
|
|
let mut kill_rx = kill_rx.clone();
|
2022-01-24 22:38:24 +00:00
|
|
|
let executor = executor.clone();
|
|
|
|
Box::pin(async move {
|
2022-03-02 02:02:12 +00:00
|
|
|
if let Some(executor) = executor.upgrade() {
|
|
|
|
executor.simulate_random_delay().await;
|
|
|
|
}
|
2022-03-04 23:52:02 +00:00
|
|
|
|
|
|
|
// Reads from a half-open TCP connection will hang.
|
|
|
|
if kill_rx.try_recv().is_ok() {
|
|
|
|
futures::future::pending::<()>().await;
|
2022-01-24 22:38:24 +00:00
|
|
|
}
|
2022-03-04 23:52:02 +00:00
|
|
|
|
|
|
|
Ok(msg)
|
2022-01-24 22:38:24 +00:00
|
|
|
})
|
2021-09-09 11:27:44 +00:00
|
|
|
}
|
|
|
|
});
|
2021-09-09 14:25:58 +00:00
|
|
|
|
2022-03-04 23:52:02 +00:00
|
|
|
(Box::new(tx), Box::new(rx))
|
2021-09-09 14:25:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|