Skip to content

Commit

Permalink
Make 'voice' feature not require 'cache'
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Austin Hellyer committed Dec 17, 2016
1 parent 2416813 commit 7b45f16
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/client/gateway/shard.rs
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions src/ext/voice/connection.rs
Expand Up @@ -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<u8>),
Expand Down Expand Up @@ -59,6 +60,7 @@ pub struct Connection {
thread_items: ThreadItems,
timestamp: u32,
udp: UdpSocket,
user_id: UserId,
}

impl Connection {
Expand Down Expand Up @@ -159,6 +161,7 @@ impl Connection {
ssrc: hello.ssrc,
thread_items: thread_items,
timestamp: 0,
user_id: info.user_id,
})
}

Expand Down
3 changes: 3 additions & 0 deletions 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,
}
9 changes: 6 additions & 3 deletions src/ext/voice/handler.rs
Expand Up @@ -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
Expand Down Expand Up @@ -39,7 +39,7 @@ pub struct Handler {
self_mute: bool,
sender: MpscSender<VoiceStatus>,
session_id: Option<String>,
user_id: u64,
user_id: UserId,
ws: MpscSender<GatewayStatus>,
}

Expand All @@ -53,7 +53,7 @@ impl Handler {
///
/// [`Manager::join`]: struct.Manager.html#method.join
#[doc(hidden)]
pub fn new(target: Target, ws: MpscSender<GatewayStatus>, user_id: u64)
pub fn new(target: Target, ws: MpscSender<GatewayStatus>, user_id: UserId)
-> Self {
let (tx, rx) = mpsc::channel();

Expand Down Expand Up @@ -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,
}))
}

Expand Down
6 changes: 3 additions & 3 deletions src/ext/voice/manager.rs
Expand Up @@ -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,
Expand All @@ -22,13 +22,13 @@ use ::model::{ChannelId, GuildId};
/// [WebSocket connection]: ../../client/struct.Connection.html
pub struct Manager {
handlers: HashMap<Target, Handler>,
user_id: u64,
user_id: UserId,
ws: MpscSender<GatewayStatus>,
}

impl Manager {
#[doc(hidden)]
pub fn new(ws: MpscSender<GatewayStatus>, user_id: u64) -> Manager {
pub fn new(ws: MpscSender<GatewayStatus>, user_id: UserId) -> Manager {
Manager {
handlers: HashMap::new(),
user_id: user_id,
Expand Down
4 changes: 3 additions & 1 deletion src/ext/voice/payload.rs
Expand Up @@ -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]
Expand All @@ -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()
}

Expand Down

0 comments on commit 7b45f16

Please sign in to comment.