C# 并發(fā)讀寫內(nèi)存文件映射

using System;

using System.Collections.Generic;

using System.Runtime.InteropServices;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading;

using SharpGLWinformsApplication1.General;

using System.Windows.Forms;

using System.Collections;

namespace SharpGLWinformsApplication1.General

{

? ? #region 內(nèi)存映射大文件

? ? public class ShareMemory

? ? {

? ? ? ? /*

? ? ? ? *0.獲取文件句柄 :_lopen

? ? ? ? *1.創(chuàng)建內(nèi)核文件獲得句柄 : CreateFile

? ? ? ? *2.根據(jù)文件句柄獲取共享內(nèi)存句柄 :CreateFileMapping

? ? ? ? *3.根據(jù)讀取共享內(nèi)存大小獲取數(shù)據(jù)塊的句柄 : MapViewOfFile

? ? ? ? *4.根據(jù)數(shù)據(jù)塊句柄讀取文件內(nèi)容到byte數(shù)組中

? ? ? ? *5.讀完之后,清除使用過的句柄 : UnmapViewOfFile

? ? ? ? *6.獲取下一塊數(shù)據(jù)塊大小和句柄央渣,獲取下一塊內(nèi)存大小

? ? ? ? *概述:首先要通過CreateFile()函數(shù)來創(chuàng)建或打開一個文件內(nèi)核對象,這個對象標識了磁盤上將要用作內(nèi)存映射文件的文件。在用CreateFile()將文件映像在物理存儲器的位置通告給操作系統(tǒng)后,只指定了映像文件的路徑迄埃,映像的長度還沒有指定。為了指定文件映射對象需要多大的物理存儲空間還需要通過CreateFileMapping()函數(shù)來創(chuàng)建一個文件映射內(nèi)核對象以告訴系統(tǒng)文件的尺寸以及訪問文件的方式。在創(chuàng)建了文件映射對象后垦江,還必須為文件數(shù)據(jù)保留一個地址空間區(qū)域,并把文件數(shù)據(jù)作為映射到該區(qū)域的物理存儲器進行提交搅方。由MapViewOfFile()函數(shù)負責通過系統(tǒng)的管理而將文件映射對象的全部或部分映射到進程地址空間比吭。此時,對內(nèi)存映射文件的使用和處理同通常加載到內(nèi)存中的文件數(shù)據(jù)的處理方式基本一樣姨涡,在完成了對內(nèi)存映射文件的使用時衩藤,還要通過一系列的操作完成對其的清除和使用過資源的釋放。這部分相對比較簡單涛漂,可以通過UnmapViewOfFile()完成從進程的地址空間撤消文件數(shù)據(jù)的映像赏表、通過CloseHandle()關(guān)閉前面創(chuàng)建的文件映射對象和文件對象。

? ? ? ? */

? ? ? ? /// <summary>

? ? ? ? /// 獲取當前線程文件句柄

? ? ? ? /// </summary>

? ? ? ? /// <param name="lpPathName">file path</param>

? ? ? ? /// <param name="iReadWrite"></param>

? ? ? ? /// <returns></returns>

? ? ? ? [DllImport("kernel32.dll")]

? ? ? ? public static extern IntPtr _lopen(string lpPathName, int iReadWrite);

? ? ? ? [DllImport("user32.dll", CharSet = CharSet.Auto)]

? ? ? ? public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam);


? ? ? ? /// <summary>

? ? ? ? /// 創(chuàng)建共分享文件

? ? ? ? /// </summary>

? ? ? ? /// <param name="hFile">內(nèi)核文件句柄</param>

? ? ? ? /// <param name="lpAttributes">安全模式</param>

? ? ? ? /// <param name="flProtect">打開模式匈仗,份配對句柄的權(quán)限瓢剿,例如讀寫</param>

? ? ? ? /// <param name="dwMaxSizeHi">將文件大小轉(zhuǎn)換成64進制的前32位高位</param>

? ? ? ? /// <param name="dwMaxSizeLow">將文件大小轉(zhuǎn)換成64進制的后32位低位</param>

? ? ? ? /// <param name="lpName">文件名,不在一臺機器上沖突就行</param>

? ? ? ? /// <returns></returns>

? ? ? ? [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]

? ? ? ? public static extern IntPtr CreateFileMapping(IntPtr hFile, IntPtr lpAttributes, uint flProtect, uint dwMaxSizeHi, uint dwMaxSizeLow, string lpName);

? ? ? ? [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]

? ? ? ? public static extern IntPtr OpenFileMapping(int dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, string lpName);

? ? ? ? /// <summary>

? ? ? ? /// 創(chuàng)建映射文件

? ? ? ? /// </summary>

? ? ? ? /// <param name="hFileMapping">創(chuàng)建映射文件句柄</param>

? ? ? ? /// <param name="dwDesiredAccess">安全模式</param>

? ? ? ? /// <param name="dwMaxSizeHi">將文件讀到大小轉(zhuǎn)換成64進制的前32位高位</param>

? ? ? ? /// <param name="dwMaxSizeLow">將文件讀到大小轉(zhuǎn)換成64進制的后32位低位</param>

? ? ? ? /// <param name="dwNumberOfBytesToMap">創(chuàng)建映射的大小锚沸,不設(shè)置也可以使用但是會拋異常跋选,太大也會排異暴心,建議50-10M</param>

? ? ? ? /// <returns></returns>

? ? ? ? [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]

? ? ? ? public static extern IntPtr MapViewOfFile(IntPtr hFileMapping, uint dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap);

? ? ? ? /// <summary>

? ? ? ? /// 關(guān)閉映射文件句柄資源占用

? ? ? ? /// </summary>

? ? ? ? /// <param name="handle">映射文件句柄</param>

? ? ? ? /// <returns></returns>

? ? ? ? [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]

? ? ? ? public static extern bool UnmapViewOfFile(IntPtr pvBaseAddress);

? ? ? ? /// <summary>

? ? ? ? /// 關(guān)閉文件句柄

? ? ? ? /// </summary>

? ? ? ? /// <param name="handle">文件句柄</param>

? ? ? ? /// <returns></returns>

? ? ? ? [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]

? ? ? ? public static extern bool CloseHandle(IntPtr handle);

? ? ? ? /// <summary>

? ? ? ? /// 獲取上一個錯誤

? ? ? ? /// </summary>

? ? ? ? [DllImport("kernel32", EntryPoint = "GetLastError")]

? ? ? ? public static extern int GetLastError();

? ? ? ? [DllImport("kernel32.dll")]

? ? ? ? static extern void GetSystemInfo(out SYSTEM_INFO lpSystemInfo);

? ? ? ? [StructLayout(LayoutKind.Sequential)]

? ? ? ? public struct SYSTEM_INFO

? ? ? ? {

? ? ? ? ? ? public ushort processorArchitecture;

? ? ? ? ? ? ushort reserved;

? ? ? ? ? ? public uint pageSize;

? ? ? ? ? ? public IntPtr minimumApplicationAddress;

? ? ? ? ? ? public IntPtr maximumApplicationAddress;

? ? ? ? ? ? public IntPtr activeProcessorMask;

? ? ? ? ? ? public uint numberOfProcessors;

? ? ? ? ? ? public uint processorType;

? ? ? ? ? ? public uint allocationGranularity;

? ? ? ? ? ? public ushort processorLevel;

? ? ? ? ? ? public ushort processorRevision;

? ? ? ? }

? ? ? ? /// <summary>

? ? ? ? /// 獲取系統(tǒng)的分配粒度

? ? ? ? /// </summary>

? ? ? ? /// <returns></returns>

? ? ? ? public static uint GetPartitionsize()

? ? ? ? {

? ? ? ? ? ? SYSTEM_INFO sysInfo;

? ? ? ? ? ? GetSystemInfo(out sysInfo);

? ? ? ? ? ? return sysInfo.allocationGranularity;

? ? ? ? }

? ? ? ? const int ERROR_ALREADY_EXISTS = 183;

? ? ? ? const int FILE_MAP_COPY = 0x0001;

? ? ? ? const int FILE_MAP_WRITE = 0x0002;

? ? ? ? const int FILE_MAP_READ = 0x0004;

? ? ? ? const int FILE_MAP_ALL_ACCESS = 0x0002 | 0x0004;

? ? ? ? const int PAGE_READONLY = 0x02;

? ? ? ? const int PAGE_READWRITE = 0x04;

? ? ? ? const int PAGE_WRITECOPY = 0x08;

? ? ? ? const int PAGE_EXECUTE = 0x10;

? ? ? ? const int PAGE_EXECUTE_READ = 0x20;

? ? ? ? const int PAGE_EXECUTE_READWRITE = 0x40;

? ? ? ? const int SEC_COMMIT = 0x8000000;

? ? ? ? const int SEC_IMAGE = 0x1000000;

? ? ? ? const int SEC_NOCACHE = 0x10000000;

? ? ? ? const int SEC_RESERVE = 0x4000000;

? ? ? ? //文件句柄

? ? ? ? IntPtr m_fHandle;

? ? ? ? //共享內(nèi)存文件句柄

? ? ? ? IntPtr m_hSharedMemoryFile = IntPtr.Zero;

? ? ? ? //當前句柄

? ? ? ? IntPtr m_pwData = IntPtr.Zero;

? ? ? ? //異步操作時下一個句柄

? ? ? ? IntPtr syn_m_pwData = IntPtr.Zero;

? ? ? ? bool m_bAlreadyExist = false;

? ? ? ? bool m_bInit = false;

? ? ? ? //緩沖區(qū)大小识颊,初始化的值不一定被采用

? ? ? ? uint m_MemSize = 0x1400000;//20M

? ? ? ? //下一個數(shù)據(jù)塊的長度

? ? ? ? public? uint nextMLength = 0;

? ? ? ? //讀到的位置

? ? ? ? long m_offsetBegin = 0;

? ? ? ? //文件大小

? ? ? ? long m_FileSize = 0;

? ? ? ? public static IntPtr hTemplateFile = IntPtr.Zero;

? ? ? ? FileReader File = new FileReader();

? ? ? ? List<IntPtr> syn_m_pwDatas = new List<IntPtr>();

? ? ? ? public long get_m_FileSize()

? ? ? ? {

? ? ? ? ? ? return m_FileSize;

? ? ? ? }

? ? ? ? public long get_m_offsetBegin()

? ? ? ? {

? ? ? ? ? ? return m_offsetBegin;

? ? ? ? }

? ? ? ? //配合異步操作及時執(zhí)行,

? ? ? ? public void do_syn_m_pwData()

? ? ? ? {

? ? ? ? ? m_pwData = syn_m_pwData;

? ? ? ? }

? ? ? ? public IntPtr get_m_pwData()

? ? ? ? {

? ? ? ? ? ? return m_pwData;

? ? ? ? }

? ? ? ? /// <summary>

? ? ? ? ///? 初始化文件

? ? ? ? /// 解釋:

? ? ? ? /// 1卑笨,可以指定讀取塊的大小距潘,一般不使用

? ? ? ? /// </summary>

? ? ? ? /// <param name="MemSize">緩沖大小</param>

? ? ? ? /// <param name="filename">文件路徑</param>

? ? ? ? public ShareMemory(string filename, uint memSize)

? ? ? ? {

? ? ? ? ? ? // 分頁映射文件時炼列,每頁的起始位置startpos,必須為64K的整數(shù)倍。

? ? ? ? ? ? // memSize即緩存區(qū)的大小必須是系統(tǒng)分配粒度的整倍說音比,window系統(tǒng)的分配粒度是64KB

? ? ? ? ? ? this.m_MemSize = memSize;


? ? ? ? ? ? Init(filename);

? ? ? ? }

? ? ? ? /// <summary>.

? ? ? ? /// 初始化俭尖,常用的函數(shù),執(zhí)行的第1步

? ? ? ? /// 解釋:

? ? ? ? /// 1洞翩,默認映射 GetPartitionsize()*800時52.4M緩沖//2000=131M

? ? ? ? /// 2稽犁,創(chuàng)建文件句柄,設(shè)置為讀寫模式骚亿,這是獲取的第1個句柄已亥,其他繼承這個句柄都需要模式與上級模式相同

? ? ? ? /// </summary>

? ? ? ? /// <param name="filename"></param>

? ? ? ? public ShareMemory(string filename)

? ? ? ? {

? ? ? ? ? ? this.m_MemSize =? GetPartitionsize()*800;//2000=131M

? ? ? ? ? ? if (!System.IO.File.Exists(filename)) throw new Exception("未找到文件");

? ? ? ? ? // this.m_offsetBegin = this.m_MemSize;

? ? ? ? ? ? hTemplateFile = _lopen(filename, FILE_MAP_READ |FILE_MAP_WRITE );

? ? ? ? ? ? Init(filename);

? ? ? ? }

? ? ? ? //析構(gòu)函數(shù)

? ? ? ? ~ShareMemory()

? ? ? ? {

? ? ? ? ? /// Close();

? ? ? ? }

? ? ? ? /// <summary>

? ? ? ? /// 初始化共享內(nèi)存,第2步

? ? ? ? /// 解釋:

? ? ? ? /// 1来屠, File.Open(strName);虑椎,獲取內(nèi)核文件句柄震鹉,并獲取讀寫訪問權(quán),第2個句柄

? ? ? ? /// 2捆姜,CreateFileMapping传趾,獲取共享文件句柄,并獲取讀寫訪問權(quán)泥技,第3個句柄

? ? ? ? /// 共享內(nèi)存名稱

? ? ? ? /// 共享內(nèi)存大小

? ? ? ? /// </summary>

? ? ? ? /// <param name="strName"></param>

? ? ? ? protected void Init(string strName)

? ? ? ? {

? ? ? ? ? ? //if (lngSize <= 0 || lngSize > 0x00800000) lngSize = 0x00800000;

? ? ? ? ? ? //創(chuàng)建文件IO

? ? ? ? ? ? System.IO.FileInfo f = new System.IO.FileInfo(strName);

? ? ? ? ? ? //獲取文件大小

? ? ? ? ? ? m_FileSize = f.Length;


? ? ? ? ? ? //獲取內(nèi)核文件句柄

? ? ? ? ? ? m_fHandle = File.Open(strName);

? ? ? ? ? ? if (strName.Length > 0)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? //創(chuàng)建文件映射

? ? ? ? ? ? ? ? //m_hSharedMemoryFile = CreateFileMapping(m_fHandle, IntPtr.Zero, (uint)PAGE_READONLY, 0, (uint)m_FileSize, "mdata");

? ? ? ? ? ? ? ? //const int PAGE_READONLY = 0x02;

? ? ? ? ? ? ? ? //const int PAGE_READWRITE = 0x04;

? ? ? ? ? ? ? ? //const int PAGE_WRITECOPY = 0x08;

? ? ? ? ? ? ? ? //const int PAGE_EXECUTE = 0x10;

? ? ? ? ? ? ? ? //const int PAGE_EXECUTE_READ = 0x20;

? ? ? ? ? ? ? ? //const int PAGE_EXECUTE_READWRITE = 0x40;

? ? ? ? ? ? ? ? m_hSharedMemoryFile = CreateFileMapping(m_fHandle, IntPtr.Zero, (uint)PAGE_READWRITE, 0, (uint)m_FileSize/*(uint)m_FileSize-*/, "~MappingTemp");

? ? ? ? ? ? ? ? if (m_hSharedMemoryFile == IntPtr.Zero)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? m_bAlreadyExist = false;

? ? ? ? ? ? ? ? ? ? m_bInit = false;

? ? ? ? ? ? ? ? ? ? throw new Exception("CreateFileMapping失敗LastError=" + GetLastError().ToString());

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? else

? ? ? ? ? ? ? ? ? ? m_bInit = true;

? ? ? ? ? ? ? ? ////映射第一塊文件

? ? ? ? ? ? ? ? //m_pwData = MapViewOfFile(m_hSharedMemoryFile, FILE_MAP_READ, 0, 0, (uint)m_MemSize);

? ? ? ? ? ? ? ? //if (m_pwData == IntPtr.Zero)

? ? ? ? ? ? ? ? //{

? ? ? ? ? ? ? ? //? ? m_bInit = false;

? ? ? ? ? ? ? ? //? ? throw new Exception("m_hSharedMemoryFile失敗LastError=" + GetLastError().ToString());

? ? ? ? ? ? ? ? //}

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? /// <summary>

? ? ? ? /// 獲取高32位

? ? ? ? /// </summary>

? ? ? ? /// <param name="intValue"></param>

? ? ? ? /// <returns></returns>

? ? ? ? private static uint GetHighWord(UInt64 intValue)

? ? ? ? {

? ? ? ? ? ? return Convert.ToUInt32(intValue >> 32);

? ? ? ? }

? ? ? ? /// <summary>

? ? ? ? /// 獲取低32位

? ? ? ? /// </summary>

? ? ? ? /// <param name="intValue"></param>

? ? ? ? /// <returns></returns>

? ? ? ? private static uint GetLowWord(UInt64 intValue)

? ? ? ? {

? ? ? ? ? ? return Convert.ToUInt32(intValue & 0x00000000FFFFFFFF);

? ? ? ? }

? ? ? ? /// <summary>

? ? ? ? /// 獲取下一個文件塊 塊大小為20M

? ? ? ? /// </summary>

? ? ? ? /// <returns>false 表示已經(jīng)是最后一塊文件</returns>

? ? ? ? public uint GetNextblock()

? ? ? ? {

? ? ? ? ? ? if (m_pwData != IntPtr.Zero && m_pwData != new IntPtr(-1))

? ? ? ? ? ? {

? ? ? ? ? ? ? ? //卸載前一個文件

? ? ? ? ? ? ? ? try { UnmapViewOfFile(m_pwData); }

? ? ? ? ? ? ? ? catch { throw new Exception("緩存句柄銷毀異常"); }

? ? ? ? ? ? }

? ? ? ? ? ? if (!this.m_bInit) throw new Exception("文件未初始化,at? GetNextblock浆兰。");

? ? ? ? ? ? //if ( m_offsetBegin + m_MemSize >= m_FileSize ) return false;

? ? ? ? ? ? uint m_Size = GetMemberSize();

? ? ? ? ? ? if (m_Size == 0) return m_Size;

? ? ? ? ? ? // 更改緩沖區(qū)大小

? ? ? ? ? ? m_MemSize = m_Size;

? ? ? ? ? ? //卸載前一個文件

? ? ? ? ? ? //bool l_result = UnmapViewOfFile( m_pwData );

? ? ? ? ? ? //m_pwData = IntPtr.Zero;

? ? ? ? ? ? //try

? ? ? ? ? ? //{

? ? ? ? ? ? ? ? m_pwData = MapViewOfFile(m_hSharedMemoryFile, FILE_MAP_READ | FILE_MAP_WRITE, GetHighWord((UInt64)m_offsetBegin), GetLowWord((UInt64)m_offsetBegin), m_MemSize);

? ? ? ? ? ? ? ? if (m_pwData == IntPtr.Zero)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? //? MapViewOfFile(

? ? ? ? ? ? ? ? //m_hSharedMemoryFile, // Handle to File Mapping object from CreateFileMapping()

? ? ? ? ? ? ? ? //FILE_MAP_READ | FILE_MAP_WRITE, // FILE_MAP_ALL_ACCESS

? ? ? ? ? ? ? ? //GetHighWord((UInt64)m_offsetBegin), // High order offset (32 bytes) in to start View.

? ? ? ? ? ? ? ? //GetLowWord((UInt64)m_offsetBegin),

? ? ? ? ? ? ? ? //0);

? ? ? ? ? ? ? ? ? ? m_bInit = false;

? ? ? ? ? ? ? ? ? ? if (GetLastError() == 8)

? ? ? ? ? ? ? ? ? ? ? ? throw new Exception("映射文件塊失敗" + GetLastError().ToString() + ":存儲空間不足,無法處理此命令");

? ? ? ? ? ? ? ? ? ? throw new Exception("映射文件塊失敗" + GetLastError().ToString()+",at GetNextblock");

? ? ? ? ? ? ? ? }

? ? ? ? ? ? //}

? ? ? ? ? ? //catch (Exception)

? ? ? ? ? ? //{

? ? ? ? ? ? //? ? MessageBox.Show("文件映射創(chuàng)建失敗", "提示");

? ? ? ? ? ? //? ? try { this.Close(); }

? ? ? ? ? ? //? ? catch { }

? ? ? ? ? ? //}

? ? ? ? ? ? m_offsetBegin = m_offsetBegin + m_Size;

? ? ? ? ? ? return m_Size; //創(chuàng)建成功

? ? ? ? }

? ? ? ? /// <summary>

? ? ? ? /// 獲取下一塊數(shù)據(jù)句長和下個映射句柄,異步保存

? ? ? ? /// </summary>

? ? ? ? /// <returns></returns>

? ? ? ? public uint synGetNextblock()

? ? ? ? {

? ? ? ? ? ? if (!this.m_bInit) throw new Exception("文件未初始化,at? synGetNextblock珊豹。");

? ? ? ? ? ? //if ( m_offsetBegin + m_MemSize >= m_FileSize ) return false;

? ? ? ? ? ? uint m_Size = GetMemberSize();

? ? ? ? ? ? if (m_Size == 0)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? return m_Size;

? ? ? ? ? ? }

? ? ? ? ? ? // 更改緩沖區(qū)大小

? ? ? ? ? ? m_MemSize = m_Size;

? ? ? ? ? ? //卸載前一個文件

? ? ? ? ? ? //bool l_result = UnmapViewOfFile( m_pwData );

? ? ? ? ? ? //m_pwData = IntPtr.Zero;

? ? ? ? ? ? //try

? ? ? ? ? ? //{

? ? ? ? ? ? syn_m_pwData = MapViewOfFile(m_hSharedMemoryFile, FILE_MAP_READ | FILE_MAP_WRITE, GetHighWord((UInt64)m_offsetBegin), GetLowWord((UInt64)m_offsetBegin), m_MemSize);

? ? ? ? ? ? if (syn_m_pwData == IntPtr.Zero)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? //? MapViewOfFile(

? ? ? ? ? ? ? ? //m_hSharedMemoryFile, // Handle to File Mapping object from CreateFileMapping()

? ? ? ? ? ? ? ? //FILE_MAP_READ | FILE_MAP_WRITE, // FILE_MAP_ALL_ACCESS

? ? ? ? ? ? ? ? //GetHighWord((UInt64)m_offsetBegin), // High order offset (32 bytes) in to start View.

? ? ? ? ? ? ? ? //GetLowWord((UInt64)m_offsetBegin),

? ? ? ? ? ? ? ? //0);

? ? ? ? ? ? ? ? m_bInit = false;

? ? ? ? ? ? ? ? if (GetLastError() == 8)

? ? ? ? ? ? ? ? ? ? throw new Exception("映射文件塊失敗" + GetLastError().ToString() + ":存儲空間不足镊讼,無法處理此命令");

? ? ? ? ? ? ? ? throw new Exception("映射文件塊失敗" + GetLastError().ToString()+",at synGetNextblock");

? ? ? ? ? ? }

