2022-04-26 17:15:41 +00:00
|
|
|
use async_tungstenite::tungstenite::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:
|
2022-04-26 17:15:41 +00:00
|
|
|
Box<dyn 'static + Send + Unpin + futures::Sink<WebSocketMessage, Error = anyhow::Error>>,
|
2021-09-09 00:49:07 +00:00
|
|
|
pub(crate) rx: Box<
|
|
|
|
dyn 'static
|
|
|
|
+ Send
|
|
|
|
+ Unpin
|
2022-04-26 17:15:41 +00:00
|
|
|
+ futures::Stream<Item = Result<WebSocketMessage, anyhow::Error>>,
|
2021-09-09 00:49:07 +00:00
|
|
|
>,
|
|
|
|
}
|
|
|
|
|
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
|
2022-04-26 17:15:41 +00:00
|
|
|
+ futures::Sink<WebSocketMessage, Error = anyhow::Error>
|
|
|
|
+ futures::Stream<Item = Result<WebSocketMessage, anyhow::Error>>,
|
2021-09-09 00:49:07 +00:00
|
|
|
{
|
|
|
|
let (tx, rx) = stream.split();
|
|
|
|
Self {
|
|
|
|
tx: Box::new(tx),
|
|
|
|
rx: Box::new(rx),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-26 17:15:41 +00:00
|
|
|
pub async fn send(&mut self, message: WebSocketMessage) -> Result<(), anyhow::Error> {
|
2021-09-09 00:49:07 +00:00
|
|
|
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-04-08 14:04:03 +00:00
|
|
|
) -> (Self, Self, std::sync::Arc<std::sync::atomic::AtomicBool>) {
|
|
|
|
use std::sync::{
|
|
|
|
atomic::{AtomicBool, Ordering::SeqCst},
|
|
|
|
Arc,
|
|
|
|
};
|
2021-09-09 00:49:07 +00:00
|
|
|
|
2022-04-08 14:04:03 +00:00
|
|
|
let killed = Arc::new(AtomicBool::new(false));
|
|
|
|
let (a_tx, a_rx) = channel(killed.clone(), executor.clone());
|
|
|
|
let (b_tx, b_rx) = channel(killed.clone(), executor);
|
2022-03-04 23:52:02 +00:00
|
|
|
return (
|
2021-09-09 11:27:44 +00:00
|
|
|
Self { tx: a_tx, rx: b_rx },
|
|
|
|
Self { tx: b_tx, rx: a_rx },
|
2022-04-08 14:04:03 +00:00
|
|
|
killed,
|
2022-03-04 23:52:02 +00:00
|
|
|
);
|
2021-09-09 11:27:44 +00:00
|
|
|
|
2022-08-10 21:39:24 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
2022-03-04 23:52:02 +00:00
|
|
|
fn channel(
|
2022-04-08 14:04:03 +00:00
|
|
|
killed: Arc<AtomicBool>,
|
|
|
|
executor: Arc<gpui::executor::Background>,
|
2022-03-04 23:52:02 +00:00
|
|
|
) -> (
|
2022-04-26 17:15:41 +00:00
|
|
|
Box<dyn Send + Unpin + futures::Sink<WebSocketMessage, Error = anyhow::Error>>,
|
|
|
|
Box<dyn Send + Unpin + futures::Stream<Item = Result<WebSocketMessage, anyhow::Error>>>,
|
2022-03-04 23:52:02 +00:00
|
|
|
) {
|
2022-04-26 17:15:41 +00:00
|
|
|
use anyhow::anyhow;
|
2022-03-04 23:52:02 +00:00
|
|
|
use futures::channel::mpsc;
|
2022-04-08 14:04:03 +00:00
|
|
|
use std::io::{Error, ErrorKind};
|
2021-09-09 11:27:44 +00:00
|
|
|
|
2022-03-04 23:52:02 +00:00
|
|
|
let (tx, rx) = mpsc::unbounded::<WebSocketMessage>();
|
|
|
|
|
2022-04-26 17:15:41 +00:00
|
|
|
let tx = tx.sink_map_err(|error| anyhow!(error)).with({
|
|
|
|
let killed = killed.clone();
|
|
|
|
let executor = Arc::downgrade(&executor);
|
|
|
|
move |msg| {
|
2022-04-08 14:04:03 +00:00
|
|
|
let killed = killed.clone();
|
2022-04-26 17:15:41 +00:00
|
|
|
let executor = executor.clone();
|
|
|
|
Box::pin(async move {
|
|
|
|
if let Some(executor) = executor.upgrade() {
|
|
|
|
executor.simulate_random_delay().await;
|
|
|
|
}
|
2022-03-04 23:52:02 +00:00
|
|
|
|
2022-04-26 17:15:41 +00:00
|
|
|
// Writes to a half-open TCP connection will error.
|
|
|
|
if killed.load(SeqCst) {
|
2022-08-10 21:39:24 +00:00
|
|
|
std::io::Result::Err(Error::new(ErrorKind::Other, "connection lost"))?;
|
2022-04-26 17:15:41 +00:00
|
|
|
}
|
2022-03-04 23:52:02 +00:00
|
|
|
|
2022-04-26 17:15:41 +00:00
|
|
|
Ok(msg)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|
2022-03-04 23:52:02 +00:00
|
|
|
|
|
|
|
let rx = rx.then({
|
2022-08-10 21:39:24 +00:00
|
|
|
let killed = killed;
|
2022-03-04 23:52:02 +00:00
|
|
|
let executor = Arc::downgrade(&executor);
|
2021-09-09 11:27:44 +00:00
|
|
|
move |msg| {
|
2022-04-08 14:04:03 +00:00
|
|
|
let killed = killed.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.
|
2022-04-08 14:04:03 +00:00
|
|
|
if killed.load(SeqCst) {
|
2022-03-04 23:52:02 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|