HPThreadPool.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace HPSocketCS
  6. {
  7. public class HPThreadPool
  8. {
  9. private IntPtr pThreadPool = IntPtr.Zero;
  10. public HPThreadPool()
  11. {
  12. pThreadPool = ThreadPoolSdk.Create_HP_ThreadPool();
  13. }
  14. ~HPThreadPool()
  15. {
  16. if (pThreadPool != IntPtr.Zero)
  17. {
  18. ThreadPoolSdk.Destroy_HP_ThreadPool(pThreadPool);
  19. }
  20. }
  21. /// <summary>
  22. /// 启动线程池组件
  23. /// </summary>
  24. /// <param name="threadCount">线程数量, 大于0 -> dwThreadCount, 等于0 -> (CPU核数* 2 + 2), 小于0 -> (CPU核数* (-threadCount))</param>
  25. /// <param name="policy">任务拒绝处理策略</param>
  26. /// <param name="maxQueueSize">任务队列最大容量(0:不限制,默认:0)</param>
  27. /// <param name="stackSize">线程堆栈空间大小(默认:0 -> 操作系统默认)</param>
  28. /// <returns>TRUE -- 成功,FALSE -- 失败,可通过 SYS_GetLastError() 获取系统错误代码</returns>
  29. public bool Start(uint threadCount, RejectedPolicy policy, uint maxQueueSize = 0, uint stackSize = 0)
  30. {
  31. return ThreadPoolSdk.HP_ThreadPool_Start(pThreadPool, threadCount, maxQueueSize, policy, stackSize);
  32. }
  33. /// <summary>
  34. /// 在规定时间内关闭线程池组件,如果工作线程在最大等待时间内未能正常关闭,会尝试强制关闭,这种情况下很可能会造成系统资源泄漏
  35. /// </summary>
  36. /// <param name="maxWait">最大等待时间(毫秒,默认:INFINITE即-1,一直等待)</param>
  37. /// <returns>TRUE -- 成功,FALSE -- 失败,可通过 SYS_GetLastError() 获取系统错误代码</returns>
  38. public bool Stop(int maxWait = -1)
  39. {
  40. return ThreadPoolSdk.HP_ThreadPool_Stop(pThreadPool, maxWait);
  41. }
  42. /// <summary>
  43. /// 向线程池提交异步任务
  44. /// </summary>
  45. /// <param name="fnTaskProc">任务处理函数</param>
  46. /// <param name="args">任务参数</param>
  47. /// <param name="maxWait">最大等待时间(毫秒,默认:INFINITE即-1,一直等待)</param>
  48. /// <returns>TRUE -- 成功,FALSE -- 失败,可通过 SYS_GetLastError() 获取系统错误代码,其中,错误码 #ERROR_DESTINATION_ELEMENT_FULL 表示任务队列已满</returns>
  49. public bool Submit(TaskProc fnTaskProc, IntPtr args, int maxWait = -1)
  50. {
  51. return ThreadPoolSdk.HP_ThreadPool_Submit(pThreadPool, fnTaskProc, args, maxWait);
  52. }
  53. /// <summary>
  54. /// 创建 TSocketTask 对象
  55. /// 创建任务对象,该对象最终需由 DestroySocketTask() 销毁
  56. /// </summary>
  57. /// <param name="socketTaskProc">任务入口函数</param>
  58. /// <param name="sender">发起对象, 如server, client,agent对象</param>
  59. /// <param name="connId">连接id</param>
  60. /// <param name="buffer">数据</param>
  61. /// <param name="bufferSize">数据长度</param>
  62. /// <param name="taskBufferType">数据类型</param>
  63. /// <param name="wParam">自定义参数</param>
  64. /// <param name="lParam">自定义参数</param>
  65. /// <returns>TRUE -- 成功,FALSE -- 失败,可通过 SYS_GetLastError() 获取系统错误代码,其中,错误码 ERROR_DESTINATION_ELEMENT_FULL 表示任务队列已满</returns>
  66. public static IntPtr CreateSocketTask(SocketTaskProc socketTaskProc, IntPtr sender, IntPtr connId, byte[] buffer, int bufferSize, TaskBufferType taskBufferType, IntPtr wParam, IntPtr lParam)
  67. {
  68. return ThreadPoolSdk.Create_HP_SocketTaskObj(socketTaskProc, sender, connId, buffer, bufferSize, taskBufferType, wParam, lParam);
  69. }
  70. /// <summary>
  71. /// 销毁 TSocketTask 对象
  72. /// </summary>
  73. /// <param name="task"></param>
  74. public static void DestroySocketTask(IntPtr task)
  75. {
  76. ThreadPoolSdk.Destroy_HP_SocketTaskObj(task);
  77. }
  78. /// <summary>
  79. /// 提交 Socket 任务
  80. /// 向线程池提交异步 Socket 任务
  81. /// </summary>
  82. /// <param name="task">任务参数 HP_LPTSocketTask</param>
  83. /// <param name="maxWait">最大等待时间(毫秒,默认:INFINITE即-1,一直等待)</param>
  84. /// <returns>TRUE -- 成功,FALSE -- 失败,可通过 SYS_GetLastError() 获取系统错误代码,其中,错误码 ERROR_DESTINATION_ELEMENT_FULL 表示任务队列已满</returns>
  85. public bool SubmitSocketTask(IntPtr task, int maxWait = -1)
  86. {
  87. return ThreadPoolSdk.HP_ThreadPool_Submit_Task(pThreadPool, task, maxWait);
  88. }
  89. /// <summary>
  90. /// 创建 TSocketTask 对象并向线程池提交异步 Socket 任务
  91. /// </summary>
  92. /// <param name="socketTaskProc">任务入口函数</param>
  93. /// <param name="sender">发起对象, 如server, client,agent对象</param>
  94. /// <param name="connId">连接id</param>
  95. /// <param name="buffer">数据</param>
  96. /// <param name="bufferSize">数据长度</param>
  97. /// <param name="taskBufferType">数据类型</param>
  98. /// <param name="wParam">自定义参数</param>
  99. /// <param name="lParam">自定义参数</param>
  100. /// <param name="maxWait">最大等待时间(毫秒,默认:INFINITE即-1,一直等待)</param>
  101. /// <returns>TRUE -- 成功,FALSE -- 失败,可通过 SYS_GetLastError() 获取系统错误代码,其中,错误码 ERROR_DESTINATION_ELEMENT_FULL 表示任务队列已满</returns>
  102. public bool SubmitSocketTask(SocketTaskProc socketTaskProc, IntPtr sender, IntPtr connId, byte[] buffer, int bufferSize, TaskBufferType taskBufferType, IntPtr wParam, IntPtr lParam, int maxWait = -1)
  103. {
  104. var task = CreateSocketTask(socketTaskProc, sender, connId, buffer, bufferSize, taskBufferType, wParam, lParam);
  105. if (task == IntPtr.Zero)
  106. {
  107. return false;
  108. }
  109. var ret = SubmitSocketTask(task, maxWait);
  110. if (!ret)
  111. {
  112. DestroySocketTask(task);
  113. }
  114. return ret;
  115. }
  116. /// <summary>
  117. /// 增加或减少线程池的工作线程数量
  118. /// </summary>
  119. /// <param name="newThreadCount">线程数量, 大于0 -> dwNewThreadCount, 等于0 -> (CPU核数 * 2 + 2),小于0 -> (CPU核数 * (-dwNewThreadCount))</param>
  120. /// <returns>TRUE -- 成功,FALSE -- 失败,可通过 SYS_GetLastError() 获取系统错误代码</returns>
  121. public bool AdjustThreadCount(int newThreadCount)
  122. {
  123. return ThreadPoolSdk.HP_ThreadPool_AdjustThreadCount(pThreadPool, newThreadCount);
  124. }
  125. /// <summary>
  126. /// 获取或设置线程池数量
  127. /// </summary>
  128. /// <returns></returns>
  129. public int ThreadCount
  130. {
  131. get
  132. {
  133. return (int)ThreadPoolSdk.HP_ThreadPool_GetThreadCount(pThreadPool);
  134. }
  135. set
  136. {
  137. if (pThreadPool == IntPtr.Zero)
  138. {
  139. throw new InvalidOperationException("必须先调用Create方法!");
  140. }
  141. AdjustThreadCount(value);
  142. }
  143. }
  144. /// <summary>
  145. /// 检查线程池组件是否已启动
  146. /// </summary>
  147. /// <returns></returns>
  148. public bool HasStarted()
  149. {
  150. return ThreadPoolSdk.HP_ThreadPool_HasStarted(pThreadPool);
  151. }
  152. /// <summary>
  153. /// 查看线程池组件当前状态
  154. /// </summary>
  155. public ServiceState State
  156. {
  157. get
  158. {
  159. return ThreadPoolSdk.HP_ThreadPool_GetState(pThreadPool);
  160. }
  161. }
  162. /// <summary>
  163. /// 获取当前任务队列大小
  164. /// </summary>
  165. public uint QueueSize
  166. {
  167. get
  168. {
  169. return ThreadPoolSdk.HP_ThreadPool_GetQueueSize(pThreadPool);
  170. }
  171. }
  172. /// <summary>
  173. /// 获取任务队列最大容量
  174. /// </summary>
  175. public uint MaxQueueSize
  176. {
  177. get
  178. {
  179. return ThreadPoolSdk.HP_ThreadPool_GetMaxQueueSize(pThreadPool);
  180. }
  181. }
  182. public RejectedPolicy RejectedPolicy
  183. {
  184. get
  185. {
  186. return ThreadPoolSdk.HP_ThreadPool_GetRejectedPolicy(pThreadPool);
  187. }
  188. }
  189. /// <summary>
  190. /// 获取系统返回的错误码
  191. /// </summary>
  192. /// <returns></returns>
  193. public int SYSGetLastError()
  194. {
  195. return Sdk.SYS_GetLastError();
  196. }
  197. }
  198. }