From 40b474bf0aae502b6194c90644880e3562196224 Mon Sep 17 00:00:00 2001 From: johannesd3 Date: Wed, 26 May 2021 16:08:30 +0200 Subject: [PATCH] Removed modifications in session.rs --- core/src/lib.rs | 1 + core/src/session.rs | 79 +++++++++++++-------------------------------- 2 files changed, 23 insertions(+), 57 deletions(-) diff --git a/core/src/lib.rs b/core/src/lib.rs index ce89464b6..c6f6e190b 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -14,6 +14,7 @@ pub mod cache; pub mod channel; pub mod config; mod connection; +#[allow(dead_code)] mod dealer; #[doc(hidden)] pub mod diffie_hellman; diff --git a/core/src/session.rs b/core/src/session.rs index e11c61f4e..f43a4cc02 100644 --- a/core/src/session.rs +++ b/core/src/session.rs @@ -1,4 +1,3 @@ -use std::convert::Infallible; use std::future::Future; use std::io; use std::pin::Pin; @@ -16,7 +15,6 @@ use once_cell::sync::OnceCell; use thiserror::Error; use tokio::sync::mpsc; use tokio_stream::wrappers::UnboundedReceiverStream; -use url::Url; use crate::apresolve::apresolve; use crate::audio_key::AudioKeyManager; @@ -25,8 +23,6 @@ use crate::cache::Cache; use crate::channel::ChannelManager; use crate::config::SessionConfig; use crate::connection::{self, AuthenticationError}; -use crate::dealer::{Builder, Dealer}; -use crate::keymaster; use crate::mercury::MercuryManager; #[derive(Debug, Error)] @@ -53,8 +49,6 @@ struct SessionInternal { audio_key: OnceCell, channel: OnceCell, mercury: OnceCell, - dealer: tokio::sync::OnceCell, - cache: Option>, handle: tokio::runtime::Handle, @@ -85,7 +79,25 @@ impl Session { cache.save_credentials(&reusable_credentials); } - let (sink, stream) = conn.split(); + let session = Session::create( + conn, + config, + cache, + reusable_credentials.username, + tokio::runtime::Handle::current(), + ); + + Ok(session) + } + + fn create( + transport: connection::Transport, + config: SessionConfig, + cache: Option, + username: String, + handle: tokio::runtime::Handle, + ) -> Session { + let (sink, stream) = transport.split(); let (sender_tx, sender_rx) = mpsc::unbounded_channel(); let session_id = SESSION_COUNTER.fetch_add(1, Ordering::Relaxed); @@ -96,7 +108,7 @@ impl Session { config, data: RwLock::new(SessionData { country: String::new(), - canonical_username: reusable_credentials.username, + canonical_username: username, invalid: false, time_delta: 0, }), @@ -105,8 +117,7 @@ impl Session { audio_key: OnceCell::new(), channel: OnceCell::new(), mercury: OnceCell::new(), - dealer: tokio::sync::OnceCell::new(), - handle: tokio::runtime::Handle::current(), + handle, session_id, })); @@ -123,10 +134,7 @@ impl Session { } }); - // TODO: Initialize the dealer where it makes sense. - let _ = session.dealer().await; - - Ok(session) + session } pub fn audio_key(&self) -> &AudioKeyManager { @@ -147,48 +155,6 @@ impl Session { .get_or_init(|| MercuryManager::new(self.weak())) } - pub async fn dealer(&self) -> Result<&Dealer, ()> { - self.0 - .dealer - .get_or_try_init(|| { - let weak = self.weak(); - let builder = Builder::new(); - builder.launch( - move || { - let session = weak.try_upgrade(); - - async move { - let session = if let Some(session) = session { - session - } else { - // Just don't finish and hope that someone will come along and clean up the Dealer. - futures_util::future::pending::().await; - unreachable!() - }; - - // TODO: How to handle errors here? - - let token = keymaster::get_token( - &session, - "65b708073fc0480ea92a077233ca87bd", - "playlist-read", - ) - .await - .unwrap(); - - let mut url: Url = "wss://dealer.spotify.com:443/".parse().unwrap(); - url.query_pairs_mut() - .append_pair("access_token", &token.access_token); - url - } - }, - self.0.config.proxy.clone(), - ) - }) - .await - .map_err(|_| ()) - } - pub fn time_delta(&self) -> i64 { self.0.data.read().unwrap().time_delta } @@ -277,7 +243,6 @@ impl Session { self.0.data.write().unwrap().invalid = true; self.mercury().shutdown(); self.channel().shutdown(); - // TODO: Enable async shutdown to shutdown dealer properly. } pub fn is_invalid(&self) -> bool {