Advertisement
GlobalAccessSoftware

MemoryMapper1.cs Class Update.

Mar 23rd, 2019
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.47 KB | None | 0 0
  1. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  2. // MemoryMapper1.cs 15-May-18//\\//\\//\\
  3. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
  4. // v4.4.5.53 Mapped in to Lock Out//\\//\\
  5. // v4.4.5.53 Split from PrintForm2\\//\\//
  6. // v4.4.6.54 mm2 & mm3 transition //\\//\\
  7. // Obsoletes mmf2 and mmf3 stuff. \\//\\//
  8. // (c) John P. Edwards 20-May-2018//\\//\\
  9. // v4.5.6.54 name & path/no-path mech/\\//
  10. // Mapped in to Lock Out, extended//\\//\\
  11. // v4.5.7.54neatobeto! All Dialed in!/\\//
  12. // v4.5.7.56nights Brings in Strings!\//\\
  13. // v4.5.7.56nighttime perfects class//\\//
  14. // v4.5.7.56nightvision has Auto-Size!//\\
  15. // v4.5.7.57 Auto-Size & args[6] Delete!//
  16. // v4.5.7.57 Perfects Int64 Handling. Fini
  17. // v4.5.7.58 Polished & Welltested. Jun-28
  18. // v4.5.8.60 Revised For: Int64 lengths.
  19. // v4.5.9.63 Final Integrations. 15-Aug
  20. // v4.6.9.64 26-Aug-2018 Added omitted
  21. // locking on the rest of the methods.oop
  22. // 29-Aug-2018 Clarifies Resource Access:
  23. // v4.6.9.65 Replaced Lock w/ Mutex 4 IPC!
  24. // v4.6.9.67 08-Sep-2018 Mutex Properties.
  25. // v4.8.1.78 06-Oct-2018 LastAccess Time.
  26. // v4.9.3.87 15-Nov-2018 Favorites Mods.
  27. // v4.9.4.89 20-Nov-2018 Ironing & Polish.
  28. // v5.0.6.00 21-Feb-2019 Updated all:
  29. // List<T> to Collection<T>
  30. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  31.  
  32. using System;
  33. using System.Collections.Generic;
  34. using System.Collections.ObjectModel;
  35. using System.IO;
  36. using System.Linq;
  37. using System.Threading;
  38.  
  39. using MCR =
  40.   MyCustomLibrary.Properties.Resources;
  41.  
  42. namespace MyCustomLibrary
  43. {
  44.   public static partial class MemoryMapper
  45.   {
  46.     private static readonly Mutex
  47.       Mtx1 = new Mutex(false, MCR.M1);
  48.  
  49.     private static readonly Mutex
  50.       Mtx2 = new Mutex(false, MCR.M2);
  51.  
  52.     private static readonly Mutex
  53.       Mtx3 = new Mutex(false, MCR.M3);
  54.  
  55.     #region Doc
  56.     // mmf = Memory Mapped File(s) Arguments
  57.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  58.     // args[0] = Full Path to mmf.
  59.     // args[1] = Size of file in bytes,
  60.     // args[2] = Start @ Byte (offset)
  61.     // args[3] = What to write? (OR Shred? T/F)
  62.     // args[4] = true; Read?
  63.     // args[5] = this mapping's name.
  64.     // args[6] = true; Delete after Read?
  65.     // args[7] = View Length from Offset.
  66.     // (if Given) Not Required, either way.8
  67.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  68.     #endregion
  69.  
  70.  
  71. #region MemoryMapper Class: Common Code.
  72.  
  73.     private static void Writer(string path)
  74.     {
  75.       using (new Mutex(false, "Writer"))
  76.       {
  77.         var time = DateTime.Now;
  78.         File.SetLastWriteTime(path, time);
  79.         File.SetLastAccessTime(path, time);
  80.       }
  81.     }
  82.  
  83.     private static void EndGame(
  84.       bool del, string path)
  85.     {
  86.       using (new Mutex(false, "EndGame"))
  87.       {
  88.         if (del && File.Exists(path))
  89.           File.Delete(path);
  90.         else if (File.Exists(path))
  91.           File.SetLastAccessTime(
  92.             path, DateTime.Now);
  93.       }
  94.     }
  95. #endregion Common Code
  96.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  97.  
  98.  
  99. #region Memory Mapped Files: Read & Write Bytes
  100.  
  101.     // Reads a List of Bytes from mmf.
  102.     // v4.5.7.56 04-Jun-2018 for ReUseAbility.
  103.     public static Collection<byte>
  104.       ReadBytes(object[] args)
  105.     {
  106.       Mtx1.WaitOne();
  107.       const string t = "Read Bytes";
  108.       long size, offs, view = 0;
  109.       if (args == null || args.Length < 7)
  110.         throw new ArgumentException(t);
  111.       bool read, dele, shred;
  112.       string path, name;
  113.       try // now unbox args.
  114.       { path =args[0].ToString();//[0]);
  115.         size = Convert.ToInt64(args[1]);
  116.         offs = Convert.ToInt64(args[2]);
  117.         try { shred =
  118.           Convert.ToBoolean(   args[3]);}
  119.         catch(FormatException){shred=false;}
  120.         catch(InvalidCastException){shred=false;}
  121.         read=Convert.ToBoolean(args[4]);
  122.         name=args[5].ToString(); //[5]);
  123.         dele=Convert.ToBoolean(args[6]);
  124.         if (args.Length > 7)// args[7]);
  125.         { try { view = Convert.ToInt64(args[7]); }
  126.           catch (FormatException)    { view = 0; }
  127.           catch(InvalidCastException){ view = 0;}}
  128.       } catch (InvalidCastException)
  129.         { throw new InvalidCastException(t); }
  130.       catch (FormatException)
  131.       { throw new FormatException(t); }
  132.       var bites = ReadBytesKernal(path, name,
  133.         size, read, offs, view, shred);
  134.       EndGame(dele, path);
  135.       Mtx1.ReleaseMutex();
  136.       return bites;
  137.     }
  138.  
  139.     /********************************************
  140.      * Self Contained Instance-N ^v Methods:    *
  141.      * ThreadFileSafe Useable by any instance.  *
  142.      * so long as the file varies or such.-JpE- *
  143.      ********************************************/
  144.     public static bool WriteBytes(object[] args)
  145.     {
  146.       Mtx1.WaitOne();
  147.       if(args==null||args.Length < 7) throw new
  148.         ArgumentException("Write Bytes");
  149.       string path,name;long size,offset,view = 0;
  150.       try { path = args[0].ToString(); }
  151.       catch (FormatException)
  152.       { path = Convert.ToString(args[0]); }
  153.       try { size =(Convert.ToInt64(args[1]));}
  154.       catch (InvalidCastException){size= 1;}
  155.       try { offset =Convert.ToInt64(args[2]);}
  156.       catch (FormatException)
  157.       { Mtx1.ReleaseMutex(); return false; }
  158.       catch(InvalidCastException)
  159.       { Mtx1.ReleaseMutex(); return false; }
  160.       try { name = args[5].ToString(); }
  161.       catch(InvalidCastException){name="mmf";}
  162.       catch(FormatException) { name = "mmf"; }
  163.       if (args.Length > 7)
  164.       { try { view = Convert.ToInt64(args[7]); }
  165.         catch (FormatException) { view = 0; }
  166.         catch (InvalidCastException) { view = 0L; }
  167.         catch(OverflowException) { view = 0; } }
  168.       if (!WriteBytesKernal(path, name, size,
  169.         offset, view, args[3]))
  170.       { Mtx1.ReleaseMutex();
  171.         return false; }
  172.       Writer(path);
  173.       Mtx1.ReleaseMutex();
  174.       return true;
  175.     }
  176. #endregion Read/Write Bytes
  177.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  178.  
  179.  
  180. #region Mmf: Int64 Words @ 8 bytes each.
  181.  
  182.     /// <summary> Server-Side Code
  183.     /// Generic enough to reuse from
  184.     /// either side, just watch for,
  185.     /// like, file already open errors.
  186.     /// Updated to return Generic List
  187.     /// v4.5.7.56 04-May-2018 -JpE-
  188.     /// NOTE: args[3] is now a List
  189.     /// of Long Integers. Adds Reuseability.
  190.     /// v4.6.9.65 29-Aug-2018 Mutex'd
  191.     /// </summary>
  192.     /// <param name="array"></param>
  193.     /// <returns></returns>
  194.     public static Collection<long>
  195.       ReadInt64(object[] array)
  196.     {
  197.       Mtx2.WaitOne();
  198.       if (array == null || array[2] == null
  199.         || array.Length < 7) throw new
  200.           ArgumentException("Read Int64");
  201.       string t, h;
  202.       long s, o, v = 0L;
  203.       bool r, d, b;
  204.       var c = new Collection<long>();
  205.       try{h =array[0].ToString();//[0]);
  206.         s = Convert.ToInt64(  array[1]);
  207.         o = Convert.ToInt64(  array[2]);
  208.         try { b =
  209.           Convert.ToBoolean(  array[3]); }
  210.         catch (FormatException) { b = false;}
  211.         catch(InvalidCastException){b=false;}
  212.         r = Convert.ToBoolean(array[4]);
  213.         t = array[5].ToString(); //[5]);
  214.         d = Convert.ToBoolean(array[6]);
  215.         if (array.Length > 7)
  216.           v = Convert.ToInt64(array[7]); }
  217.       catch (InvalidCastException)
  218.       { Mtx2.ReleaseMutex(); return c; }
  219.       catch (FormatException)
  220.       { Mtx2.ReleaseMutex(); return c; }
  221.       c = ReadInt64Kernal(s, h, t, r, o, v, b);
  222.       if (!r) { Mtx1.ReleaseMutex();
  223.         return c; }
  224.       EndGame(d, h);
  225.       Mtx2.ReleaseMutex();
  226.       return c;
  227.     }
  228.  
  229.     /// <summary> Client-Side Code
  230.     /// v4.5.6.54nab 20-May-2018
  231.     /// Generic enough to reuse from
  232.     /// either side, just watch for,
  233.     /// like, file already open errors in IO.
  234.     /// v4.5.7.54 25-May-2018 Int64 @ 8 bytes ea.
  235.     /// v4.5.7.56 04-Jun args[3] is now
  236.     /// a List of Int64 = Long values.
  237.     /// </summary>
  238.     /// <param name="args"></param>
  239.     public static bool
  240.       WriteInt64(object[] args)
  241.     {
  242.       Mtx2.WaitOne();
  243.       try { if (args == null
  244.         ||  args[2]  == null
  245.         ||  args.Length < 7
  246.         || !args.Any()) return false; }
  247.       catch(ArgumentException){return false;}
  248.       const long k = 0L;
  249.       const string m = "mm3";
  250.       long  s, o, v = 0L; string n, h;
  251.       try { h = args[0].ToString(); }
  252.       catch (FormatException) { h = ""; }
  253.       try { s = Convert.ToInt64(args[1]); }
  254.       catch (InvalidCastException) { s = k; }
  255.       catch (FormatException) { s = k; }
  256.       try { o = Convert.ToInt64(args[2]); }
  257.       catch (InvalidCastException){ o = 0L; }
  258.       catch (FormatException) { o = 0L; }
  259.       try { n = args[5].ToString(); }
  260.       catch (FormatException) { n = m; }
  261.       catch (InvalidCastException) { n = m; }
  262.       if (args.Length > 7)
  263.       { try { v = Convert.ToInt64(args[7]); }
  264.         catch (FormatException) { v = 0; }
  265.         catch (InvalidCastException){v = 0;}
  266.         catch (OverflowException){v = 0; } }
  267.       if (!WriteLongKernal(
  268.         args[3], s, h, n, o, v))
  269.       { Mtx2.ReleaseMutex();
  270.         return false; }
  271.       Writer(h);
  272.       Mtx2.ReleaseMutex();
  273.       return true;
  274.     }
  275. #endregion Mmf: Int64 Words @ 8 bytes each.
  276.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  277.  
  278.  
  279. #region Read/Write Strings
  280.  
  281.     /// <summary> Read in List of strings.
  282.     /// -JpE- v4.5.7.56 01-Jun-18 (from Mmf ^)
  283.     /// SEE DOC ABOVE FOR REQUIRED ARG VALUES.
  284.     /// Returns a List of string references.
  285.     /// If size arg is 0, it calculates j.i.t.
  286.     /// args[6], the 7th element now contains
  287.     /// a bool for delete file when finished. T/F
  288.     /// v4.9.3.86 Added the 8th element:
  289.     /// View Length. November 11th, 2018 -JpE-
  290.     /// </summary>
  291.     /// <param name="args"></param>
  292.     /// <returns></returns>
  293.     public static Collection<string>
  294.       ReadStrings(object[] args)
  295.     {
  296.       Mtx3.WaitOne();
  297.       if (args == null || args.Length < 7
  298.         || string.IsNullOrEmpty(
  299.           args[0].ToString()))
  300.       { Mtx3.ReleaseMutex(); return null; }
  301.       string path, name;
  302.       long size, offs, view = 0L;
  303.       bool read, del, shred = false;
  304.       try{path = args[0].ToString();//0]);
  305.         size =   Convert.ToInt64(args[1]);
  306.         offs =   Convert.ToInt64(args[2]);
  307.         //  Only used for Writes args[3]); BUT...
  308.         read = Convert.ToBoolean(args[4]);
  309.         name = args[5].ToString(); //[5]);
  310.         del  = Convert.ToBoolean(args[6]);
  311.         if (args.Length > 7)
  312.           view = Convert.ToInt64(args[7]);
  313.         try // v Reused here as a Special Case.
  314.         { if (args[3] != null) shred =
  315.             Convert.ToBoolean(args[3]); }
  316.         catch (FormatException) {   shred=false; }
  317.         catch(InvalidCastException){shred=false;}}
  318.       catch (ArgumentException)
  319.       { Mtx3.ReleaseMutex(); return null; }
  320.       catch (InvalidCastException)
  321.       { Mtx3.ReleaseMutex(); return null; }
  322.       if (!File.Exists(path))
  323.       { Mtx3.ReleaseMutex(); return null; }
  324.       var input = ReadStringsKernal(
  325.         path, size, offs, name, read, view, shred);
  326.       if (input == null || !read )
  327.       { Mtx3.ReleaseMutex();
  328.         return null; }
  329.       EndGame(del, path);
  330.       Mtx3.ReleaseMutex();
  331.       return input;
  332.     }
  333.  
  334.     /// <summary> Write List of strings
  335.     /// to memory mapped file (args[0]).
  336.     /// Return true only if EVERYTHING went ok.
  337.     /// REM: args[3] is a List of Strings!
  338.     /// Strings to write as UNICODE encoded bytes.
  339.     /// SEE DOC ABOVE FOR REQUIRED ARG VALUES.
  340.     /// </summary>
  341.     /// <param name="args"></param>
  342.     /// <returns></returns>
  343.     public static bool WriteStrings(object[] args)
  344.     {
  345.       if (args == null
  346.         || args.Length < 7) return false;
  347.       Mtx3.WaitOne();
  348.       Collection<string> strings;
  349.       try { strings = args[3] as Collection<string>; }
  350.       catch(ArgumentException)
  351.       { Mtx3.ReleaseMutex(); return false; }
  352.       catch(InvalidCastException)
  353.       { Mtx3.ReleaseMutex(); return false; }
  354.       if ( strings == null || strings.Count < 1
  355.         || string.IsNullOrEmpty(args[0].ToString()))
  356.       { Mtx3.ReleaseMutex(); return false; }
  357.       string file, name; long size, offs, view = 0L;
  358.       try// REM: args[4] doesn't apply to writes.
  359.       { file = args[0].ToString();//[0]);
  360.         size = Convert.ToInt64( args[1]);
  361.         offs = Convert.ToInt64( args[2]);
  362.         name = args[5].ToString(); //5]);
  363.         if (args.Length > 7)
  364.           view = Convert.ToInt64(args[7]); }
  365.       catch (ArgumentException)
  366.       { Mtx3.ReleaseMutex(); return false; }
  367.       catch (InvalidCastException)
  368.       { Mtx3.ReleaseMutex(); return false; }
  369.       if (size == 0) size = offs + CalcSize(strings);
  370.       //  This Call to CalcSize is ^ for AutoSize! 0
  371.       var is1 = WriteStringsKernal(file,
  372.         name, size, offs, view, strings);
  373.       if (is1) Writer(file);
  374.       Mtx3.ReleaseMutex();
  375.       return is1;
  376.     }
  377.  
  378.     private static long CalcSize(
  379.       ICollection<string> strings)
  380.     {
  381.       if (strings == null || strings.Count < 1)
  382.         throw new ArgumentException("CalcSize");
  383.       long size; try
  384.       { size = strings.Sum(s => s.Length);
  385.         size = (size* 2) + (2* strings.Count); }
  386.       catch(ArgumentNullException){size = 256; }
  387.       catch (OverflowException) {  size = 256; }
  388.         // =-> w/Int16 UniCode it's 2 bytes per char.
  389.       return size;// 2 x 8 ^
  390.       // NOTE: So 2x Chars + 2 ea. <-= <-=
  391.     }// <-= EOM <-= for ^ int16 ^ lengths.
  392.     //=-> =-> =-> (Length stored before ea string.)
  393. #endregion Read/Write Strings
  394.   //\\//\\//\\//\\//\\//\\//\\//\\//\\// EOC
  395.  
  396.  
  397.   }
  398. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement