Esempio n. 1
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);
            }
        }
Esempio n. 2
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;
            }
        }