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); }
//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); } }
//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); }
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; } }
public bool Remove(out TransferFileDataObj obj) { //return ServerFilesBlocksQueue.TryDequeue(out obj); return(ServerFilesBlocksBag.TryTake(out obj)); }