wip: winapi core

This commit is contained in:
Xory 2025-12-01 20:44:11 +02:00
parent 7304b33b7c
commit a521782a37
6 changed files with 638 additions and 73 deletions

View file

@ -1,14 +1,11 @@
use futures_util::{SinkExt, stream::StreamExt};
use skylink::WsTx;
use skylink::{ping_job, reconnect_websocket, WsTx};
use skylink::lib::logger::{LogLevel, log};
use skylink::{LOG_PATH, WS_URL};
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio_tungstenite::tungstenite::protocol::Message;
// This is the type for the SENDER half of the WebSocket stream.
// It will be the shared state.
// Some parts of this function were generated by an LLM. I'm taking note of this in case a
// weird barely detectable bug pops up, as LLMs tend to generate.
async fn websocket_handler(ws_tx: WsTx) {
@ -20,23 +17,13 @@ async fn websocket_handler(ws_tx: WsTx) {
let ws_stream = match connection_result {
Ok((stream, _)) => {
log(
LogLevel::Info,
LOG_PATH,
"[ws] WebSocket connection established.".to_string(),
)
.await;
log(LogLevel::Info, LOG_PATH, "[ws] WebSocket connection established.".to_string()).await;
stream
}
Err(e) => {
log(
LogLevel::Warning,
LOG_PATH,
format!("[ws] Failed to connect: {:?}. Retrying in 5s....", e)
)
.await;
log(LogLevel::Warning, LOG_PATH, format!("[ws] Failed to connect: {:?}. Retrying in 5s....", e)).await;
tokio::time::sleep(Duration::from_secs(5)).await;
continue; // Go to the next iteration of the loop to retry.
continue;
}
};
@ -46,80 +33,63 @@ async fn websocket_handler(ws_tx: WsTx) {
let mut unlocked = ws_tx.lock().await;
*unlocked = Some(ws_send);
}
let ws_tx_clone = ws_tx.clone();
tokio::spawn(async move {
loop {
if let Err(_) = ping_job(ws_tx_clone.clone()).await {
reconnect_websocket(ws_tx_clone.clone()).await;
}
}
});
while let Some(msg) = ws_recv.next().await {
match msg {
// break is used to trigger reconnect.
Ok(Message::Text(text)) => {
log(
LogLevel::Debug,
LOG_PATH,
format!("[ws] Received text: {}", &text)
)
.await;
log(LogLevel::Debug, LOG_PATH, format!("[ws] received text: {}", &text)).await;
}
Ok(Message::Close(_)) => {
log(
LogLevel::Warning,
LOG_PATH,
format!("[ws] Received close frame, disconnecting.")
)
.await;
break;
log(LogLevel::Warning, LOG_PATH, format!("[ws] received close frame, disconnecting.")).await;
break;
}
Ok(Message::Ping(h)) => {
log(
LogLevel::Debug,
LOG_PATH,
format!("[ws] Received Ping, sending Pong")
)
.await;
log(LogLevel::Debug, LOG_PATH, format!("[ws] received ping, sending pong")).await;
let mut unlocked = ws_tx.lock().await;
dbg!(&ws_tx);
dbg!(&unlocked);
match unlocked.as_mut() {
Some(v) => {
if let Err(e) = v.send(Message::Pong(h)).await {
log(LogLevel::Error, LOG_PATH, format!("[ws] Failed to send Pong: {e}, reconnecting.")).await;
log(LogLevel::Error, LOG_PATH, format!("[ws] failed to send pong: {e}, reconnecting.")).await;
break;
}
}
None => {
log(LogLevel::Error, LOG_PATH, format!("[ws] Failed to respond: no sink, reconnecting.")).await;
log(LogLevel::Error, LOG_PATH, format!("[ws] failed to respond: no sink, reconnecting.")).await;
break;
}
}
}
Err(e) => {
log(
LogLevel::Error,
LOG_PATH,
format!("[ws] Error receiving message: {:?}.", e),
)
.await;
log(LogLevel::Error, LOG_PATH, format!("[ws] error receiving message: {:?}.", e)).await;
break;
}
_ => { /* Ignore other message types */ }
}
{
let mut unlocked_ws_tx = ws_tx.lock().await;
*unlocked_ws_tx = None;
_ => {}
}
}
log(
LogLevel::Error,
LOG_PATH,
format!("[ws] Connection lost.")
)
.await;
{
let mut unlocked_ws_tx = ws_tx.lock().await;
*unlocked_ws_tx = None;
}
// Reconnecting is handled by the loop
// So, we sleep to avoid spamming the server.
std::thread::sleep(std::time::Duration::from_secs(5));
log(LogLevel::Error, LOG_PATH, format!("[ws] connection lost.")).await;
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let ws_tx: WsTx = Arc::new(Mutex::new(None));