I am practicing on implementing a MMF as IPC, I have tried it via wpf as listener and set it's event handler which does trigger.
Now that I have moved the same setter code into a Wpf (the setter till that point was Console App) on the Wpf App I could not get the event handler to fire.
this is the code I have successfully implemented and I am not sure if this implementation is right, ...the only thing I ma sure of is that it sends data, receives a reply(from wpf actually) and fires its event(on console).
MMF Class
public class MMFDepositT //: DiposiT<MyData>
{
private System.Threading.SendOrPostCallback callback;
public event EventHandler<MemoryMappedDataReceivedEventArgs> DataReceived;
private System.ComponentModel.AsyncOperation operation;
public virtual string DipositChlName { get; set; }
public virtual string DipositThrdName { get; set; }
public virtual int DipositSize { get; set; }
bool started;
private bool disposed;
public int ReadPosition { get; set; }
public List<string> statusSet;
private int writePosition;
public int WritePosition
{
get { return writePosition; }
set
{
if (value != writePosition)
{
this.writePosition = value;
this.accessor.Write(WritePosition + READ_CONFIRM_OFFSET, true);
}
}
}
private const int DATA_AVAILABLE_OFFSET = 0;
private const int READ_CONFIRM_OFFSET = DATA_AVAILABLE_OFFSET + 1;
private const int DATA_LENGTH_OFFSET = READ_CONFIRM_OFFSET + 1;
private const int DATA_OFFSET = DATA_LENGTH_OFFSET + 10;
public MMFDepositT(string ctrIpcChannelNameStr, string ctrIpcThreadName, int ctrMMFSize)
{
this.DipositChlName = ctrIpcChannelNameStr;
this.DipositSize = ctrMMFSize;
this.DipositThrdName = ctrIpcThreadName;
this.statusSet = new List<string>();
try
{
mmf = MemoryMappedFile.CreateOrOpen(DipositChlName, DipositSize);
accessor = mmf.CreateViewAccessor(0, DipositSize, System.IO.MemoryMappedFiles.MemoryMappedFileAccess.ReadWrite);//if (started)
//smLock = new System.Threading.Mutex(true, IpcMutxName, out locked);
ReadPosition = -1;
writePosition = -1;
this.dataToSend = new List<byte[]>();
this.callback = new System.Threading.SendOrPostCallback(OnDataReceivedInternal);
this.operation = System.ComponentModel.AsyncOperationManager.CreateOperation(null);
this.statusSet.Add("accessorOk & mmfOk");
}
catch (Exception e)
{
this.statusSet.Add("accessor Excep:-> " + e.Message);
}
// return;
//System.Threading.Thread t = new System.Threading.Thread(ReaderThread);
//t.IsBackground = true;
//t.Start();
//this.started = true;
}
public void StartReader()
{
////FieldInfo field = typeof(DipositValClF.ValueStrNameMax10SrV).GetField("Val");
////object[] attributes = field.GetCustomAttributes(typeof(MarshalAsAttribute), false);
////MarshalAsAttribute marshal = (MarshalAsAttribute)attributes[0];
// Create named MMF
if (started) return;
if (ReadPosition < 0 || writePosition < 0)
throw new ArgumentException();
System.Threading.Thread t = new System.Threading.Thread(ReaderThread);
t.IsBackground = true;
t.Start();
this.started = true;
}
private void ReaderThread(object stateInfo)
{
while (started)
{
// Checks if there is something to read.
var dataAvailable = accessor.ReadBoolean(ReadPosition + DATA_AVAILABLE_OFFSET);
if (dataAvailable)
{
// Checks how many bytes to read.
int availableBytes = accessor.ReadInt32(ReadPosition + DATA_LENGTH_OFFSET);
var bytes = new byte[availableBytes];
// Reads the byte array.
int read = accessor.ReadArray<byte>(ReadPosition + DATA_OFFSET, bytes, 0, availableBytes);
// Sets the flag used to signal that there aren't available data anymore.
accessor.Write(ReadPosition + DATA_AVAILABLE_OFFSET, false);
// Sets the flag used to signal that data has been read.
accessor.Write(ReadPosition + READ_CONFIRM_OFFSET, true);
MemoryMappedDataReceivedEventArgs args = new MemoryMappedDataReceivedEventArgs(bytes, read);
operation.Post(callback, args);
}
System.Threading.Thread.Sleep(500);
}
}
public void CloseReader()
{
started = false;
}
private System.Threading.Thread writerThread;
private List<byte[]> dataToSend;
private bool writerThreadRunning;
public void Write(byte[] data)
{
if (ReadPosition < 0 || writePosition < 0)
throw new ArgumentException();
this.statusSet.Add("ReadWrite:-> " + ReadPosition + "-" + writePosition);
// var s = (FsMomitorIPCCrier)data;
lock (this.dataToSend)
this.dataToSend.Add(data);
if (!writerThreadRunning)
{
writerThreadRunning = true;
writerThread = new System.Threading.Thread(WriterThread);
writerThread.IsBackground = true;
writerThread.Name = this.DipositThrdName;
writerThread.Start();
}
}
public void WriterThread(object stateInfo)
{
while (dataToSend.Count > 0 && !this.disposed)
{
byte[] data = null;
lock (dataToSend)
{
data = dataToSend[0];
dataToSend.RemoveAt(0);
}
while (!this.accessor.ReadBoolean(WritePosition + READ_CONFIRM_OFFSET))
System.Threading.Thread.Sleep(500);
// Sets length and write data.
this.accessor.Write(writePosition + DATA_LENGTH_OFFSET, data.Length);
this.accessor.WriteArray<byte>(writePosition + DATA_OFFSET, data, 0, data.Length);
// Resets the flag used to signal that data has been read.
this.accessor.Write(writePosition + READ_CONFIRM_OFFSET, false);
// Sets the flag used to signal that there are data avaibla.
this.accessor.Write(writePosition + DATA_AVAILABLE_OFFSET, true);
}
writerThreadRunning = false;
}
private void OnDataReceivedInternal(object state)
{
OnDataReceived(state as MemoryMappedDataReceivedEventArgs);
}
protected virtual void OnDataReceived(MemoryMappedDataReceivedEventArgs e)
{
if (e != null && DataReceived != null)
DataReceived(this, e);
}
public virtual void Close()
{
//accessor.Dispose();
//mmf.Dispose();
//smLock.Close();
started = false;
if (accessor != null)
{
try
{
accessor.Dispose();
accessor = null;
}
catch { }
}
if (this.mmf != null)
{
try
{
mmf.Dispose();
mmf = null;
}
catch { }
}
disposed = true;
GC.SuppressFinalize(this);
}
private System.IO.MemoryMappedFiles.MemoryMappedFile mmf;
private System.IO.MemoryMappedFiles.MemoryMappedViewAccessor accessor;
}
setting a couple of EventWaitHandle I have replaced all:
and it's companions...
my new MMF could easily take 10G of object I did 5g in blink of an eye, full duplex inter process communication is done here.