aboutsummaryrefslogtreecommitdiff
path: root/src/voice/connection.rs
diff options
context:
space:
mode:
authorZeyla Hellyer <[email protected]>2017-11-19 09:53:59 -0800
committerZeyla Hellyer <[email protected]>2017-11-19 09:53:59 -0800
commitc3aa63faee8b3ae6d5126aa27a74876766c61e4c (patch)
tree560ae5fcf011a8bb7cbaa5e4a5fc3bb2ee89ee31 /src/voice/connection.rs
parentAdd `model::Reaction::user` (diff)
downloadserenity-c3aa63faee8b3ae6d5126aa27a74876766c61e4c.tar.xz
serenity-c3aa63faee8b3ae6d5126aa27a74876766c61e4c.zip
Implement Deserialize for {,Gateway,Voice}Event
Implement Deserialize for `model::event::GatewayEvent` and `model::event::VoiceEvent`, and derive it for `model::event::Event`. Due to the natural potential slowness of deserializing into`Event` (attempting to deserialize into each variant until successful), a function named `model::event::deserialize_event_with_type` is provided for quickly deserializing into a known type if the dispatch type is known.
Diffstat (limited to 'src/voice/connection.rs')
-rw-r--r--src/voice/connection.rs35
1 files changed, 27 insertions, 8 deletions
diff --git a/src/voice/connection.rs b/src/voice/connection.rs
index 12c7eee..9b9c68f 100644
--- a/src/voice/connection.rs
+++ b/src/voice/connection.rs
@@ -12,6 +12,7 @@ use opus::{
Encoder as OpusEncoder,
};
use parking_lot::Mutex;
+use serde::Deserialize;
use sodiumoxide::crypto::secretbox::{self, Key, Nonce};
use std::collections::HashMap;
use std::io::Write;
@@ -69,15 +70,19 @@ impl Connection {
let url = generate_url(&mut info.endpoint)?;
let mut client = ClientBuilder::from_url(&url).connect_secure(None)?;
-
client.send_json(&payload::build_identify(&info))?;
let hello = loop {
- match client.recv_json(VoiceEvent::decode)? {
- Some(VoiceEvent::Hello(received_hello)) => {
+ let value = match client.recv_json()? {
+ Some(value) => value,
+ None => continue,
+ };
+
+ match VoiceEvent::deserialize(value)? {
+ VoiceEvent::Hello(received_hello) => {
break received_hello;
},
- Some(VoiceEvent::Heartbeat(_)) => continue,
+ VoiceEvent::Heartbeat(_) => continue,
other => {
debug!("[Voice] Expected hello/heartbeat; got: {:?}", other);
@@ -382,8 +387,13 @@ fn generate_url(endpoint: &mut String) -> Result<WebsocketUrl> {
#[inline]
fn encryption_key(client: &mut Client) -> Result<Key> {
loop {
- match client.recv_json(VoiceEvent::decode)? {
- Some(VoiceEvent::Ready(ready)) => {
+ let value = match client.recv_json()? {
+ Some(value) => value,
+ None => continue,
+ };
+
+ match VoiceEvent::deserialize(value)? {
+ VoiceEvent::Ready(ready) => {
if ready.mode != CRYPTO_MODE {
return Err(Error::Voice(VoiceError::VoiceModeInvalid));
}
@@ -391,7 +401,7 @@ fn encryption_key(client: &mut Client) -> Result<Key> {
return Key::from_slice(&ready.secret_key)
.ok_or(Error::Voice(VoiceError::KeyGen));
},
- Some(VoiceEvent::Unknown(op, value)) => {
+ VoiceEvent::Unknown(op, value) => {
debug!(
"[Voice] Expected ready for key; got: op{}/v{:?}",
op.num(),
@@ -447,7 +457,16 @@ fn start_threads(client: Arc<Mutex<Client>>, udp: &UdpSocket) -> Result<ThreadIt
let ws_thread = ThreadBuilder::new()
.name(format!("{} WS", thread_name))
.spawn(move || loop {
- while let Ok(Some(msg)) = client.lock().recv_json(VoiceEvent::decode) {
+ while let Ok(Some(value)) = client.lock().recv_json() {
+ let msg = match VoiceEvent::deserialize(value) {
+ Ok(msg) => msg,
+ Err(why) => {
+ warn!("Error deserializing voice event: {:?}", why);
+
+ break;
+ },
+ };
+
if tx_clone.send(ReceiverStatus::Websocket(msg)).is_ok() {
return;
}