From a89e970be35e01eb8a59c368e7b817e93984c137 Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Sun, 25 Aug 2024 23:34:40 +0000 Subject: [PATCH] switched from using a oneshot --- src/lib.rs | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9c60ad0..e307758 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,10 +7,7 @@ use futures_util::stream::{SplitSink, SplitStream, StreamExt}; use futures_util::SinkExt; use serde::{Serialize, Deserialize}; use tokio::runtime::Handle; -use tokio::{ - net::TcpStream, - sync::oneshot -}; +use tokio::net::TcpStream; use tokio_tungstenite::{connect_async, tungstenite::Message, MaybeTlsStream, WebSocketStream}; use tokio_tungstenite::tungstenite::error::Error as tungstenite_error; use tracing::{error, info, debug, instrument}; @@ -34,37 +31,23 @@ pub type AppReceiver = Receiver; /// It will attempt to connect to the ws string, and will either /// return an `AppSender` or the websocket error. /// If the oneshot is unable to be sent to, it will trace and error and close down -#[instrument(skip(result_oneshot, rt))] +#[instrument(skip(rt))] pub async fn connect_to_server( connection_string: String, - result_oneshot: oneshot::Sender>, rt: Handle, - ) { + ) -> Result<(AppSender, AppReceiver), tungstenite_error> { debug!("Connecting to parent!"); match connect_async(connection_string).await { Err(e) => { - if let Err(e) = result_oneshot.send(Err(e)) { - error!("WS connection failed, and could not send error back to the main program! \n{:?}", e); - return; - } + return Err(e); } - Ok((mut ws, _)) => { + Ok((ws, _)) => { debug!("Connection successful"); let (to_core_sender, to_core_reciever) = async_channel::bounded::(MAX_MESSAGE); let (to_app_events, from_app_events) = async_channel::bounded::(MAX_MESSAGE); - debug!("created channels for oneshot"); - - if let Err(e) = result_oneshot.send(Ok((to_core_sender as AppSender, from_app_events as AppReceiver))) { - if let Err(e2) = ws.close(None).await { - error!("Could not close connection to websocket! {e2}"); - } - error!("WS connection succeeded, and could not send error back to the main program! \n{:?}", e); - return; - } - debug!("splitting websocket!"); let (ws_sender, ws_recv) = ws.split(); @@ -76,6 +59,7 @@ pub async fn connect_to_server( rt.spawn(send_to_wc_task(to_core_reciever, receiver_is_closed, ws_sender)); info!("Websocket connect successfully"); + return Ok((to_core_sender as AppSender, from_app_events as AppReceiver)); } } }