示例#1
0
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                // Dispose managed
            }

            // Dispose unmanaged
            if (!IsInvalid)
            {
                try
                {
                    _mainThreadNotification.Set();

                    lock (_eventQueueLock)
                    {
                        Monitor.Pulse(_eventQueueLock);
                    }

                    if (_callbacks != null)
                    {
                        _callbacks.Dispose();
                        _callbacks = null;
                    }

                    PlaylistTrackManager.RemoveAll(this);
                    TrackManager.DisposeAll(this);

                    LinkManager.RemoveAll(this);
                    UserManager.RemoveAll(this);

                    PlaylistContainerManager.RemoveAll(this);
                    PlaylistManager.RemoveAll(this);
                    ContainerPlaylistManager.RemoveAll(this);
                    ArtistManager.RemoveAll();
                    AlbumManager.RemoveAll();

                    SessionManager.Remove(Handle);

                    lock (Spotify.Mutex)
                    {
                        Error error = Error.OK;

                        if (ConnectionState == ConnectionState.LoggedIn)
                        {
                            error = Spotify.sp_session_logout(Handle);
                            Debug.WriteLineIf(error != Error.OK, error.GetMessage());
                        }

                        error = Spotify.sp_session_release(Handle);
                        Debug.WriteLineIf(error != Error.OK, error.GetMessage());
                        Handle = IntPtr.Zero;
                    }
                }
                catch
                {
                    // Ignore
                }
                finally
                {
                    Debug.WriteLine("Session disposed");
                }
            }

            base.Dispose(disposing);
        }
示例#2
0
        public override void Initialize()
        {
            _callbacks = new NativeSessionCallbacks(this);

            if (!string.IsNullOrEmpty(_options.SettingsLocation))
            {
                Directory.CreateDirectory(_options.SettingsLocation);
            }

            var sessionConfig = new Spotify.SpotifySessionConfig
            {
                ApiVersion                   = Spotify.SPOTIFY_API_VERSION,
                CacheLocation                = _options.CacheLocation,
                SettingsLocation             = _options.SettingsLocation,
                UserAgent                    = _options.UserAgent,
                CompressPlaylists            = _options.CompressPlaylists,
                DontSaveMetadataForPlaylists = _options.DontSavePlaylistMetadata,
                InitiallyUnloadPlaylists     = _options.InitiallyUnloadPlaylists,
                ApplicationKey               = Marshal.AllocHGlobal(_applicationKey.Length),
                ApplicationKeySize           = _applicationKey.Length,
                Callbacks                    = _callbacks.CallbackHandle,
                DeviceID      = _options.DeviceID,
                TraceFile     = _options.TraceFileLocation,
                Proxy         = _options.Proxy,
                ProxyUsername = _options.ProxyUsername,
                ProxyPassword = _options.ProxyPassword
            };

            try
            {
                Marshal.Copy(_applicationKey, 0, sessionConfig.ApplicationKey, _applicationKey.Length);

                lock (Spotify.Mutex)
                {
                    IntPtr sessionPtr;
                    Error  res = Spotify.sp_session_create(ref sessionConfig, out sessionPtr);

                    if (res != Error.OK)
                    {
                        throw new TorshifyException(res.GetMessage(), res);
                    }

                    Handle = sessionPtr;
                }
            }
            finally
            {
                if (sessionConfig.ApplicationKey != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(sessionConfig.ApplicationKey);
                    sessionConfig.ApplicationKey = IntPtr.Zero;
                }
            }

            _mainThreadNotification  = new AutoResetEvent(false);
            _mainThread              = new Thread(MainThreadLoop);
            _mainThread.Name         = "MainLoop";
            _mainThread.IsBackground = true;
            _mainThread.Start();

            _eventQueue = new Queue <DelegateInvoker>();

            _eventThread              = new Thread(EventThreadLoop);
            _eventThread.Name         = "EventLoop";
            _eventThread.IsBackground = true;
            _eventThread.Start();

            AppDomain.CurrentDomain.ProcessExit += OnHostProcessExit;
        }
