Skip to content

Commit

Permalink
feat: add sequence number
Browse files Browse the repository at this point in the history
  • Loading branch information
kozabrada123 committed Dec 17, 2023
1 parent 3875e2e commit 19dc9c8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
19 changes: 7 additions & 12 deletions src/voice/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ impl UdpHandle {
/// Constructs and sends encoded opus rtp data.
///
/// Automatically makes an [RtpPacket](discorrtp::rtp::RtpPacket), encrypts it and sends it.
pub async fn send_opus_data(&self, sequence: u16, timestamp: u32, payload: Vec<u8>) {
let data_lock = self.data.read().await;
let ssrc = data_lock.ready_data.clone().unwrap().ssrc;
pub async fn send_opus_data(&self, timestamp: u32, payload: Vec<u8>) {
let ssrc = self.data.read().await.ready_data.clone().unwrap().ssrc.clone();

Check warning

Code scanning / clippy

using clone on type u32 which implements the Copy trait Warning

using clone on type u32 which implements the Copy trait
let sequence_number = self.data.read().await.last_sequence_number.clone().wrapping_add(1);

Check warning

Code scanning / clippy

using clone on type u16 which implements the Copy trait Warning

using clone on type u16 which implements the Copy trait
self.data.write().await.last_sequence_number = sequence_number;

let payload_len = payload.len();

Expand All @@ -59,14 +60,12 @@ impl UdpHandle {
marker: 0,
payload_type: discortp::rtp::RtpType::Dynamic(120),
// Actually variable
sequence: sequence.into(),
sequence: sequence_number.into(),
timestamp: timestamp.into(),
ssrc,
payload,
};

debug!("VUDP: Constructed udp data: {:?}", rtp_data);

let mut buffer = Vec::new();

let buffer_size = payload_len + RTP_HEADER_SIZE as usize;
Expand Down Expand Up @@ -98,9 +97,7 @@ impl UdpHandle {
) -> Vec<u8> {
let payload = packet.payload();

let data_lock = self.data.read().await;

let session_description_result = data_lock.session_description.clone();
let session_description_result = self.data.read().await.session_description.clone();

if session_description_result.is_none() {
// FIXME: Make this function reutrn a result with a proper error type for these kinds
Expand Down Expand Up @@ -286,9 +283,7 @@ impl UdpHandler {
let ciphertext = buf[12..buf.len()].to_vec();
trace!("VUDP: Parsed packet as rtp!");

let data_lock = self.data.read().await;

let session_description_result = data_lock.session_description.clone();
let session_description_result = self.data.read().await.session_description.clone();

if session_description_result.is_none() {
warn!("VUDP: Received encyrpted voice data, but no encryption key, CANNOT DECRYPT!");
Expand Down
2 changes: 2 additions & 0 deletions src/voice/voice_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ pub struct VoiceData {
pub session_description: Option<SessionDescription>,
pub user_id: Snowflake,
pub session_id: String,
/// The last sequence number we used, has to be incremeted by one every time we send a message
pub last_sequence_number: u16,
pub ip_discovery: Option<IpDiscovery>,
}

0 comments on commit 19dc9c8

Please sign in to comment.