Extra.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections.Concurrent;
  6. namespace HPSocketCS
  7. {
  8. public class Extra<T>
  9. {
  10. ConcurrentDictionary<IntPtr, T> dict = new ConcurrentDictionary<IntPtr, T>();
  11. /// <summary>
  12. /// 获取附加数据
  13. /// </summary>
  14. /// <param name="key"></param>
  15. /// <returns></returns>
  16. public T Get(IntPtr key)
  17. {
  18. T value;
  19. if (dict.TryGetValue(key, out value))
  20. {
  21. return value;
  22. }
  23. return default(T);
  24. }
  25. /// <summary>
  26. /// 设置附加数据
  27. ///
  28. /// </summary>
  29. /// <param name="key"></param>
  30. /// <param name="newValue"></param>
  31. /// <returns></returns>
  32. public bool Set(IntPtr key, T newValue)
  33. {
  34. try
  35. {
  36. dict.AddOrUpdate(key, newValue, (tKey, existingVal) => { return newValue; });
  37. return true;
  38. }
  39. catch (OverflowException)
  40. {
  41. // 字典数目超过int.max
  42. return false;
  43. }
  44. catch (ArgumentNullException)
  45. {
  46. // 参数为空
  47. return false;
  48. }
  49. catch (Exception)
  50. {
  51. return false;
  52. }
  53. }
  54. /// <summary>
  55. /// 删除附加数据
  56. /// </summary>
  57. /// <param name="key"></param>
  58. /// <returns></returns>
  59. public bool Remove(IntPtr key)
  60. {
  61. T value;
  62. return dict.TryRemove(key, out value);
  63. }
  64. }
  65. public class ConnectionExtra
  66. {
  67. ConcurrentDictionary<IntPtr, object> dict = new ConcurrentDictionary<IntPtr, object>();
  68. /// <summary>
  69. /// 获取附加数据
  70. /// </summary>
  71. /// <param name="key"></param>
  72. /// <returns></returns>
  73. public object GetExtra(IntPtr key)
  74. {
  75. object value;
  76. if (dict.TryGetValue(key, out value))
  77. {
  78. return value;
  79. }
  80. return null;
  81. }
  82. /// <summary>
  83. /// 获取附加数据
  84. /// </summary>
  85. /// <param name="key"></param>
  86. /// <returns></returns>
  87. public T GetExtra<T>(IntPtr key)
  88. {
  89. object value;
  90. if (dict.TryGetValue(key, out value))
  91. {
  92. return (T)value;
  93. }
  94. return default(T);
  95. }
  96. /// <summary>
  97. /// 设置附加数据
  98. ///
  99. /// </summary>
  100. /// <param name="key"></param>
  101. /// <param name="value"></param>
  102. /// <returns></returns>
  103. public bool SetExtra(IntPtr key, object newValue)
  104. {
  105. try
  106. {
  107. dict.AddOrUpdate(key, newValue, (tKey, existingVal) => { return newValue; });
  108. return true;
  109. }
  110. catch (OverflowException)
  111. {
  112. // 字典数目超过int.max
  113. return false;
  114. }
  115. catch (ArgumentNullException)
  116. {
  117. // 参数为空
  118. return false;
  119. }
  120. catch (Exception)
  121. {
  122. return false;
  123. }
  124. }
  125. /// <summary>
  126. /// 删除附加数据
  127. /// </summary>
  128. /// <param name="key"></param>
  129. /// <returns></returns>
  130. public bool RemoveExtra(IntPtr key)
  131. {
  132. object value;
  133. return dict.TryRemove(key, out value);
  134. }
  135. }
  136. }