Ejemplo n.º 1
0
 public bool Add(string guId, DownLoadFileOoj_Server obj)
 {
     DownLoadFileOoj_Server value = null;
     if (ServerDownLoadFilesDic.TryGetValue(guId, out value))
     {
         return ServerDownLoadFilesDic.TryUpdate(guId, obj, value);
     }
     else
     {
         return ServerDownLoadFilesDic.TryAdd(guId, obj);
     }
 }
Ejemplo n.º 2
0
 public bool Get(string guId, out DownLoadFileOoj_Server obj)
 {
     return ServerDownLoadFilesDic.TryGetValue(guId, out obj);
 }
Ejemplo n.º 3
0
 public bool Remove(string guId, out DownLoadFileOoj_Server obj)
 {
     return ServerDownLoadFilesDic.TryRemove(guId, out obj);
 }
 //删除下载文件队列对应的文件Id,会话异常则返回"Session Error";
 public string DeleteServerFileId(string DownFileId, string UserID, string PCClientCookieStr)
 {
     if (SessionCookie.CheckPCClientCookieStr(UserID, PCClientCookieStr))
     {   
     DownLoadFileOoj_Server fileObj = new DownLoadFileOoj_Server();
     bool bResult = DownLoadFilesQueue.Instance.Remove(DownFileId, out fileObj);
     return Newtonsoft.Json.JsonConvert.SerializeObject(bResult);
     }
     else
     {
         return "Session Error";
     }        
 }
        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);
        }
 //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 DownLoadFile(string path, string fileName, string guId, string UserID, string PCClientCookieStr, out string msg, out long FileSize)
 {
     if (!SessionCookie.CheckPCClientCookieStr(UserID, PCClientCookieStr))
     {
         msg = "Session Error";
         FileSize = 0;
         return false;
     }
     bool result = false;
     try
     {
         string fileFullPath = Path.Combine(FilesBlocksQueue.Instance.SavePath + path, fileName);//合并路径生成文件存放路径 
         if (!File.Exists(fileFullPath))
         {
             msg = "File not find";
             FileSize = 0;
             return result;
         }
         FileInfo fileInfo = new FileInfo(fileFullPath);
         FileSize = fileInfo.Length;
             DownLoadFileOoj_Server fileObj = new DownLoadFileOoj_Server();
             fileObj.FileName = fileName;
             fileObj.serverDir = path;
             fileObj.FileSize = fileInfo.Length;
             fileObj.ReadSize = 0;
             fileObj.SendSize = 0;
             fileObj.ReadStatus = GetFileReadStatus.StartRead;
             fileObj.SendStatus = GetFileSendStatus.waiting;
             fileObj.guId = guId;
             DownLoadFilesQueue.Instance.Add(guId, fileObj);
             msg = "File have find, reading...";
             lock (mylock0)
             {
                 ParameterizedThreadStart ParStart = new ParameterizedThreadStart(ThreadMethod);
                 Thread myThread = new Thread(ParStart);
                 myThread.IsBackground = true;
                 FileThreadPramObj o = new FileThreadPramObj();
                 o.guId = guId;
                 o.myThread = myThread;
                 myThread.Start(o);
                 result = true;
             }
             //Action<Stream, string, string, string> readFile = SafeReadFile;
             //readFile.BeginInvoke(fs, guId, fileName, path, null, null);
             //result = true;
        
     }
     catch (Exception ex)
     {
         msg = "error";
         result = false;
         throw ex;               
     }
     return result;
 }