123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace HPSocketCS
- {
- public class HPThreadPool
- {
- private IntPtr pThreadPool = IntPtr.Zero;
- public HPThreadPool()
- {
- pThreadPool = ThreadPoolSdk.Create_HP_ThreadPool();
- }
- ~HPThreadPool()
- {
- if (pThreadPool != IntPtr.Zero)
- {
- ThreadPoolSdk.Destroy_HP_ThreadPool(pThreadPool);
- }
- }
-
-
-
-
-
-
-
-
- public bool Start(uint threadCount, RejectedPolicy policy, uint maxQueueSize = 0, uint stackSize = 0)
- {
- return ThreadPoolSdk.HP_ThreadPool_Start(pThreadPool, threadCount, maxQueueSize, policy, stackSize);
- }
-
-
-
-
-
- public bool Stop(int maxWait = -1)
- {
- return ThreadPoolSdk.HP_ThreadPool_Stop(pThreadPool, maxWait);
- }
-
-
-
-
-
-
-
- public bool Submit(TaskProc fnTaskProc, IntPtr args, int maxWait = -1)
- {
- return ThreadPoolSdk.HP_ThreadPool_Submit(pThreadPool, fnTaskProc, args, maxWait);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static IntPtr CreateSocketTask(SocketTaskProc socketTaskProc, IntPtr sender, IntPtr connId, byte[] buffer, int bufferSize, TaskBufferType taskBufferType, IntPtr wParam, IntPtr lParam)
- {
- return ThreadPoolSdk.Create_HP_SocketTaskObj(socketTaskProc, sender, connId, buffer, bufferSize, taskBufferType, wParam, lParam);
- }
-
-
-
-
- public static void DestroySocketTask(IntPtr task)
- {
- ThreadPoolSdk.Destroy_HP_SocketTaskObj(task);
- }
-
-
-
-
-
-
-
- public bool SubmitSocketTask(IntPtr task, int maxWait = -1)
- {
- return ThreadPoolSdk.HP_ThreadPool_Submit_Task(pThreadPool, task, maxWait);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public bool SubmitSocketTask(SocketTaskProc socketTaskProc, IntPtr sender, IntPtr connId, byte[] buffer, int bufferSize, TaskBufferType taskBufferType, IntPtr wParam, IntPtr lParam, int maxWait = -1)
- {
- var task = CreateSocketTask(socketTaskProc, sender, connId, buffer, bufferSize, taskBufferType, wParam, lParam);
- if (task == IntPtr.Zero)
- {
- return false;
- }
- var ret = SubmitSocketTask(task, maxWait);
- if (!ret)
- {
- DestroySocketTask(task);
- }
- return ret;
- }
-
-
-
-
-
- public bool AdjustThreadCount(int newThreadCount)
- {
- return ThreadPoolSdk.HP_ThreadPool_AdjustThreadCount(pThreadPool, newThreadCount);
- }
-
-
-
-
- public int ThreadCount
- {
- get
- {
- return (int)ThreadPoolSdk.HP_ThreadPool_GetThreadCount(pThreadPool);
- }
- set
- {
- if (pThreadPool == IntPtr.Zero)
- {
- throw new InvalidOperationException("必须先调用Create方法!");
- }
- AdjustThreadCount(value);
- }
- }
-
-
-
-
- public bool HasStarted()
- {
- return ThreadPoolSdk.HP_ThreadPool_HasStarted(pThreadPool);
- }
-
-
-
- public ServiceState State
- {
- get
- {
- return ThreadPoolSdk.HP_ThreadPool_GetState(pThreadPool);
- }
- }
-
-
-
- public uint QueueSize
- {
- get
- {
- return ThreadPoolSdk.HP_ThreadPool_GetQueueSize(pThreadPool);
- }
- }
-
-
-
- public uint MaxQueueSize
- {
- get
- {
- return ThreadPoolSdk.HP_ThreadPool_GetMaxQueueSize(pThreadPool);
- }
- }
- public RejectedPolicy RejectedPolicy
- {
- get
- {
- return ThreadPoolSdk.HP_ThreadPool_GetRejectedPolicy(pThreadPool);
- }
- }
-
-
-
-
- public int SYSGetLastError()
- {
- return Sdk.SYS_GetLastError();
- }
- }
- }
|