? ? ? ? ? ? //}

? ? ? ? ? ? //catch (Exception)

? ? ? ? ? ? //{

? ? ? ? ? ? //? ? MessageBox.Show("文件映射創(chuàng)建失敗", "提示");

? ? ? ? ? ? //? ? try { this.Close(); }

? ? ? ? ? ? //? ? catch { }

? ? ? ? ? ? //}

? ? ? ? ? ? m_offsetBegin = m_offsetBegin + m_Size;

? ? ? ? ? ? return m_Size; //創(chuàng)建成功

? ? ? ? }

? ? ? ? /// <summary>

? ? ? ? /// 獲取下一塊數(shù)據(jù)的句柄

? ? ? ? /// </summary>

? ? ? ? /// <returns></returns>

? ? ? ? public IntPtr getNextblockIntPtr()

? ? ? ? {

? ? ? ? ? ? if (!this.m_bInit) throw new Exception("文件未初始化。");

? ? ? ? ? ? //if ( m_offsetBegin + m_MemSize >= m_FileSize ) return false;

? ? ? ? ? ? uint m_Size = GetMemberSize();

? ? ? ? ? ? if (m_Size == 0)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? return IntPtr.Zero;

? ? ? ? ? ? }

? ? ? ? ? ? // 更改緩沖區(qū)大小

? ? ? ? ? ? m_MemSize = m_Size;

? ? ? ? ? ? //卸載前一個文件

? ? ? ? ? ? //bool l_result = UnmapViewOfFile( m_pwData );

? ? ? ? ? ? //m_pwData = IntPtr.Zero;

? ? ? ? ? ? //try

? ? ? ? ? ? //{

? ? ? ? ? ? m_pwData = MapViewOfFile(m_hSharedMemoryFile, FILE_MAP_READ | FILE_MAP_WRITE, GetHighWord((UInt64)m_offsetBegin), GetLowWord((UInt64)m_offsetBegin), m_MemSize);

? ? ? ? ? ? if (m_pwData == IntPtr.Zero)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? m_bInit = false;

? ? ? ? ? ? ? ? if (GetLastError() == 8)

? ? ? ? ? ? ? ? ? ? throw new Exception("映射文件塊失敗" + GetLastError().ToString() + ":存儲空間不足平夜,無法處理此命令");

? ? ? ? ? ? ? ? throw new Exception("映射文件塊失敗" + GetLastError().ToString());

? ? ? ? ? ? }

? ? ? ? ? ? //}

? ? ? ? ? ? //catch (Exception)

? ? ? ? ? ? //{

? ? ? ? ? ? //? ? MessageBox.Show("文件映射創(chuàng)建失敗", "提示");

