Advertisement
Guest User

Untitled

a guest
Sep 12th, 2023
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.51 KB | None | 0 0
  1. public bool Receive<T>(ReceiveParameters<T> args)
  2.         {
  3.             if (args.Eop == null && args.Count == 0 && !args.AllData)
  4.             {
  5.                 throw new ArgumentException("Either Count or Eop must be set.");
  6.             }
  7.             int nSize = 0;
  8.             byte[] terminator = null;
  9.             if (args.Eop != null)
  10.             {
  11.                 if (args.Eop is Array)
  12.                 {
  13.                     Array arr = args.Eop as Array;
  14.                     terminator = GXCommon.GetAsByteArray(arr.GetValue(0));
  15.                 }
  16.                 else
  17.                 {
  18.                     terminator = GXCommon.GetAsByteArray(args.Eop);
  19.                 }
  20.                 nSize = terminator.Length;
  21.             }
  22.  
  23.             int nMinSize = (int)Math.Max(args.Count, nSize);
  24.             if (nMinSize == 0)
  25.             {
  26.                 nMinSize = 1;
  27.             }
  28.             int waitTime = args.WaitTime;
  29.             if (waitTime <= 0)
  30.             {
  31.                 waitTime = -1;
  32.             }
  33.  
  34.             //Wait until reply occurred.       
  35.             int nFound = -1;
  36.             int LastBuffSize = 0;
  37.             DateTime StartTime = DateTime.Now;
  38.             bool retValue = true;
  39.             do
  40.             {
  41.                 if (waitTime == 0)
  42.                 {
  43.                     //If we do not want to read all data.
  44.                     if (!args.AllData)
  45.                     {
  46.                         return false;
  47.                     }
  48.                     retValue = false;
  49.                     break;
  50.                 }
  51.                 if (waitTime != -1)
  52.                 {
  53.                     waitTime -= (int)(DateTime.Now - StartTime).TotalMilliseconds;
  54.                     StartTime = DateTime.Now;
  55.                     if (waitTime < 0)
  56.                     {
  57.                         waitTime = 0;
  58.                     }
  59.                 }
  60.                 bool received;
  61.                 lock (receivedSync)
  62.                 {
  63.                     received = !(LastBuffSize == receivedSize || receivedSize < nMinSize);
  64.                 }
  65.                 //Do not wait if there is data on the buffer...
  66.                 if (!received)
  67.                 {
  68.                     if (waitTime == -1)
  69.                     {
  70.                         received = receivedEvent.WaitOne();
  71.                     }
  72.                     else
  73.                     {
  74.                         received = receivedEvent.WaitOne(waitTime);
  75.                     }
  76.                     if (!received && args.Eop == null)
  77.                     {
  78.                         lock (receivedSync)
  79.                         {
  80.                             received = !(LastBuffSize == receivedSize || receivedSize < nMinSize);
  81.                         }
  82.                     }
  83.                 }
  84.                 if (received)
  85.                 {
  86.                     lock (receivedSync)
  87.                     {
  88.                         if (receivedSize == -1)
  89.                         {
  90.                             receivedSize = 0;
  91.                             return false;
  92.                         }
  93.                     }
  94.                 }
  95.                 if (this.Exception != null)
  96.                 {
  97.                     Exception ex = this.Exception;
  98.                     this.Exception = null;
  99.                     throw ex;
  100.                 }
  101.                 //If timeout occurred.
  102.                 if (!received)
  103.                 {
  104.                     //If we do not want to read all data.
  105.                     if (!args.AllData)
  106.                     {
  107.                         return false;
  108.                     }
  109.                     retValue = false;
  110.                     break;
  111.                 }
  112.                 lock (receivedSync)
  113.                 {
  114.                     LastBuffSize = receivedSize;
  115.                     //Read more data, if not enough
  116.                     if (receivedSize < nMinSize)
  117.                     {
  118.                         continue;
  119.                     }
  120.                     //If only byte count matters.
  121.                     if (nSize == 0)
  122.                     {
  123.                         nFound = args.Count;
  124.                     }
  125.                     else
  126.                     {
  127.                         int index = lastPosition != 0 && lastPosition < receivedSize ? lastPosition : args.Count;
  128.                         //If terminator found.
  129.                         if (args.Eop is Array)
  130.                         {
  131.                             foreach (object it in args.Eop as Array)
  132.                             {
  133.                                 byte[] term = GXCommon.GetAsByteArray(it);
  134.                                 if (term.Length != 1 && receivedSize - index < term.Length)
  135.                                 {
  136.                                     index = receivedSize - term.Length;
  137.                                 }
  138.                                 nFound = GXCommon.IndexOf(m_Received, term, index, receivedSize);
  139.                                 if (nFound != -1)
  140.                                 {
  141.                                     break;
  142.                                 }
  143.                             }
  144.                         }
  145.                         else
  146.                         {
  147.                             if (terminator.Length != 1 && receivedSize - index < terminator.Length)
  148.                             {
  149.                                 index = receivedSize - terminator.Length;
  150.                             }
  151.                             nFound = GXCommon.IndexOf(m_Received, terminator, index, receivedSize);
  152.                         }
  153.                         lastPosition = receivedSize;
  154.                         if (nFound != -1)
  155.                         {
  156.                             nFound += terminator.Length;
  157.                         }
  158.                     }
  159.                 }
  160.             }
  161.             while (nFound == -1);
  162.             if (nSize == 0) //If terminator is not given read only bytes that are needed.
  163.             {
  164.                 nFound = args.Count;
  165.             }
  166.             if (args.AllData) //If all data is copied.
  167.             {
  168.                 nFound = receivedSize;
  169.             }
  170.             if (nFound == 0)
  171.             {
  172.                 retValue = false;
  173.             }
  174.             //Convert bytes to object.
  175.             byte[] tmp = new byte[nFound];
  176.             lock (receivedSync)
  177.             {
  178.                 Array.Copy(m_Received, tmp, nFound);
  179.             }
  180.             int readBytes = 0;
  181.             object data = GXCommon.ByteArrayToObject(tmp, typeof(T), out readBytes);
  182.             //Remove read data.
  183.             receivedSize -= nFound;
  184.             //Received size can go less than zero if we have received data and we try to read more.
  185.             if (receivedSize < 0)
  186.             {
  187.                 receivedSize = 0;
  188.             }
  189.             if (receivedSize != 0)
  190.             {
  191.                 lock (receivedSync)
  192.                 {
  193.                     Array.Copy(m_Received, nFound, m_Received, 0, receivedSize);
  194.                 }
  195.             }
  196.             else
  197.             {
  198.                 lastPosition = 0;
  199.             }
  200.             //Reset count after read.
  201.             args.Count = 0;
  202.             //Append data.
  203.             int oldReplySize = 0;
  204.             if (args.Reply == null)
  205.             {
  206.                 args.Reply = (T)data;
  207.             }
  208.             else
  209.             {
  210.                 if (args.Reply is Array)
  211.                 {
  212.                     Array oldArray = args.Reply as Array;
  213.                     Array newArray = data as Array;
  214.                     if (newArray == null)
  215.                     {
  216.                         throw new ArgumentException();
  217.                     }
  218.                     oldReplySize = oldArray.Length;
  219.                     int len = oldArray.Length + newArray.Length;
  220.                     Array arr = (Array)Activator.CreateInstance(typeof(T), len);
  221.                     //Copy old values.
  222.                     Array.Copy(args.Reply as Array, arr, oldArray.Length);
  223.                     //Copy new values.
  224.                     Array.Copy(newArray, 0, arr, oldArray.Length, newArray.Length);
  225.                     object tmp2 = arr;
  226.                     args.Reply = (T)tmp2;
  227.                 }
  228.                 else if (args.Reply is string)
  229.                 {
  230.                     string str = args.Reply as string;
  231.                     str += (string)data;
  232.                     data = str;
  233.                     args.Reply = (T)data;
  234.                 }
  235.             }
  236.             return retValue;
  237.         }
  238.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement