Compare commits

..

1 Commits

Author SHA1 Message Date
kozabrada123 9622415d0d
Merge badf3e9d47 into 82a3f98db7 2024-01-18 16:09:43 +00:00
7 changed files with 36 additions and 73 deletions

25
Cargo.lock generated
View File

@ -233,6 +233,7 @@ dependencies = [
"reqwest",
"rustls",
"rustls-native-certs",
"safina-timer",
"serde",
"serde-aux",
"serde_json",
@ -246,7 +247,6 @@ dependencies = [
"wasm-bindgen",
"wasm-bindgen-futures",
"wasm-bindgen-test",
"wasmtimer",
"ws_stream_wasm",
]
@ -1800,6 +1800,15 @@ version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c"
[[package]]
name = "safina-timer"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1081a264d1a3e81b75c4bcd5696094fb6ce470c2ded14cbd47bcb5229079b9df"
dependencies = [
"once_cell",
]
[[package]]
name = "salsa20"
version = "0.10.2"
@ -2810,20 +2819,6 @@ dependencies = [
"syn 2.0.48",
]
[[package]]
name = "wasmtimer"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f656cd8858a5164932d8a90f936700860976ec21eb00e0fe2aa8cab13f6b4cf"
dependencies = [
"futures",
"js-sys",
"parking_lot",
"pin-utils",
"slab",
"wasm-bindgen",
]
[[package]]
name = "web-sys"
version = "0.3.66"

View File

@ -57,6 +57,7 @@ sqlx = { version = "0.7.3", features = [
], optional = true }
discortp = { version = "0.5.0", optional = true, features = ["rtp", "discord", "demux"] }
crypto_secretbox = {version = "0.1.1", optional = true}
safina-timer = "0.1.11"
rand = "0.8.5"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
@ -74,7 +75,6 @@ getrandom = { version = "0.2.12" }
getrandom = { version = "0.2.12", features = ["js"] }
ws_stream_wasm = "0.7.4"
wasm-bindgen-futures = "0.4.39"
wasmtimer = "0.2.0"
[dev-dependencies]
lazy_static = "1.4.0"

View File

@ -8,11 +8,6 @@ use chorus::{
use std::{sync::Arc, time::Duration};
use tokio::{self};
#[cfg(not(target_arch = "wasm32"))]
use tokio::time::sleep;
#[cfg(target_arch = "wasm32")]
use wasmtimer::tokio::sleep;
// This example creates a simple gateway connection and a basic observer struct
// Due to certain limitations all observers must impl debug
@ -59,9 +54,10 @@ async fn main() {
let mut identify = GatewayIdentifyPayload::common();
identify.token = token;
gateway.send_identify(identify).await;
safina_timer::start_timer_thread();
// Do something on the main thread so we don't quit
loop {
sleep(Duration::from_secs(3600)).await;
safina_timer::sleep_for(Duration::MAX).await
}
}

View File

@ -3,11 +3,6 @@ use std::time::Duration;
use chorus::gateway::Gateway;
use chorus::{self, types::GatewayIdentifyPayload};
#[cfg(not(target_arch = "wasm32"))]
use tokio::time::sleep;
#[cfg(target_arch = "wasm32")]
use wasmtimer::tokio::sleep;
/// This example creates a simple gateway connection and a session with an Identify event
#[tokio::main(flavor = "current_thread")]
async fn main() {
@ -15,7 +10,7 @@ async fn main() {
let websocket_url_spacebar = "wss://gateway.old.server.spacebar.chat/".to_string();
// Initiate the gateway connection, starting a listener in one thread and a heartbeat handler in another
let gateway = Gateway::spawn(websocket_url_spacebar).await.unwrap();
let _ = Gateway::spawn(websocket_url_spacebar).await.unwrap();
// At this point, we are connected to the server and are sending heartbeats, however we still haven't authenticated
@ -31,10 +26,10 @@ async fn main() {
identify.token = token;
// Send off the event
gateway.send_identify(identify).await;
safina_timer::start_timer_thread();
// Do something on the main thread so we don't quit
loop {
sleep(Duration::from_secs(3600)).await;
safina_timer::sleep_for(Duration::MAX).await
}
}

View File

@ -1,20 +1,9 @@
use futures_util::SinkExt;
use log::*;
#[cfg(not(target_arch = "wasm32"))]
use tokio::time::Instant;
#[cfg(target_arch = "wasm32")]
use wasmtimer::std::Instant;
#[cfg(not(target_arch = "wasm32"))]
use tokio::time::sleep_until;
#[cfg(target_arch = "wasm32")]
use wasmtimer::tokio::sleep_until;
use std::time::Duration;
use std::time::{self, Duration, Instant};
use tokio::sync::mpsc::{Receiver, Sender};
use safina_timer::sleep_until;
#[cfg(not(target_arch = "wasm32"))]
use tokio::task;
@ -68,10 +57,12 @@ impl HeartbeatHandler {
mut receive: Receiver<HeartbeatThreadCommunication>,
mut kill_receive: tokio::sync::broadcast::Receiver<()>,
) {
let mut last_heartbeat_timestamp: Instant = Instant::now();
let mut last_heartbeat_timestamp: Instant = time::Instant::now();
let mut last_heartbeat_acknowledged = true;
let mut last_seq_number: Option<u64> = None;
safina_timer::start_timer_thread();
loop {
if kill_receive.try_recv().is_ok() {
trace!("GW: Closing heartbeat task");
@ -132,7 +123,7 @@ impl HeartbeatHandler {
break;
}
last_heartbeat_timestamp = Instant::now();
last_heartbeat_timestamp = time::Instant::now();
last_heartbeat_acknowledged = false;
}
}

View File

@ -1,25 +1,12 @@
use futures_util::SinkExt;
use log::*;
#[cfg(not(target_arch = "wasm32"))]
use tokio::time::Instant;
#[cfg(target_arch = "wasm32")]
use wasmtimer::std::Instant;
#[cfg(not(target_arch = "wasm32"))]
use tokio::time::sleep_until;
#[cfg(target_arch = "wasm32")]
use wasmtimer::tokio::sleep_until;
use std::{sync::Arc, time::Duration};
use tokio::sync::{
mpsc::{Receiver, Sender},
Mutex,
use std::{
sync::Arc,
time::{Duration, Instant},
};
#[cfg(not(target_arch = "wasm32"))]
use tokio::task;
use futures_util::SinkExt;
use log::*;
use safina_timer::sleep_until;
use tokio::sync::{mpsc::Sender, Mutex};
use crate::{
gateway::heartbeat::HEARTBEAT_ACK_TIMEOUT,
@ -50,7 +37,7 @@ impl VoiceHeartbeatHandler {
let kill_receive = kill_rc.resubscribe();
#[cfg(not(target_arch = "wasm32"))]
task::spawn(async move {
tokio::task::spawn(async move {
Self::heartbeat_task(
websocket_tx,
heartbeat_interval,
@ -86,13 +73,15 @@ impl VoiceHeartbeatHandler {
websocket_tx: Arc<Mutex<Sink>>,
heartbeat_interval: Duration,
starting_nonce: u64,
mut receive: Receiver<VoiceHeartbeatThreadCommunication>,
mut receive: tokio::sync::mpsc::Receiver<VoiceHeartbeatThreadCommunication>,
mut kill_receive: tokio::sync::broadcast::Receiver<()>,
) {
let mut last_heartbeat_timestamp: Instant = Instant::now();
let mut last_heartbeat_acknowledged = true;
let mut nonce: u64 = starting_nonce;
safina_timer::start_timer_thread();
loop {
if kill_receive.try_recv().is_ok() {
trace!("VGW: Closing heartbeat task");

View File

@ -12,11 +12,6 @@ use wasm_bindgen_test::*;
#[cfg(target_arch = "wasm32")]
wasm_bindgen_test_configure!(run_in_browser);
#[cfg(not(target_arch = "wasm32"))]
use tokio::time::sleep;
#[cfg(target_arch = "wasm32")]
use wasmtimer::tokio::sleep;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
/// Tests establishing a connection (hello and heartbeats) on the local gateway;
@ -66,9 +61,11 @@ async fn test_gateway_authenticate() {
gateway.send_identify(identify).await;
let current_time = std::time::Instant::now();
tokio::select! {
// Fail, we timed out waiting for it
() = sleep(Duration::from_secs(20)) => {
() = safina_timer::sleep_until(current_time + Duration::from_secs(20)) => {
println!("Timed out waiting for event, failing..");
assert!(false);
}