组件

NetLinkMain.h 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*************************************************************************
  2. 【文件名】 NetLinkMain.h
  3. 【功能模块和目的】 网络连接主管理类头文件
  4. 【开发者及日期】 郑石诺 2015/07/19
  5. 【版本】 V1.0.0
  6. 【版权信息】 Copyright (C)2015 河南华谊网络科技有限公司
  7. 【更改记录】
  8. *************************************************************************/
  9. #pragma once
  10. #include "NetCtrlInterface.h"
  11. #include "NetRepInterface.h"
  12. #include "PduEventHost.h"
  13. #include "ClientContainer.h"
  14. class CNetClient;
  15. class CNetServer;
  16. class CSocketBase;
  17. /*************************************************************************
  18. 【类名】 CNetLinkMain
  19. 【功能】
  20. 【接口说明】 网络连接主管理类
  21. 【开发者及日期】 郑石诺 2015/07/19
  22. 【版本】 V1.0.0
  23. 【版权信息】 Copyright (C)2015 河南华谊网络科技有限公司
  24. 【更改记录】
  25. *************************************************************************/
  26. class CNetLinkMain : public IPduComm, public IPduLink
  27. {
  28. SINGLETON_DECLARE(CNetLinkMain)
  29. public:
  30. CNetLinkMain(void);
  31. virtual ~CNetLinkMain(void);
  32. BOOL Send(CPduEntity* a_pCmd, PDU_DEV_TYPE a_nDestType, int a_nDestId); // 发送PDU
  33. void Send2All(CPduEntity* a_pCmd);
  34. // 连接管理
  35. void getLocalInfo(PDU_DEV_TYPE& a_DevType, int& a_DevId); // 获取本端设备类型及ID
  36. BOOL GetLinkInfo(CString& a_strIp, int& a_nPort, PDU_DEV_TYPE a_nType, int a_nId); // 获取指定连接的网络信息
  37. BOOL GetPeerIp(CString& a_strIp, CPduEntity* a_pCmd); // 获取PDU命令的发送方IP
  38. BOOL CreatePduServer(int a_nListenPort, PDU_DEV_TYPE a_nLocalType, int a_nLocalId); // 创建PDU通讯服务器
  39. BOOL CreatePduClient(CString a_strFarIp, int a_nFarPort, PDU_DEV_TYPE a_nLocalType, int a_nLocalId,
  40. PDU_DEV_TYPE a_nFarType, int a_nFarId, bool IsAutoReconnect); // 创建PDU通讯客户端
  41. BOOL ClosePduClient(PDU_DEV_TYPE a_nFarType, int a_nFarId); // 关闭PDU通讯客户端
  42. void StopAll(void);
  43. // 事件订阅接口实现(注:事件订阅应在创建操作之前调用)
  44. BOOL RegistPduLinkProc(IPduLinkProc* a_pPduLinkProc, BOOL bIsInsert);
  45. BOOL RegistPduCommProc(IPduCommProc* a_pPduCommProc, BOOL bIsInsert);
  46. CNetClient* getClient(CNetClient* a_pClient) { return m_ClientContainer.getClient(a_pClient); }
  47. // 绑定通信一端
  48. bool waitNetEnd(CSocketBase* pNetEnd);
  49. // 客户端逻辑控制相关操作
  50. void onConnEstablished(CNetClient* a_pClient); // 连接建立处理
  51. void onConnFailed(CNetClient* a_pClient); // 连接失败/断开处理
  52. void onConnRegistOK(CNetClient* a_pClient); // 注册成功
  53. void onConnRegistFailed(CNetClient* a_pClient); // 注册失败
  54. void onRecvCommand(CPduEntity* a_pCmd); // 命令接收处理
  55. private:
  56. static DWORD WINAPI __workerThread(LPVOID lpParam);
  57. int __getCountOfProcessors(void);
  58. int __getThreadCount(PDU_DEV_TYPE a_LocalType);
  59. bool __setupIocp(PDU_DEV_TYPE a_LocalType);
  60. bool __handleNetEndError(CSocketBase* pNetEnd, DWORD ErrorCode);
  61. void __setLocalInfo(PDU_DEV_TYPE a_nDevType, int a_nDevId); // 设定PDU本端设备类型及ID
  62. void __setLinkContent(PduLinkContent& linkContent, PDU_LINK_STATE a_nLinkInfo, CNetClient* a_pClient); // 生成连接信息内容
  63. void __setLinkContent(PduLinkContent& linkContent, CNetClient* a_pClient); // 生成连接信息内容
  64. bool __isClientExisted(CSocketBase* pClient); // 判断客户端是否还存在
  65. private:
  66. CPduEventHost m_EventHost; // 事件订阅宿主类
  67. CClientContainer m_ClientContainer; // 客户端容器
  68. CNetServer* m_pServer;
  69. HANDLE m_hIocp; // 完成端口的句柄
  70. HANDLE* m_phWorkerThreads; // 工作者线程的句柄指针
  71. int m_ThreadCount;
  72. bool m_ThreadStop;
  73. // 属性
  74. PDU_DEV_TYPE m_DevType; // 本端设备类型
  75. int m_DevId; // 本端设备ID
  76. };