private void ListenerThread() { IntPtr queuePtr; IntPtr map = CreateFileMapping(0xFFFFFFFFu, IntPtr.Zero, enumProtect.PAGE_READWRITE, 0, (uint)Marshal.SizeOf(typeof(MessageQueue)), "BadgerPacketQueue"); if (Marshal.GetLastWin32Error() == ERROR_ALREADY_EXISTS) { map = OpenFileMapping(enumFileMap.FILE_MAP_WRITE, false, "BadgerPacketQueue"); } queuePtr = MapViewOfFile(map, enumFileMap.FILE_MAP_WRITE, 0, 0, (uint)Marshal.SizeOf(typeof(MessageQueue))); Mutex queueMutex = new Mutex(false, "BadgerQueueMutex"); AutoResetEvent readyEvent = new AutoResetEvent(false); readyEvent.SafeWaitHandle = CreateEvent(IntPtr.Zero, false, // bManualReset false, // bInitialState "BadgerPacketReady"); IntPtr numElementsPtr = (IntPtr)(queuePtr.ToInt64() + Marshal.OffsetOf(typeof(MessageQueue), "num_elements").ToInt64()); IntPtr elementsPtr = (IntPtr)(queuePtr.ToInt64() + Marshal.OffsetOf(typeof(MessageQueue), "elements").ToInt64()); int elementSize = Marshal.SizeOf(typeof(MessageQueueElement)); IntPtr tmpElementPtr = Marshal.AllocHGlobal(elementSize); try { // // First off, upload softwall rules // if (!queueMutex.WaitOne(5000, false)) { Stopped(); return; } try { // Empty message queue //Marshal.WriteInt32(numElementsPtr, 0); Marshal.WriteIntPtr(queuePtr, Marshal.OffsetOf(typeof(MessageQueue), "num_softwall_rules").ToInt32(), (IntPtr) rules.Length); IntPtr p = (IntPtr)(queuePtr.ToInt64() + Marshal.OffsetOf(typeof(MessageQueue), "softwall_rules").ToInt64()); foreach (SoftwallRule rule in rules) { Marshal.StructureToPtr(rule, p, false); p = (IntPtr)(p.ToInt64() + Marshal.SizeOf(typeof(SoftwallRule))); } } finally { queueMutex.ReleaseMutex(); } // // Then start monitoring // while (listenerThread != null) { Int32 len; byte[] bytes = null; Thread.Sleep(1000); if (readyEvent.WaitOne(500, false)) { if (!queueMutex.WaitOne(5000, false)) { Stopped(); return; } try { len = Marshal.ReadInt32(numElementsPtr); if (len > 0) { bytes = new byte[len * Marshal.SizeOf(typeof(MessageQueueElement))]; Marshal.Copy(elementsPtr, bytes, 0, bytes.Length); Marshal.WriteInt32(numElementsPtr, 0); } } finally { queueMutex.ReleaseMutex(); } if (bytes != null) { MessageQueueElement[] elements = new MessageQueueElement[len]; for (int i = 0; i < len; i++) { Marshal.Copy(bytes, i * elementSize, tmpElementPtr, elementSize); elements[i] = (MessageQueueElement)Marshal.PtrToStructure(tmpElementPtr, typeof(MessageQueueElement)); } MessageElementsReceived(elements); } } } } finally { Marshal.FreeHGlobal(tmpElementPtr); } Stopped(); }
private void ListenerThread() { IntPtr queuePtr; IntPtr map = CreateFileMapping(0xFFFFFFFFu, IntPtr.Zero, enumProtect.PAGE_READWRITE, 0, (uint)Marshal.SizeOf(typeof(MessageQueue)), "BadgerPacketQueue"); if (Marshal.GetLastWin32Error() == ERROR_ALREADY_EXISTS) { map = OpenFileMapping(enumFileMap.FILE_MAP_WRITE, false, "BadgerPacketQueue"); } queuePtr = MapViewOfFile(map, enumFileMap.FILE_MAP_WRITE, 0, 0, (uint)Marshal.SizeOf(typeof(MessageQueue))); Mutex queueMutex = new Mutex(false, "BadgerQueueMutex"); AutoResetEvent readyEvent = new AutoResetEvent(false); readyEvent.SafeWaitHandle = CreateEvent(IntPtr.Zero, false, // bManualReset false, // bInitialState "BadgerPacketReady"); IntPtr numElementsPtr = (IntPtr)(queuePtr.ToInt64() + Marshal.OffsetOf(typeof(MessageQueue), "num_elements").ToInt64()); IntPtr elementsPtr = (IntPtr)(queuePtr.ToInt64() + Marshal.OffsetOf(typeof(MessageQueue), "elements").ToInt64()); int elementSize = Marshal.SizeOf(typeof(MessageQueueElement)); IntPtr tmpElementPtr = Marshal.AllocHGlobal(elementSize); try { // // First off, upload softwall rules // if (!queueMutex.WaitOne(5000, false)) { Stopped(); return; } try { // Empty message queue //Marshal.WriteInt32(numElementsPtr, 0); Marshal.WriteIntPtr(queuePtr, Marshal.OffsetOf(typeof(MessageQueue), "num_softwall_rules").ToInt32(), (IntPtr)rules.Length); IntPtr p = (IntPtr)(queuePtr.ToInt64() + Marshal.OffsetOf(typeof(MessageQueue), "softwall_rules").ToInt64()); foreach (SoftwallRule rule in rules) { Marshal.StructureToPtr(rule, p, false); p = (IntPtr)(p.ToInt64() + Marshal.SizeOf(typeof(SoftwallRule))); } } finally { queueMutex.ReleaseMutex(); } // // Then start monitoring // while (listenerThread != null) { Int32 len; byte[] bytes = null; Thread.Sleep(1000); if (readyEvent.WaitOne(500, false)) { if (!queueMutex.WaitOne(5000, false)) { Stopped(); return; } try { len = Marshal.ReadInt32(numElementsPtr); if (len > 0) { bytes = new byte[len * Marshal.SizeOf(typeof(MessageQueueElement))]; Marshal.Copy(elementsPtr, bytes, 0, bytes.Length); Marshal.WriteInt32(numElementsPtr, 0); } } finally { queueMutex.ReleaseMutex(); } if (bytes != null) { MessageQueueElement[] elements = new MessageQueueElement[len]; for (int i = 0; i < len; i++) { Marshal.Copy(bytes, i * elementSize, tmpElementPtr, elementSize); elements[i] = (MessageQueueElement)Marshal.PtrToStructure(tmpElementPtr, typeof(MessageQueueElement)); } MessageElementsReceived(elements); } } } } finally { Marshal.FreeHGlobal(tmpElementPtr); } Stopped(); }
public MessageQueueElement(MessageQueueElement <T> message) { NotifyClient = message.NotifyClient; WarframeEvent = message.WarframeEvent; EventHasExpired = message.EventHasExpired; }