Example #1
0
        /// <summary>
        ///     Creates and returns a <see cref="Soulseek.Directory"/> in response to a remote request.
        /// </summary>
        /// <param name="username">The username of the requesting user.</param>
        /// <param name="endpoint">The IP endpoint of the requesting user.</param>
        /// <param name="token">The unique token for the request, supplied by the requesting user.</param>
        /// <param name="directory">The requested directory.</param>
        /// <returns>A Task resolving an instance of Soulseek.Directory containing the contents of the requested directory.</returns>
        private Task <Soulseek.Directory> DirectoryContentsResponseResolver(string username, IPEndPoint endpoint, int token, string directory)
        {
            var result = new Soulseek.Directory(directory, System.IO.Directory.GetFiles(directory)
                                                .Select(f => new Soulseek.File(1, Path.GetFileName(f), new FileInfo(f).Length, Path.GetExtension(f), 0)));

            return(Task.FromResult(result));
        }
Example #2
0
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;

            Username        = Configuration.GetValue <string>("USERNAME");
            Password        = Configuration.GetValue <string>("PASSWORD");
            WebRoot         = Configuration.GetValue <string>("WEBROOT");
            ListenPort      = Configuration.GetValue <int>("LISTEN_PORT");
            OutputDirectory = Configuration.GetValue <string>("OUTPUT_DIR");
            SharedDirectory = Configuration.GetValue <string>("SHARED_DIR");

            SharedDirectory = @"\\WSE\Music\Processed\Rage Against the Machine\Bootlegs\Killing Your Enemy In 1995";

            var options = new ClientOptions(
                listenPort: ListenPort,
                concurrentDistributedChildrenLimit: 10,
                minimumDiagnosticLevel: DiagnosticLevel.Debug,
                concurrentPeerMessageConnectionLimit: 1000000,
                serverConnectionOptions: new ConnectionOptions(inactivityTimeout: 15),
                peerConnectionOptions: new ConnectionOptions(inactivityTimeout: 5),
                transferConnectionOptions: new ConnectionOptions(inactivityTimeout: 5),
                userInfoResponseResolver: (u, i, p) =>
            {
                var info = new UserInfoResponse(
                    description: $"i'm a test! also, your username is {u}, IP address is {i}, and the port on which you connected to me is {p}",
                    picture: System.IO.File.ReadAllBytes(@"etc/slsk_bird.jpg"),
                    uploadSlots: 0,
                    queueLength: 0,
                    hasFreeUploadSlot: false);

                return(Task.FromResult(info));
            },
                browseResponseResolver: (u, i, p) =>
            {
                // limited to just the root for now
                var files = System.IO.Directory.GetFiles(SharedDirectory)
                            .Select(f => new Soulseek.File(1, Path.GetFileName(f), new FileInfo(f).Length, Path.GetExtension(f), 0));

                var dir = new Soulseek.Directory(SharedDirectory, files.Count(), files);

                return(Task.FromResult(new BrowseResponse(1, new List <Soulseek.Directory>()
                {
                    dir
                })));
            }, queueDownloadAction: (u, i, p, f) =>
            {
                Console.WriteLine($"Dispositioning {f}");

                Task.Run(async() => await Client.UploadAsync(u, f, System.IO.File.ReadAllBytes(f)))
                .ContinueWith(t => { throw (Exception)Activator.CreateInstance(typeof(Exception), t.Exception.Message, t.Exception); }, TaskContinuationOptions.OnlyOnFaulted);

                return(Task.CompletedTask);
            }, searchResponseResolver: (u, t, q) =>
            {
                //Console.WriteLine($"Search request: {q}");

                if (q == "killing your enemy in 1995")
                {
                    var files = System.IO.Directory.GetFiles(SharedDirectory)
                                .Select(f => new Soulseek.File(1, f, new FileInfo(f).Length, Path.GetExtension(f), 0));

                    return(Task.FromResult(new SearchResponse(Username, t, files.Count(), 0, 0, 0, files)));
                }

                return(Task.FromResult <SearchResponse>(null));
            });

            Client = new SoulseekClient(options: options);
            Client.DiagnosticGenerated += (e, args) =>
            {
                lock (ConsoleSyncRoot)
                {
                    if (args.Level == DiagnosticLevel.Debug)
                    {
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                    }
                    if (args.Level == DiagnosticLevel.Warning)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                    }

                    Console.WriteLine($"[DIAGNOSTIC:{e.GetType().Name}] [{args.Level}] {args.Message}");
                    Console.ResetColor();
                }
            };

            Client.TransferStateChanged += (e, args) => Console.WriteLine($"[{args.Direction.ToString().ToUpper()}] [{args.Username}/{Path.GetFileName(args.Filename)}] {args.PreviousState} => {args.State}");
            Client.UserStatusChanged    += (e, args) => Console.WriteLine($"[USER] {args.Username}: {args.Status}");
            //Client.TransferProgressUpdated += (e, args) => Console.WriteLine($"[{args.Direction.ToString().ToUpper()}] [{args.Username}/{Path.GetFileName(args.Filename)}] {args.PercentComplete} {args.AverageSpeed}kb/s");
        }
