[FIX] server no longer freezes main
This commit is contained in:
parent
059f2e9825
commit
ab1c36c9a0
2 changed files with 12 additions and 31 deletions
|
|
@ -14,11 +14,8 @@ use crate::{
|
|||
};
|
||||
use dashmap::DashMap;
|
||||
use futures::Stream;
|
||||
use futures::stream::{SplitSink, SplitStream};
|
||||
use futures_util::sink::Sink;
|
||||
use futures_util::{SinkExt, StreamExt};
|
||||
use hyper::upgrade::Upgraded;
|
||||
use hyper_util::rt::TokioIo;
|
||||
use json::JsonValue;
|
||||
use json::number::Number;
|
||||
use std::collections::HashMap;
|
||||
|
|
@ -53,25 +50,6 @@ impl OmikronConnection {
|
|||
is_connected: Arc::new(Mutex::new(false)),
|
||||
}
|
||||
}
|
||||
pub async fn client(
|
||||
writer: SplitSink<tokio_tungstenite::WebSocketStream<TokioIo<Upgraded>>, Message>,
|
||||
reader: SplitStream<tokio_tungstenite::WebSocketStream<TokioIo<Upgraded>>>,
|
||||
) -> Arc<Self> {
|
||||
let connection = Arc::new(Self {
|
||||
writer: Arc::new(Mutex::new(Some(Box::new(writer)
|
||||
as Box<dyn Sink<Message, Error = tungstenite::Error> + Send + Unpin>))),
|
||||
waiting: Arc::new(DashMap::new()),
|
||||
last_ping: Arc::new(Mutex::new(-1)),
|
||||
message_send_times: Arc::new(Mutex::new(HashMap::new())),
|
||||
is_connected: Arc::new(Mutex::new(true)),
|
||||
});
|
||||
let boxed_reader: Box<
|
||||
dyn Stream<Item = Result<Message, tungstenite::Error>> + Send + Unpin,
|
||||
> = Box::new(reader);
|
||||
|
||||
connection.spawn_listener(boxed_reader).await;
|
||||
connection
|
||||
}
|
||||
pub async fn is_connected(&self) -> bool {
|
||||
*self.is_connected.lock().await
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,10 +25,12 @@ async fn ws_handler(req: HttpRequest, stream: web::Payload) -> Result<impl Respo
|
|||
ws::start(session, &req, stream)
|
||||
}
|
||||
|
||||
pub async fn start(port: u16) -> bool {
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
use tokio::sync::oneshot;
|
||||
|
||||
let server_task = tokio::spawn(async move {
|
||||
pub async fn start(port: u16) -> bool {
|
||||
let (tx, rx) = oneshot::channel::<ServerHandle>();
|
||||
|
||||
let _ = tokio::spawn(async move {
|
||||
let server = match load_tls_config() {
|
||||
Ok(Some(tls_config)) => {
|
||||
log_message(format!("HTTPS (HTTP/2) Server running on 0.0.0.0:{}", port));
|
||||
|
|
@ -72,13 +74,14 @@ pub async fn start(port: u16) -> bool {
|
|||
log_message("Web Server shutdown complete.");
|
||||
});
|
||||
|
||||
let server_handle = rx.recv().unwrap();
|
||||
|
||||
tokio::spawn(async move {
|
||||
wait_for_shutdown(server_handle).await;
|
||||
});
|
||||
|
||||
server_task.await.is_ok()
|
||||
if let Ok(server_handle) = rx.await {
|
||||
tokio::spawn(async move {
|
||||
wait_for_shutdown(server_handle).await;
|
||||
});
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
async fn wait_for_shutdown(server_handle: ServerHandle) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue