Esempio n. 1
0
 public void Add(string guId, TransferSendFileObj obj)
 {
     TransferSendFileObj value = null;
     if (PCClientFilesDic.TryGetValue(guId, out value))
     {
         PCClientFilesDic.TryUpdate(guId, obj, value);
     }
     else
     {
         PCClientFilesDic.TryAdd(guId, obj);
     }
 }
       public static void SafeReadFile(Stream stream, string guId, string FileName, string FilePath)
       {
           int bufferLength = 32768;
           //文件数据块编号
           int _order_Num = 0;
           //初始化一个32k的缓存
           //byte[] buffer = new byte[32768];
           byte[] buffer = new byte[bufferLength];
           int byteLength = 0;
           int read = 0;
           int block;
           //每次从流中读取缓存大小的数据,知道读取完所有的流为止
           TransferSendFileObj fileObj0 = new TransferSendFileObj();
           FilesQueue.Instance.Get(guId, out fileObj0);
           fileObj0.Status = SendFileStatus.Reading;
           FilesQueue.Instance.Add(guId, fileObj0);
           //Utility.Log.Log.writeLineToLog("开始读取文件","Read");
           while ((block = stream.Read(buffer, 0, buffer.Length)) > 0)
           {
               byteLength = byteLength + block;
               //重新设定读取位置
               read += block;
               _order_Num = _order_Num + 1;
               //检查是否到达了缓存的边界,检查是否还有可以读取的信息
               if (block == buffer.Length)
               {
                   //压入文件块传输缓存队列
                   TransferFileDataObj fileBlock = new TransferFileDataObj();
                   fileBlock.guId = guId;
                   Array.Copy(buffer, fileBlock.FileData, buffer.Length);
                   fileBlock.FilePath = FilePath;
                   fileBlock.Order_Num = _order_Num;
                   fileBlock.FileName = FileName;
                   fileBlock.CanReadLength = bufferLength;
                   FilesBlocksQueue.Instance.Add(fileBlock);
                   //Utility.Log.Log.writeLineToLog("读取文件块入队列:" + _order_Num.ToString(), "Read");
                   FilesQueue.Instance.Get(guId, out fileObj0);
                   fileObj0.ReadSize = fileObj0.ReadSize + block;
                   fileObj0.Status = SendFileStatus.Reading;
                                

                   // 尝试读取一个字节
                   int nextByte = stream.ReadByte();
                   // 读取失败则说明读取完成可以返回结果
                   if (nextByte == -1)
                   {
                       fileBlock.EndFlag = true;
                       //FilesQueue.Instance.Get(guId, out fileObj0);
                       fileObj0.Status = SendFileStatus.ReadEnd;
                       FilesQueue.Instance.Add(guId, fileObj0);
                       TransferSendFileObj2 fileObj2 = new TransferSendFileObj2();
                       fileObj2.BlockNum = _order_Num;
                       fileObj2.DataBlockCount = _order_Num;
                       fileObj2.FileName = fileObj0.FileName;
                       fileObj2.FilePath = fileObj0.FilePath;
                       fileObj2.FileSize = fileObj0.FileSize;
                       fileObj2.ReadSize = fileObj0.ReadSize;
                       fileObj2.SendSize = fileObj0.SendSize;
                       fileObj2.serverDir = fileObj0.serverDir;
                       fileObj2.Status = fileObj0.Status;
                       UpLoadFileMsg(new UpLoadFileEventArgs(guId, fileObj2));
                   }
                   else
                   {
                       fileBlock.EndFlag = false;
                       fileObj0.Status = SendFileStatus.Reading;
                       TransferSendFileObj2 fileObj2 = new TransferSendFileObj2();
                       fileObj2.BlockNum = _order_Num;
                       fileObj2.DataBlockCount = 0;
                       fileObj2.FileName = fileObj0.FileName;
                       fileObj2.FilePath = fileObj0.FilePath;
                       fileObj2.FileSize = fileObj0.FileSize;
                       fileObj2.ReadSize = fileObj0.ReadSize;
                       fileObj2.SendSize = fileObj0.SendSize;
                       fileObj2.serverDir = fileObj0.serverDir;
                       fileObj2.Status = fileObj0.Status;
                       UpLoadFileMsg(new UpLoadFileEventArgs(guId, fileObj2));
                   }
                   FilesQueue.Instance.Add(guId, fileObj0); 
                   stream.Seek(-1, SeekOrigin.Current);
                 
                   //清空缓存区
                   Array.Clear(buffer, 0, buffer.Length);
                   byteLength = 0;
               }              
           }
           //压入文件块传输缓存队列
           TransferFileDataObj fileBlockEnd = new TransferFileDataObj();
           fileBlockEnd.guId = guId;
           Array.Copy(buffer, fileBlockEnd.FileData, byteLength);
           fileBlockEnd.FilePath = FilePath;
           fileBlockEnd.CanReadLength = byteLength;
           fileBlockEnd.Order_Num = _order_Num;
           fileBlockEnd.FileName = FileName;
           fileBlockEnd.EndFlag = true;
           FilesBlocksQueue.Instance.Add(fileBlockEnd);
           //Utility.Log.Log.writeLineToLog("读取文件--尾块入队列:" + _order_Num.ToString(), "Read");
           //更新Files队列中,对应File的ReadSize    
           FilesQueue.Instance.Get(guId, out fileObj0);
           fileObj0.ReadSize = fileObj0.ReadSize + byteLength;
           fileObj0.Status = SendFileStatus.ReadEnd;
           fileObj0.DataBlockCount = fileBlockEnd.Order_Num;
           FilesQueue.Instance.Add(guId, fileObj0);
           TransferSendFileObj2 fileObj3 = new TransferSendFileObj2();
           fileObj3.BlockNum = _order_Num;
           fileObj3.DataBlockCount = 0;
           fileObj3.FileName = fileObj0.FileName;
           fileObj3.FilePath = fileObj0.FilePath;
           fileObj3.FileSize = fileObj0.FileSize;
           fileObj3.ReadSize = fileObj0.ReadSize;
           fileObj3.SendSize = fileObj0.SendSize;
           fileObj3.serverDir = fileObj0.serverDir;
           fileObj3.Status = fileObj0.Status;
           UpLoadFileMsg(new UpLoadFileEventArgs(guId, fileObj3));
           //清空缓存区
           Array.Clear(buffer, 0, buffer.Length);
       }
       //private static void loadData(string path, string fileName, string guId)
       //{
       //    DownLoadFileDataObj obj = new DownLoadFileDataObj();
       //    if (GlobalStaticObj.Instance.proxyFile.GetSucDataBlock(guId))
       //    {
       //        DownLoadFileOoj_Client fileObj = new DownLoadFileOoj_Client();
       //        fileObj.FileName = fileName;
       //        fileObj.guId = guId;
       //        fileObj.DownLoadStatus = GetFileDownLoadStatus.StartDownLoad;
       //        fileObj.DownLoadSize = 0;
       //        DownLoadFilesQueue.Instance.Add(guId, fileObj);
       //        DownLoadFileMsg(new DownLoadFileEventArgs(guId,fileObj));

       //        getFileDataBlocks(path, fileName, guId, out obj);
       //        if (!obj.EndFlag)
       //        {
       //            loadData(path, fileName, guId);
       //        }
       //        else
       //        {
       //        DownLoadFileOoj_Client fileObj0 = new DownLoadFileOoj_Client();
       //        fileObj0.FileName = fileName;
       //        fileObj0.guId = guId;
       //        fileObj0.DownLoadStatus = GetFileDownLoadStatus.DownLoadEnd;
       //        DownLoadFilesQueue.Instance.Add(guId, fileObj0);
       //        DownLoadFileMsg(new DownLoadFileEventArgs(guId,fileObj0));
       //            return;
       //        }
       //    }
       //    else {
       //        loadData(path, fileName, guId);
       //    }
       //}

       // private static void getFileDataBlocks(string path, string fileName, string guId, out DownLoadFileDataObj obj0)
       //{
       //  string resultStr = GlobalStaticObj.Instance.proxyFile.DownLoadFileDataBlock(path, fileName, guId);
       //  DownLoadFileDataObj obj = JsonConvert.DeserializeObject<DownLoadFileDataObj>(resultStr);
       //  if (!string.IsNullOrEmpty(resultStr))
       //  {
       //      obj0 = obj;
       //      DownLoadFilesBlocksQueue.Instance.Add(obj);  
       //      DownLoadFileOoj_Client fileObj0 = new DownLoadFileOoj_Client();
       //        fileObj0.FileName = fileName;
       //        fileObj0.guId = guId;
       //        fileObj0.DownLoadStatus = GetFileDownLoadStatus.DownLoadEnd;
       //        fileObj0.DownLoadSize =  fileObj0.DownLoadSize + obj.FileData.Length;
       //        DownLoadFilesQueue.Instance.Add(guId, fileObj0);
       //        DownLoadFileMsg(new DownLoadFileEventArgs(guId,fileObj0));
       //  }
       //  if(obj.EndFlag)
       //  {
       //      obj0 = obj;
       //      DownLoadFileOoj_Client fileObj0 = new DownLoadFileOoj_Client();
       //      fileObj0.FileName = fileName;
       //      fileObj0.guId = guId;
       //      fileObj0.DownLoadStatus = GetFileDownLoadStatus.DownLoadEnd;
       //      fileObj0.DownLoadSize = fileObj0.DownLoadSize + obj.FileData.Length;
       //      DownLoadFilesQueue.Instance.Add(guId, fileObj0);
       //      DownLoadFileMsg(new DownLoadFileEventArgs(guId, fileObj0));
       //      return; 
       //  }
       //  getFileDataBlocks(path,fileName,guId,out obj0);
       //}

        //private static void getFileDataBlocks(string guId)
        //{
        //    string resultStr = GlobalStaticObj.Instance.proxyFile.DownLoadFileDataBlock(guId);
        //    DownLoadFileDataObj obj = JsonConvert.DeserializeObject<DownLoadFileDataObj>(resultStr);
        //    if (!string.IsNullOrEmpty(resultStr))
        //    {
        //        DownLoadFileOoj_Client fileObj0 = new DownLoadFileOoj_Client();
        //        DownLoadFilesQueue.Instance.Get(obj.guId, out fileObj0);
        //        DownLoadFilesBlocksQueue.Instance.Add(obj);              
        //        fileObj0.DownLoadStatus = GetFileDownLoadStatus.DownLoadEnd;
        //        fileObj0.DownLoadSize = fileObj0.DownLoadSize + obj.FileData.Length;
        //        DownLoadFilesQueue.Instance.Add(guId, fileObj0);
        //        DownLoadFileMsg(new DownLoadFileEventArgs(guId, fileObj0));
        //    }
        //    if (obj.EndFlag)
        //    {
        //        obj0 = obj;
        //        DownLoadFileOoj_Client fileObj0 = new DownLoadFileOoj_Client();
        //        fileObj0.FileName = fileName;
        //        fileObj0.guId = guId;
        //        fileObj0.DownLoadStatus = GetFileDownLoadStatus.DownLoadEnd;
        //        fileObj0.DownLoadSize = fileObj0.DownLoadSize + obj.FileData.Length;
        //        DownLoadFilesQueue.Instance.Add(guId, fileObj0);
        //        DownLoadFileMsg(new DownLoadFileEventArgs(guId, fileObj0));
        //        return;
        //    }
        //    getFileDataBlocks(path, fileName, guId, out obj0);
        //}

       

       public static bool ClientSendFileDataBlock(TransferFileDataObj item)
       {
           bool result = false;
           if (WCFClientProxy.TestPCClientProxy())
           {
               TransferSendFileObj fileObj =  new TransferSendFileObj();
               FilesQueue.Instance.Get(item.guId, out fileObj);
               string resultStr = GlobalStaticObj.Instance.proxyFile.ReceiveFile(item.FileData,item.CanReadLength, item.FilePath, item.FileName, item.Order_Num, item.guId, fileObj.serverDir, fileObj.FileSize, item.EndFlag, fileObj.DataBlockCount,UserId, PCClientCookieStr);             
               bool.TryParse(resultStr, out result);
               if (FilesBlocksQueue.PCClientFilesBlocksQueue.IsEmpty)
               {
                   FilesBlocksQueue.RunFlag = true;                   
                   //Utility.Log.Log.writeLineToLog("发送线程终止:" + item.Order_Num.ToString() + " 结果:" + result.ToString(), "Send");
               }
               //Utility.Log.Log.writeLineToLog("发送文件块:" + item.Order_Num.ToString() + " 结果:" + result.ToString(), "Send");
           }
           return result;
       }
 /// <summary>
 /// 上传文件
 /// </summary>
 /// <param name="filePath">文件路径</param>
 /// <param name="guId"></param>
 /// <param name="serverDir"></param>
  public static void ClientUpLoadFile(string filePath, string guId, string serverDir)
 {
     string fileName = Path.GetFileName(filePath);
     string fileNameWithoutExt = Path.GetFileNameWithoutExtension(filePath);
     string fileType = Path.GetExtension(filePath);
     FileInfo fileInfo = new FileInfo(filePath);           
     using(FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
     {
         TransferSendFileObj fileObj = new TransferSendFileObj();
         fileObj.FileName = fileName;
         fileObj.FilePath = filePath;
         fileObj.FileSize = fileInfo.Length;
         fileObj.ReadSize = 0;
         fileObj.SendSize = 0;
         fileObj.Status = SendFileStatus.StartRead;
         fileObj.serverDir = serverDir;
         FilesQueue.Instance.Add(guId, fileObj);
         AddIdList(guId);
         TransferSendFileObj2 fileObj2 = new TransferSendFileObj2();
         fileObj2.BlockNum = 0;
         fileObj2.DataBlockCount = 0;
         fileObj2.FileName = fileObj.FileName;
         fileObj2.FilePath = fileObj.FilePath;
         fileObj2.FileSize = fileObj.FileSize;
         fileObj2.ReadSize = fileObj.ReadSize;
         fileObj2.SendSize = fileObj.SendSize;
         fileObj2.serverDir = fileObj.serverDir;
         fileObj2.Status = fileObj.Status;
         UpLoadFileMsg(new UpLoadFileEventArgs(guId,fileObj2));
         InitUpLoadTimer();
         SafeReadFile(fs, guId, fileName, filePath);
     }
     //byte[] data = new byte[fs.Length];
     //fs.Read(data, 0, data.Length);
 }
Esempio n. 5
0
 private static void NewThreadFunc(Thread ParObject)
 {
     RunFlag = false;
     while (!RunFlag)
     {              
         TransferFileDataObj item = null;
         if (PCClientFilesBlocksQueue.TryPeek(out item))
         {
             if (item != null)
             {
                 if (FileTransferClient.ClientSendFileDataBlock(item))
                 {
                     TransferFileDataObj block = null;
                     if (PCClientFilesBlocksQueue.TryDequeue(out block))
                     {
                         Interlocked.Decrement(ref queueLength);
                         TransferSendFileObj fileObj = new TransferSendFileObj();
                         FilesQueue.Instance.Get(block.guId, out fileObj);
                         if (fileObj != null && block != null)
                         {
                             fileObj.SendSize = fileObj.SendSize + block.FileData.Length;
                             if (item.Order_Num == 1)
                             {
                                 fileObj.Status = SendFileStatus.StartSend;
                             }
                             else {
                                 fileObj.Status = SendFileStatus.Sending;
                             }
                             if (fileObj.FileSize == fileObj.SendSize)
                             {
                                 fileObj.Status = SendFileStatus.SendEnd;
                             }
                             FilesQueue.Instance.Add(block.guId, fileObj);
                             TransferSendFileObj2 fileObj2 = new TransferSendFileObj2();
                             fileObj2.BlockNum = item.Order_Num;
                             fileObj2.DataBlockCount = fileObj.DataBlockCount;
                             fileObj2.FileName = fileObj.FileName;
                             fileObj2.FilePath = fileObj.FilePath;
                             fileObj2.FileSize = fileObj.FileSize;
                             fileObj2.ReadSize = fileObj.ReadSize;
                             fileObj2.SendSize = fileObj.SendSize;
                             fileObj2.serverDir = fileObj.serverDir;
                             fileObj2.Status = fileObj.Status;
                             FileTransferClient.UpLoadFileMsg(new UpLoadFileEventArgs(block.guId, fileObj2));
                         }
                     }
                 }
             }
         }
     }
     //RunFlag = true;
     ////Utility.Log.Log.writeLineToLog("发送线程终止000:" , "Send");
     ////Thread.Sleep(3000);
     //ParObject.Abort();
     //ParObject = null;
 }
Esempio n. 6
0
        private static void NewThreadFunc()
        {
            //RunFlag = false;
            while (true)
            {
                TransferFileDataObj item = null;
                if (PCClientFilesBlocksQueue.TryPeek(out item))
                {
                    if (item != null)
                    {
                        if (FileTransferClient.ClientSendFileDataBlock(item))
                        {
                            TransferFileDataObj block = null;
                            if (PCClientFilesBlocksQueue.TryDequeue(out block))
                            {
                                Interlocked.Decrement(ref queueLength);
                                TransferSendFileObj fileObj = new TransferSendFileObj();
                                FilesQueue.Instance.Get(block.guId, out fileObj);
                                if (fileObj != null && block != null)
                                {

                                    fileObj.SendSize = fileObj.SendSize + block.CanReadLength;
                                    if (item.Order_Num == 1)
                                    {
                                        fileObj.Status = SendFileStatus.StartSend;
                                    }
                                    else
                                    {
                                        fileObj.Status = SendFileStatus.Sending;
                                    }
                                    if (fileObj.FileSize == fileObj.SendSize)
                                    {
                                        fileObj.Status = SendFileStatus.SendEnd;
                                    }
                                    FilesQueue.Instance.Add(block.guId, fileObj);
                                    TransferSendFileObj2 fileObj2 = new TransferSendFileObj2();
                                    fileObj2.BlockNum = item.Order_Num;
                                    fileObj2.DataBlockCount = fileObj.DataBlockCount;
                                    fileObj2.FileName = fileObj.FileName;
                                    fileObj2.FilePath = fileObj.FilePath;
                                    fileObj2.FileSize = fileObj.FileSize;
                                    fileObj2.ReadSize = fileObj.ReadSize;
                                    fileObj2.SendSize = fileObj.SendSize;
                                    fileObj2.serverDir = fileObj.serverDir;
                                    fileObj2.Status = fileObj.Status;
                                    FileTransferClient.UpLoadFileMsg(new UpLoadFileEventArgs(block.guId, fileObj2));
                                }
                            }
                        }
                    }
                }
            }        
        }
Esempio n. 7
0
 public bool Get(string guId, out TransferSendFileObj obj)
 {
     return PCClientFilesDic.TryGetValue(guId, out obj);
 }
Esempio n. 8
0
 private static bool Remove(string guId, out TransferSendFileObj obj)
 {
     return PCClientFilesDic.TryRemove(guId, out obj);
 }