switched from using a oneshot

This commit is contained in:
Nickiel12 2024-08-25 23:34:40 +00:00
parent 59b0b88c53
commit a89e970be3

View file

@ -7,10 +7,7 @@ use futures_util::stream::{SplitSink, SplitStream, StreamExt};
use futures_util::SinkExt; use futures_util::SinkExt;
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use tokio::runtime::Handle; use tokio::runtime::Handle;
use tokio::{ use tokio::net::TcpStream;
net::TcpStream,
sync::oneshot
};
use tokio_tungstenite::{connect_async, tungstenite::Message, MaybeTlsStream, WebSocketStream}; use tokio_tungstenite::{connect_async, tungstenite::Message, MaybeTlsStream, WebSocketStream};
use tokio_tungstenite::tungstenite::error::Error as tungstenite_error; use tokio_tungstenite::tungstenite::error::Error as tungstenite_error;
use tracing::{error, info, debug, instrument}; use tracing::{error, info, debug, instrument};
@ -34,37 +31,23 @@ pub type AppReceiver = Receiver<ApplicationMessage>;
/// It will attempt to connect to the ws string, and will either /// It will attempt to connect to the ws string, and will either
/// return an `AppSender` or the websocket error. /// return an `AppSender` or the websocket error.
/// If the oneshot is unable to be sent to, it will trace and error and close down /// 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( pub async fn connect_to_server(
connection_string: String, connection_string: String,
result_oneshot: oneshot::Sender<Result<(AppSender, AppReceiver), tungstenite_error>>,
rt: Handle, rt: Handle,
) { ) -> Result<(AppSender, AppReceiver), tungstenite_error> {
debug!("Connecting to parent!"); debug!("Connecting to parent!");
match connect_async(connection_string).await { match connect_async(connection_string).await {
Err(e) => { Err(e) => {
if let Err(e) = result_oneshot.send(Err(e)) { return Err(e);
error!("WS connection failed, and could not send error back to the main program! \n{:?}", e);
return;
} }
} Ok((ws, _)) => {
Ok((mut ws, _)) => {
debug!("Connection successful"); debug!("Connection successful");
let (to_core_sender, to_core_reciever) = async_channel::bounded::<ApplicationMessage>(MAX_MESSAGE); let (to_core_sender, to_core_reciever) = async_channel::bounded::<ApplicationMessage>(MAX_MESSAGE);
let (to_app_events, from_app_events) = async_channel::bounded::<ApplicationMessage>(MAX_MESSAGE); let (to_app_events, from_app_events) = async_channel::bounded::<ApplicationMessage>(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!"); debug!("splitting websocket!");
let (ws_sender, ws_recv) = ws.split(); 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)); rt.spawn(send_to_wc_task(to_core_reciever, receiver_is_closed, ws_sender));
info!("Websocket connect successfully"); info!("Websocket connect successfully");
return Ok((to_core_sender as AppSender, from_app_events as AppReceiver));
} }
} }
} }