ANTFS_Common.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. This software is subject to the license described in the License.txt file
  3. included with this software distribution. You may not use this file except
  4. in compliance with this license.
  5. Copyright (c) Dynastream Innovations Inc. 2016
  6. All rights reserved.
  7. */
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Text;
  11. namespace ANT_Managed_Library.ANTFS
  12. {
  13. /// <summary>
  14. /// Internal structure
  15. /// </summary>
  16. internal class ResponseTimeout<ResponseType>
  17. {
  18. /// <summary>
  19. /// Initial time
  20. /// </summary>
  21. internal DateTime timeStart = DateTime.MaxValue;
  22. /// <summary>
  23. /// Desired timeout
  24. /// </summary>
  25. internal uint timeLeft = UInt32.MaxValue; // in seconds
  26. /// <summary>
  27. /// ID of response we are waiting for
  28. /// </summary>
  29. internal ResponseType ResponseID;
  30. /// <summary>
  31. /// Flag to indicate whether we are waiting for a response or not
  32. /// </summary>
  33. internal bool bWaitingForResponse = false;
  34. /// <summary>
  35. /// Configure a timeout to wait for a response
  36. /// </summary>
  37. /// <param name="theResponse">Response we are looking for</param>
  38. /// <param name="theTimeout">Timeout, in miliseconds</param>
  39. internal void SetTimeout(ResponseType theResponse, uint theTimeout)
  40. {
  41. timeStart = DateTime.Now;
  42. timeLeft = theTimeout;
  43. ResponseID = theResponse;
  44. bWaitingForResponse = true;
  45. }
  46. /// <summary>
  47. /// Clear timeout
  48. /// </summary>
  49. internal void ClearTimeout()
  50. {
  51. timeStart = DateTime.MaxValue;
  52. timeLeft = UInt32.MaxValue;
  53. bWaitingForResponse = false;
  54. }
  55. /// <summary>
  56. /// Check if the timeout has expired.
  57. /// Timeout is not triggered again, until enabled explicitly
  58. /// </summary>
  59. /// <returns>True if the timeout has expired, false otherwise</returns>
  60. internal bool HasTimeoutExpired()
  61. {
  62. if (!bWaitingForResponse || (timeStart == DateTime.MaxValue))
  63. {
  64. return false; // We were not waiting for a response
  65. }
  66. if (DateTime.Compare(DateTime.Now, timeStart.AddMilliseconds((double)timeLeft)) > 0)
  67. {
  68. ClearTimeout();
  69. return true;
  70. }
  71. else
  72. {
  73. return false;
  74. }
  75. }
  76. }
  77. internal class Common
  78. {
  79. internal static byte[] ConvertToByteArray(string myString)
  80. {
  81. // Convert as UTF-8
  82. byte[] myArray = System.Text.Encoding.UTF8.GetBytes(myString);
  83. // Append null character
  84. Array.Resize(ref myArray, myArray.Length + 1);
  85. myArray[myArray.Length - 1] = 0;
  86. return myArray;
  87. }
  88. internal static string ConvertToString(byte[] myArray)
  89. {
  90. // Convert as UTF-8
  91. string myString = System.Text.Encoding.UTF8.GetString(myArray);
  92. // Remove trailing null characters
  93. myString = myString.Remove(myString.IndexOf('\0'));
  94. return myString;
  95. }
  96. }
  97. }