// public static bool GetFile(string path, string fileName, string guId, out string msg) //{ // if (GlobalStaticObj.Instance.proxyFile.DownLoadFile(path, fileName, guId, out msg)) // { // DownLoadFileOoj_Client fileObj = new DownLoadFileOoj_Client(); // fileObj.FileName = fileName; // fileObj.guId = guId; // fileObj.DownLoadStatus = GetFileDownLoadStatus.waiting; // DownLoadFilesQueue.Instance.Add(guId, fileObj); // DownLoadFileMsg(new DownLoadFileEventArgs(guId,fileObj)); // loadData(path, fileName, guId); // return true; // } // return false; //} private static void ThreadDownLoadMethod(object ParObject) { FileThreadPramObj o = (FileThreadPramObj)ParObject; DownLoadFileOoj_Client fileObj0 = new DownLoadFileOoj_Client(); DownLoadFilesQueue.Instance.Get(o.guId, out fileObj0); fileObj0.DownLoadStatus = GetFileDownLoadStatus.StartDownLoad; fileObj0.DownLoadSize = 0; DownLoadFilesQueue.Instance.Add(o.guId, fileObj0); DownLoadFileMsg(new DownLoadFileEventArgs(o.guId, fileObj0)); bool firstWrite = true; while (true) { if (!firstWrite) { DownLoadFileOoj_Client fileObj1 = new DownLoadFileOoj_Client(); DownLoadFilesQueue.Instance.Get(o.guId, out fileObj1); if ((System.DateTime.Now - fileObj1.getFileTime).TotalMinutes > 3)//写入等待超时3分钟 { o.myThread.Abort(); o.myThread = null; } } DownLoadFileDataObj obj = new DownLoadFileDataObj(); if (GlobalStaticObj.Instance.proxyFile.GetSucDataBlock(o.guId,UserId, PCClientCookieStr)) { string resultStr = GlobalStaticObj.Instance.proxyFile.DownLoadFileDataBlock(o.guId,UserId, PCClientCookieStr); if (!string.IsNullOrEmpty(resultStr)) { DownLoadFileDataObj dataObj = JsonConvert.DeserializeObject<DownLoadFileDataObj>(resultStr); if (dataObj != null) { DownLoadFilesQueue.Instance.Get(o.guId, out fileObj0); firstWrite = false; DownLoadFilesBlocksQueue.Instance.Add(dataObj); fileObj0.DownLoadStatus = GetFileDownLoadStatus.DownLoading; fileObj0.DownLoadSize = fileObj0.DownLoadSize + dataObj.CanReadLength; fileObj0.getFileTime = System.DateTime.Now; if (fileObj0.DownLoadSize == fileObj0.FileSize) { fileObj0.DownLoadStatus = GetFileDownLoadStatus.DownLoadEnd; } DownLoadFilesQueue.Instance.Add(o.guId, fileObj0); DownLoadFileMsg(new DownLoadFileEventArgs(o.guId, fileObj0)); if (fileObj0.DownLoadSize == fileObj0.FileSize) { Thread.Sleep(300); o.myThread.Abort(); o.myThread = null; } } } } } }
public void SafeReadFile(Stream stream, string guId, string FileName,string path) { //文件数据块编号 int _order_Num = 0; //初始化一个32k的缓存 byte[] buffer = new byte[32768]; int read = 0; int block; //每次从流中读取缓存大小的数据,知道读取完所有的流为止 DownLoadFileOoj_Server fileObj0 = new DownLoadFileOoj_Server(); DownLoadFilesQueue.Instance.Get(guId, out fileObj0); fileObj0.ReadStatus = GetFileReadStatus.Reading; DownLoadFilesQueue.Instance.Add(guId, fileObj0); while ((block = stream.Read(buffer, 0, buffer.Length)) > 0) { //重新设定读取位置 read += block; //检查是否到达了缓存的边界,检查是否还有可以读取的信息 if (block == buffer.Length) { //压入文件块传输缓存队列 DownLoadFileDataObj fileBlock = new DownLoadFileDataObj(); fileBlock.guId = guId; Array.Copy(buffer, fileBlock.FileData, buffer.Length); fileBlock.serverDir = path; fileBlock.Order_Num = _order_Num; DownLoadFilesBlocksQueue.Instance.Add(fileBlock); DownLoadFilesQueue.Instance.Get(guId, out fileObj0); fileObj0.ReadSize = fileObj0.ReadSize + block; fileObj0.ReadStatus = GetFileReadStatus.Reading; DownLoadFilesQueue.Instance.Add(guId, fileObj0); // 尝试读取一个字节 int nextByte = stream.ReadByte(); // 读取失败则说明读取完成可以返回结果 if (nextByte == -1) { fileBlock.EndFlag = true; } else { fileBlock.EndFlag = false; } stream.Seek(-1, SeekOrigin.Current); _order_Num = _order_Num + 1; //清空缓存区 Array.Clear(buffer, 0, buffer.Length); } } //压入文件块传输缓存队列 DownLoadFileDataObj fileBlockEnd = new DownLoadFileDataObj(); fileBlockEnd.guId = guId; Array.Copy(buffer, fileBlockEnd.FileData, buffer.Length); fileBlockEnd.serverDir = path; fileBlockEnd.Order_Num = _order_Num; fileBlockEnd.EndFlag = true; DownLoadFilesBlocksQueue.Instance.Add(fileBlockEnd); //更新Files队列中,对应File的ReadSize DownLoadFilesQueue.Instance.Get(guId, out fileObj0); fileObj0.ReadSize = fileObj0.ReadSize + fileBlockEnd.FileData.Length; fileObj0.ReadStatus = GetFileReadStatus.ReadEnd; DownLoadFilesQueue.Instance.Add(guId, fileObj0); //清空缓存区 Array.Clear(buffer, 0, buffer.Length); }
public void Add(DownLoadFileDataObj item) { PCClientDownLoadFileBlocksBag.Add(item); Interlocked.Increment(ref queueLength); lock (mylock3) { if (item.Order_Num == 1) { ParameterizedThreadStart ParStart = new ParameterizedThreadStart(ThreadWriteFileMethod); Thread myThread = new Thread(ParStart); myThread.IsBackground = true; FileThreadPramObj o = new FileThreadPramObj(); o.guId = item.guId; o.myThread = myThread; myThread.Start(o); } } }
//public string DownLoadFileDataBlock(string path, string fileName, string guId) //{ // string objStr = string.Empty; // DownLoadFileDataObj dataBlock0 = new DownLoadFileDataObj(); // foreach (DownLoadFileDataObj obj in DownLoadFilesBlocksQueue.Instance.ServerDownLoadFileBlocksQueue) // { // if (obj.guId == guId) // { // dataBlock0 = obj; // } // } // objStr = Newtonsoft.Json.JsonConvert.SerializeObject(dataBlock0); // return objStr; //} public string DownLoadFileDataBlock(string guId, string UserID, string PCClientCookieStr) { if (!SessionCookie.CheckPCClientCookieStr(UserID, PCClientCookieStr)) { return "Session Error"; } string objStr = string.Empty; DownLoadFileDataObj dataBlock0 = null; //foreach (DownLoadFileDataObj obj in DownLoadFilesBlocksQueue.Instance.ServerDownLoadFileBlocksQueue) //{ // if (obj.guId == guId) // { // dataBlock0 = obj; // } //} //for (int i = 0; i < DownLoadFilesBlocksQueue.Instance.ServerDownLoadFileBlocksBag.Count; i++) //{ DownLoadFileDataObj block = null; //DownLoadFileDataObj block = DownLoadFilesBlocksQueue.Instance.ServerDownLoadFileBlocksBag.ElementAt<DownLoadFileDataObj>(i); if(DownLoadFilesBlocksQueue.Instance.ServerDownLoadFileBlocksBag.TryPeek(out block)) { if (block.guId == guId) { dataBlock0 = new DownLoadFileDataObj(); dataBlock0.CanReadLength = block.CanReadLength; dataBlock0.EndFlag = block.EndFlag; dataBlock0.FileData = block.FileData; dataBlock0.FileName = block.FileName; dataBlock0.guId = block.guId; dataBlock0.Order_Num = block.Order_Num; dataBlock0.serverDir = block.serverDir; DownLoadFilesBlocksQueue.Instance.ServerDownLoadFileBlocksBag.TryTake(out block); } } //} if (dataBlock0 == null) { objStr = string.Empty; } else { objStr = Newtonsoft.Json.JsonConvert.SerializeObject(dataBlock0); } return objStr; }
//ThreadMethod如下: public void ThreadMethod(object ParObject) { FileThreadPramObj o = (FileThreadPramObj)ParObject; DownLoadFileOoj_Server fileObj0 = new DownLoadFileOoj_Server(); DownLoadFilesQueue.Instance.Get(o.guId, out fileObj0); //文件数据块编号 int bufferLength = 32768; int _order_Num = 0; //初始化一个32k的缓存 byte[] buffer = new byte[bufferLength]; int read = 0; int block; int byteLength = 0; //每次从流中读取缓存大小的数据,知道读取完所有的流为止 fileObj0.ReadStatus = GetFileReadStatus.StartRead; DownLoadFilesQueue.Instance.Add(o.guId, fileObj0); string fileFullPath = Path.Combine(FilesBlocksQueue.Instance.SavePath + fileObj0.serverDir, fileObj0.FileName);//合并路径生成文件存放路径 using (FileStream stream = new FileStream(fileFullPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { while ((block = stream.Read(buffer, 0, buffer.Length)) > 0) { byteLength = byteLength + block; //重新设定读取位置 read += block; _order_Num = _order_Num + 1; //检查是否到达了缓存的边界,检查是否还有可以读取的信息 if (block == buffer.Length) { DownLoadFilesQueue.Instance.Get(o.guId, out fileObj0); //压入文件块传输缓存队列 DownLoadFileDataObj fileBlock = new DownLoadFileDataObj(); fileBlock.guId = o.guId; Array.Copy(buffer, fileBlock.FileData, buffer.Length); fileBlock.serverDir = fileObj0.serverDir; fileBlock.Order_Num = _order_Num; fileBlock.FileName = fileObj0.FileName; fileBlock.CanReadLength = bufferLength; fileObj0.ReadSize = fileObj0.ReadSize + block; fileObj0.ReadStatus = GetFileReadStatus.Reading; // 尝试读取一个字节 int nextByte = stream.ReadByte(); // 读取失败则说明读取完成可以返回结果 if (nextByte == -1) { fileBlock.EndFlag = true; } else { fileBlock.EndFlag = false; } DownLoadFilesBlocksQueue.Instance.Add(fileBlock); //Utility.Log.Log.writeLineToLog("发送文件读取文件块:" + fileBlock.Order_Num.ToString(), "DownLoadRead"); DownLoadFilesQueue.Instance.Add(o.guId, fileObj0); stream.Seek(-1, SeekOrigin.Current); //清空缓存区 Array.Clear(buffer, 0, buffer.Length); byteLength = 0; } } //压入文件块传输缓存队列 DownLoadFileDataObj fileBlockEnd = new DownLoadFileDataObj(); fileBlockEnd.guId = o.guId; Array.Copy(buffer, fileBlockEnd.FileData, byteLength); fileBlockEnd.serverDir = fileObj0.serverDir; fileBlockEnd.FileName = fileObj0.FileName; fileBlockEnd.Order_Num = _order_Num; fileBlockEnd.EndFlag = true; fileBlockEnd.CanReadLength = byteLength; DownLoadFilesBlocksQueue.Instance.Add(fileBlockEnd); //Utility.Log.Log.writeLineToLog("发送文件读取文件块:" + fileBlockEnd.Order_Num.ToString(), "DownLoadRead"); //更新Files队列中,对应File的ReadSize DownLoadFilesQueue.Instance.Get(o.guId, out fileObj0); fileObj0.ReadSize = fileObj0.ReadSize + byteLength; fileObj0.ReadStatus = GetFileReadStatus.ReadEnd; DownLoadFilesQueue.Instance.Add(o.guId, fileObj0); //清空缓存区 Array.Clear(buffer, 0, buffer.Length); } }
public bool Remove(out DownLoadFileDataObj obj) { return ServerDownLoadFileBlocksBag.TryTake(out obj); }
public bool Get(out DownLoadFileDataObj obj) { return ServerDownLoadFileBlocksBag.TryPeek(out obj); }
//public ConcurrentQueue<DownLoadFileDataObj> ServerDownLoadFileBlocksQueue //{ // get // { // if (_serverDownLoadFileBlocksQueue == null) // { // lock (locker) // { // if (_serverDownLoadFileBlocksQueue == null) // { // _serverDownLoadFileBlocksQueue = new ConcurrentQueue<DownLoadFileDataObj>(); // } // } // } // return _serverDownLoadFileBlocksQueue; // } //} public void Add(DownLoadFileDataObj item) { ServerDownLoadFileBlocksBag.Add(item); }