コード例 #1
0
 /// <summary>
 /// Updates the username used for soulseek authentication
 /// </summary>
 /// <param name="username"></param>
 public void SetUsername(string username)
 {
     Config.Instance.SetValue <string>(CONFIG_USERNAME, username);
     if (client != null)
     {
         client.Disconnect();
     }
 }
コード例 #2
0
        public async Task Disconnect_Clears_Searches()
        {
            var c = new Mock <IMessageConnection>();

            using (var s = new SoulseekClient(Guid.NewGuid().ToString(), new Random().Next(), serverConnection: c.Object))
            {
                await s.ConnectAsync();

                using (var search1 = new Search(string.Empty, 0, new SearchOptions()))
                    using (var search2 = new Search(string.Empty, 1, new SearchOptions()))
                    {
                        var searches = new ConcurrentDictionary <int, Search>();
                        searches.TryAdd(0, search1);
                        searches.TryAdd(1, search2);

                        s.SetProperty("Searches", searches);

                        var ex = Record.Exception(() => s.Disconnect());

                        Assert.Null(ex);
                        Assert.Equal(SoulseekClientStates.Disconnected, s.State);
                        Assert.Empty(searches);
                    }
            }
        }
コード例 #3
0
        public async Task Disconnect_Clears_Downloads()
        {
            var c = new Mock <IMessageConnection>();

            var s = new SoulseekClient(Guid.NewGuid().ToString(), new Random().Next(), serverConnection: c.Object);
            await s.ConnectAsync();

            var activeDownloads = new ConcurrentDictionary <int, Download>();

            activeDownloads.TryAdd(0, new Download(string.Empty, string.Empty, 0));
            activeDownloads.TryAdd(1, new Download(string.Empty, string.Empty, 1));

            var queuedDownloads = new ConcurrentDictionary <int, Download>();

            queuedDownloads.TryAdd(0, new Download(string.Empty, string.Empty, 0));
            queuedDownloads.TryAdd(1, new Download(string.Empty, string.Empty, 1));

            s.SetProperty("ActiveDownloads", activeDownloads);
            s.SetProperty("QueuedDownloads", queuedDownloads);

            var ex = Record.Exception(() => s.Disconnect());

            Assert.Null(ex);
            Assert.Equal(SoulseekClientStates.Disconnected, s.State);
            Assert.Empty(activeDownloads);
            Assert.Empty(queuedDownloads);
        }
コード例 #4
0
        public async Task Disconnect_Cancels_Searches()
        {
            var c = new Mock <IMessageConnection>();

            var p = new Mock <IPeerConnectionManager>();

            using (var search = new SearchInternal("foo", 1))
            {
                var searches = new ConcurrentDictionary <int, SearchInternal>();
                searches.TryAdd(1, search);

                using (var s = new SoulseekClient(serverConnection: c.Object, peerConnectionManager: p.Object))
                {
                    s.SetProperty("State", SoulseekClientStates.Connected);
                    s.SetProperty("Searches", searches);

                    var searchWait = search.WaitForCompletion(CancellationToken.None);

                    s.Disconnect();

                    var ex = await Record.ExceptionAsync(() => searchWait);

                    Assert.NotNull(ex);
                    Assert.IsType <OperationCanceledException>(ex);
                }
            }
        }
コード例 #5
0
        public void Disconnect_Clears_Searches()
        {
            var c = new Mock <IMessageConnection>();

            using (var s = new SoulseekClient(serverConnection: c.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected);

                using (var search1 = new SearchInternal(string.Empty, 0, new SearchOptions()))
                    using (var search2 = new SearchInternal(string.Empty, 1, new SearchOptions()))
                    {
                        var searches = new ConcurrentDictionary <int, SearchInternal>();
                        searches.TryAdd(0, search1);
                        searches.TryAdd(1, search2);

                        s.SetProperty("Searches", searches);

                        var ex = Record.Exception(() => s.Disconnect());

                        Assert.Null(ex);
                        Assert.Equal(SoulseekClientStates.Disconnected, s.State);
                        Assert.Empty(searches);
                    }
            }
        }
コード例 #6
0
        public async Task Client_Disconnects()
        {
            var client = new SoulseekClient();
            await client.ConnectAsync();

            var ex = Record.Exception(() => client.Disconnect());

            Assert.Null(ex);
            Assert.Equal(SoulseekClientStates.Disconnected, client.State);
        }
コード例 #7
0
        public async Task Disconnect_Disconnects()
        {
            var c = new Mock <IMessageConnection>();

            var s = new SoulseekClient(Guid.NewGuid().ToString(), new Random().Next(), serverConnection: c.Object);
            await s.ConnectAsync();

            var ex = Record.Exception(() => s.Disconnect());

            Assert.Null(ex);
            Assert.Equal(SoulseekClientStates.Disconnected, s.State);
        }
