Пример #1
0
        public async Task Start()
        {
            _socket = new TcpClient();

            VerboseLog("Starting connection to " + _address + ":" + _port);

            _socket.Connect(new IPEndPoint(_address, _port));
            _stream = _socket.GetStream();

            _file = new SegmentedFile(System.IO.File.OpenRead(_path));

            long pos;

            if (TryLoadState(out pos))
            {
                _file.Position = pos;
            }

            byte[] fileInfo = new byte[1 + 4 + 8 + 8];

            fileInfo[0] = (byte)PacketType.SetFileInfo;

            VerboseLog("Sending file with length " + _file.Length);

            Array.Copy(BitConverter.GetBytes(_code), 0, fileInfo, 1, 4);
            Array.Copy(BitConverter.GetBytes(_file.Length), 0, fileInfo, 5, 8);
            Array.Copy(BitConverter.GetBytes(_file.Position), 0, fileInfo, 13, 8);

            VerboseLog("Sending welcome packet: " + string.Join(", ", fileInfo));
            await _stream.WriteAsync(fileInfo, 0, fileInfo.Length);
        }
Пример #2
0
        public void Update(SegmentedFile file)
        {
            if (file == null)
            {
                return;
            }

            StringBuilder sb = new StringBuilder();

            sb.Append('\r');

            double percentage = file.Position / (double)file.Length;

            sb.Append(string.Format("{0:P0}", percentage).PadRight(10));
            sb.Append('[');

            int spaceForBar = Console.WindowWidth - (10 + 2 + 16 + 12 + 16);
            int fill        = (int)(spaceForBar * percentage);

            for (int i = 0; i < fill; i++)
            {
                sb.Append('=');
            }
            for (int i = 0; i < spaceForBar - fill - 1; i++)
            {
                sb.Append(' ');
            }

            sb.Append("] ");
            sb.Append(string.Format("{0}", HumanifyBytes(file.Position)).PadRight(16));

            if (DateTime.Now.Subtract(_lastCheck).TotalMilliseconds > 1000)
            {
                _lastCheck = DateTime.Now;

                long delta = file.Position - _dataTransferredThen;

                sb.Append(string.Format("{0}/s", HumanifyBytes(delta)).PadRight(12));

                if (delta > 0)
                {
                    TimeSpan eta = TimeSpan.FromSeconds((file.Length - file.Position) / delta);

                    sb.Append(string.Format("ETA {0}", eta).PadRight(16));
                }

                _dataTransferredThen = file.Position;
            }

            Console.Write(sb.ToString());
        }
Пример #3
0
        private async Task AcceptClient()
        {
            TcpClient cl = await _socket.AcceptTcpClientAsync();

            if (_client != null && IsClientConnected())
            {
                cl.Close();
                return;
            }

            byte[] buffer = new byte[1 + 4 + 8 + 8];

            await cl.GetStream().ReadAsync(buffer, 0, buffer.Length);

            if (buffer[0] == (byte)PacketType.SetFileInfo)
            {
                int  pinCode  = BitConverter.ToInt32(buffer, 1);
                long length   = BitConverter.ToInt64(buffer, 5);
                long position = BitConverter.ToInt64(buffer, 13);

                VerboseLog("New connection from " + cl.Client.RemoteEndPoint);
                VerboseLog($"File len: {length} with starting pos {position}");
                VerboseLog("Payload: " + string.Join(",", buffer));

                if (pinCode == _password)
                {
                    _client       = cl;
                    _clientStream = cl.GetStream();

                    if (_file == null)
                    {
                        _file = new SegmentedFile(System.IO.File.OpenWrite(_path));
                    }
                    _file.Position         = position;
                    _file.Length           = length;
                    _file.BytesTransferred = 0;
                    _file.BytesToTransfer  = _file.Length - position;

                    VerboseLog("Connection successful!");
                }
            }
        }