static void Main(string[] args) { int size = 900 * 720 * 2; string name = "SharedMemoryTest"; data = new byte[size]; var buf = new SharedMemory.CircularBuffer(name); bufW = new SharedMemory.CircularBuffer(name + "W"); int i = 0; Task.Factory.StartNew(Read); while (true) { data[0] = (byte)Console.ReadKey().Key; int result = buf.Write(data); _Signal.WaitOne(); Console.WriteLine("========================" + data[0]); } Console.ReadKey(); }
public static void WriteCommandToCmdMMF(ServerCodes sc, byte[] data = null, UInt16 newLength = 0, ushort socketId = 0) { int mmfWriteTimeout = 1000; byte[] writtenData; if (sc == ServerCodes.SCODE_INJECTPACKET) { /* | ServerCode | SocketID | newLength | Packet | | 1-byte | 2-bytes | 2-bytes |--------------- data -------------| |------------------------------------ writtenData --------------------------------------| */ writtenData = new byte[1 + 2 + 2 + newLength]; writtenData[0] = (byte)sc; Array.Copy(FilterManager.StructToBytes(socketId), 0, writtenData, 1, 2); Array.Copy(FilterManager.StructToBytes(newLength), 0, writtenData, 3, 2); Array.Copy(data, 0, writtenData, 5, newLength); } else if (sc == ServerCodes.SCODE_STARTFILTERING) { /* | ServerCode | FilterCount | Filter Data | | 1-byte | 1-byte |-----------------Data------------------------| |------------------------ writtenData ---------------------------------------| */ writtenData = new byte[FilterManager.FILTERINBYTESSIZE * FilterManager.GetActiveFilterCount() + 2]; // Minimo: SizeOf(Filter) * cantActivos + 2 writtenData[0] = (byte)sc; writtenData[1] = (byte)FilterManager.GetActiveFilterCount(); byte[] bytes = FilterManager.ConvertFilterListToBytes(); Array.Copy(bytes, 0, writtenData, 2, FilterManager.FILTERINBYTESSIZE * FilterManager.GetActiveFilterCount()); } else if (sc == ServerCodes.SCODE_SETPACKET) { /* | ServerCode | newLength | Packet Data | | 1-byte | 2-bytes |-----------------Data-----------------------------| |------------------------------------- writtenData ----------------------------| */ writtenData = new byte[1 + 2 + newLength]; writtenData[0] = (byte)sc; Array.Copy(FilterManager.StructToBytes(newLength), 0, writtenData, 1, 2); Array.Copy(data, 0, writtenData, 3, newLength); } else if (sc == ServerCodes.SCODE_LOADDLLEX) { /* | ServerCode | fileName string | | 1-byte |---------- string.Length --------| |------------------- writtenData --------------| */ writtenData = new byte[1 + Settings.DLLEx.Length]; writtenData[0] = (byte)sc; Array.Copy(Encoding.ASCII.GetBytes(Settings.DLLEx), 0, writtenData, 1, Settings.DLLEx.Length); } else if (sc == ServerCodes.SCODE_STARTCAPTURE || sc == ServerCodes.SCODE_STOPCAPTURE || sc == ServerCodes.SCODE_STOPFILTERING || sc == ServerCodes.SCODE_UNLOADDLLEX) { writtenData = new byte[1]; writtenData[0] = (byte)sc; } else { throw new NotImplementedException("ServerCode " + (int)sc + " NOT IMPLEMENTED"); } // Spawneamos un thread que puede esperar el mmfWriteTimeout para escribir el comando //int threadCount = 0; //int myThreadIndex = Interlocked.Increment(ref threadCount); if (CheckMMFileExists(_cmdMmfName)) { cmdMMF = new SharedMemory.CircularBuffer(_cmdMmfName); } else { System.Windows.Forms.MessageBox.Show("ERROR, MMF does not exists!"); // No esta el archivo return; } //The number of milliseconds to wait, or Timeout.Infinite (-1) to wait indefinitely. int amount = cmdMMF.Write(writtenData, 0, mmfWriteTimeout); if (amount == 0) { throw new Exception("Write 0 bytes to command MMF!"); } //Output.outString("Write data: {0}", BitConverter.ToString(writtenData)); }