Library: Add compatibility for legacy Tokio 0.2 (#40)
Adds support to the library for tokio 0.2 backward-compatibility. This should hopefully benefit, and prevent lavalink-rs from being blocked on this feature. These can be reached using, e.g., `gateway-tokio-02`, `driver-tokio-02`, `serenity-rustls-tokio-02`, and `serenity-native-tokio-02` features. Naturally, this requires some jiggering about with features and the underlying CI, which has been taken care of. Twilight can't be handled in this way, as their last tokio 0.2 version uses the deprecated Discord Gateway v6.
This commit is contained in:
@@ -3,48 +3,93 @@ use crate::constants::*;
|
||||
use discortp::discord::MutableKeepalivePacket;
|
||||
use flume::Receiver;
|
||||
use std::sync::Arc;
|
||||
#[cfg(not(feature = "tokio-02-marker"))]
|
||||
use tokio::{
|
||||
net::UdpSocket,
|
||||
time::{timeout_at, Instant},
|
||||
};
|
||||
#[cfg(feature = "tokio-02-marker")]
|
||||
use tokio_compat::{
|
||||
net::udp::SendHalf,
|
||||
time::{timeout_at, Instant},
|
||||
};
|
||||
use tracing::{error, info, instrument, trace};
|
||||
|
||||
struct UdpTx {
|
||||
ssrc: u32,
|
||||
rx: Receiver<UdpTxMessage>,
|
||||
|
||||
#[cfg(not(feature = "tokio-02-marker"))]
|
||||
udp_tx: Arc<UdpSocket>,
|
||||
#[cfg(feature = "tokio-02-marker")]
|
||||
udp_tx: SendHalf,
|
||||
}
|
||||
|
||||
impl UdpTx {
|
||||
async fn run(&mut self) {
|
||||
let mut keepalive_bytes = [0u8; MutableKeepalivePacket::minimum_packet_size()];
|
||||
let mut ka = MutableKeepalivePacket::new(&mut keepalive_bytes[..])
|
||||
.expect("FATAL: Insufficient bytes given to keepalive packet.");
|
||||
ka.set_ssrc(self.ssrc);
|
||||
|
||||
let mut ka_time = Instant::now() + UDP_KEEPALIVE_GAP;
|
||||
|
||||
loop {
|
||||
use UdpTxMessage::*;
|
||||
match timeout_at(ka_time, self.rx.recv_async()).await {
|
||||
Err(_) => {
|
||||
trace!("Sending UDP Keepalive.");
|
||||
if let Err(e) = self.udp_tx.send(&keepalive_bytes[..]).await {
|
||||
error!("Fatal UDP keepalive send error: {:?}.", e);
|
||||
break;
|
||||
}
|
||||
ka_time += UDP_KEEPALIVE_GAP;
|
||||
},
|
||||
Ok(Ok(Packet(p))) =>
|
||||
if let Err(e) = self.udp_tx.send(&p[..]).await {
|
||||
error!("Fatal UDP packet send error: {:?}.", e);
|
||||
break;
|
||||
},
|
||||
Ok(Err(e)) => {
|
||||
error!("Fatal UDP packet receive error: {:?}.", e);
|
||||
break;
|
||||
},
|
||||
Ok(Ok(Poison)) => {
|
||||
break;
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "tokio-02-marker"))]
|
||||
#[instrument(skip(udp_msg_rx))]
|
||||
pub(crate) async fn runner(udp_msg_rx: Receiver<UdpTxMessage>, ssrc: u32, udp_tx: Arc<UdpSocket>) {
|
||||
info!("UDP transmit handle started.");
|
||||
|
||||
let mut keepalive_bytes = [0u8; MutableKeepalivePacket::minimum_packet_size()];
|
||||
let mut ka = MutableKeepalivePacket::new(&mut keepalive_bytes[..])
|
||||
.expect("FATAL: Insufficient bytes given to keepalive packet.");
|
||||
ka.set_ssrc(ssrc);
|
||||
let mut txer = UdpTx {
|
||||
ssrc,
|
||||
rx: udp_msg_rx,
|
||||
udp_tx,
|
||||
};
|
||||
|
||||
let mut ka_time = Instant::now() + UDP_KEEPALIVE_GAP;
|
||||
|
||||
loop {
|
||||
use UdpTxMessage::*;
|
||||
match timeout_at(ka_time, udp_msg_rx.recv_async()).await {
|
||||
Err(_) => {
|
||||
trace!("Sending UDP Keepalive.");
|
||||
if let Err(e) = udp_tx.send(&keepalive_bytes[..]).await {
|
||||
error!("Fatal UDP keepalive send error: {:?}.", e);
|
||||
break;
|
||||
}
|
||||
ka_time += UDP_KEEPALIVE_GAP;
|
||||
},
|
||||
Ok(Ok(Packet(p))) =>
|
||||
if let Err(e) = udp_tx.send(&p[..]).await {
|
||||
error!("Fatal UDP packet send error: {:?}.", e);
|
||||
break;
|
||||
},
|
||||
Ok(Err(e)) => {
|
||||
error!("Fatal UDP packet receive error: {:?}.", e);
|
||||
break;
|
||||
},
|
||||
Ok(Ok(Poison)) => {
|
||||
break;
|
||||
},
|
||||
}
|
||||
}
|
||||
txer.run().await;
|
||||
|
||||
info!("UDP transmit handle stopped.");
|
||||
}
|
||||
|
||||
#[cfg(feature = "tokio-02-marker")]
|
||||
#[instrument(skip(udp_msg_rx))]
|
||||
pub(crate) async fn runner(udp_msg_rx: Receiver<UdpTxMessage>, ssrc: u32, udp_tx: SendHalf) {
|
||||
info!("UDP transmit handle started.");
|
||||
|
||||
let mut txer = UdpTx {
|
||||
ssrc,
|
||||
rx: udp_msg_rx,
|
||||
udp_tx,
|
||||
};
|
||||
|
||||
txer.run().await;
|
||||
|
||||
info!("UDP transmit handle stopped.");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user