Exemple #1
0
        private int InternalExecute()
        {
            if (SendOption.HasValue() && ReceiveOption.HasValue())
            {
                Console.WriteLine("Use either -s or -r option.");
                return(ExitCodes.SendAndReceiveSpecified);
            }

            var isSend = !ReceiveOption.HasValue();
            var file   = FileArgument.Value;

            if (isSend && string.IsNullOrEmpty(file))
            {
                Console.WriteLine("No file specified.");
                return(ExitCodes.NoFileSpecified);
            }
            if (!string.IsNullOrEmpty(file) && !CheckFile(file))
            {
                return(ExitCodes.InvalidFileSpecified);
            }

            ushort port = 56657;

            if (PortOption.HasValue())
            {
                var canPortParse = PortOption.HasValue() && ushort.TryParse(PortOption.Value(), out port);
                if (!canPortParse)
                {
                    Console.WriteLine("Invalid port.");
                    return(ExitCodes.InvalidPort);
                }
            }

            var bufferSize = 65536 * 32;

            if (BufferOption.HasValue())
            {
                var canBufferParse = BufferOption.HasValue() && int.TryParse(BufferOption.Value(), out bufferSize);
                if (!canBufferParse)
                {
                    Console.WriteLine("Invalid Buffer Size.");
                    return(ExitCodes.InvalidBufferSize);
                }
            }

            bool AcceptFileCallback(string name, long size, string sender, IPAddress source)
            {
                Console.Write($"File {name} ({Progress.ToFileSize(size)}) announced by {sender} ({source}), accept? (y/n) ");
                var read = Console.ReadLine();

                return(read == "y");
            }

            var worker = isSend
                ? (Worker) new SendWorker(port, bufferSize, file)
                : new ReceiveWorker(port, bufferSize, file, AcceptFileCallback);

            return(worker.Run());
        }