using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using HPSocketCS; namespace HPSocketCS { /// /// 通信组件服务状态,用程序可以通过通信组件的 GetState() 方法获取组件当前服务状态 /// public enum ServiceState { /// /// 正在启动 /// Starting = 0, /// /// 已经启动 /// Started = 1, /// /// 正在停止 /// Stoping = 2, /// /// 已经启动 /// Stoped = 3, } /// /// Socket 操作类型,应用程序的 OnErrror() 事件中通过该参数标识是哪种操作导致的错误 /// public enum SocketOperation { Unknown = 0, // Unknown Acccept = 1, // Acccept Connnect = 2, // Connnect Send = 3, // Send Receive = 4, // Receive }; /// /// 事件通知处理结果,事件通知的返回值,不同的返回值会影响通信组件的后续行为 /// public enum HandleResult { /// /// 成功 /// Ok = 0, /// /// 忽略 /// Ignore = 1, /// /// 错误 /// Error = 2, }; /// /// 名称:操作结果代码 /// 描述:组件 Start() / Stop() 方法执行失败时,可通过 GetLastError() 获取错误代码 /// public enum SocketError { /// /// 成功 /// Ok = 0, /// /// 当前状态不允许操作 /// IllegalState = 1, /// /// 非法参数 /// InvalidParam = 2, /// /// 创建 SOCKET 失败 /// SocketCreate = 3, /// /// 绑定 SOCKET 失败 /// SocketBind = 4, /// /// 设置 SOCKET 失败 /// SocketPrepare = 5, /// /// 监听 SOCKET 失败 /// SocketListen = 6, /// /// 创建完成端口失败 /// CPCreate = 7, /// /// 创建工作线程失败 /// WorkerThreadCreate = 8, /// /// 创建监测线程失败 /// DetectThreadCreate = 9, /// /// 绑定完成端口失败 /// SocketAttachToCP = 10, /// /// 连接服务器失败 /// ConnectServer = 11, /// /// 网络错误 /// Network = 12, /// /// 数据处理错误 /// DataProc = 13, /// /// 数据发送失败 /// DataSend = 14, }; /// /// 数据抓取结果,数据抓取操作的返回值 /// public enum FetchResult { /// /// 成功 /// Ok = 0, /// /// 抓取长度过大 /// LengthTooLong = 1, /// /// 找不到 ConnID 对应的数据 /// DataNotFound = 2, }; /// /// 发送策略 /// public enum SendPolicy { /// /// 打包模式(默认) /// Pack = 0, /// /// 安全模式 /// Safe = 1, /// /// 直接模式 /// Direct = 2, }; /// /// 接收策略 /// public enum RecvPolicy { /// /// 串行模式(默认) /// Serial = 0, /// /// 并行模式 /// Parallel = 1, }; /****************************************************/ /************** sockaddr结构体,udp服务器时OnAccept最后个参数可转化 **************/ [StructLayout(LayoutKind.Sequential)] public struct in_addr { public ulong S_addr; } //[StructLayout(LayoutKind.Sequential)] //public struct in_addr //{ // public byte s_b1, s_b2, s_b3, s_b4; //} [StructLayout(LayoutKind.Sequential)] public struct sockaddr_in { public short sin_family; public ushort sin_port; public in_addr sin_addr; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] public byte[] sLibNamesin_zero; } /****************************************************/ [StructLayout(LayoutKind.Sequential)] public struct WSABUF { public int Length; public IntPtr Buffer; } } namespace HPSocketCS.SDK { /// /// Unicode版本 /// public class HPSocketSdk { /// /// HPSocket的文件路径 /// //#if DEBUG // private const string SOCKET_DLL_PATH = "HPSocket4C_UD.dll"; //#else private const string SOCKET_DLL_PATH = "HPSocket4C_U.dll"; //#endif /*****************************************************************************************************/ /******************************************** 公共类、接口 ********************************************/ /*****************************************************************************************************/ /****************************************************/ /************** HPSocket4C.dll 回调函数 **************/ /* Agent & Server & Clent */ public delegate HandleResult OnSend(IntPtr connId, IntPtr pData, int length); public delegate HandleResult OnReceive(IntPtr connId, IntPtr pData, int length); public delegate HandleResult OnPullReceive(IntPtr connId, int length); public delegate HandleResult OnClose(IntPtr connId); public delegate HandleResult OnError(IntPtr connId, SocketOperation enOperation, int errorCode); /* Agent & Server */ public delegate HandleResult OnShutdown(); /* Agent & Client */ public delegate HandleResult OnPrepareConnect(IntPtr connId /* IntPtr pClient */, uint socket); public delegate HandleResult OnConnect(IntPtr connId /* IntPtr pClient */); /* Server */ public delegate HandleResult OnPrepareListen(IntPtr soListen); public delegate HandleResult OnAccept(IntPtr connId, IntPtr pClient); /****************************************************/ /************** HPSocket4C.dll 导出函数 **************/ /// /// 创建 TcpServer 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern IntPtr Create_HP_TcpServer(IntPtr pListener); /// /// 创建 TcpClient 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern IntPtr Create_HP_TcpClient(IntPtr pListener); /// /// 创建 TcpAgent 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern IntPtr Create_HP_TcpAgent(IntPtr pListener); /// /// 创建 TcpPullServer 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern IntPtr Create_HP_TcpPullServer(IntPtr pListener); /// /// 创建 TcpPullClient 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern IntPtr Create_HP_TcpPullClient(IntPtr pListener); /// /// 创建 TcpPullAgent 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern IntPtr Create_HP_TcpPullAgent(IntPtr pListener); /// /// 创建 UdpServer 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern IntPtr Create_HP_UdpServer(IntPtr pListener); /// /// 创建 UdpClient 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern IntPtr Create_HP_UdpClient(IntPtr pListener); /// /// 销毁 TcpServer 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void Destroy_HP_TcpServer(IntPtr pServer); /// /// 销毁 TcpClient 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void Destroy_HP_TcpClient(IntPtr pClient); /// /// 销毁 TcpAgent 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void Destroy_HP_TcpAgent(IntPtr pAgent); /// /// 销毁 TcpPullServer 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void Destroy_HP_TcpPullServer(IntPtr pServer); /// /// 销毁 TcpPullClient 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void Destroy_HP_TcpPullClient(IntPtr pClient); /// /// 销毁 TcpPullAgent 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void Destroy_HP_TcpPullAgent(IntPtr pAgent); /// /// 销毁 UdpServer 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void Destroy_HP_UdpServer(IntPtr pServer); /// /// 销毁 UdpClient 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void Destroy_HP_UdpClient(IntPtr pClient); /// /// 创建 TcpServerListener 对象 /// /// [DllImport(SOCKET_DLL_PATH)] public static extern IntPtr Create_HP_TcpServerListener(); /// /// 创建 TcpClientListener 对象 /// /// [DllImport(SOCKET_DLL_PATH)] public static extern IntPtr Create_HP_TcpClientListener(); /// /// 创建 TcpAgentListener 对象 /// /// [DllImport(SOCKET_DLL_PATH)] public static extern IntPtr Create_HP_TcpAgentListener(); /// /// 创建 TcpPullServerListener 对象 /// /// [DllImport(SOCKET_DLL_PATH)] public static extern IntPtr Create_HP_TcpPullServerListener(); /// /// 创建 TcpPullClientListener 对象 /// /// [DllImport(SOCKET_DLL_PATH)] public static extern IntPtr Create_HP_TcpPullClientListener(); /// /// 创建 TcpPullAgentListener 对象 /// /// [DllImport(SOCKET_DLL_PATH)] public static extern IntPtr Create_HP_TcpPullAgentListener(); /// /// 创建 UdpServerListener 对象 /// /// [DllImport(SOCKET_DLL_PATH)] public static extern IntPtr Create_HP_UdpServerListener(); /// /// 创建 UdpClientListener 对象 /// /// [DllImport(SOCKET_DLL_PATH)] public static extern IntPtr Create_HP_UdpClientListener(); /// /// 销毁 TcpServerListener 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void Destroy_HP_TcpServerListener(IntPtr pListener); /// /// 销毁 TcpClientListener 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void Destroy_HP_TcpClientListener(IntPtr pListener); /// /// 销毁 TcpAgentListener 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void Destroy_HP_TcpAgentListener(IntPtr pListener); /// /// 销毁 TcpPullServerListener 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void Destroy_HP_TcpPullServerListener(IntPtr pListener); /// /// 销毁 TcpPullClientListener 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void Destroy_HP_TcpPullClientListener(IntPtr pListener); /// /// 销毁 TcpPullAgentListener 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void Destroy_HP_TcpPullAgentListener(IntPtr pListener); /// /// 销毁 UdpServerListener 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void Destroy_HP_UdpServerListener(IntPtr pListener); /// /// 销毁 UdpClientListener 对象 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void Destroy_HP_UdpClientListener(IntPtr pListener); /**********************************************************************************/ /***************************** Server 回调函数设置方法 *****************************/ [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Set_FN_Server_OnPrepareListen(IntPtr pListener, OnPrepareListen fn); [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Set_FN_Server_OnAccept(IntPtr pListener, OnAccept fn); [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Set_FN_Server_OnSend(IntPtr pListener, OnSend fn); [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Set_FN_Server_OnReceive(IntPtr pListener, OnReceive fn); [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Set_FN_Server_OnPullReceive(IntPtr pListener, OnPullReceive fn); [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Set_FN_Server_OnClose(IntPtr pListener, OnClose fn); [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Set_FN_Server_OnError(IntPtr pListener, OnError fn); [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Set_FN_Server_OnShutdown(IntPtr pListener, OnShutdown fn); /**********************************************************************************/ /***************************** Client 回调函数设置方法 *****************************/ [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Set_FN_Client_OnPrepareConnect(IntPtr pListener, OnPrepareConnect fn); [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Set_FN_Client_OnConnect(IntPtr pListener, OnConnect fn); [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Set_FN_Client_OnSend(IntPtr pListener, OnSend fn); [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Set_FN_Client_OnReceive(IntPtr pListener, OnReceive fn); [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Set_FN_Client_OnPullReceive(IntPtr pListener, OnPullReceive fn); [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Set_FN_Client_OnClose(IntPtr pListener, OnClose fn); [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Set_FN_Client_OnError(IntPtr pListener, OnError fn); /**********************************************************************************/ /****************************** Agent 回调函数设置方法 *****************************/ [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Set_FN_Agent_OnPrepareConnect(IntPtr pListener, OnPrepareConnect fn); [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Set_FN_Agent_OnConnect(IntPtr pListener, OnConnect fn); [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Set_FN_Agent_OnSend(IntPtr pListener, OnSend fn); [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Set_FN_Agent_OnReceive(IntPtr pListener, OnReceive fn); [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Set_FN_Agent_OnPullReceive(IntPtr pListener, OnPullReceive fn); [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Set_FN_Agent_OnClose(IntPtr pListener, OnClose fn); [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Set_FN_Agent_OnError(IntPtr pListener, OnError fn); [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Set_FN_Agent_OnShutdown(IntPtr pListener, OnShutdown fn); /**************************************************************************/ /***************************** Server 操作方法 *****************************/ /// /// 名称:启动通信组件 /// 描述:启动服务端通信组件,启动完成后可开始接收客户端连接并收发数据 /// /// /// 监听地址 /// 监听端口 /// 失败,可通过 GetLastError() 获取错误代码 [DllImport(SOCKET_DLL_PATH, CharSet = CharSet.Unicode)] public static extern bool HP_Server_Start(IntPtr pServer, String pszBindAddress, ushort usPort); /// /// 关闭服务端通信组件,关闭完成后断开所有客户端连接并释放所有资源 /// /// /// 失败,可通过 GetLastError() 获取错误代码 [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Server_Stop(IntPtr pServer); /// /// 用户通过该方法向指定客户端发送数据 /// /// /// 连接 ID /// 发送数据长度 /// 发送数据长度 /// [DllImport(SOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)] public static extern bool HP_Server_Send(IntPtr pServer, IntPtr connId, byte[] pBuffer, int length); /// /// 用户通过该方法向指定客户端发送数据 /// /// /// 连接 ID /// 发送数据长度 /// 发送数据长度 /// [DllImport(SOCKET_DLL_PATH, SetLastError = true)] public static extern bool HP_Server_Send(IntPtr pServer, IntPtr connId, IntPtr pBuffer, int length); /// /// 用户通过该方法向指定客户端发送数据 /// /// /// /// /// /// 针对pBuffer的偏移 /// [DllImport(SOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)] public static extern bool HP_Server_SendPart(IntPtr pServer, IntPtr connId, byte[] pBuffer, int length, int iOffset); /// /// 用户通过该方法向指定客户端发送数据 /// /// /// /// /// /// 针对pBuffer的偏移 /// [DllImport(SOCKET_DLL_PATH, SetLastError = true)] public static extern bool HP_Server_SendPart(IntPtr pServer, IntPtr connId, IntPtr pBuffer, int length, int iOffset); /// /// 发送多组数据 /// 向指定连接发送多组数据 /// TCP - 顺序发送所有数据包 /// UDP - 把所有数据包组合成一个数据包发送(数据包的总长度不能大于设置的 UDP 包最大长度) /// /// /// 连接 ID /// 发送缓冲区数组 /// 发送缓冲区数目 /// TRUE.成功,FALSE.失败,可通过 Windows API 函数 ::GetLastError() 获取 Windows 错误代码 [DllImport(SOCKET_DLL_PATH, SetLastError = true)] public static extern bool HP_Server_SendPackets(IntPtr pServer, IntPtr connId, WSABUF[] pBuffers, int iCount); /// /// 断开与某个客户端的连接 /// /// /// 连接 ID /// 是否强制断开连接 /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Server_Disconnect(IntPtr pServer, IntPtr connId, bool bForce); /// /// 断开超过指定时长的连接 /// /// /// 时长(毫秒) /// 是否强制断开连接 /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Server_DisconnectLongConnections(IntPtr pServer, uint dwPeriod, bool bForce); /// /// 断开超过指定时长的静默连接 /// /// /// 时长(毫秒) /// 是否强制断开连接 /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Server_DisconnectSilenceConnections(IntPtr pServer, uint dwPeriod, bool bForce); /******************************************************************************/ /***************************** Server 属性访问方法 *****************************/ /// /// 设置数据发送策略 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Server_SetSendPolicy(IntPtr pServer, SendPolicy enSendPolicy); /// /// 获取数据发送策略 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern SendPolicy HP_Server_GetSendPolicy(IntPtr pServer); /// /// 设置数据接受策略 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Server_SetRecvPolicy(IntPtr pServer, RecvPolicy enSendPolicy); /// /// 获取数据接收策略 /// [DllImport(SOCKET_DLL_PATH)] public static extern RecvPolicy HP_Server_GetRecvPolicy(IntPtr pServer); /// /// 设置连接的附加数据 /// 是否为连接绑定附加数据或者绑定什么样的数据,均由应用程序只身决定 /// /// /// 连接 ID /// /// 若返回 false 失败则为(无效的连接 ID) [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Server_SetConnectionExtra(IntPtr pServer, IntPtr connId, IntPtr pExtra); /// /// 获取连接的附加数据 /// 是否为连接绑定附加数据或者绑定什么样的数据,均由应用程序只身决定 /// /// /// 连接 ID /// 数据指针 /// 若返回 false 失败则为(无效的连接 ID) [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Server_GetConnectionExtra(IntPtr pServer, IntPtr connId, ref IntPtr pExtra); /// /// 检查通信组件是否已启动 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Server_HasStarted(IntPtr pServer); /// /// 查看通信组件当前状态 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern ServiceState HP_Server_GetState(IntPtr pServer); /// /// 获取最近一次失败操作的错误代码 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern SocketError HP_Server_GetLastError(IntPtr pServer); /// /// 获取最近一次失败操作的错误描述 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern IntPtr HP_Server_GetLastErrorDesc(IntPtr pServer); /// /// 获取连接中未发出数据的长度 /// /// /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Server_GetPendingDataLength(IntPtr pServer, IntPtr connId, ref int piPending); /// /// 获取客户端连接数 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_Server_GetConnectionCount(IntPtr pServer); /// /// 获取所有连接的 CONNID /// /// /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Server_GetAllConnectionIDs(IntPtr pServer, IntPtr[] pIDs, ref uint pdwCount); /// /// 获取某个客户端连接时长(毫秒) /// /// /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Server_GetConnectPeriod(IntPtr pServer, IntPtr connId, ref uint pdwPeriod); /// /// 获取某个连接静默时间(毫秒) /// /// /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Server_GetSilencePeriod(IntPtr pServer, IntPtr connId, ref uint pdwPeriod); /// /// 获取监听 Socket 的地址信息 /// /// /// /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Server_GetListenAddress(IntPtr pServer, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpszAddress, ref int piAddressLen, ref ushort pusPort); /// /// 获取某个客户端连接的地址信息 /// /// /// /// /// 传入传出值,大小最好在222.222.222.222的长度以上 /// /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Server_GetRemoteAddress(IntPtr pServer, IntPtr connId, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpszAddress, ref int piAddressLen, ref ushort pusPort); /// /// 设置 Socket 缓存对象锁定时间(毫秒,在锁定期间该 Socket 缓存对象不能被获取使用) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Server_SetFreeSocketObjLockTime(IntPtr pServer, uint dwFreeSocketObjLockTime); /// /// 设置 Socket 缓存池大小(通常设置为平均并发连接数量的 1/3 - 1/2) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Server_SetFreeSocketObjPool(IntPtr pServer, uint dwFreeSocketObjPool); /// /// 设置内存块缓存池大小(通常设置为 Socket 缓存池大小的 2 - 3 倍) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Server_SetFreeBufferObjPool(IntPtr pServer, uint dwFreeBufferObjPool); /// /// 设置 Socket 缓存池回收阀值(通常设置为 Socket 缓存池大小的 3 倍) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Server_SetFreeSocketObjHold(IntPtr pServer, uint dwFreeSocketObjHold); /// /// 设置内存块缓存池回收阀值(通常设置为内存块缓存池大小的 3 倍) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Server_SetFreeBufferObjHold(IntPtr pServer, uint dwFreeBufferObjHold); /// /// 设置工作线程数量(通常设置为 2 * CPU + 2) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Server_SetWorkerThreadCount(IntPtr pServer, uint dwWorkerThreadCount); /// /// 设置关闭服务前等待连接关闭的最长时限(毫秒,0 则不等待) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Server_SetMaxShutdownWaitTime(IntPtr pServer, uint dwMaxShutdownWaitTime); /// /// 设置是否标记静默时间(设置为 TRUE 时 DisconnectSilenceConnections() 和 GetSilencePeriod() 才有效,默认:FALSE) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Server_SetMarkSilence(IntPtr pServer, bool bMarkSilence); /// /// 获取 Socket 缓存对象锁定时间 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_Server_GetFreeSocketObjLockTime(IntPtr pServer); /// /// 获取 Socket 缓存池大小 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_Server_GetFreeSocketObjPool(IntPtr pServer); /// /// 获取内存块缓存池大小 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_Server_GetFreeBufferObjPool(IntPtr pServer); /// /// 获取 Socket 缓存池回收阀值 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_Server_GetFreeSocketObjHold(IntPtr pServer); /// /// 获取内存块缓存池回收阀值 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_Server_GetFreeBufferObjHold(IntPtr pServer); /// /// 获取工作线程数量 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_Server_GetWorkerThreadCount(IntPtr pServer); /// /// 获取关闭服务前等待连接关闭的最长时限 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_Server_GetMaxShutdownWaitTime(IntPtr pServer); /// /// 检测是否标记静默时间 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Server_IsMarkSilence(IntPtr pServer); /**********************************************************************************/ /***************************** TCP Server 操作方法 *****************************/ /// /// 名称:发送小文件 /// 描述:向指定连接发送 4096 KB 以下的小文件 /// /// /// 连接 ID /// 文件路径 /// 头部附加数据 /// 尾部附加数据 /// TRUE.成功 FALSE -- 失败,可通过 Windows API 函数 ::GetLastError() 获取 Windows 错误代码 [DllImport(SOCKET_DLL_PATH, CharSet = CharSet.Unicode, SetLastError = true)] public static extern bool HP_TcpServer_SendSmallFile(IntPtr pServer, IntPtr connId, string lpszFileName, ref WSABUF pHead, ref WSABUF pTail); /**********************************************************************************/ /***************************** TCP Server 属性访问方法 *****************************/ /// /// 设置 Accept 预投递数量(根据负载调整设置,Accept 预投递数量越大则支持的并发连接请求越多) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_TcpServer_SetAcceptSocketCount(IntPtr pServer, uint dwAcceptSocketCount); /// /// 设置通信数据缓冲区大小(根据平均通信数据包大小调整设置,通常设置为 1024 的倍数) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_TcpServer_SetSocketBufferSize(IntPtr pServer, uint dwSocketBufferSize); /// /// 设置监听 Socket 的等候队列大小(根据并发连接数量调整设置) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_TcpServer_SetSocketListenQueue(IntPtr pServer, uint dwSocketListenQueue); /// /// 设置心跳包间隔(毫秒,0 则不发送心跳包) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_TcpServer_SetKeepAliveTime(IntPtr pServer, uint dwKeepAliveTime); /// /// 设置心跳确认包检测间隔(毫秒,0 不发送心跳包,如果超过若干次 [默认:WinXP 5 次, Win7 10 次] 检测不到心跳确认包则认为已断线) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_TcpServer_SetKeepAliveInterval(IntPtr pServer, uint dwKeepAliveInterval); /// /// 获取 Accept 预投递数量 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_TcpServer_GetAcceptSocketCount(IntPtr pServer); /// /// 获取通信数据缓冲区大小 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_TcpServer_GetSocketBufferSize(IntPtr pServer); /// /// 获取监听 Socket 的等候队列大小 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_TcpServer_GetSocketListenQueue(IntPtr pServer); /// /// 获取心跳检查次数 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_TcpServer_GetKeepAliveTime(IntPtr pServer); /// /// 获取心跳检查间隔 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_TcpServer_GetKeepAliveInterval(IntPtr pServer); /**********************************************************************************/ /***************************** UDP Server 属性访问方法 *****************************/ /// /// 设置数据报文最大长度(建议在局域网环境下不超过 1472 字节,在广域网环境下不超过 548 字节) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_UdpServer_SetMaxDatagramSize(IntPtr pServer, uint dwMaxDatagramSize); /// /// 获取数据报文最大长度 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_UdpServer_GetMaxDatagramSize(IntPtr pServer); /// /// 设置 Receive 预投递数量(根据负载调整设置,Receive 预投递数量越大则丢包概率越小) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_UdpServer_SetPostReceiveCount(IntPtr pServer, uint dwPostReceiveCount); /// /// 获取 Receive 预投递数量 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_UdpServer_GetPostReceiveCount(IntPtr pServer); /// /// 设置监测包尝试次数(0 则不发送监测跳包,如果超过最大尝试次数则认为已断线) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_UdpServer_SetDetectAttempts(IntPtr pServer, uint dwMaxDatagramSize); /// /// 设置监测包发送间隔(秒,0 不发送监测包) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_UdpServer_SetDetectInterval(IntPtr pServer, uint dwMaxDatagramSize); /// /// 获取心跳检查次数 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_UdpServer_GetDetectAttempts(IntPtr pServer); /// /// 获取心跳检查间隔 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_UdpServer_GetDetectInterval(IntPtr pServer); /******************************************************************************/ /***************************** Client 组件操作方法 *****************************/ /// /// 启动客户端通信组件并连接服务端,启动完成后可开始收发数据 /// /// /// 服务端地址 /// 服务端端口 /// 是否采用异步 Connnect /// 失败,可通过 GetLastError() 获取错误代码 [DllImport(SOCKET_DLL_PATH, CharSet = CharSet.Unicode)] public static extern bool HP_Client_Start(IntPtr pClient, string pszRemoteAddress, ushort usPort, bool bAsyncConnect); /// /// 关闭客户端通信组件,关闭完成后断开与服务端的连接并释放所有资源 /// /// /// 失败,可通过 GetLastError() 获取错误代码 [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Client_Stop(IntPtr pClient); /// /// 用户通过该方法向服务端发送数据 /// /// /// 连接 ID(保留参数,目前该参数并未使用) /// 发送数据缓冲区 /// 发送数据长度 /// 失败,可通过 GetLastError() 获取错误代码 [DllImport(SOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)] public static extern bool HP_Client_Send(IntPtr pClient, byte[] pBuffer, int length); /// /// 用户通过该方法向服务端发送数据 /// /// /// 连接 ID(保留参数,目前该参数并未使用) /// 发送数据缓冲区 /// 发送数据长度 /// 失败,可通过 GetLastError() 获取错误代码 [DllImport(SOCKET_DLL_PATH, SetLastError = true)] public static extern bool HP_Client_Send(IntPtr pClient, IntPtr pBuffer, int length); /// /// 用户通过该方法向服务端发送数据 /// /// /// /// /// 针对pBuffer的偏移 /// [DllImport(SOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)] public static extern bool HP_Client_SendPart(IntPtr pClient, byte[] pBuffer, int length, int iOffset); /// /// 用户通过该方法向服务端发送数据 /// /// /// /// /// 针对pBuffer的偏移 /// [DllImport(SOCKET_DLL_PATH, SetLastError = true)] public static extern bool HP_Client_SendPart(IntPtr pClient, IntPtr pBuffer, int length, int iOffset); /// /// 发送多组数据 /// 向服务端发送多组数据 /// TCP - 顺序发送所有数据包 /// UDP - 把所有数据包组合成一个数据包发送(数据包的总长度不能大于设置的 UDP 包最大长度) /// /// /// 发送缓冲区数组 /// 发送缓冲区数目 /// TRUE.成功,FALSE.失败,可通过 Windows API 函数 ::GetLastError() 获取 Windows 错误代码 [DllImport(SOCKET_DLL_PATH, SetLastError = true)] public static extern bool HP_Client_SendPackets(IntPtr pClient, WSABUF[] pBuffers, int iCount); /******************************************************************************/ /***************************** Client 属性访问方法 *****************************/ /// /// 设置连接的附加数据 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Client_SetExtra(IntPtr pClient, IntPtr pExtra); /// /// 获取连接的附加数据 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern IntPtr HP_Client_GetExtra(IntPtr pClient); /// /// 检查通信组件是否已启动 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Client_HasStarted(IntPtr pClient); /// /// 查看通信组件当前状态 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern ServiceState HP_Client_GetState(IntPtr pClient); /// /// 获取最近一次失败操作的错误代码 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern SocketError HP_Client_GetLastError(IntPtr pClient); /// /// 获取最近一次失败操作的错误描述 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern IntPtr HP_Client_GetLastErrorDesc(IntPtr pClient); /// /// 获取该组件对象的连接 ID /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern IntPtr HP_Client_GetConnectionID(IntPtr pClient); /// /// 获取 Client Socket 的地址信息 /// /// /// /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Client_GetLocalAddress(IntPtr pClient, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpszAddress, ref int piAddressLen, ref ushort pusPort); /// /// 获取连接中未发出数据的长度 /// /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Client_GetPendingDataLength(IntPtr pClient, ref int piPending); /// /// 设置内存块缓存池大小(通常设置为 -> PUSH 模型:5 - 10;PULL 模型:10 - 20 ) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Client_SetFreeBufferPoolSize(IntPtr pClient, uint dwFreeBufferPoolSize); /// /// 设置内存块缓存池回收阀值(通常设置为内存块缓存池大小的 3 倍) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Client_SetFreeBufferPoolHold(IntPtr pClient, uint dwFreeBufferPoolHold); /// /// 获取内存块缓存池大小 /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_Client_GetFreeBufferPoolSize(IntPtr pClient); /// /// 获取内存块缓存池回收阀值 /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_Client_GetFreeBufferPoolHold(IntPtr pClient); /**********************************************************************************/ /***************************** TCP Client 操作方法 *****************************/ /// /// 名称:发送小文件 /// 描述:向指定连接发送 4096 KB 以下的小文件 /// /// /// 文件路径 /// 头部附加数据 /// 尾部附加数据 /// TRUE.成功 FALSE -- 失败,可通过 Windows API 函数 ::GetLastError() 获取 Windows 错误代码 [DllImport(SOCKET_DLL_PATH, CharSet = CharSet.Unicode, SetLastError = true)] public static extern bool HP_TcpClient_SendSmallFile(IntPtr pClient, string lpszFileName, ref WSABUF pHead, ref WSABUF pTail); /**********************************************************************************/ /***************************** TCP Client 属性访问方法 *****************************/ /// /// 设置通信数据缓冲区大小(根据平均通信数据包大小调整设置,通常设置为:(N * 1024) - sizeof(TBufferObj)) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_TcpClient_SetSocketBufferSize(IntPtr pClient, uint dwSocketBufferSize); /// /// 设置心跳包间隔(毫秒,0 则不发送心跳包) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_TcpClient_SetKeepAliveTime(IntPtr pClient, uint dwKeepAliveTime); /// /// 设置心跳确认包检测间隔(毫秒,0 不发送心跳包,如果超过若干次 [默认:WinXP 5 次, Win7 10 次] 检测不到心跳确认包则认为已断线) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_TcpClient_SetKeepAliveInterval(IntPtr pClient, uint dwKeepAliveInterval); /// /// 获取通信数据缓冲区大小 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_TcpClient_GetSocketBufferSize(IntPtr pClient); /// /// 获取心跳检查次数 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_TcpClient_GetKeepAliveTime(IntPtr pClient); /// /// 获取心跳检查间隔 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_TcpClient_GetKeepAliveInterval(IntPtr pClient); /**********************************************************************************/ /***************************** UDP Client 属性访问方法 *****************************/ /// /// 设置数据报文最大长度(建议在局域网环境下不超过 1472 字节,在广域网环境下不超过 548 字节) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_UdpClient_SetMaxDatagramSize(IntPtr pClient, uint dwMaxDatagramSize); /// /// 获取数据报文最大长度 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_UdpClient_GetMaxDatagramSize(IntPtr pClient); /// /// 设置监测包尝试次数(0 则不发送监测跳包,如果超过最大尝试次数则认为已断线 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_UdpClient_SetDetectAttempts(IntPtr pClient, uint dwDetectAttempts); /// /// 设置监测包发送间隔(秒,0 不发送监测包) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_UdpClient_SetDetectInterval(IntPtr pClient, uint dwDetectInterval); /// /// 获取心跳检查次数 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_UdpClient_GetDetectAttempts(IntPtr pClient); /// /// 获取心跳检查间隔 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_UdpClient_GetDetectInterval(IntPtr pClient); /**************************************************************************/ /***************************** Agent 操作方法 *****************************/ /// /// 启动通信组件 /// 启动通信代理组件,启动完成后可开始连接远程服务器 /// /// /// 监听地址 /// 是否采用异步 Connect /// 失败,可通过 GetLastError() 获取错误代码 [DllImport(SOCKET_DLL_PATH, CharSet = CharSet.Unicode)] public static extern bool HP_Agent_Start(IntPtr pAgent, String pszBindAddress, bool bAsyncConnect); /// /// 关闭通信组件 /// 关闭通信组件,关闭完成后断开所有连接并释放所有资源 /// /// /// -- 失败,可通过 GetLastError() 获取错误代码 [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Agent_Stop(IntPtr pAgent); /// /// 连接服务器 /// 连接服务器,连接成功后 IAgentListener 会接收到 OnConnect() 事件 /// /// /// 服务端地址 /// 服务端端口 /// 传出连接 ID /// 失败,可通过 SYS_GetLastError() 获取 Windows 错误代码 [DllImport(SOCKET_DLL_PATH, CharSet = CharSet.Unicode, SetLastError = true)] public static extern bool HP_Agent_Connect(IntPtr pAgent, String pszBindAddress, ushort usPort, ref IntPtr pconnId); /// /// 发送数据 /// 用户通过该方法向指定连接发送数据 /// /// /// 连接 ID /// 发送数据缓冲区 /// 发送数据长度 /// [DllImport(SOCKET_DLL_PATH, CharSet = CharSet.Ansi, SetLastError = true)] public static extern bool HP_Agent_Send(IntPtr pAgent, IntPtr connId, byte[] pBuffer, int length); /// /// 发送数据 /// 用户通过该方法向指定连接发送数据 /// /// /// 连接 ID /// 发送数据缓冲区 /// 发送数据长度 /// [DllImport(SOCKET_DLL_PATH, SetLastError = true)] public static extern bool HP_Agent_Send(IntPtr pAgent, IntPtr connId, IntPtr pBuffer, int length); /// /// 发送数据 /// 用户通过该方法向指定连接发送数据 /// /// /// /// /// /// 针对pBuffer的偏移 /// [DllImport(SOCKET_DLL_PATH, SetLastError = true)] public static extern bool HP_Agent_SendPart(IntPtr pAgent, IntPtr connId, byte[] pBuffer, int length, int iOffset); /// /// 发送数据 /// 用户通过该方法向指定连接发送数据 /// /// /// /// /// /// 针对pBuffer的偏移 /// [DllImport(SOCKET_DLL_PATH, SetLastError = true)] public static extern bool HP_Agent_SendPart(IntPtr pAgent, IntPtr connId, IntPtr pBuffer, int length, int iOffset); /// /// 发送多组数据 /// 向指定连接发送多组数据 /// TCP - 顺序发送所有数据包 /// UDP - 把所有数据包组合成一个数据包发送(数据包的总长度不能大于设置的 UDP 包最大长度) /// /// /// 连接 ID /// 发送缓冲区数组 /// 发送缓冲区数目 /// TRUE.成功,FALSE .失败,可通过 Windows API 函数 ::GetLastError() 获取 Windows 错误代码 [DllImport(SOCKET_DLL_PATH, SetLastError = true)] public static extern bool HP_Agent_SendPackets(IntPtr pAgent, IntPtr connId, WSABUF[] pBuffers, int iCount); /// /// 断开某个连接 /// /// /// 连接 ID /// 是否强制断开连接 /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Agent_Disconnect(IntPtr pAgent, IntPtr connId, bool bForce); /// /// 断开超过指定时长的连接 /// /// /// 时长(毫秒) /// 是否强制断开连接 /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Agent_DisconnectLongConnections(IntPtr pAgent, uint dwPeriod, bool bForce); /// /// 断开超过指定时长的静默连接 /// /// /// 时长(毫秒) /// 是否强制断开连接 /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Agent_DisconnectSilenceConnections(IntPtr pAgent, uint dwPeriod, bool bForce); /******************************************************************************/ /***************************** Agent 操作方法 *****************************/ /// /// 名称:发送小文件 /// 描述:向指定连接发送 4096 KB 以下的小文件 /// /// /// 连接 ID /// 文件路径 /// 头部附加数据 /// 尾部附加数据 /// TRUE.成功 FALSE -- 失败,可通过 Windows API 函数 ::GetLastError() 获取 Windows 错误代码 [DllImport(SOCKET_DLL_PATH, CharSet = CharSet.Unicode, SetLastError = true)] public static extern bool HP_TcpAgent_SendSmallFile(IntPtr pAgent, IntPtr connId, string lpszFileName, ref WSABUF pHead, ref WSABUF pTail); /******************************************************************************/ /***************************** Agent 属性访问方法 *****************************/ /// /// 设置数据发送策略 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Agent_SetSendPolicy(IntPtr pAgent, SendPolicy enSendPolicy); /// /// 获取数据发送策略 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern SendPolicy HP_Agent_GetSendPolicy(IntPtr pAgent); /// /// 获取数据接收策略 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern RecvPolicy HP_Agent_GetRecvPolicy(IntPtr pAgent); /// /// 设置数据接收策略 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Agent_SetRecvPolicy(IntPtr pAgent, RecvPolicy enRecvPolicy); /// /// 设置连接的附加数据 /// 是否为连接绑定附加数据或者绑定什么样的数据,均由应用程序只身决定 /// /// /// 连接 ID /// 数据 /// FALSE -- 失败(无效的连接 ID) [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Agent_SetConnectionExtra(IntPtr pAgent, IntPtr connId, IntPtr pExtra); /// /// 获取连接的附加数据 /// 是否为连接绑定附加数据或者绑定什么样的数据,均由应用程序只身决定 /// /// /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Agent_GetConnectionExtra(IntPtr pAgent, IntPtr connId, ref IntPtr pExtra); /// /// 检查通信组件是否已启动 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Agent_HasStarted(IntPtr pAgent); /// /// 查看通信组件当前状态 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern ServiceState HP_Agent_GetState(IntPtr pAgent); /// /// 获取连接数 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_Agent_GetConnectionCount(IntPtr pAgent); /// /// 获取所有连接的 CONNID /// /// /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Agent_GetAllConnectionIDs(IntPtr pAgent, IntPtr[] pIDs, ref uint pdwCount); /// /// 获取某个连接时长(毫秒) /// /// /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Agent_GetConnectPeriod(IntPtr pAgent, IntPtr connId, ref uint pdwPeriod); /// /// 获取某个连接静默时间(毫秒) /// /// /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Agent_GetSilencePeriod(IntPtr pAgent, IntPtr connId, ref uint pdwPeriod); /// /// 获取监听 Socket 的地址信息 /// /// /// /// /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Agent_GetLocalAddress(IntPtr pAgent, IntPtr connId, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpszAddress, ref int piAddressLen, ref ushort pusPort); /// /// 获取某个连接的地址信息 /// /// /// /// /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Agent_GetRemoteAddress(IntPtr pAgent, IntPtr connId, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpszAddress, ref int piAddressLen, ref ushort pusPort); /// /// 获取最近一次失败操作的错误代码 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern SocketError HP_Agent_GetLastError(IntPtr pAgent); /// /// 获取最近一次失败操作的错误描述 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern IntPtr HP_Agent_GetLastErrorDesc(IntPtr pAgent); /// /// 获取连接中未发出数据的长度 /// /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Agent_GetPendingDataLength(IntPtr pAgent, IntPtr connId, ref int piPending); /// /// 设置 Socket 缓存对象锁定时间(毫秒,在锁定期间该 Socket 缓存对象不能被获取使用) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Agent_SetFreeSocketObjLockTime(IntPtr pAgent, uint dwFreeSocketObjLockTime); /// /// 设置 Socket 缓存池大小(通常设置为平均并发连接数量的 1/3 - 1/2) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Agent_SetFreeSocketObjPool(IntPtr pAgent, uint dwFreeSocketObjPool); /// /// 设置内存块缓存池大小(通常设置为 Socket 缓存池大小的 2 - 3 倍) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Agent_SetFreeBufferObjPool(IntPtr pAgent, uint dwFreeBufferObjPool); /// /// 设置 Socket 缓存池回收阀值(通常设置为 Socket 缓存池大小的 3 倍) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Agent_SetFreeSocketObjHold(IntPtr pAgent, uint dwFreeSocketObjHold); /// /// 设置内存块缓存池回收阀值(通常设置为内存块缓存池大小的 3 倍) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Agent_SetFreeBufferObjHold(IntPtr pAgent, uint dwFreeBufferObjHold); /// /// 设置工作线程数量(通常设置为 2 * CPU + 2) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Agent_SetWorkerThreadCount(IntPtr pAgent, uint dwWorkerThreadCount); /// /// 设置关闭组件前等待连接关闭的最长时限(毫秒,0 则不等待) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Agent_SetMaxShutdownWaitTime(IntPtr pAgent, uint dwMaxShutdownWaitTime); /// /// 设置是否标记静默时间(设置为 TRUE 时 DisconnectSilenceConnections() 和 GetSilencePeriod() 才有效,默认:FALSE) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_Agent_SetMarkSilence(IntPtr pAgent, bool bMarkSilence); /// /// 获取 Socket 缓存对象锁定时间 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_Agent_GetFreeSocketObjLockTime(IntPtr pAgent); /// /// 获取 Socket 缓存池大小 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_Agent_GetFreeSocketObjPool(IntPtr pAgent); /// /// 获取内存块缓存池大小 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_Agent_GetFreeBufferObjPool(IntPtr pAgent); /// /// 获取 Socket 缓存池回收阀值 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_Agent_GetFreeSocketObjHold(IntPtr pAgent); /// /// 获取内存块缓存池回收阀值 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_Agent_GetFreeBufferObjHold(IntPtr pAgent); /// /// 获取工作线程数量 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_Agent_GetWorkerThreadCount(IntPtr pAgent); /// /// 获取关闭组件前等待连接关闭的最长时限 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_Agent_GetMaxShutdownWaitTime(IntPtr pAgent); /// /// 检测是否标记静默时间 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_Agent_IsMarkSilence(IntPtr pAgent); /**********************************************************************************/ /***************************** TCP Agent 属性访问方法 *****************************/ /// /// 置是否启用地址重用机制(默认:不启用) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_TcpAgent_SetReuseAddress(IntPtr pAgent, bool bReuseAddress); /// /// 检测是否启用地址重用机制 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern bool HP_TcpAgent_IsReuseAddress(IntPtr pAgent); /// /// 设置通信数据缓冲区大小(根据平均通信数据包大小调整设置,通常设置为 1024 的倍数) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_TcpAgent_SetSocketBufferSize(IntPtr pAgent, uint dwSocketBufferSize); /// /// 设置心跳包间隔(毫秒,0 则不发送心跳包) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_TcpAgent_SetKeepAliveTime(IntPtr pAgent, uint dwKeepAliveTime); /// /// 设置心跳确认包检测间隔(毫秒,0 不发送心跳包,如果超过若干次 [默认:WinXP 5 次, Win7 10 次] 检测不到心跳确认包则认为已断线) /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern void HP_TcpAgent_SetKeepAliveInterval(IntPtr pAgent, uint dwKeepAliveInterval); /// /// 获取通信数据缓冲区大小 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_TcpAgent_GetSocketBufferSize(IntPtr pAgent); /// /// 获取心跳检查次数 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_TcpAgent_GetKeepAliveTime(IntPtr pAgent); /// /// 获取心跳检查间隔 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern uint HP_TcpAgent_GetKeepAliveInterval(IntPtr pAgent); /***************************************************************************************/ /***************************** TCP Pull Server 组件操作方法 *****************************/ /// /// 抓取数据 /// 用户通过该方法从 Socket 组件中抓取数据 /// /// /// 连接 ID /// 数据抓取缓冲区 /// 抓取数据长度 /// [DllImport(SOCKET_DLL_PATH)] public static extern FetchResult HP_TcpPullServer_Fetch(IntPtr pServer, IntPtr connId, IntPtr pBuffer, int length); /// /// 窥探数据(不会移除缓冲区数据) /// 描述:用户通过该方法从 Socket 组件中窥探数据 /// /// /// 连接 ID /// 窥探缓冲区 /// 窥探数据长度 /// [DllImport(SOCKET_DLL_PATH)] public static extern FetchResult HP_TcpPullServer_Peek(IntPtr pServer, IntPtr connId, IntPtr pBuffer, int length); /***************************************************************************************/ /***************************** TCP Pull Server 属性访问方法 *****************************/ /***************************************************************************************/ /***************************** TCP Pull Client 组件操作方法 *****************************/ /// /// 抓取数据 /// 用户通过该方法从 Socket 组件中抓取数据 /// /// /// 连接 ID /// 数据抓取缓冲区 /// 抓取数据长度 /// [DllImport(SOCKET_DLL_PATH)] public static extern FetchResult HP_TcpPullClient_Fetch(IntPtr pClient, IntPtr pBuffer, int length); /// /// 名称:窥探数据(不会移除缓冲区数据) /// 描述:用户通过该方法从 Socket 组件中窥探数据 /// /// /// 连接 ID /// 数据抓取缓冲区 /// 抓取数据长度 /// [DllImport(SOCKET_DLL_PATH)] public static extern FetchResult HP_TcpPullClient_Peek(IntPtr pClient, IntPtr pBuffer, int length); /***************************************************************************************/ /***************************** TCP Pull Client 属性访问方法 *****************************/ /***************************************************************************************/ /***************************** TCP Pull Agent 组件操作方法 *****************************/ /// /// 抓取数据 /// 用户通过该方法从 Socket 组件中抓取数据 /// /// /// 连接 ID /// 数据抓取缓冲区 /// 抓取数据长度 /// [DllImport(SOCKET_DLL_PATH)] public static extern FetchResult HP_TcpPullAgent_Fetch(IntPtr pAgent, IntPtr connId, IntPtr pBuffer, int length); /// /// 名称:窥探数据(不会移除缓冲区数据) /// 描述:用户通过该方法从 Socket 组件中窥探数据 /// /// /// 连接 ID /// 数据抓取缓冲区 /// 抓取数据长度 /// [DllImport(SOCKET_DLL_PATH)] public static extern FetchResult HP_TcpPullAgent_Peek(IntPtr pAgent, IntPtr connId, IntPtr pBuffer, int length); /***************************************************************************************/ /***************************** TCP Pull Agent 属性访问方法 *****************************/ /***************************************************************************************/ /*************************************** 其它方法 ***************************************/ /// /// 获取错误描述文本 /// /// /// [DllImport(SOCKET_DLL_PATH)] public static extern IntPtr HP_GetSocketErrorDesc(SocketError enCode); /// /// 调用系统的 ::GetLastError() 方法获取系统错误代码 /// /// public static int SYS_GetLastError() { return Marshal.GetLastWin32Error(); } /// /// 调用系统的 ::WSAGetLastError() 方法获取通信错误代码 /// /// [DllImport(SOCKET_DLL_PATH, SetLastError = true)] public static extern int SYS_WSAGetLastError(); /// /// 调用系统的 setsockopt() /// /// /// /// /// /// /// [DllImport(SOCKET_DLL_PATH, SetLastError = true)] public static extern int SYS_SetSocketOption(IntPtr sock, int level, int name, IntPtr val, int len); /// /// 调用系统的 getsockopt() /// /// /// /// /// /// /// [DllImport(SOCKET_DLL_PATH, SetLastError = true)] public static extern int SYS_GetSocketOption(IntPtr sock, int level, int name, IntPtr val, ref int len); /// /// 调用系统的 ioctlsocket() /// /// /// /// /// [DllImport(SOCKET_DLL_PATH, SetLastError = true)] public static extern int SYS_IoctlSocket(IntPtr sock, long cmd, IntPtr arg); /// /// 调用系统的 ::WSAIoctl() /// /// /// /// /// /// /// /// /// [DllImport(SOCKET_DLL_PATH, SetLastError = true)] public static extern int SYS_WSAIoctl(IntPtr sock, uint dwIoControlCode, IntPtr lpvInBuffer, uint cbInBuffer, IntPtr lpvOutBuffer, uint cbOutBuffer, uint lpcbBytesReturned); } }