? ? ? ? ? ? //? ? try { this.Close(); }

? ? ? ? ? ? //? ? catch { }

? ? ? ? ? ? //}

? ? ? ? ? ? m_offsetBegin = m_offsetBegin + m_Size;

? ? ? ? ? ? return m_pwData; //創(chuàng)建成功

? ? ? ? }

? ? ? ? /// <summary>

? ? ? ? /// 返回映射區(qū)大小

? ? ? ? /// </summary>

? ? ? ? /// <returns></returns>

? ? ? ? private uint GetMemberSize()

? ? ? ? {

? ? ? ? ? ? if (m_offsetBegin >= m_FileSize)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? return 0;

? ? ? ? ? ? }

? ? ? ? ? ? else if (m_offsetBegin + m_MemSize >= m_FileSize)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? long temp = m_FileSize - m_offsetBegin;

? ? ? ? ? ? ? ? return (uint)temp;

? ? ? ? ? ? }

? ? ? ? ? ? else

? ? ? ? ? ? ? ? return m_MemSize;

? ? ? ? }

? ? ? ? /// <summary>

? ? ? ? /// 關(guān)閉內(nèi)存映射

? ? ? ? /// </summary>

? ? ? ? public void Close()

? ? ? ? {

? ? ? ? ? ? if (m_bInit)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? UnmapViewOfFile(syn_m_pwData);

? ? ? ? ? ? ? ? //syn_m_pwData = IntPtr.Zero;

? ? ? ? ? ? ? ? //foreach遍歷

? ? ? ? ? ? ? ? foreach (IntPtr obj in syn_m_pwDatas)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? UnmapViewOfFile(obj);


? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? //UnmapViewOfFile(m_pwData);

? ? ? ? ? ? ? ? //m_pwData = IntPtr.Zero;

? ? ? ? ? ? ? ? ////try

? ? ? ? ? ? ? ? ////{

? ? ? ? ? ? ? ? CloseHandle(m_hSharedMemoryFile);

? ? ? ? ? ? ? ? m_hSharedMemoryFile=IntPtr.Zero;

? ? ? ? ? ? ? ? //}m_pwData

? ? ? ? ? ? ? ? //catch (Exception)

? ? ? ? ? ? ? ? //{

? ? ? ? ? ? ? ? //? ? throw new Exception("在CloseHandle時" + "CreateFileMapping失敗LastError=" + GetLastError().ToString());

? ? ? ? ? ? ? ? //}


? ? ? ? ? ? ? ? File.Close();

? ? ? ? ? ? ? ? //try

? ? ? ? ? ? ? ? //{

? ? ? ? ? ? ? ? CloseHandle(hTemplateFile);

? ? ? ? ? ? ? ? m_hSharedMemoryFile= IntPtr.Zero;

? ? ? ? ? ? ? ? //}

? ? ? ? ? ? ? ? //catch (Exception)

? ? ? ? ? ? ? ? //{

? ? ? ? ? ? ? ? //? ? throw new Exception("在CloseHandle時" + "CreateFileMapping失敗LastError=" + GetLastError().ToString());

? ? ? ? ? ? ? ? //}

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? /// <summary>

? ? ? ? /// 從當前塊中獲取數(shù)據(jù)

? ? ? ? /// </summary>

? ? ? ? /// <param name="bytData">數(shù)據(jù)</param>

? ? ? ? /// <param name="lngAddr">起始數(shù)據(jù)</param>

? ? ? ? /// <param name="lngSize">數(shù)據(jù)長度,最大值=緩沖長度</param>

? ? ? ? /// <param name="Unmap">讀取完成是否卸載緩沖區(qū)</param>

? ? ? ? /// <returns></returns>

? ? ? ? public void Read(ref byte[] bytData, int lngAddr, int lngSize, bool Unmap)

? ? ? ? {

? ? ? ? ? ? if (lngAddr + lngSize > m_MemSize)

? ? ? ? ? ? ? ? throw new Exception("Read操作超出數(shù)據(jù)區(qū)");

? ? ? ? ? ? if (m_bInit)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? // string bb = Marshal.PtrToStringAuto(m_pwData);//

? ? ? ? ? ? ? ? Marshal.Copy(m_pwData, bytData, lngAddr, lngSize);

? ? ? ? ? ? }

? ? ? ? ? ? else

? ? ? ? ? ? {

? ? ? ? ? ? ? ? throw new Exception("文件未初始化");

? ? ? ? ? ? }

? ? ? ? ? ? if (Unmap)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? bool l_result = UnmapViewOfFile(m_pwData);

? ? ? ? ? ? ? ? if (l_result)

? ? ? ? ? ? ? ? m_pwData = IntPtr.Zero;

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? /// <summary>

? ? ? ? /// 從當前塊中獲取數(shù)據(jù)

? ? ? ? /// </summary>

? ? ? ? /// <param name="bytData">數(shù)據(jù)</param>

? ? ? ? /// <param name="lngAddr">起始數(shù)據(jù)</param>

? ? ? ? /// <param name="lngSize">數(shù)據(jù)長度卸亮,最大值=緩沖長度</param>

? ? ? ? /// <exception cref="Exception: Read操作超出數(shù)據(jù)區(qū)"></exception>

? ? ? ? /// <exception cref="Exception: 文件未初始化"></exception>

? ? ? ? /// <returns></returns>

? ? ? ? public void Read(ref byte[] bytData, int lngAddr, int lngSize)

? ? ? ? {

? ? ? ? ? ? //if (lngAddr + lngSize > m_MemSize)

? ? ? ? ? ? //? ? throw new Exception("Read操作超出數(shù)據(jù)區(qū)");

? ? ? ? ? ? if (m_bInit)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? try

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? //m_pwData = MapViewOfFile(m_hSharedMemoryFile, FILE_MAP_COPY | FILE_MAP_READ | FILE_MAP_WRITE, GetHighWord((UInt64)m_offsetBegin), GetLowWord((UInt64)m_offsetBegin), IntPtr.Zero);

? ? ? ? ? ? ? ? ? ? if (m_pwData == IntPtr.Zero || m_pwData ==new IntPtr(-1)) throw new Exception("讀取時獲得句柄異常");

? ? ? ? ? ? ? ? ? ? Marshal.Copy(m_pwData, bytData, lngAddr, lngSize);


? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? catch (Exception)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? throw new Exception("在Read時讀取長度超限度" + "CreateFileMapping失敗LastError=" + GetLastError().ToString());

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? else

? ? ? ? ? ? {

? ? ? ? ? ? ? ? throw new Exception("文件未初始化");

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? /// <summary>

? ? ? ? /// 異步讀文件

? ? ? ? /// </summary>

? ? ? ? /// <param name="bytData"></param>

? ? ? ? /// <param name="lngAddr"></param>

? ? ? ? /// <param name="lngSize"></param>

? ? ? ? public void synRead(ref byte[] bytData, int lngAddr, int lngSize)

? ? ? ? {

? ? ? ? ? ? //if (lngAddr + lngSize > m_MemSize)

? ? ? ? ? ? //? ? throw new Exception("Read操作超出數(shù)據(jù)區(qū)");

? ? ? ? ? ? if (m_bInit)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? try

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? //m_pwData = MapViewOfFile(m_hSharedMemoryFile, FILE_MAP_COPY | FILE_MAP_READ | FILE_MAP_WRITE, GetHighWord((UInt64)m_offsetBegin), GetLowWord((UInt64)m_offsetBegin), IntPtr.Zero);

? ? ? ? ? ? ? ? ? ? if (m_pwData == IntPtr.Zero || m_pwData == new IntPtr(-1)) throw new Exception("讀取時獲得句柄異常");

? ? ? ? ? ? ? ? ? ? Marshal.Copy(m_pwData, bytData, lngAddr, lngSize);

? ? ? ? ? ? ? ? ? ? UnmapViewOfFile(m_pwData);

? ? ? ? ? ? ? ? ? ? //syn_m_pwDatas.Add(m_pwData);

? ? ? ? ? ? ? ? ? ? //if (syn_m_pwDatas.Count >=6)

? ? ? ? ? ? ? ? ? ? //{

? ? ? ? ? ? ? ? ? ? //? ? UnmapViewOfFile(syn_m_pwDatas[0]);

? ? ? ? ? ? ? ? ? ? //? ? syn_m_pwDatas.RemoveAt(0);

? ? ? ? ? ? ? ? ? ? //}

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? catch (Exception)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? throw new Exception("在Read時讀取長度超限度" + "CreateFileMapping失敗LastError=" + GetLastError().ToString());

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? else

? ? ? ? ? ? {

? ? ? ? ? ? ? ? throw new Exception("文件未初始化,at? synRead");

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? /// <summary>

? ? ? ? /// 從當前塊中獲取數(shù)據(jù)

? ? ? ? /// </summary>

? ? ? ? /// <param name="lngAddr">緩存區(qū)偏移量</param>

? ? ? ? /// <param name="byteData">數(shù)據(jù)數(shù)組</param>

? ? ? ? /// <param name="StartIndex">數(shù)據(jù)數(shù)組開始復(fù)制的下標</param>

? ? ? ? /// <param name="lngSize">數(shù)據(jù)長度忽妒,最大值=緩沖長度</param>

? ? ? ? /// <exception cref="Exception: 起始數(shù)據(jù)超過緩沖區(qū)長度"></exception>

? ? ? ? /// <exception cref="Exception: 文件未初始化"></exception>

? ? ? ? /// <returns>返回實際讀取值</returns>

? ? ? ? public uint ReadBytes(int lngAddr, ref byte[] byteData, int StartIndex, uint intSize)

? ? ? ? {

? ? ? ? ? ? if (lngAddr >= m_MemSize)

? ? ? ? ? ? ? ? throw new Exception("起始數(shù)據(jù)超過緩沖區(qū)長度");

? ? ? ? ? ? if (lngAddr + intSize > m_MemSize)

? ? ? ? ? ? ? ? intSize = m_MemSize - (uint)lngAddr;

? ? ? ? ? ? if (m_bInit)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? IntPtr s = new IntPtr((long)m_pwData + lngAddr); // 地址偏移

? ? ? ? ? ? ? ? Marshal.Copy(s, byteData, StartIndex, (int)intSize);

? ? ? ? ? ? }

? ? ? ? ? ? else

? ? ? ? ? ? {

? ? ? ? ? ? ? ? throw new Exception("文件未初始化");

? ? ? ? ? ? }

? ? ? ? ? ? return intSize;

? ? ? ? }

? ? ? ? /// <summary>

? ? ? ? /// 寫數(shù)據(jù)

? ? ? ? /// </summary>

? ? ? ? /// <param name="bytData">數(shù)據(jù)</param>

? ? ? ? /// <param name="lngAddr">起始地址</param>

? ? ? ? /// <param name="lngSize">個數(shù)</param>

? ? ? ? /// <returns></returns>

? ? ? ? private int Write(byte[] bytData, int lngAddr, int lngSize)

? ? ? ? {

? ? ? ? ? ? if (lngAddr + lngSize > m_MemSize) return 2; //超出數(shù)據(jù)區(qū)

? ? ? ? ? ? if (m_bInit)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? Marshal.Copy(bytData, lngAddr, m_pwData, lngSize);

? ? ? ? ? ? }

? ? ? ? ? ? else

? ? ? ? ? ? {

? ? ? ? ? ? ? ? return 1; //共享內(nèi)存未初始化

? ? ? ? ? ? }

? ? ? ? ? ? return 0; //寫成功

? ? ? ? }

? ? }

? ? internal class FileReader

? ? {


? ? ? ? const int GENERIC_READ = -2147483648;//0x80000000;

? ? ? ? public const int GENERIC_WRITE = 0x40000000;

? ? ? ? const uint OPEN_EXISTING = 3;

? ? ? ? public const int FILE_ATTRIBUTE_NORMAL = 0x80;

? ? ? ? ////0x80000000;讀2G

? ? ? ? public const int FILE_FLAG_SEQUENTIAL_SCAN = 0x8000000;

? ? ? ? System.IntPtr handle;

? ? ? ? [DllImport("kernel32", SetLastError = true)]

? ? ? ? public static extern System.IntPtr CreateFile(

? ? ? ? ? ? string FileName,? ? ? ? ? // file name

? ? ? ? ? ? int DesiredAccess,? ? ? // access mode

? ? ? ? ? ? FileShare ShareMode,? ? ? ? ? // share mode

? ? ? ? ? ? IntPtr SecurityAttributes,? // Security Attributes

? ? ? ? ? ? FileMode CreationDisposition, // how to create

? ? ? ? ? ? int FlagsAndAttributes,? // file attributes

? ? ? ? ? ? IntPtr hTemplateFile? ? ? ? // handle to template file

? ? ? ? );

? ? ? ? [System.Runtime.InteropServices.DllImport("kernel32", SetLastError = true)]

? ? ? ? //關(guān)閉文件句柄

? ? ? ? static extern bool CloseHandle

? ? ? ? (

? ? ? ? ? ? System.IntPtr hObject // handle to object

? ? ? ? );

? ? ? ? //創(chuàng)建文件句柄

? ? ? ? public IntPtr Open(string FileName)

? ? ? ? {

? ? ? ? ? ? // open the existing file for reading? ? ?

? ? ? ? ? ? handle = CreateFile

? ? ? ? ? ? (

? ? ? ? ? ? ? ? FileName,

? ? ? ? ? ? ? ? GENERIC_READ | GENERIC_WRITE,

? ? ? ? ? ? ? ? FileShare.Read | FileShare.Write,

? ? ? ? ? ? ? ? IntPtr.Zero,

? ? ? ? ? ? ? ? FileMode.Open,

? ? ? ? ? ? ? ? FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,

? ? ? ? ? ? ? ? ShareMemory.hTemplateFile

? ? ? ? ? ? );

? ? ? ? ? ? if ( handle != System.IntPtr.Zero )

? ? ? ? ? ? {

? ? ? ? ? ? ? ? return handle;

? ? ? ? ? ? }

? ? ? ? ? ? else

? ? ? ? ? ? {

? ? ? ? ? ? ? ? throw new Exception( "打開文件失敗" );

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? //private IntPtr CreateFile(string FileName, uint GENERIC_READ, int p1, int p2, uint OPEN_EXISTING, int p3, IntPtr intPtr)

? ? ? ? //{

? ? ? ? //? ? throw new NotImplementedException();

? ? ? ? //}

? ? ? ? public bool Close()

? ? ? ? {

? ? ? ? ? ? bool temp =CloseHandle(handle);

? ? ? ? ? ? handle = IntPtr.Zero;

? ? ? ? ? ? return temp;

? ? ? ? }


? ? }

? ? #endregion

}

//并發(fā)部分


#region syn_pcap 異步內(nèi)存映射讀取

? ? ? ? /// <summary>

? ? ? ? /// 異步多核三個線程讀取paca文件信息的入口函數(shù),主要功能是計算byte數(shù)組

? ? ? ? /// 解釋:

? ? ? ? /// 1,執(zhí)行parsePcapSyn兼贸,初始化數(shù)據(jù)段直,獲取第一塊映射的全部數(shù)據(jù)數(shù)據(jù)以執(zhí)行parsePcapSyn第一次循環(huán)到達等待第二層getBytesSyn執(zhí)行完畢返回數(shù)據(jù)數(shù)據(jù),獲取第二包映射數(shù)組長度和句柄溶诞,以執(zhí)行第一次getBytesSyn的執(zhí)行鸯檬,直到等待第三層getIntPtrSyn執(zhí)行完畢返回數(shù)據(jù)數(shù)據(jù)。

? ? ? ? /// 2螺垢,進入 while (leftLength > 0)循環(huán)喧务,while (!ar.IsCompleted) 確保執(zhí)行到此處還有數(shù)據(jù)沒有讀完,還有循環(huán)讀取一個數(shù)據(jù)塊的內(nèi)容

? ? ? ? /// 3枉圃,while (temp < 0)這里判斷是否是剛從新數(shù)據(jù)快獲取數(shù)據(jù)功茴,如果是進行數(shù)據(jù)的交接

? ? ? ? /// 4,while (!ar.IsCompleted)孽亲,等待子線程getBytesSyn調(diào)度完畢坎穿,當子線程沒有完成之前,主線程可以在 該while語句塊內(nèi)進行任何后續(xù)操作,而且不用等待子線程的結(jié)束,在這個循環(huán)中編寫代碼是線程不安全的返劲。

? ? ? ? /// 5玲昧,進行新舊數(shù)據(jù)的交接

? ? ? ? /// 6.sm.Close();關(guān)閉資源


? ? ? ? /// </summary>

? ? ? ? /// <param name="index"></param>

? ? ? ? /// <param name="sm"></param>

? ? ? ? public static void parsePcapSyn(int index, ShareMemory sm)

? ? ? ? {

? ? ? ? ? ? Func<ShareMemory, byte[]> fun = getBytesSyn;

? ? ? ? ? ? byte[] pktmdata = null;

? ? ? ? ? ? byte[] byteTemp = null;

? ? ? ? ? ? Global.openGLs[index].frameSum = 0;

? ? ? ? ? ? Global.openGLs[index].frameStartDictionary.Clear();

? ? ? ? ? ? //獲取第一個數(shù)據(jù)塊數(shù)據(jù),長度

? ? ? ? ? ? uint mLength = sm.GetNextblock();

? ? ? ? ? ? pktmdata = new byte[mLength];

? ? ? ? ? ? sm.synRead(ref pktmdata, 0, pktmdata.Length);

? ? ? ? ? ? //上一塊數(shù)據(jù)的長度,

? ? ? ? ? ? uint liftBlockLength = 0;

? ? ? ? ? ? //定義上一塊讀到的位置

? ? ? ? ? ? int liftBlockTemp = 0;

? ? ? ? ? ? //為第一次進入子線程調(diào)度做準備

? ? ? ? ? ? sm.nextMLength = sm.GetNextblock();

? ? ? ? ? ? //相位角

? ? ? ? ? ? int l_unAngleNo = 0, preAngle = 0;

? ? ? ? ? ? int packLength = 0;

? ? ? ? ? ? int temp = 24;

? ? ? ? ? ? int fileStart = 24;

? ? ? ? ? ? long leftLength = sm.get_m_FileSize() - fileStart;

? ? ? ? ? ? //文件開始讀取的位置

? ? ? ? ? ? Global.openGLs[index].frameSum += 1;

? ? ? ? ? ? Global.openGLs[index].frameStartDictionary.Add(Global.openGLs[index].frameSum, fileStart);

? ? ? ? ? ? while (leftLength > 0)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? IAsyncResult ar = fun.BeginInvoke(sm, null, null);//主線程調(diào)用子線程開始執(zhí)行TakeAWhile方法,并給它傳遞了參數(shù)

? ? ? ? ? ? ? ? SharpGLForm.Instance.showPrograss(index, (int)((1.0 - (double)leftLength / sm.get_m_FileSize()) * 100));

? ? ? ? ? ? ? ? //如果剩下的數(shù)據(jù)不足一包數(shù)據(jù)執(zhí)行以下部分

? ? ? ? ? ? ? ? //這里判斷是否是剛從新數(shù)據(jù)快獲取數(shù)據(jù),如果是進行數(shù)據(jù)的交接

? ? ? ? ? ? ? ? while (temp < 0)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? //如果第一次進入此部分

? ? ? ? ? ? ? ? ? ? packLength = (temp + 9 >= 0 ? pktmdata[temp + 9] : byteTemp[liftBlockTemp + 9]) * 256 + (temp + 8 >= 0 ? pktmdata[temp + 8] : byteTemp[liftBlockTemp + 8]);

? ? ? ? ? ? ? ? ? ? fileStart += 16;

? ? ? ? ? ? ? ? ? ? leftLength -= 16;

? ? ? ? ? ? ? ? ? ? liftBlockTemp += 16;

? ? ? ? ? ? ? ? ? ? temp += 16;

? ? ? ? ? ? ? ? ? ? if (packLength != 1392)

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? liftBlockTemp += packLength;

? ? ? ? ? ? ? ? ? ? ? ? temp += packLength;

? ? ? ? ? ? ? ? ? ? ? ? fileStart += packLength;

? ? ? ? ? ? ? ? ? ? ? ? leftLength -= packLength;

? ? ? ? ? ? ? ? ? ? ? ? continue;

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? if ((temp + 42 >= 0 ? pktmdata[temp + 42] : byteTemp[liftBlockTemp + 42]) == 0xff && (temp + 43 >= 0 ? pktmdata[temp + 43] : byteTemp[liftBlockTemp + 43]) == 0xee)

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? preAngle = l_unAngleNo;

? ? ? ? ? ? ? ? ? ? ? ? l_unAngleNo = ((temp + +42 + 132 + 3 >= 0 ? pktmdata[temp + 42 + 132 + 3] : byteTemp[liftBlockTemp + 42 + 132 + 3]) * 256 + (temp + 42 + 132 + 2 >= 0 ? pktmdata[temp + 42 + 132 + 2] : byteTemp[liftBlockTemp + 42 + 132 + 2])) / 10;

? ? ? ? ? ? ? ? ? ? ? ? if (l_unAngleNo == 3600) l_unAngleNo = 0;

? ? ? ? ? ? ? ? ? ? ? ? if (l_unAngleNo < preAngle)

? ? ? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? ? ? l_unAngleNo = 0;

? ? ? ? ? ? ? ? ? ? ? ? ? ? Global.openGLs[index].frameSum++;

? ? ? ? ? ? ? ? ? ? ? ? ? ? Global.openGLs[index].frameStartDictionary.Add(Global.openGLs[index].frameSum, fileStart + 1392);

? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? temp += 1392;

? ? ? ? ? ? ? ? ? ? fileStart += 1392;

? ? ? ? ? ? ? ? ? ? leftLength -= 1392;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? while (temp + 1408 < mLength && mLength != 0)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? packLength = pktmdata[temp + 9] * 256 + pktmdata[temp + 8];

? ? ? ? ? ? ? ? ? ? ? ? fileStart += 16;

? ? ? ? ? ? ? ? ? ? ? ? leftLength -= 16;

? ? ? ? ? ? ? ? ? ? ? ? temp += 16;

? ? ? ? ? ? ? ? ? ? ? ? //如果不等于1392則跳過這包數(shù)據(jù)

? ? ? ? ? ? ? ? ? ? ? ? if (packLength != 1392)

? ? ? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? ? ? temp += packLength;

? ? ? ? ? ? ? ? ? ? ? ? ? ? fileStart += packLength;

? ? ? ? ? ? ? ? ? ? ? ? ? ? leftLength -= packLength;

? ? ? ? ? ? ? ? ? ? ? ? ? ? continue;

? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? if (pktmdata[temp + 42] == 0xff && pktmdata[temp + 43] == 0xee)

? ? ? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? ? ? preAngle = l_unAngleNo;

? ? ? ? ? ? ? ? ? ? ? ? ? ? l_unAngleNo = (pktmdata[temp + 42 + 132 + 3] * 256 + pktmdata[temp + 42 + 132 + 2]) / 10;

? ? ? ? ? ? ? ? ? ? ? ? ? ? if (l_unAngleNo == 3600) l_unAngleNo = 0;

? ? ? ? ? ? ? ? ? ? ? ? ? ? if (l_unAngleNo < preAngle)

? ? ? ? ? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? l_unAngleNo = 0;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Global.openGLs[index].frameSum++;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Global.openGLs[index].frameStartDictionary.Add(Global.openGLs[index].frameSum, fileStart + 1392);

? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? temp += 1392;

? ? ? ? ? ? ? ? ? ? ? ? fileStart += 1392;

? ? ? ? ? ? ? ? ? ? ? ? leftLength -= 1392;



? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? while (!ar.IsCompleted)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? //等待子線程調(diào)度完畢

? ? ? ? ? ? ? ? ? ? //當子線程沒有完成之前,主線程可以在 該while語句塊內(nèi)進行任何后續(xù)操作,而且不用等待子線程的結(jié)束

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? //獲取新的數(shù)據(jù)塊篮绿,整合舊數(shù)據(jù)塊

? ? ? ? ? ? ? ? byteTemp = pktmdata;

? ? ? ? ? ? ? ? liftBlockTemp = temp;

? ? ? ? ? ? ? ? liftBlockLength = mLength;

? ? ? ? ? ? ? ? pktmdata = fun.EndInvoke(ar);//6秒之后我需要子線程的結(jié)果了,ok,從子線程中拿到結(jié)果

? ? ? ? ? ? ? ? if (pktmdata == null) break;

? ? ? ? ? ? ? ? mLength =(uint) pktmdata.Length;

? ? ? ? ? ? ? ? temp = liftBlockTemp - (int)liftBlockLength;


? ? ? ? ? ? }

? ? ? ? ? ? sm.Close();

? ? ? ? }

? ? ? ? /// <summary>

? ? ? ? /// 異步多核三個線程讀取paca文件信息的中間函數(shù)孵延,主要功能是獲取byte數(shù)組

? ? ? ? /// </summary>

? ? ? ? /// <param name="sm"></param>

? ? ? ? /// <returns></returns>

? ? ? ? static byte[] getBytesSyn(ShareMemory sm)

? ? ? ? {

? ? ? ? ? //? Log.printLog("進入TakeAWhile,句柄為:" + sm.get_m_pwData());

? ? ? ? ? ? //如果沒有這一包

? ? ? ? ? ? if (sm.nextMLength == 0) return null;

? ? ? ? ? ? Func<ShareMemory, uint> fun = getIntPtrSyn;

? ? ? ? ? ? byte[] pktmdata = null;

? ? ? ? ? ? IAsyncResult ar = fun.BeginInvoke(sm, null, null);//主線程調(diào)用子線程開始執(zhí)行TakeAWhile方法,并給它傳遞了參數(shù)

? ? ? ? ? ? pktmdata = new byte[sm.nextMLength];

? ? ? ? ? ? sm.synRead(ref pktmdata, 0, pktmdata.Length);

? ? ? ? ? ? while (!ar.IsCompleted)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? //當子線程沒有完成之前,主線程可以在該while語句塊內(nèi)進行任何后續(xù)操作,而且不用等待子線程的結(jié)束

? ? ? ? ? ? //? Log.printLog("TakeAWhile,等待Take結(jié)束,句柄為:" + sm.get_m_pwData());

? ? ? ? ? ? }

? ? ? ? ? ? sm.nextMLength = fun.EndInvoke(ar);//6秒之后我需要子線程的結(jié)果了,ok,從子線程中拿到結(jié)果

? ? ? ? ? ? sm.do_syn_m_pwData();

? ? ? ? ? ? return pktmdata;

? ? ? ? }

? ? ? ? /// <summary>

? ? ? ? /// 異步多核三個線程讀取paca文件信息的第三層函數(shù)搔耕,主要功能是獲取byte數(shù)組的長度和下一個數(shù)據(jù)映射的句柄

? ? ? ? /// </summary>

? ? ? ? /// <param name="sm"></param>

? ? ? ? /// <returns></returns>

? ? ? ? static uint getIntPtrSyn(ShareMemory sm)

? ? ? ? {

? ? ? ? ? // Log.printLog("進入Take,句柄為:" + sm.get_m_pwData());

? ? ? ? ? ? uint temp = sm.synGetNextblock();

? ? ? ? ? // Log.printLog("Take結(jié)束,句柄為:" + sm.get_m_pwData());

? ? ? ? ? ? return temp;

? ? ? ? }

? ? ? ? #endregion

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末隙袁,一起剝皮案震驚了整個濱河市痰娱,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌菩收,老刑警劉巖梨睁,帶你破解...
    沈念sama閱讀 212,029評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異娜饵,居然都是意外死亡坡贺,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,395評論 3 385
  • 文/潘曉璐 我一進店門箱舞,熙熙樓的掌柜王于貴愁眉苦臉地迎上來遍坟,“玉大人,你說我怎么就攤上這事晴股≡赴椋” “怎么了?”我有些...
    開封第一講書人閱讀 157,570評論 0 348
  • 文/不壞的土叔 我叫張陵电湘,是天一觀的道長隔节。 經(jīng)常有香客問我,道長寂呛,這世上最難降的妖魔是什么怎诫? 我笑而不...
    開封第一講書人閱讀 56,535評論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮贷痪,結(jié)果婚禮上幻妓,老公的妹妹穿的比我還像新娘。我一直安慰自己劫拢,他們只是感情好肉津,可當我...
    茶點故事閱讀 65,650評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著舱沧,像睡著了一般阀圾。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上狗唉,一...
    開封第一講書人閱讀 49,850評論 1 290
  • 那天初烘,我揣著相機與錄音,去河邊找鬼分俯。 笑死肾筐,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的缸剪。 我是一名探鬼主播吗铐,決...
    沈念sama閱讀 39,006評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼杏节!你這毒婦竟也來了唬渗?” 一聲冷哼從身側(cè)響起典阵,我...
    開封第一講書人閱讀 37,747評論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎镊逝,沒想到半個月后壮啊,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,207評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡撑蒜,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,536評論 2 327
  • 正文 我和宋清朗相戀三年歹啼,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片座菠。...
    茶點故事閱讀 38,683評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡狸眼,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出浴滴,到底是詐尸還是另有隱情拓萌,我是刑警寧澤,帶...
    沈念sama閱讀 34,342評論 4 330
  • 正文 年R本政府宣布升略,位于F島的核電站司志,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏降宅。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,964評論 3 315
  • 文/蒙蒙 一囚霸、第九天 我趴在偏房一處隱蔽的房頂上張望腰根。 院中可真熱鬧,春花似錦拓型、人聲如沸额嘿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,772評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽册养。三九已至,卻和暖如春压固,著一層夾襖步出監(jiān)牢的瞬間球拦,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,004評論 1 266
  • 我被黑心中介騙來泰國打工帐我, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留坎炼,地道東北人。 一個月前我還...
    沈念sama閱讀 46,401評論 2 360
  • 正文 我出身青樓拦键,卻偏偏與公主長得像谣光,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子芬为,可洞房花燭夜當晚...
    茶點故事閱讀 43,566評論 2 349

推薦閱讀更多精彩內(nèi)容

  • Lua 5.1 參考手冊 by Roberto Ierusalimschy, Luiz Henrique de F...
    蘇黎九歌閱讀 13,764評論 0 38
  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,448評論 0 13
  • 1.在C/C++中實現(xiàn)本地方法 生成C/C++頭文件之后萄金,你就需要寫頭文件對應(yīng)的本地方法蟀悦。注意:所有的本地方法的第...
    JayQiu閱讀 2,339評論 0 3
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 2,694評論 0 3
  • DiskLurCache 使用教程 源碼解析 使用 打開緩存 打開緩存函數(shù)public static DiskLr...
    super小立立閱讀 909評論 1 1