Ejemplo n.º 1
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="Site"></param>
 public TransferQueueItem(FtpSiteData Site)
 {
     this._SiteData       = Site;
     this.FtpSiteDataGuid = Site.GUID;
 }
Ejemplo n.º 2
0
        private void QueueThread()
        {
            //触发队列启动事件
            if (QueueStarted != null)
            {
                QueueStarted(this, new QueueEventArgs());
            }

            //初始化相关变量
            TransferedFileCount  = 0;
            TransferedDataLength = 0;
            TransferStartTime    = DateTime.Now;

            //开始传输队列
            while (QueueItemList.Count > 0)
            {
                if (QueueState == TransferQueueState.Stoped)
                {
                    break;
                }
                var item = QueueItemList[0];
                CurrentQueueItem = item;

                //处理队列对象
                item.State = TransferQueueItem.TransferQueueItemStateEnum.Transfering;

                CurrentSiteData = item.SiteData;
                var CurrentSite = CurrentSiteData.GetFtpSite();
                var CurrentGroupSiteDataList = new List <FtpSiteData>(CurrentSiteData.Group.Sites);

                if (item.Type == TransferQueueItem.TransferQueueItemTypeEnum.Download)
                {
                    var baseFile = item.RemoteBaseFile;

                    //如果是目录
                    if (baseFile.IsFolder)
                    {
                        var subLocalPath = System.IO.Path.Combine(item.LocalPath, item.Name);
                        if (!aaaSoft.Helpers.IoHelper.CreateMultiFolder(subLocalPath))
                        {
                            item.State = TransferQueueItem.TransferQueueItemStateEnum.Error;
                            item.Tip   = String.Format("创建目录 {0} 时失败。", subLocalPath);
                            RemoveFromQueue(item);
                            continue;
                        }
                        //列出目录
                        var subBaseFiles = CurrentSite.ListDirectory(baseFile.FullName);
                        if (subBaseFiles == null)
                        {
                            item.State = TransferQueueItem.TransferQueueItemStateEnum.Error;
                            item.Tip   = String.Format("列目录 {0} 时失败,原因:{1}。", baseFile.FullName, CurrentSite.ErrMsg);
                            RemoveFromQueue(item);
                            continue;
                        }
                        foreach (var subBaseFile in subBaseFiles)
                        {
                            var subItem = new TransferQueueItem(CurrentSiteData);
                            subItem.Type           = TransferQueueItem.TransferQueueItemTypeEnum.Download;
                            subItem.RemoteBaseFile = subBaseFile;
                            subItem.RemotePath     = subBaseFile.FullName;
                            subItem.LocalPath      = System.IO.Path.Combine(subLocalPath, subBaseFile.Name);
                            InsertIntoQueue(subItem);
                        }
                        item.State = TransferQueueItem.TransferQueueItemStateEnum.TransferComplete;
                    }
                    //如果是文件
                    else if (baseFile is FtpBaseFileInfo)
                    {
                        //如果当前目录不是要下载文件的目录,则改变工作目录
                        if (CurrentSite.CurrentDirectoryPath.ToUpper() != baseFile.ParentPath.ToUpper())
                        {
                            CurrentSite.ListDirectory(baseFile.ParentPath);
                        }
                        if (!CurrentSite.DownloadFile(baseFile as FtpBaseFileInfo, item.LocalPath))
                        {
                            item.Tip   = String.Format("下载文件 {0} 时失败,原因:{1}。", baseFile.FullName, CurrentSite.ErrMsg);
                            item.State = TransferQueueItem.TransferQueueItemStateEnum.Error;
                            RemoveFromQueue(item);
                            continue;
                        }
                        item.State = TransferQueueItem.TransferQueueItemStateEnum.TransferComplete;
                        TransferedFileCount++;
                        TransferedDataLength += CurrentSite.TransferedDataLength;
                    }
                }
                else if (item.Type == TransferQueueItem.TransferQueueItemTypeEnum.Upload)
                {
                    //如果是文件
                    if (System.IO.File.Exists(item.LocalPath))
                    {
                        //远端目录路径
                        var remoteFolderPath = aaaSoft.Helpers.IoHelper.GetParentPath(item.RemotePath, '/');
                        foreach (var siteData in CurrentGroupSiteDataList)
                        {
                            var site = siteData.GetFtpSite();
                            if (site.CurrentDirectoryPath != remoteFolderPath)
                            {
                                var baseFiles = site.ListDirectory(remoteFolderPath);
                                //如果列目录失败
                                if (baseFiles == null)
                                {
                                    item.State = TransferQueueItem.TransferQueueItemStateEnum.Error;
                                    item.Tip   = String.Format("列目录 {0} 时失败,原因:{1}。", remoteFolderPath, site.ErrMsg);
                                    RemoveFromQueue(item);
                                    continue;
                                }
                            }
                        }

                        //远端文件名
                        var remoteFileName = aaaSoft.Helpers.IoHelper.GetFileOrFolderName(item.RemotePath, '/');

                        //是否传输成功
                        bool IsTransferSuccess = true;
                        //已成功传输的站点数量
                        Int32 successedSiteCount = 0;

                        foreach (var siteData in CurrentGroupSiteDataList)
                        {
                            ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(Object state)
                            {
                                var ositeData = state as FtpSiteData;
                                var osite     = ositeData.GetFtpSite();
                                if (!osite.UploadFile(remoteFileName, item.LocalPath))
                                {
                                    IsTransferSuccess = false;
                                    item.State        = TransferQueueItem.TransferQueueItemStateEnum.Error;
                                    item.Tip          = String.Format("上传文件 {0} 时失败,原因:{1}。", item.LocalPath, osite.ErrMsg);
                                    Debug.Print("TransferQueue.QueueThread():" + item.Tip);
                                }
                                lock (TransferedFileCountObj)
                                    TransferedFileCount++;
                                lock (TransferedDataLengthObj)
                                    TransferedDataLength += osite.TransferedDataLength;
                                successedSiteCount++;
                            }), siteData);
                        }

                        while (IsTransferSuccess && successedSiteCount < CurrentSiteData.Group.Sites.Count)
                        {
                            Thread.Sleep(ProgressUpdateTimeInterval);
                        }

                        if (!IsTransferSuccess)
                        {
                            RemoveFromQueue(item);
                            continue;
                        }

                        item.State = TransferQueueItem.TransferQueueItemStateEnum.TransferComplete;
                    }
                    //如果是目录
                    else if (System.IO.Directory.Exists(item.LocalPath))
                    {
                        //=====================
                        //检查远端目录是否存在
                        //=====================
                        foreach (var siteData in CurrentGroupSiteDataList)
                        {
                            var site      = siteData.GetFtpSite();
                            var baseFiles = site.ListDirectory(item.RemotePath);
                            //如果远端目录不存在
                            if (baseFiles == null)
                            {
                                if (!site.CreateDirectory(item.RemotePath))
                                {
                                    item.State = TransferQueueItem.TransferQueueItemStateEnum.Error;
                                    item.Tip   = String.Format("创建远端目录 {0} 时失败,原因:{0}。", item.LocalPath, site.ErrMsg);
                                    RemoveFromQueue(item);
                                    continue;
                                }
                            }
                        }

                        try
                        {
                            var localFolderInfo = new System.IO.DirectoryInfo(item.LocalPath);

                            List <String> PathList = new List <string>();
                            foreach (var subFileInfo in localFolderInfo.GetFiles())
                            {
                                PathList.Add(subFileInfo.FullName);
                            }
                            foreach (var subFolderInfo in localFolderInfo.GetDirectories())
                            {
                                PathList.Add(subFolderInfo.FullName);
                            }

                            foreach (var subPath in PathList)
                            {
                                var subItem = new TransferQueueItem(CurrentSiteData);
                                subItem.Type       = TransferQueueItem.TransferQueueItemTypeEnum.Upload;
                                subItem.LocalPath  = subPath;
                                subItem.RemotePath = (item.RemotePath + "/" + IoHelper.GetFileOrFolderName(subPath, System.IO.Path.DirectorySeparatorChar)).Replace("//", "/");
                                InsertIntoQueue(subItem);
                            }
                            item.State = TransferQueueItem.TransferQueueItemStateEnum.TransferComplete;
                        }
                        catch (Exception ex)
                        {
                            item.State = TransferQueueItem.TransferQueueItemStateEnum.Error;
                            item.Tip   = String.Format("上传目录 {0} 时失败,原因:{0}。", item.LocalPath, ex.Message);
                            RemoveFromQueue(item);
                            continue;
                        }
                    }
                }

                //如果处理成功则从队列中移除
                RemoveFromQueue(item);
            }
            TransferEndTime  = DateTime.Now;
            trdTransfer      = null;
            CurrentSiteData  = null;
            CurrentQueueItem = null;
            if (QueueItemList.Count == 0)
            {
                //触发队列完成事件
                if (QueueCompleted != null)
                {
                    QueueCompleted(this, new QueueEventArgs());
                }
            }
            else
            {
                //触发队列停止事件
                if (QueueStoped != null)
                {
                    QueueStoped(this, new QueueEventArgs());
                }
            }
        }