From 7b45f16f063a47dc8a302dce5b016cf43a3edcc1 Mon Sep 17 00:00:00 2001 From: Austin Hellyer Date: Fri, 16 Dec 2016 21:26:54 -0800 Subject: [PATCH] Make 'voice' feature not require 'cache' The voice module required the cache feature in order to access the current user's Id. Instead, just copy the Id into the VoiceManager and distribute it from there -- the memory impact will be very minimal in comparison to the benefits of not needing to constantly unlock the Cache and not needing the user to be forced to use the Cache. --- src/client/gateway/shard.rs | 2 +- src/ext/voice/connection.rs | 3 +++ src/ext/voice/connection_info.rs | 3 +++ src/ext/voice/handler.rs | 9 ++++++--- src/ext/voice/manager.rs | 6 +++--- src/ext/voice/payload.rs | 4 +++- 6 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/client/gateway/shard.rs b/src/client/gateway/shard.rs index c026f0cba01..bc2b8135e3d 100644 --- a/src/client/gateway/shard.rs +++ b/src/client/gateway/shard.rs @@ -149,7 +149,7 @@ impl Shard { session_id: Some(ready.ready.session_id.clone()), shard_info: shard_info, ws_url: base_url.to_owned(), - manager: VoiceManager::new(tx, ready.ready.user.id.0), + manager: VoiceManager::new(tx, ready.ready.user.id), } } else { Shard { diff --git a/src/ext/voice/connection.rs b/src/ext/voice/connection.rs index 373f4ef48b6..3f40d851977 100644 --- a/src/ext/voice/connection.rs +++ b/src/ext/voice/connection.rs @@ -27,6 +27,7 @@ use ::internal::prelude::*; use ::internal::ws_impl::{ReceiverExt, SenderExt}; use ::internal::Timer; use ::model::event::VoiceEvent; +use ::model::UserId; enum ReceiverStatus { Udp(Vec), @@ -59,6 +60,7 @@ pub struct Connection { thread_items: ThreadItems, timestamp: u32, udp: UdpSocket, + user_id: UserId, } impl Connection { @@ -159,6 +161,7 @@ impl Connection { ssrc: hello.ssrc, thread_items: thread_items, timestamp: 0, + user_id: info.user_id, }) } diff --git a/src/ext/voice/connection_info.rs b/src/ext/voice/connection_info.rs index c257e4adec4..282b5f12a0e 100644 --- a/src/ext/voice/connection_info.rs +++ b/src/ext/voice/connection_info.rs @@ -1,7 +1,10 @@ +use ::model::UserId; + #[derive(Clone, Debug)] pub struct ConnectionInfo { pub endpoint: String, pub session_id: String, pub target_id: u64, pub token: String, + pub user_id: UserId, } diff --git a/src/ext/voice/handler.rs b/src/ext/voice/handler.rs index 16f700ae5d9..e9e5cd69725 100644 --- a/src/ext/voice/handler.rs +++ b/src/ext/voice/handler.rs @@ -5,7 +5,7 @@ use super::connection_info::ConnectionInfo; use super::{Status as VoiceStatus, Target}; use ::client::gateway::GatewayStatus; use ::constants::VoiceOpCode; -use ::model::{ChannelId, GuildId, VoiceState}; +use ::model::{ChannelId, GuildId, UserId, VoiceState}; use super::threading; /// The handler is responsible for "handling" a single voice connection, acting @@ -39,7 +39,7 @@ pub struct Handler { self_mute: bool, sender: MpscSender, session_id: Option, - user_id: u64, + user_id: UserId, ws: MpscSender, } @@ -53,7 +53,7 @@ impl Handler { /// /// [`Manager::join`]: struct.Manager.html#method.join #[doc(hidden)] - pub fn new(target: Target, ws: MpscSender, user_id: u64) + pub fn new(target: Target, ws: MpscSender, user_id: UserId) -> Self { let (tx, rx) = mpsc::channel(); @@ -266,11 +266,14 @@ impl Handler { return; }; + let user_id = self.user_id; + self.send(VoiceStatus::Connect(ConnectionInfo { endpoint: endpoint, session_id: session_id, target_id: target_id, token: token, + user_id: user_id, })) } diff --git a/src/ext/voice/manager.rs b/src/ext/voice/manager.rs index b4f3501258a..4e7119592a0 100644 --- a/src/ext/voice/manager.rs +++ b/src/ext/voice/manager.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use std::sync::mpsc::Sender as MpscSender; use super::{Handler, Target}; use ::client::gateway::GatewayStatus; -use ::model::{ChannelId, GuildId}; +use ::model::{ChannelId, GuildId, UserId}; /// A manager is a struct responsible for managing [`Handler`]s which belong to /// a single [WebSocket connection]. This is a fairly complex key-value store, @@ -22,13 +22,13 @@ use ::model::{ChannelId, GuildId}; /// [WebSocket connection]: ../../client/struct.Connection.html pub struct Manager { handlers: HashMap, - user_id: u64, + user_id: UserId, ws: MpscSender, } impl Manager { #[doc(hidden)] - pub fn new(ws: MpscSender, user_id: u64) -> Manager { + pub fn new(ws: MpscSender, user_id: UserId) -> Manager { Manager { handlers: HashMap::new(), user_id: user_id, diff --git a/src/ext/voice/payload.rs b/src/ext/voice/payload.rs index c9ceeeb8fac..45ccf4d19bb 100644 --- a/src/ext/voice/payload.rs +++ b/src/ext/voice/payload.rs @@ -2,6 +2,8 @@ use serde_json::builder::ObjectBuilder; use serde_json::Value; use super::connection_info::ConnectionInfo; use ::constants::VoiceOpCode; + +#[cfg(feature="cache")] use ::client::CACHE; #[inline] @@ -12,7 +14,7 @@ pub fn build_identify(info: &ConnectionInfo) -> Value { .insert("server_id", info.target_id) .insert("session_id", &info.session_id) .insert("token", &info.token) - .insert("user_id", CACHE.read().unwrap().user.id.0)) + .insert("user_id", info.user_id.0)) .build() }