Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Allows programs to send information to each other via MemoryMappedFiles in an event driven way.
- using System;
- using System.IO;
- using System.IO.MemoryMappedFiles;
- using System.Threading;
- namespace MemoryMappedComms
- {
- public class Sender : IDisposable
- {
- private string Name;
- public Sender(string Name)
- {
- this.Name = Name;
- }
- public bool Send(byte[] Bytes)
- {
- return Send(Bytes, Name);
- }
- public static bool Send(byte[] Bytes, string Name, int TimeOut = 0)
- {
- using (Mutex SingleOut = new Mutex(false, "MemoryMappedComms:712247:" + Name + ":SendSingleOutMutex"))
- {
- try
- {
- if (SingleOut.WaitOne(TimeOut) && CheckForReader(Name))
- {
- using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew("MemoryMappedComms:712247:" + Name + ":Data", Bytes.Length))
- {
- using (MemoryMappedViewStream mmfv = mmf.CreateViewStream())
- {
- using (BinaryWriter bw = new BinaryWriter(mmfv))
- {
- bw.Write(Bytes);
- }
- }
- using (EventWaitHandle Signal = new EventWaitHandle(false, EventResetMode.AutoReset, "MemoryMappedComms:712247:" + Name + ":Signal"))
- {
- using (EventWaitHandle WaitForTransfer = new EventWaitHandle(false, EventResetMode.AutoReset, "MemoryMappedComms:712247:" + Name + ":WaitForTransfer"))
- {
- using (Mutex StatMutex = new Mutex(false, "MemoryMappedComms:712247:" + Name + ":StatusMutex"))
- {
- Signal.Set();
- WaitHandle.WaitAny(new WaitHandle[] { WaitForTransfer, StatMutex });
- }
- }
- }
- }
- return true;
- }
- }
- catch (Exception)
- {
- }
- }
- return false;
- }
- public static bool CheckForReader(string Name)
- {
- try
- {
- using (MemoryMappedFile mmfc = MemoryMappedFile.OpenExisting("MemoryMappedComms:712247:" + Name + ":Presence"))
- {
- return true;
- }
- }
- catch (Exception)
- {
- return false;
- }
- }
- public void Dispose()
- {
- throw new NotImplementedException();
- }
- }
- public class Receiver : IDisposable
- {
- public event BytesReceivedEventHandler BytesReceived;
- public delegate void BytesReceivedEventHandler(byte[] Bytes);
- private string Name;
- private bool Running = true;
- private Thread CommunicationsThread;
- public Receiver(string Name)
- {
- this.Name = Name;
- CommunicationsThread = new Thread(() =>
- {
- using (Mutex SingleOut = new Mutex(false, "MemoryMappedComms:712247:" + Name + ":RecvSingleOutMutex"))
- {
- try
- {
- SingleOut.WaitOne();
- }
- catch (Exception)
- {
- }
- try
- {
- using (MemoryMappedFile Presence = MemoryMappedFile.CreateNew("MemoryMappedComms:712247:" + Name + ":Presence", 1))
- {
- using (Mutex StatMutex = new Mutex(true, "MemoryMappedComms:712247:" + Name + ":StatusMutex"))
- {
- while (Running)
- {
- using (EventWaitHandle Signal = new EventWaitHandle(false, EventResetMode.AutoReset, "MemoryMappedComms:712247:" + this.Name + ":Signal"))
- {
- Signal.WaitOne();
- using (EventWaitHandle WaitForTransfer = new EventWaitHandle(false, EventResetMode.AutoReset, "MemoryMappedComms:712247:" + Name + ":WaitForTransfer"))
- {
- using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("MemoryMappedComms:712247:" + Name + ":Data"))
- {
- using (MemoryMappedViewStream mmfv = mmf.CreateViewStream())
- {
- using (BinaryReader br = new BinaryReader(mmfv))
- {
- if (BytesReceived != null)
- {
- BytesReceived(br.ReadBytes((int)mmfv.Length));
- }
- }
- }
- }
- WaitForTransfer.Set();
- }
- }
- }
- }
- }
- }
- catch (Exception)
- {
- }
- }
- })
- { IsBackground = true };
- CommunicationsThread.Start();
- }
- public void Dispose()
- {
- Running = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement