Advertisement
TizzyT

MemoryMappedComms(C#) -TizzyT

Aug 1st, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.17 KB | None | 0 0
  1. // Allows programs to send information to each other via MemoryMappedFiles in an event driven way.
  2.  
  3. using System;
  4. using System.IO;
  5. using System.IO.MemoryMappedFiles;
  6. using System.Threading;
  7.  
  8. namespace MemoryMappedComms
  9. {
  10.     public class Sender : IDisposable
  11.     {
  12.         private string Name;
  13.  
  14.         public Sender(string Name)
  15.         {
  16.             this.Name = Name;
  17.         }
  18.  
  19.         public bool Send(byte[] Bytes)
  20.         {
  21.             return Send(Bytes, Name);
  22.         }
  23.  
  24.         public static bool Send(byte[] Bytes, string Name, int TimeOut = 0)
  25.         {
  26.             using (Mutex SingleOut = new Mutex(false, "MemoryMappedComms:712247:" + Name + ":SendSingleOutMutex"))
  27.             {
  28.                 try
  29.                 {
  30.                     if (SingleOut.WaitOne(TimeOut) && CheckForReader(Name))
  31.                     {
  32.                         using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew("MemoryMappedComms:712247:" + Name + ":Data", Bytes.Length))
  33.                         {
  34.                             using (MemoryMappedViewStream mmfv = mmf.CreateViewStream())
  35.                             {
  36.                                 using (BinaryWriter bw = new BinaryWriter(mmfv))
  37.                                 {
  38.                                     bw.Write(Bytes);
  39.                                 }
  40.                             }
  41.                             using (EventWaitHandle Signal = new EventWaitHandle(false, EventResetMode.AutoReset, "MemoryMappedComms:712247:" + Name + ":Signal"))
  42.                             {
  43.                                 using (EventWaitHandle WaitForTransfer = new EventWaitHandle(false, EventResetMode.AutoReset, "MemoryMappedComms:712247:" + Name + ":WaitForTransfer"))
  44.                                 {
  45.                                     using (Mutex StatMutex = new Mutex(false, "MemoryMappedComms:712247:" + Name + ":StatusMutex"))
  46.                                     {
  47.                                         Signal.Set();
  48.                                         WaitHandle.WaitAny(new WaitHandle[] { WaitForTransfer, StatMutex });
  49.                                     }
  50.                                 }
  51.                             }
  52.                         }
  53.                         return true;
  54.                     }
  55.                 }
  56.                 catch (Exception)
  57.                 {
  58.                 }
  59.             }
  60.             return false;
  61.         }
  62.  
  63.         public static bool CheckForReader(string Name)
  64.         {
  65.             try
  66.             {
  67.                 using (MemoryMappedFile mmfc = MemoryMappedFile.OpenExisting("MemoryMappedComms:712247:" + Name + ":Presence"))
  68.                 {
  69.                     return true;
  70.                 }
  71.             }
  72.             catch (Exception)
  73.             {
  74.                 return false;
  75.             }
  76.         }
  77.  
  78.         public void Dispose()
  79.         {
  80.             throw new NotImplementedException();
  81.         }
  82.     }
  83.  
  84.     public class Receiver : IDisposable
  85.     {
  86.         public event BytesReceivedEventHandler BytesReceived;
  87.         public delegate void BytesReceivedEventHandler(byte[] Bytes);
  88.  
  89.         private string Name;
  90.         private bool Running = true;
  91.         private Thread CommunicationsThread;
  92.  
  93.         public Receiver(string Name)
  94.         {
  95.             this.Name = Name;
  96.             CommunicationsThread = new Thread(() =>
  97.             {
  98.                 using (Mutex SingleOut = new Mutex(false, "MemoryMappedComms:712247:" + Name + ":RecvSingleOutMutex"))
  99.                 {
  100.                     try
  101.                     {
  102.                         SingleOut.WaitOne();
  103.                     }
  104.                     catch (Exception)
  105.                     {
  106.                     }
  107.                     try
  108.                     {
  109.                         using (MemoryMappedFile Presence = MemoryMappedFile.CreateNew("MemoryMappedComms:712247:" + Name + ":Presence", 1))
  110.                         {
  111.                             using (Mutex StatMutex = new Mutex(true, "MemoryMappedComms:712247:" + Name + ":StatusMutex"))
  112.                             {
  113.                                 while (Running)
  114.                                 {
  115.                                     using (EventWaitHandle Signal = new EventWaitHandle(false, EventResetMode.AutoReset, "MemoryMappedComms:712247:" + this.Name + ":Signal"))
  116.                                     {
  117.                                         Signal.WaitOne();
  118.                                         using (EventWaitHandle WaitForTransfer = new EventWaitHandle(false, EventResetMode.AutoReset, "MemoryMappedComms:712247:" + Name + ":WaitForTransfer"))
  119.                                         {
  120.                                             using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("MemoryMappedComms:712247:" + Name + ":Data"))
  121.                                             {
  122.                                                 using (MemoryMappedViewStream mmfv = mmf.CreateViewStream())
  123.                                                 {
  124.                                                     using (BinaryReader br = new BinaryReader(mmfv))
  125.                                                     {
  126.                                                         if (BytesReceived != null)
  127.                                                         {
  128.                                                             BytesReceived(br.ReadBytes((int)mmfv.Length));
  129.                                                         }
  130.                                                     }
  131.                                                 }
  132.                                             }
  133.                                             WaitForTransfer.Set();
  134.                                         }
  135.                                     }
  136.                                 }
  137.                             }
  138.                         }
  139.                     }
  140.                     catch (Exception)
  141.                     {
  142.                     }
  143.                 }
  144.             })
  145.             { IsBackground = true };
  146.             CommunicationsThread.Start();
  147.         }
  148.  
  149.         public void Dispose()
  150.         {
  151.             Running = false;
  152.         }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement