switched from using a oneshot
This commit is contained in:
parent
59b0b88c53
commit
a89e970be3
1 changed files with 6 additions and 22 deletions
28
src/lib.rs
28
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<ApplicationMessage>;
|
|||
/// 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<Result<(AppSender, AppReceiver), tungstenite_error>>,
|
||||
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::<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!");
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue