Ejemplo n.º 1
0
        private void WriteFile2(string guId, TransferReceiveFileObj fileObj0, Thread myThread)
        {
            FileStream fsWrite = null;
            long fileSize = 0;
            bool firstWrite = true;
            TransferReceiveFileObj fileObjAim = fileObj0;
            while (true)
            {
            start:
                if (!firstWrite)
                {
                    TransferReceiveFileObj fileObj1 = new TransferReceiveFileObj();
                    FilesQueue.Get(guId, out fileObj1);
                    fileObjAim = fileObj1;

                    if ((System.DateTime.Now - fileObjAim.writeTime).TotalMinutes > 3)//写入等待超时3分钟
                    {
                        if (fsWrite != null)
                        {
                            fsWrite.Close();
                            fsWrite.Dispose();
                        }
                        //Utility.Log.Log.writeLineToLog("写入文件--尾块:" + block.Order_Num.ToString(), "Write" + guId);
                        fileObjAim.Status = ReceiveFileStatus.WriteWaitTimeOut;
                        FilesQueue.Add(guId, fileObjAim);
                        //Utility.Log.Log.writeLineToLog("写入线程终止--Rest点:" + block.Order_Num.ToString(), "Write" + guId);
                        Thread.Sleep(300);
                        myThread.Abort();
                        myThread = null;
                    }                    
                }


                for (int i = 0; i < ServerFilesBlocksBag.Count; i++)
                {
                    TransferFileDataObj block = ServerFilesBlocksBag.ElementAt<TransferFileDataObj>(i);
                    if (block.Order_Num != fileObjAim.NextDataBlockNum)
                    {
                        return;
                    }
                    if (guId != block.guId)
                    {
                        return;
                    }

                    if (!Directory.Exists(Instance.SavePath))//存放的默认文件夹是否存在
                    {
                        Directory.CreateDirectory(Instance.SavePath);//不存在则创建
                    }
                    string fileFullPath = Path.Combine(Instance.SavePath + fileObjAim.serverDir, fileObjAim.FileName);//合并路径生成文件存放路径
                    //创建文件流,读取流中的数据生成文件
                    //lock (SynWriteObject)
                    //{
                    //fsWriteAppend.Lock(
                    if (fsWrite == null)
                    {
                        if (block.Order_Num == 1)
                        {
                            fsWrite = new FileStream(fileFullPath, FileMode.Create, FileAccess.Write, FileShare.Write);
                            firstWrite = false;
                        }
                        else {
                            fsWrite = new FileStream(fileFullPath, FileMode.Append, FileAccess.Write, FileShare.Write);
                        }
                    }
                    //else { 

                    //}
                    // Utility.Log.Log.writeLineToLog("写入文件:" + block.Order_Num.ToString(), "Write" + guId);
                    //fsWrite.Seek((block.Order_Num - 1) * 32768, SeekOrigin.Begin);
                    //if (block.Order_Num == 1)
                    //{
                    //    fsWrite.Seek(0, SeekOrigin.Begin);
                    //}
                    //else {
                    //    fsWrite.Seek(0, SeekOrigin.End);
                    //}
                    fsWrite.Write(block.FileData, 0, block.CanReadLength);
                    fileSize = fileSize + block.CanReadLength;
                    //}
                    fileObjAim.Status = ReceiveFileStatus.Writing;
                    fileObjAim.writeTime = System.DateTime.Now;
                    if (fileSize == fileObjAim.FileSize)//完整写入
                    {
                        fsWrite.Close();
                        fsWrite.Dispose();
                        //Utility.Log.Log.writeLineToLog("写入文件--尾块:" + block.Order_Num.ToString(), "Write" + guId);
                        fileObjAim.Status = ReceiveFileStatus.WriteEnd;
                        //Utility.Log.Log.writeLineToLog("写入线程终止--Rest点:" + block.Order_Num.ToString(), "Write" + guId);
                        Thread.Sleep(300);
                        myThread.Abort();
                        myThread = null;
                    }
                    fileObjAim.WriteSize = fileObjAim.WriteSize + block.FileData.Length; ;
                    fileObjAim.NextDataBlockNum = fileObjAim.NextDataBlockNum + 1;
                    FilesQueue.Add(guId, fileObjAim);
                    //ServerFilesBlocksQueue.TryDequeue(out block);
                    ServerFilesBlocksBag.TryTake(out block);
                    //Thread.Sleep(50);
                   
                }
                goto start;
            }
         
        }
Ejemplo n.º 2
0
 private static void WriteRestFile(string guId, TransferReceiveFileObj fileObj0,TransferFileDataObj block)
 {
     if (block.Order_Num != fileObj0.NextDataBlockNum)
     {
         return;
     }
     if (!Directory.Exists(Instance.SavePath))//存放的默认文件夹是否存在
     {
         Directory.CreateDirectory(Instance.SavePath);//不存在则创建
     }
     string fileFullPath = Path.Combine(Instance.SavePath + fileObj0.serverDir, fileObj0.FileName);//合并路径生成文件存放路径
     //创建文件流,读取流中的数据生成文件
     //lock (SynWriteObject)
     //{
     FileStream fs = new FileStream(fileFullPath, FileMode.Append, FileAccess.Write, FileShare.Write);
         //Utility.Log.Log.writeLineToLog("写入文件:" + block.Order_Num.ToString(), "Write" + guId);
         fs.Write(block.FileData, 0, block.CanReadLength);
         fs.Close();
         fs.Dispose();
     //}
         fileObj0.Status = ReceiveFileStatus.Writing;
         if (block.EndFlag)
         {
             //Utility.Log.Log.writeLineToLog("写入文件--尾块:" + block.Order_Num.ToString(), "Write" + guId);
             fileObj0.Status = ReceiveFileStatus.WriteEnd;               
         }
         fileObj0.WriteSize = fileObj0.WriteSize + block.FileData.Length; ;
         fileObj0.NextDataBlockNum = fileObj0.NextDataBlockNum+1;
         FilesQueue.Add(guId, fileObj0);
         //ServerFilesBlocksQueue.TryDequeue(out block);
         ServerFilesBlocksBag.TryTake(out block); 
 }
Ejemplo n.º 3
0
        //FileStream fsWriteAppend = null;
        private void WriteRestFile(string guId, TransferReceiveFileObj fileObj0, TransferFileDataObj block0, Thread myThread)
        {
            FileStream fsWriteAppend = null;
            for (int i=0;i<ServerFilesBlocksBag.Count;i++)
            {
                TransferFileDataObj block = ServerFilesBlocksBag.ElementAt <TransferFileDataObj>(i);
                if (block0.Order_Num != fileObj0.NextDataBlockNum)
                {
                    return;
                }
                if (guId != block.guId)
                {
                    return;
                }

                if (!Directory.Exists(Instance.SavePath))//存放的默认文件夹是否存在
                {
                    Directory.CreateDirectory(Instance.SavePath);//不存在则创建
                }
                string fileFullPath = Path.Combine(Instance.SavePath + fileObj0.serverDir, fileObj0.FileName);//合并路径生成文件存放路径
                //创建文件流,读取流中的数据生成文件
                //lock (SynWriteObject)
                //{
                //fsWriteAppend.Lock(
                if (fsWriteAppend == null)
                {
                fsWriteAppend = new FileStream(fileFullPath, FileMode.Append, FileAccess.Write, FileShare.Write);
                }
                //else { 

                //}
                // Utility.Log.Log.writeLineToLog("写入文件:" + block.Order_Num.ToString(), "Write" + guId);
                fsWriteAppend.Seek((block.Order_Num - 1) * 32768, SeekOrigin.Begin);
                fsWriteAppend.Write(block.FileData, 0, block.CanReadLength);
                fsWriteAppend.Close();
                fsWriteAppend.Dispose();
                //}
                fileObj0.Status = ReceiveFileStatus.Writing;
                if (block.EndFlag)
                {
                    //Utility.Log.Log.writeLineToLog("写入文件--尾块:" + block.Order_Num.ToString(), "Write" + guId);
                    fileObj0.Status = ReceiveFileStatus.WriteEnd;
                    //Utility.Log.Log.writeLineToLog("写入线程终止--Rest点:" + block.Order_Num.ToString(), "Write" + guId);
                    Thread.Sleep(300);
                    myThread.Abort();
                    myThread = null;
                }
                fileObj0.WriteSize = fileObj0.WriteSize + block.FileData.Length; ;
                fileObj0.NextDataBlockNum = fileObj0.NextDataBlockNum + 1;
                FilesQueue.Add(guId, fileObj0);
                //ServerFilesBlocksQueue.TryDequeue(out block);
                ServerFilesBlocksBag.TryTake(out block);
                //Thread.Sleep(50);
            }
        }
Ejemplo n.º 4
0
        private void findEndFlagFileBlock(Thread myThread)
        {
            //TransferFileDataObj block = new TransferFileDataObj();
            //if(ServerFilesBlocksQueue.TryPeek(out block)){
            //foreach (TransferFileDataObj block in ServerFilesBlocksQueue)
            //{

            //foreach (TransferFileDataObj block in ServerFilesBlocksBag)
            //{
            //    if (block.Order_Num == 1)
            //    {
            //        TransferReceiveFileObj fileObj0 = new TransferReceiveFileObj();
            //        FilesQueue.Get(block.guId, out fileObj0);
            //        WriteNewFile(block.guId, fileObj0, block,myThread);
            //    }
            //    else
            //    {
            //        TransferReceiveFileObj fileObj0 = new TransferReceiveFileObj();
            //        FilesQueue.Get(block.guId, out fileObj0);
            //        if (block.Order_Num == fileObj0.NextDataBlockNum)
            //        {
            //            WriteRestFile(block.guId, fileObj0, block,myThread);
            //        }
            //    }
            ////}
            //} 

            foreach (TransferFileDataObj block in ServerFilesBlocksBag)
            {
                if (block.Order_Num == 1)
                {
                    TransferReceiveFileObj fileObj0 = new TransferReceiveFileObj();
                    FilesQueue.Get(block.guId, out fileObj0);
                    WriteFile2(block.guId, fileObj0, myThread);
                }              
            }            
         
        }
Ejemplo n.º 5
0
 //FileStream fsWriteNew = null;
 private void WriteNewFile(string guId, TransferReceiveFileObj fileObj0, TransferFileDataObj block, Thread myThread)
 {
     if (!Directory.Exists(Path.Combine(Instance.SavePath + fileObj0.serverDir)))//存放的默认文件夹是否存在
     {
         Directory.CreateDirectory(Path.Combine(Instance.SavePath + fileObj0.serverDir));//不存在则创建
     }
     string fileFullPath = Path.Combine(Instance.SavePath + fileObj0.serverDir, fileObj0.FileName);//合并路径生成文件存放路径
     //创建文件流,读取流中的数据生成文件
     int offset = 0;
     //lock (SynWriteObject)
     //{
     //if (fsWriteNew == null)
     //{
         FileStream fsWriteNew = new FileStream(fileFullPath, FileMode.Create, FileAccess.Write, FileShare.Write);
     //}
         //Utility.Log.Log.writeLineToLog("开始写入文件:" + block.Order_Num.ToString(), "Write" + guId);
         fsWriteNew.Seek(0, SeekOrigin.Begin);
         fsWriteNew.Write(block.FileData, 0, block.CanReadLength);
         fsWriteNew.Close();
         fsWriteNew.Dispose();
     //}
     offset = offset + block.FileData.Length;
     fileObj0.Status = ReceiveFileStatus.StartWrite;
     if (block.EndFlag)
     {
         //Utility.Log.Log.writeLineToLog("写入文件--尾块:" + block.Order_Num.ToString(), "Write" + guId);
         fileObj0.Status = ReceiveFileStatus.WriteEnd;
         //Utility.Log.Log.writeLineToLog("写入线程终止--New点:" + block.Order_Num.ToString(), "Write" + guId);
         Thread.Sleep(300);
         myThread.Abort();
         myThread = null;
     }
     fileObj0.WriteSize = offset;
     fileObj0.NextDataBlockNum = 2;
     FilesQueue.Add(guId, fileObj0);
     //ServerFilesBlocksQueue.TryDequeue(out block);
     ServerFilesBlocksBag.TryTake(out block); 
 }
Ejemplo n.º 6
0
 private static void findEndFlagFileBlock()
 {
     foreach (TransferFileDataObj block in ServerFilesBlocksBag)
     //foreach (TransferFileDataObj block in ServerFilesBlocksQueue)
     {
         if (block.Order_Num == 1)
         {
             TransferReceiveFileObj fileObj0 = new TransferReceiveFileObj();
             FilesQueue.Get(block.guId, out fileObj0);
             //Action<string, TransferReceiveFileObj,TransferFileDataObj> generateNewFile = WriteNewFile;
             //generateNewFile.BeginInvoke(block.guId, fileObj0,block, null, null);
             WriteNewFile(block.guId, fileObj0, block);
         }
         else
         {
             //if (block.EndFlag)
             //{
             TransferReceiveFileObj fileObj0 = new TransferReceiveFileObj();
             FilesQueue.Get(block.guId, out fileObj0);
             //Action<string, TransferReceiveFileObj,TransferFileDataObj> generateRestFile = WriteRestFile;
             //generateRestFile.BeginInvoke(block.guId, fileObj0,block, null, null);
             //}
             if (block.Order_Num == fileObj0.NextDataBlockNum)
             {
                 WriteRestFile(block.guId, fileObj0, block); 
             }                   
         }
     }
 }
Ejemplo n.º 7
0
 public static bool Remove(string guId, out TransferReceiveFileObj obj)
 {
     return ServerFilesDic.TryRemove(guId, out obj);
 }
Ejemplo n.º 8
0
 public static bool Get(string guId, out TransferReceiveFileObj obj)
 {
     return ServerFilesDic.TryGetValue(guId, out obj);
 }
Ejemplo n.º 9
0
 public static bool Add(string guId, TransferReceiveFileObj obj)
 {
     TransferReceiveFileObj value = null;
     if (ServerFilesDic.TryGetValue(guId, out value))
     {
        return ServerFilesDic.TryUpdate(guId, obj, value);
     }
     else
     {
         return ServerFilesDic.TryAdd(guId, obj);
     }
 }
 public string ReceiveFile(byte[] fileDataArry, int CanReadLength, string path, string fileName, int Order_Num, string guId, string serverDir, long fileSize, bool EndFlag, int DataBlockCount, string UserID, string PCClientCookieStr)
 {
     if (!SessionCookie.CheckPCClientCookieStr(UserID, PCClientCookieStr))
     {
         return "Session Error";
     }
     string result = string.Empty;
     result = "false";
     TransferReceiveFileObj fileObj0 = new TransferReceiveFileObj();
     if (!FilesQueue.Get(guId, out fileObj0))
     {
         fileObj0 = new TransferReceiveFileObj();
         fileObj0.guId = guId;
         fileObj0.FileName = fileName;
         fileObj0.FilePath = path;
         fileObj0.FileSize = fileSize;
         fileObj0.ReceiveSize = 0;
         fileObj0.Status = ReceiveFileStatus.StartReceive;
         fileObj0.DataBlockCount = DataBlockCount;
         fileObj0.serverDir = serverDir;
         fileObj0.NextDataBlockNum = 1;
         FilesQueue.Add(guId, fileObj0);
     }
     //int len = fileDataArry.Length;
     TransferFileDataObj fileBlock = new TransferFileDataObj();
     fileBlock.guId = guId;
     Array.Copy(fileDataArry, fileBlock.FileData, CanReadLength);
     fileBlock.CanReadLength = CanReadLength;
     fileBlock.FilePath = path;
     fileBlock.Order_Num = Order_Num;
     FilesBlocksQueue.Instance.Add(fileBlock);
     if (!EndFlag)
     {
         //Utility.Log.Log.writeLineToLog("接收文件--入队列:" + Order_Num.ToString(), "Receive");
     }
     FilesQueue.Get(guId, out fileObj0);
     if (EndFlag)
     {
         fileObj0.Status = ReceiveFileStatus.ReceiveEnd;
         //Utility.Log.Log.writeLineToLog("接收文件--尾块入队列:" + Order_Num.ToString(), "Receive");
     }
     else {
         if (fileObj0.Status == ReceiveFileStatus.StartReceive)
         {
             fileObj0.Status = ReceiveFileStatus.Receiving;
         }
     }
     fileObj0.ReceiveSize = fileObj0.ReceiveSize + CanReadLength;
     FilesQueue.Add(guId, fileObj0);
     result = "true";
     return result;
 }