Example #1
0
        public async Task DownloadAsync_Throws_ArgumentException_Given_Bad_Filename(string filename)
        {
            var s = new SoulseekClient();

            var ex = await Record.ExceptionAsync(async() => await s.DownloadAsync("username", filename));

            Assert.NotNull(ex);
            Assert.IsType <ArgumentException>(ex);
        }
Example #2
0
        public async Task DownloadAsync_Throws_InvalidOperationException_When_Not_Connected()
        {
            var s = new SoulseekClient();

            var ex = await Record.ExceptionAsync(async() => await s.DownloadAsync("username", "filename"));

            Assert.NotNull(ex);
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Contains("Connected", ex.Message, StringComparison.InvariantCultureIgnoreCase);
        }
Example #3
0
        public async Task DownloadAsync_Throws_DownloadException_On_Peer_Message_Connection_Timeout()
        {
            var options = new SoulseekClientOptions(messageTimeout: 1);

            var s = new SoulseekClient("127.0.0.1", 1, options);

            s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

            var ex = await Record.ExceptionAsync(async() => await s.DownloadAsync("username", "filename"));

            Assert.NotNull(ex);
            Assert.IsType <DownloadException>(ex);
            Assert.IsType <TimeoutException>(ex.InnerException);
        }
Example #4
0
        public async Task DownloadAsync_Throws_ArgumentException_When_Token_Used()
        {
            var s = new SoulseekClient();

            s.SetProperty("State", SoulseekClientStates.Connected | SoulseekClientStates.LoggedIn);

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

            queued.TryAdd(1, new Download("foo", "bar", 1));

            s.SetProperty("QueuedDownloads", queued);

            var ex = await Record.ExceptionAsync(async() => await s.DownloadAsync("username", "filename", 1));

            Assert.NotNull(ex);
            Assert.IsType <ArgumentException>(ex);
            Assert.Contains("token", ex.Message, StringComparison.InvariantCultureIgnoreCase);
        }
Example #5
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}");
                        }
                    }
                }
            }
        }