Example #3
0
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;

            Username        = Configuration.GetValue <string>("USERNAME");
            Password        = Configuration.GetValue <string>("PASSWORD");
            WebRoot         = Configuration.GetValue <string>("WEBROOT");
            ListenPort      = Configuration.GetValue <int>("LISTEN_PORT");
            OutputDirectory = Configuration.GetValue <string>("OUTPUT_DIR");
            SharedDirectory = Configuration.GetValue <string>("SHARED_DIR");

            SharedDirectory = @"\\WSE\Music\Processed\Rage Against the Machine\Bootlegs\Killing Your Enemy In 1995";

            var resolvers = new SoulseekClientResolvers(
                browseResponse: (u, i, p) =>
            {
                // limited to just the root for now
                var files = System.IO.Directory.GetFiles(SharedDirectory)
                            .Select(f => new Soulseek.File(1, Path.GetFileName(f), new FileInfo(f).Length, Path.GetExtension(f), 0));

                var dir = new Soulseek.Directory(SharedDirectory, files.Count(), files);

                return(new BrowseResponse(1, new List <Soulseek.Directory>()
                {
                    dir
                }));
            }, queueDownloadResponse: (u, i, p, f) =>
            {
                Console.WriteLine($"Dispositioning {f}");
                //var file = $"The quick brown fox jumps over the lazy dog {System.IO.Path.GetFileName(f)}";
                Task.Run(async() => await Client.UploadAsync(u, f, System.IO.File.ReadAllBytes(f)))
                .ContinueWith(t => { throw (Exception)Activator.CreateInstance(typeof(Exception), t.Exception.Message, t.Exception); }, TaskContinuationOptions.OnlyOnFaulted);

                return(true, null);
            });

            var options = new SoulseekClientOptions(
                listenPort: ListenPort,
                minimumDiagnosticLevel: DiagnosticLevel.Debug,
                concurrentPeerMessageConnectionLimit: 1000000,
                serverConnectionOptions: new ConnectionOptions(inactivityTimeout: 15),
                peerConnectionOptions: new ConnectionOptions(inactivityTimeout: 5),
                transferConnectionOptions: new ConnectionOptions(inactivityTimeout: 5));

            Client = new SoulseekClient(resolvers: resolvers, options: options);
            Client.DiagnosticGenerated += (e, args) =>
            {
                if (args.Level == DiagnosticLevel.Debug)
                {
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                }
                if (args.Level == DiagnosticLevel.Warning)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                }

                Console.WriteLine($"[DIAGNOSTIC:{e.GetType().Name}] [{args.Level}] {args.Message}");
                Console.ResetColor();
            };

            Client.TransferStateChanged += (e, args) => Console.WriteLine($"[{args.Direction.ToString().ToUpper()}] [{args.Username}/{Path.GetFileName(args.Filename)}] {args.PreviousState} => {args.State}");
            Client.UserStatusChanged    += (e, args) => Console.WriteLine($"[USER] {args.Username}: {args.Status}");
            //Client.TransferProgressUpdated += (e, args) => Console.WriteLine($"[{args.Direction.ToString().ToUpper()}] [{args.Username}/{Path.GetFileName(args.Filename)}] {args.PercentComplete} {args.AverageSpeed}kb/s");
        }