private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < 1000000; i++) { //var buffer = new byte[TestData.Metadata.Length]; //Array.Copy(TestData.Metadata, buffer, TestData.Metadata.Length); CIztUdpHeader header = CIztUdpHeader.Load(TestData.Header, 0); CIztMetadata metadata = CIztMetadata.Load(TestData.Header, 64); CIztPacket packet = CIztPacket.Load(TestData.Header, 0); } }
public static CIztUdpHeader Load(byte[] buffer, int offset) { CIztUdpHeader header; if (buffer != null && (offset + LayoutSize) <= buffer.Length) { byte[] copydata = new byte[LayoutSize]; Array.Copy(buffer, offset, copydata, 0, LayoutSize); //GCHandleType.Pinned 允許這個buffer做出來的handle直接取用底下的實體記憶體位置 //相當於允許直接使用底層的C指標 GCHandle handle = GCHandle.Alloc(copydata, GCHandleType.Pinned); header = (CIztUdpHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(CIztUdpHeader)); handle.Free(); } else { header = new CIztUdpHeader(); } return(header); }