示例#3
0
        public override void Initialize()
        {
            _callbacks = new NativeSessionCallbacks(this);

            if (!string.IsNullOrEmpty(_options.SettingsLocation))
            {
                Directory.CreateDirectory(_options.SettingsLocation);
            }

            var sessionConfig = new Spotify.SpotifySessionConfig
            {
                ApiVersion = Spotify.SPOTIFY_API_VERSION,
                CacheLocation = _options.CacheLocation,
                SettingsLocation = _options.SettingsLocation,
                UserAgent = _options.UserAgent,
                CompressPlaylists = _options.CompressPlaylists,
                DontSaveMetadataForPlaylists = _options.DontSavePlaylistMetadata,
                InitiallyUnloadPlaylists = _options.InitiallyUnloadPlaylists,
                ApplicationKey = Marshal.AllocHGlobal(_applicationKey.Length),
                ApplicationKeySize = _applicationKey.Length,
                Callbacks = _callbacks.CallbackHandle,
                DeviceID = _options.DeviceID,
                TraceFile = _options.TraceFileLocation
            };

            try
            {
                Marshal.Copy(_applicationKey, 0, sessionConfig.ApplicationKey, _applicationKey.Length);

                lock (Spotify.Mutex)
                {
                    IntPtr sessionPtr;
                    Error res = Spotify.sp_session_create(ref sessionConfig, out sessionPtr);

                    if (res != Error.OK)
                    {
                        throw new Exception(res.GetMessage());
                    }

                    Handle = sessionPtr;
                }
            }
            finally
            {
                if (sessionConfig.ApplicationKey != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(sessionConfig.ApplicationKey);
                    sessionConfig.ApplicationKey = IntPtr.Zero;
                }
            }

            _mainThreadNotification = new AutoResetEvent(false);
            _mainThread = new Thread(MainThreadLoop);
            _mainThread.Name = "MainLoop";
            _mainThread.IsBackground = true;
            _mainThread.Start();

            _eventQueue = new Queue<DelegateInvoker>();

            _eventThread = new Thread(EventThreadLoop);
            _eventThread.Name = "EventLoop";
            _eventThread.IsBackground = true;
            _eventThread.Start();

            AppDomain.CurrentDomain.ProcessExit += OnHostProcessExit;
        }
示例#4
0
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                // Dispose managed
            }

            // Dispose unmanaged
            if (!IsInvalid)
            {
                try
                {
                    _mainThreadNotification.Set();

                    lock (_eventQueueLock)
                    {
                        Monitor.Pulse(_eventQueueLock);
                    }

                    if (_callbacks != null)
                    {
                        _callbacks.Dispose();
                        _callbacks = null;
                    }

                    PlaylistTrackManager.RemoveAll(this);
                    TrackManager.DisposeAll(this);

                    LinkManager.RemoveAll(this);
                    UserManager.RemoveAll(this);

                    PlaylistContainerManager.RemoveAll(this);
                    PlaylistManager.RemoveAll(this);
                    ContainerPlaylistManager.RemoveAll(this);
                    ArtistManager.RemoveAll();
                    AlbumManager.RemoveAll();

                    SessionManager.Remove(Handle);

                    lock (Spotify.Mutex)
                    {
                        if (ConnectionState == ConnectionState.LoggedIn)
                        {
                            try
                            {
                                Spotify.sp_session_logout(Handle);
                            }
                            catch
                            {
                            }
                        }

                        Spotify.sp_session_release(Handle);
                        Handle = IntPtr.Zero;
                    }
                }
                catch
                {
                    // Ignore
                }
                finally
                {
                    Debug.WriteLine("Session disposed");
                }
            }

            base.Dispose(disposing);
        }
示例#5
0
        public override void Initialize()
        {
            _callbacks = new NativeSessionCallbacks(this);

            var sessionConfig = new Spotify.SpotifySessionConfig
            {
                ApiVersion = Spotify.SPOTIFY_API_VERSION,
                CacheLocation = _cacheLocation,
                SettingsLocation = _settingsLocation,
                UserAgent = _userAgent,
                CompressPlaylists = false,
                DontSaveMetadataForPlaylists = false,
                InitiallyUnloadPlaylists = false,
                ApplicationKey = Marshal.AllocHGlobal(_applicationKey.Length),
                ApplicationKeySize = _applicationKey.Length,
                Callbacks = _callbacks.CallbackHandle
            };

            try
            {
                Marshal.Copy(_applicationKey, 0, sessionConfig.ApplicationKey, _applicationKey.Length);

                lock (Spotify.Mutex)
                {
                    IntPtr sessionPtr;
                    Error res = Spotify.sp_session_create(ref sessionConfig, out sessionPtr);

                    if (res != Error.OK)
                    {
                        throw new Exception(res.GetMessage());
                    }

                    Handle = sessionPtr;
                }
            }
            finally
            {
                if (sessionConfig.ApplicationKey != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(sessionConfig.ApplicationKey);
                    sessionConfig.ApplicationKey = IntPtr.Zero;
                }
            }

            _friends = new DelegateArray<IUser>(GetNumberOfFriends, GetFriendAtIndex);

            _mainThreadNotification = new AutoResetEvent(false);
            _mainThread = new Thread(MainThreadLoop);
            _mainThread.Name = "MainLoop";
            _mainThread.IsBackground = true;
            _mainThread.Start();

            _eventQueue = new Queue<DelegateInvoker>(2000);

            _eventThreadNotification = new AutoResetEvent(false);
            _eventThread = new Thread(EventThreadLoop);
            _eventThread.Name = "EventLoop";
            _eventThread.IsBackground = true;
            _eventThread.Start();

            AppDomain.CurrentDomain.ProcessExit += OnHostProcessExit;
        }