public FileQueue(FilePackageInfo info) { PackageQueues = new List <Queue <UserPackage> >(); Queueinfos = new List <QueueInfo>(); ProgressSensitive = _maxcache; Info = info; }
/// <summary> /// 根据文件信息生成队列信息 /// </summary> /// <param name="info"></param> private static QueueInfo[] generatequeueinfo(ref FilePackageInfo info, FileQueue instance) { QueueInfo[] infos = new QueueInfo[info.ThreadsCounts]; var FilePackages = (long)Math.Round((double)(info.FileSize / instance._packsize)) + 1; int queuelength = (int)Math.Round((double)(FilePackages / info.ThreadsCounts)) + 1; for (int i = 0; i < info.ThreadsCounts; i++) { QueueInfo qinfo = new QueueInfo(); if (i == info.ThreadsCounts - 1) { qinfo.PackCount = (int)(FilePackages - i * queuelength); qinfo.Offset = i * queuelength * instance._packsize; qinfo.Length = info.FileSize - qinfo.Offset; } else { qinfo.PackCount = queuelength; qinfo.Offset = i * queuelength * instance._packsize; qinfo.Length = qinfo.PackCount * instance._packsize; } infos[i] = qinfo; } return(infos); }
/// <summary> /// 根据文件大小以及所用线程数分配包大小及最大包缓存 /// </summary> /// <param name="instance"></param> private static void prearrange(FileQueue instance) { FilePackageInfo info = instance.Info; if (info.FileSize < 1 << 25) { // less than 32MB instance._maxcache = 256; instance._packsize = 1440; } else if ((info.FileSize >= 1 << 25) && (info.FileSize < 1 << 30)) { // 32 - 1024 MB instance._maxcache = 128; //UserPackage has a 32 bit control data instance._packsize = 14720 - 32; } else { // more than 1GB instance._maxcache = 256; //UserPackage has a 32 bit control data instance._packsize = 14720 - 32; } }
/// <summary> /// 将文件分包 /// </summary> /// <param name="filename">要分包的文件名称</param> /// <param name="workingthreads">使用的线程数,产生同数量的包队列</param> /// <param name="queue">文件包队列实例</param> /// <param name="OnReady"><打包准备完成时触发/param> /// <param name="OnProgress">打包过程中触发</param> /// <returns></returns> public void Package( string filename, int workingthreads, ref FileQueue queue, ref FilePackageInfo fpinfo, EventHandler <FileQueueArgs> OnReady, EventHandler <FileQueueArgs> OnProgress) { package(filename, workingthreads, ref queue, ref fpinfo, OnReady, OnProgress); }
/// <summary> /// /// </summary> /// <param name="instance"></param> private static void unpack(FileQueue instance) { FilePackageInfo info = instance.Info; MemoryMappedFile file = MemoryMappedFile.CreateFromFile(instance._filefullpath, FileMode.OpenOrCreate, info.FileName + "cached", info.FileSize, MemoryMappedFileAccess.ReadWrite); instance._filecache = file; instance.Queueinfos.AddRange(generatequeueinfo(ref info, instance)); for (int threads = instance.PackageQueues.Count; threads < info.ThreadsCounts; threads++) { unpackqueue(threads, instance, file); } }
/// <summary> /// 开始打包线程 /// </summary> private static void pack(FileQueue instance, CancellationTokenSource cancle = null) { FilePackageInfo info = instance.Info; MemoryMappedFile file = MemoryMappedFile.CreateFromFile(instance._filefullpath, FileMode.Open, info.FileName + "cached", 0, MemoryMappedFileAccess.ReadExecute); instance._filecache = file; instance.Queueinfos.AddRange(generatequeueinfo(ref info, instance)); for (int threads = instance.PackageQueues.Count; threads < info.ThreadsCounts; threads++) { packqueue(threads, instance, file); } }
/// <summary> /// 分包文件 /// </summary> /// <param name="filename"></param> private static void package(string filename, int workingthreads, ref FileQueue queue, ref FilePackageInfo fpinfo, EventHandler <FileQueueArgs> ready, EventHandler <FileQueueArgs> completed) { if (!File.Exists(filename)) { throw new ArgumentException("File Not Exist !"); } FileInfo info = new FileInfo(filename); FilePackageInfo fp = new FilePackageInfo { FileName = info.Name, FileSize = info.Length, ThreadsCounts = workingthreads }; fpinfo = fp; // packagesize = all data packages FileQueue fq = new FileQueue(fp, filename); queue = fq; fq.Ready += ready; fq.Progress += completed; fq.Pack(); }
public FileQueue(FilePackageInfo info, string fullpath) : this(info) { _filefullpath = fullpath; }
private static void unpackage(string filename, int workingthreads, ref FileQueue queue, ref FilePackageInfo fpinfo, EventHandler <FileQueueArgs> ready, EventHandler <FileQueueArgs> completed) { }
/// <summary> /// 组装文件包 /// </summary> /// <param name="info"></param> public void UnPackage(string filename, FilePackageInfo info, Action <double> Prograss) { }