Beispiel #1
0
        protected void InitializeWriting()
        {
            ConnectionStarted = DateTime.Now;

            _speedCalcTime = DateTime.Now;
            _speedCalcSize = 0;
            _receivedBytes = 0;

            File = FileActions.GetFileOrCreateNew(Packet.RealName, Packet.RealSize);
            if (File == null)
            {
                _log.Warn("InitializeWriting(" + Packet + ") cant find or create a file to download");
                _tcpClient.Close();
                return;
            }
            if (File.Connected)
            {
                _log.Warn("InitializeWriting(" + Packet + ") file already downloading");
                File = null;
                _tcpClient.Close();
                return;
            }

            // wtf?
            if (StartSize == File.Size)
            {
                _log.Error("InitializeWriting(" + Packet + ") startSize = File.Size (" + StartSize + ")");
                _tcpClient.Close();
                return;
            }

            File.Connected = true;
            File.Packet    = Packet;

            _log.Info("InitializeWriting(" + Packet + ") started (" + StartSize + " - " + File.Size + ")");

            try
            {
                var stream = new FileStream(Settings.Default.TempPath + File.TmpName, FileMode.OpenOrCreate, FileAccess.ReadWrite);

                // we are connected
                if (OnConnected != null)
                {
                    OnConnected(this, new EventArgs <Packet>(Packet));
                }

                // we seek if it is necesarry
                if (StartSize > 0)
                {
                    try
                    {
                        _reader = new BinaryReader(stream);

                        // seek to seekPos and extract the rollbackcheck bytes
                        stream.Seek(StartSize, SeekOrigin.Begin);
                        _rollbackRefernce = _reader.ReadBytes(Settings.Default.FileRollbackCheckBytes);

                        // seek back
                        stream.Seek(StartSize, SeekOrigin.Begin);
                    }
                    catch (Exception ex)
                    {
                        _log.Fatal("InitializeWriting(" + Packet + ") seek", ex);
                        _tcpClient.Close();
                        return;
                    }
                }
                else
                {
                    _streamOk = true;
                }

                _writer = new BinaryWriter(stream);

                #region EMIT CHANGES

                File.Commit();

                Packet.Connected = true;
                Packet.File      = File;
                Packet.Commit();

                Packet.Parent.State = Bot.States.Active;
                Packet.Parent.Commit();

                #endregion
            }
            catch (Exception ex)
            {
                _log.Fatal("InitializeWriting(" + Packet + ")", ex);
                _tcpClient.Close();
                return;
            }

            FireNotificationAdded(Notification.Types.BotConnected, Packet);
        }