ThreadPoolSdk.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. namespace HPSocketCS
  7. {
  8. /// <summary>
  9. /// 名称:拒绝策略
  10. /// 描述:调用被拒绝后的处理策略
  11. /// </summary>
  12. public enum RejectedPolicy
  13. {
  14. /// <summary>
  15. /// 立刻返回失败
  16. /// </summary>
  17. TRP_CALL_FAIL = 0,
  18. /// <summary>
  19. /// 等待(直到成功、超时或线程池关闭等原因导致失败)
  20. /// </summary>
  21. TRP_WAIT_FOR = 1,
  22. /// <summary>
  23. /// 调用者线程直接执行
  24. /// </summary>
  25. TRP_CALLER_RUN = 2,
  26. }
  27. /// <summary>
  28. /// 名称:任务缓冲区类型
  29. /// 描述:TSockeTask 对象创建和销毁时,根据不同类型的缓冲区类型作不同的处理
  30. /// </summary>
  31. public enum TaskBufferType
  32. {
  33. /// <summary>
  34. /// 深拷贝
  35. /// </summary>
  36. TBT_COPY = 0,
  37. /// <summary>
  38. /// 浅拷贝
  39. /// </summary>
  40. TBT_REFER = 1,
  41. /// <summary>
  42. /// 连接(不负责创建,但负责销毁)
  43. /// </summary>
  44. TBT_ATTACH = 2,
  45. }
  46. /************************************************************************
  47. ************************************************************************/
  48. /// <summary>
  49. /// 名称:任务处理函数
  50. /// 描述:任务处理入口函数
  51. /// 返回值:(无)
  52. /// </summary>
  53. /// <param name="pvArg">自定义参数</param>
  54. public delegate void TaskProc(IntPtr pvArg);
  55. /// <summary>
  56. /// 名称:Socket 任务处理函数
  57. /// 描述:Socket 任务处理入口函数
  58. /// 返回值:(无)
  59. /// </summary>
  60. /// <param name="pTask">pTask -- Socket 任务结构体指针</param>
  61. public delegate void SocketTaskProc(SocketTask task);
  62. /// <summary>
  63. /// 名称:Socket 任务结构体
  64. /// 描述:封装 Socket 任务相关数据结构
  65. /// </summary>
  66. [StructLayout(LayoutKind.Sequential)]
  67. public struct SocketTask
  68. {
  69. /// <summary>
  70. /// 任务处理函数
  71. /// </summary>
  72. public SocketTaskProc Fn;
  73. /// <summary>
  74. /// 发起对象
  75. /// </summary>
  76. public IntPtr Sender;
  77. /// <summary>
  78. /// 连接 ID
  79. /// </summary>
  80. public IntPtr ConnId;
  81. /// <summary>
  82. /// 数据缓冲区
  83. /// </summary>
  84. public IntPtr Buffer;
  85. /// <summary>
  86. /// 数据缓冲区长度
  87. /// </summary>
  88. public int BufferSize;
  89. /// <summary>
  90. /// 缓冲区类型
  91. /// </summary>
  92. public TaskBufferType BufferType;
  93. /// <summary>
  94. /// 自定义参数
  95. /// </summary>
  96. public IntPtr WParam;
  97. /// <summary>
  98. /// 自定义参数
  99. /// </summary>
  100. public IntPtr LParam;
  101. }
  102. public class ThreadPoolSdk
  103. {
  104. /// <summary>
  105. /// 创建 IHPThreadPool 对象
  106. /// </summary>
  107. /// <param name="pThreadPool"></param>
  108. /// <returns></returns>
  109. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  110. public static extern IntPtr Create_HP_ThreadPool();
  111. /// <summary>
  112. /// 销毁 IHPThreadPool 对象
  113. /// </summary>
  114. /// <param name="pThreadPool"></param>
  115. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  116. public static extern void Destroy_HP_ThreadPool(IntPtr pThreadPool);
  117. /// <summary>
  118. /// 名称:创建 TSocketTask 对象
  119. /// 描述:创建任务对象,该对象最终需由 HP_Destroy_SocketTaskObj() 销毁
  120. /// </summary>
  121. /// <param name="fnTaskProc">任务处理函数</param>
  122. /// <param name="pSender">发起对象</param>
  123. /// <param name="dwConnID">连接 ID</param>
  124. /// <param name="pBuffer">数据缓冲区</param>
  125. /// <param name="iBuffLen">数据缓冲区长度</param>
  126. /// <param name="enBuffType">数据缓冲区类型(默认:TBT_COPY) </param>
  127. /// <param name="wParam">自定义参数</param>
  128. /// <param name="lParam">自定义参数</param>
  129. /// <returns></returns>
  130. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  131. public static extern IntPtr Create_HP_SocketTaskObj(SocketTaskProc fnTaskProc, IntPtr pSender, IntPtr dwConnID, byte[] pBuffer, int iBuffLen, TaskBufferType enBuffType /*= TBT_COPY*/, IntPtr wParam /*= 0*/, IntPtr lParam /*= 0*/);
  132. /// <summary>
  133. /// 销毁 TSocketTask 对象
  134. /// </summary>
  135. /// <param name="pTask"></param>
  136. /// <returns></returns>
  137. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  138. public static extern void Destroy_HP_SocketTaskObj(IntPtr pTask);
  139. /// <summary>
  140. /// 名称:启动线程池组件
  141. /// </summary>
  142. /// <param name="pThreadPool"></param>
  143. /// <param name="dwThreadCount">线程数量,(默认:0)</param>
  144. /// <param name="dwMaxQueueSize">任务队列最大容量(默认:0,不限制)</param>
  145. /// <param name="enRejectedPolicy">任务拒绝处理策略</param>
  146. /// <param name="dwStackSize">线程堆栈空间大小(默认:0 -> 操作系统默认)</param>
  147. /// <returns> TRUE -- 成功 , FALSE -- 失败,可通过 SYS_GetLastError() 获取系统错误代码
  148. /// </returns>
  149. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  150. public static extern bool HP_ThreadPool_Start(IntPtr pThreadPool, uint dwThreadCount /*= 0*/, uint dwMaxQueueSize /*= 0*/, RejectedPolicy enRejectedPolicy /*= TRP_CALL_FAIL*/, uint dwStackSize /*= 0*/);
  151. /// <summary>
  152. /// 名称:关闭线程池组件
  153. /// 描述:在规定时间内关闭线程池组件,如果工作线程在最大等待时间内未能正常关闭,会尝试强制关闭,这种情况下很可能会造成系统资源泄漏
  154. /// </summary>
  155. /// <param name="pThreadPool"></param>
  156. /// <param name="dwMaxWait">最大等待时间(毫秒,默认:INFINITE,一直等待)</param>
  157. /// <returns> TRUE -- 成功, FALSE -- 失败,可通过 SYS_GetLastError() 获取系统错误代码
  158. /// </returns>
  159. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  160. public static extern bool HP_ThreadPool_Stop(IntPtr pThreadPool, int dwMaxWait /*= INFINITE*/);
  161. /// <summary>
  162. /// 名称:提交任务
  163. /// 描述:向线程池提交异步任务
  164. /// </summary>
  165. /// <param name="pThreadPool"></param>
  166. /// <param name="fnTaskProc">任务处理函数</param>
  167. /// <param name="pvArg">任务参数</param>
  168. /// <param name="dwMaxWait">任务提交最大等待时间(仅对 TRP_WAIT_FOR 类型线程池生效,默认:INFINITE,一直等待)</param>
  169. /// <returns>TRUE -- 成功, FALSE -- 失败,可通过 SYS_GetLastError() 获取系统错误代码, 其中,错误码 ERROR_DESTINATION_ELEMENT_FULL 表示任务队列已满 </returns>
  170. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  171. public static extern bool HP_ThreadPool_Submit(IntPtr pThreadPool, TaskProc fnTaskProc, IntPtr pvArg, int dwMaxWait /*= INFINITE*/);
  172. /// <summary>
  173. /// 名称:提交 Socket 任务
  174. /// 描述:向线程池提交异步 Socket 任务
  175. /// </summary>
  176. /// <param name="pThreadPool"></param>
  177. /// <param name="pTask">任务参数 HP_LPTSocketTask</param>
  178. /// <param name="dwMaxWait">任务提交最大等待时间(仅对 TRP_WAIT_FOR 类型线程池生效,默认:INFINITE,一直等待)</param>
  179. /// <returns>TRUE -- 成功, FALSE -- 失败,可通过 SYS_GetLastError() 获取系统错误代码其中,错误码 ERROR_DESTINATION_ELEMENT_FULL 表示任务队列已满</returns>
  180. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  181. public static extern bool HP_ThreadPool_Submit_Task(IntPtr pThreadPool, IntPtr pTask, int dwMaxWait /*= INFINITE*/);
  182. /// <summary>
  183. /// 名称:调整线程池大小
  184. /// 描述:增加或减少线程池的工作线程数量
  185. /// </summary>
  186. /// <param name="pThreadPool"></param>
  187. /// <param name="dwNewThreadCount">线程数量</param>
  188. /// <returns>TRUE -- 成功, FALSE -- 失败,可通过 SYS_GetLastError() 获取系统错误代码</returns>
  189. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  190. public static extern bool HP_ThreadPool_AdjustThreadCount(IntPtr pThreadPool, int dwNewThreadCount);
  191. /// <summary>
  192. /// 检查线程池组件是否已启动
  193. /// </summary>
  194. /// <param name="pThreadPool"></param>
  195. /// <returns></returns>
  196. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  197. public static extern bool HP_ThreadPool_HasStarted(IntPtr pThreadPool);
  198. /// <summary>
  199. /// 查看线程池组件当前状态
  200. /// </summary>
  201. /// <param name="pThreadPool"></param>
  202. /// <returns></returns>
  203. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  204. public static extern ServiceState HP_ThreadPool_GetState(IntPtr pThreadPool);
  205. /// <summary>
  206. /// 获取当前任务队列大小
  207. /// </summary>
  208. /// <param name="pThreadPool"></param>
  209. /// <returns></returns>
  210. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  211. public static extern uint HP_ThreadPool_GetQueueSize(IntPtr pThreadPool);
  212. /// <summary>
  213. /// 获取工作线程数量
  214. /// </summary>
  215. /// <param name="pThreadPool"></param>
  216. /// <returns></returns>
  217. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  218. public static extern int HP_ThreadPool_GetThreadCount(IntPtr pThreadPool);
  219. /// <summary>
  220. /// 获取任务队列最大容量
  221. /// </summary>
  222. /// <param name="pThreadPool"></param>
  223. /// <returns></returns>
  224. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  225. public static extern uint HP_ThreadPool_GetMaxQueueSize(IntPtr pThreadPool);
  226. /// <summary>
  227. /// 获取任务拒绝处理策略
  228. /// </summary>
  229. /// <param name="pThreadPool"></param>
  230. /// <returns></returns>
  231. [DllImport(Sdk.HPSOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)]
  232. public static extern RejectedPolicy HP_ThreadPool_GetRejectedPolicy(IntPtr pThreadPool);
  233. }
  234. }