コード例 #8
0
        public async Task Client_Disconnects()
        {
            using (var client = new SoulseekClient())
            {
                await client.ConnectAsync(Settings.Username, Settings.Password);

                var ex = Record.Exception(() => client.Disconnect());

                Assert.Null(ex);
                Assert.Equal(SoulseekClientStates.Disconnected, client.State);
            }
        }
コード例 #9
0
        public async Task Disconnect_Disconnects()
        {
            var c = new Mock <IMessageConnection>();

            using (var s = new SoulseekClient(serverConnection: c.Object))
            {
                await s.ConnectAsync();

                var ex = Record.Exception(() => s.Disconnect());

                Assert.Null(ex);
                Assert.Equal(SoulseekClientStates.Disconnected, s.State);
            }
        }
コード例 #10
0
        public async Task Client_Disconnect_Raises_StateChanged_Event()
        {
            SoulseekClientStateChangedEventArgs args = null;

            var client = new SoulseekClient();
            await client.ConnectAsync();

            client.StateChanged += (sender, e) => args = e;

            var ex = Record.Exception(() => client.Disconnect());

            Assert.Null(ex);
            Assert.Equal(SoulseekClientStates.Disconnected, client.State);
            Assert.Equal(SoulseekClientStates.Disconnected, args.State);
        }
コード例 #11
0
        public async Task Disconnect_Clears_Peer_Queue()
        {
            var c = new Mock <IMessageConnection>();

            var p = new Mock <IConnectionManager <IMessageConnection> >();

            var s = new SoulseekClient(Guid.NewGuid().ToString(), new Random().Next(), serverConnection: c.Object, peerConnectionManager: p.Object);
            await s.ConnectAsync();

            var ex = Record.Exception(() => s.Disconnect());

            Assert.Null(ex);
            Assert.Equal(SoulseekClientStates.Disconnected, s.State);

            p.Verify(m => m.RemoveAll(), Times.AtLeastOnce);
        }
コード例 #12
0
        public void Disconnect_Clears_Peer_Queue()
        {
            var c = new Mock <IMessageConnection>();

            var p = new Mock <IPeerConnectionManager>();

            using (var s = new SoulseekClient(Guid.NewGuid().ToString(), new Random().Next(65535), serverConnection: c.Object, peerConnectionManager: p.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected);

                var ex = Record.Exception(() => s.Disconnect());

                Assert.Null(ex);
                Assert.Equal(SoulseekClientStates.Disconnected, s.State);

                p.Verify(m => m.RemoveAndDisposeAll(), Times.AtLeastOnce);
            }
        }
コード例 #13
0
        public void Disconnect_Raises_StateChanged_Event()
        {
            var fired = false;

            var c = new Mock <IMessageConnection>();

            var s = new SoulseekClient(Guid.NewGuid().ToString(), new Random().Next(), serverConnection: c.Object);

            s.StateChanged += (sender, e) => fired = true;

            s.SetProperty("State", ConnectionState.Connected);

            var ex = Record.Exception(() => s.Disconnect());

            Assert.Null(ex);
            Assert.Equal(SoulseekClientStates.Disconnected, s.State);

            Assert.True(fired);
        }
コード例 #14
0
        public async Task Disconnect_Clears_Downloads()
        {
            var c = new Mock <IMessageConnection>();

            var s = new SoulseekClient(Guid.NewGuid().ToString(), new Random().Next(), serverConnection: c.Object);
            await s.ConnectAsync();

            var downloads = new ConcurrentDictionary <int, Transfer>();

            downloads.TryAdd(0, new Transfer(TransferDirection.Download, string.Empty, string.Empty, 0));
            downloads.TryAdd(1, new Transfer(TransferDirection.Download, string.Empty, string.Empty, 1));

            s.SetProperty("Downloads", downloads);

            var ex = Record.Exception(() => s.Disconnect());

            Assert.Null(ex);
            Assert.Equal(SoulseekClientStates.Disconnected, s.State);
            Assert.Empty(downloads);
        }
コード例 #15
0
        public void Disconnect_Clears_Downloads()
        {
            var c = new Mock <IMessageConnection>();

            using (var s = new SoulseekClient(serverConnection: c.Object))
            {
                s.SetProperty("State", SoulseekClientStates.Connected);

                var downloads = new ConcurrentDictionary <int, TransferInternal>();
                downloads.TryAdd(0, new TransferInternal(TransferDirection.Download, string.Empty, string.Empty, 0));
                downloads.TryAdd(1, new TransferInternal(TransferDirection.Download, string.Empty, string.Empty, 1));

                s.SetProperty("Downloads", downloads);

                var ex = Record.Exception(() => s.Disconnect());

                Assert.Null(ex);
                Assert.Equal(SoulseekClientStates.Disconnected, s.State);
                Assert.NotEmpty(downloads);
            }
        }
コード例 #16
0
        static async Task Main(string[] args)
        {
            using (var client = new SoulseekClient(new SoulseekClientOptions(minimumDiagnosticLevel: DiagnosticLevel.Debug)))
            {
                client.StateChanged            += Client_ServerStateChanged;
                client.SearchResponseReceived  += Client_SearchResponseReceived;
                client.SearchStateChanged      += Client_SearchStateChanged;
                client.DownloadProgressUpdated += Client_DownloadProgress;
                client.DownloadStateChanged    += Client_DownloadStateChanged;
                client.DiagnosticGenerated     += Client_DiagnosticMessageGenerated;
                client.PrivateMessageReceived  += Client_PrivateMessageReceived;

                await client.ConnectAsync();

                Console.WriteLine("Enter username and password:"******"disconnect")
                    {
                        client.Disconnect();
                        return;
                    }
                    else if (cmd.StartsWith("msg"))
                    {
                        var arr = cmd.Split(' ');

                        var peer    = arr.Skip(1).Take(1).FirstOrDefault();
                        var message = arr.Skip(2).Take(999);

                        await client.SendPrivateMessageAsync(peer, string.Join(' ', message));
                    }
                    else if (cmd.StartsWith("browse"))
                    {
                        var peer   = cmd.Split(' ').Skip(1).FirstOrDefault();
                        var result = await client.BrowseAsync(peer);

                        Console.WriteLine(JsonConvert.SerializeObject(result));
                        continue;
                    }
                    else if (cmd.StartsWith("search"))
                    {
                        using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(300)))
                        {
                            var search = string.Join(' ', cmd.Split(' ').Skip(1));
                            var token  = new Random().Next();
                            var result = await client.SearchAsync(search, token, new SearchOptions(
                                                                      filterFiles : false,
                                                                      filterResponses : false,
                                                                      fileLimit : 10000), cts.Token);

                            Console.WriteLine(JsonConvert.SerializeObject(result));
                            continue;
                        }
                    }
                    else if (cmd.StartsWith("download-folder"))
                    {
                        var peer = cmd.Split(' ').Skip(1).FirstOrDefault();

                        var files = new[]
                        {
                            @"@@djpnk\\Bootlegs\\Fear Is Your Only God\\01 - Bulls On Parade.mp3",
                            @"@@djpnk\\Bootlegs\\Fear Is Your Only God\\02 - Down Rodeo.mp3",
                            @"@@djpnk\\Bootlegs\\Fear Is Your Only God\\03 - People Of The Sun.mp3",
                            @"@@djpnk\\Bootlegs\\Fear Is Your Only God\\04 - Revolver.mp3",
                            @"@@djpnk\\Bootlegs\\Fear Is Your Only God\\05 - Roll Right.mp3",
                            @"@@djpnk\\Bootlegs\\Fear Is Your Only God\\06 - Snakecharmer.mp3",
                            @"@@djpnk\\Bootlegs\\Fear Is Your Only God\\07 - Tire Me.mp3",
                            @"@@djpnk\\Bootlegs\\Fear Is Your Only God\\08 - Vietnow.mp3",
                            @"@@djpnk\\Bootlegs\\Fear Is Your Only God\\09 - Wind Below.mp3",
                            @"@@djpnk\\Bootlegs\\Fear Is Your Only God\\10 - Without A Face.mp3",
                            @"@@djpnk\\Bootlegs\\Fear Is Your Only God\\11 - Year Of The Boomerang.mp3",
                            @"@@djpnk\\Bootlegs\\Fear Is Your Only God\\Thumbs.db",
                            @"@@djpnk\\Bootlegs\\Fear Is Your Only God\\album.nfo",
                        };

                        var task = Task.Run(() =>
                        {
                            var random = new Random();

                            Parallel.ForEach(files, async(file) =>
                            {
                                Console.WriteLine($"Attempting to download {file}");
                                var bytes    = await client.DownloadAsync(peer, file, random.Next());
                                var filename = $@"C:\tmp\{Path.GetFileName(file)}";

                                Console.WriteLine($"Bytes received: {bytes.Length}; writing to file {filename}...");
                                System.IO.File.WriteAllBytes(filename, bytes);
                                Console.WriteLine("Download complete!");
                            });
                        });

                        await task;

                        Console.WriteLine($"All files complete.");
                    }
                    else if (cmd.StartsWith("download"))
                    {
                        var peer = cmd.Split(' ').Skip(1).FirstOrDefault();
                        var file = string.Join(' ', cmd.Split(' ').Skip(2));

                        var bytes = await client.DownloadAsync(peer, file, new Random().Next());

                        var filename = $@"C:\tmp\{Path.GetFileName(file)}";

                        Console.WriteLine($"Bytes received: {bytes.Length}; writing to file {filename}...");
                        System.IO.File.WriteAllBytes(filename, bytes);
                        Console.WriteLine("Download complete!");
                    }
                    else
                    {
                        try
                        {
                            await client.LoginAsync(cmd.Split(' ')[0], cmd.Split(' ')[1]);

                            Console.WriteLine($"Logged in.");
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine($"Login failed: {ex.Message}");
                        }
                    }
                }
            }
        }