升龙物业 老版本 ocx IPO, 加密狗 转值班电话

PduMain.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. #include "stdafx.h"
  2. #include "PduMain.h"
  3. #include "PduSocketVector.h"
  4. #include "PduSocket.h"
  5. #include "PduSocketServer.h"
  6. #include "PduEventHost.h"
  7. #include "PduEntity.h"
  8. CPduMain* CPduMain::m_pInstance = NULL;
  9. CPduMain::CPduMain(void)
  10. {
  11. if (!AfxSocketInit())
  12. AfxMessageBox("AfxSocketInit执行失败");
  13. m_pSocketServer = NULL;
  14. m_pSocketVector = new CPduSocketVector();
  15. m_pEventHost = new CPduEventHost();
  16. m_nDevType = PDU_DEV_TYPE_UNKNOWN;
  17. m_nDevId = 0;
  18. }
  19. CPduMain::~CPduMain(void)
  20. {
  21. StopAll();
  22. }
  23. /*****************************************************************
  24. **【函数名称】 getCommInstance
  25. **【函数功能】 得到消息通讯接口实例
  26. **【参数】
  27. **【返回值】
  28. ****************************************************************/
  29. IPduComm* CInterfaceWindow::getCommInstance()
  30. {
  31. return CPduMain::getInstance();
  32. }
  33. /*****************************************************************
  34. **【函数名称】 getLinkInstance
  35. **【函数功能】 得到消息通讯接口实例
  36. **【参数】
  37. **【返回值】
  38. ****************************************************************/
  39. IPduLink* CInterfaceWindow::getLinkInstance()
  40. {
  41. return CPduMain::getInstance();
  42. }
  43. /*****************************************************************
  44. **【函数名称】 getInstance
  45. **【函数功能】 得到接口指针
  46. **【参数】
  47. **【返回值】
  48. ****************************************************************/
  49. CPduMain* CPduMain::getInstance()
  50. {
  51. if(NULL == m_pInstance)
  52. {
  53. m_pInstance = new CPduMain();
  54. } // end if
  55. return m_pInstance;
  56. }
  57. /*****************************************************************
  58. **【函数名称】 SetLocalInfo
  59. **【函数功能】 设定PDU本端设备类型及ID
  60. **【参数】 a_nDevType 本端设备类型
  61. a_nDevId 本端设备ID
  62. **【返回值】
  63. ****************************************************************/
  64. void CPduMain::SetLocalInfo(PDU_DEV_TYPE a_nDevType, int a_nDevId)
  65. {
  66. m_nDevType = a_nDevType;
  67. m_nDevId = a_nDevId;
  68. }
  69. /*****************************************************************
  70. **【函数名称】 GetLocalInfo
  71. **【函数功能】 读取PDU本端设备类型及ID
  72. **【参数】
  73. **【返回值】 a_nDevType 本端设备类型
  74. a_nDevId 本端设备ID
  75. ****************************************************************/
  76. void CPduMain::GetLocalInfo(PDU_DEV_TYPE& a_nDevType, int& a_nDevId)
  77. {
  78. a_nDevType = m_nDevType;
  79. a_nDevId = m_nDevId;
  80. }
  81. /*****************************************************************
  82. **【函数名称】 SetLinkContent
  83. **【函数功能】 生成连接信息内容
  84. **【参数】 a_nLinkInfo SOCKET连接状态
  85. a_npSocket 关联的SOCKET实例
  86. **【返回值】 linkContent
  87. ****************************************************************/
  88. void CPduMain::SetLinkContent(PduLinkContent& linkContent, PDU_LINK_STATE a_nLinkInfo, CPduSocket* a_pSocket)
  89. {
  90. memset(&linkContent, 0, sizeof(PduLinkContent));
  91. CString strIp = _T("");
  92. linkContent.nLocalType = m_nDevType;
  93. linkContent.nLocalId = m_nDevId;
  94. linkContent.nLinkState = a_nLinkInfo;
  95. a_pSocket->GetPeerLinkInfo(strIp, linkContent.nFarPort);
  96. a_pSocket->GetPeerDevInfo(linkContent.nFarType, linkContent.nFarId);
  97. lstrcpy(linkContent.szFarIp, strIp);
  98. }
  99. /*****************************************************************
  100. **【函数名称】 CreatePduServer
  101. **【函数功能】 创建PDU通讯服务器
  102. **【参数】 a_nListenPort SOCKET监听端口
  103. a_nLocalType 服务器设备类型
  104. a_nLocalId 服务器设备ID
  105. **【返回值】
  106. ****************************************************************/
  107. BOOL CPduMain::CreatePduServer(int a_nListenPort, PDU_DEV_TYPE a_nLocalType, int a_nLocalId)
  108. {
  109. m_pSocketServer = new CPduSocketServer(this);
  110. // 启动SOCKET SERVER
  111. if(m_pSocketServer->Init(a_nListenPort))
  112. {
  113. SetLocalInfo(a_nLocalType, a_nLocalId);
  114. return TRUE;
  115. }
  116. else
  117. {
  118. delete m_pSocketServer;
  119. m_pSocketServer = NULL;
  120. } // end if
  121. return FALSE;
  122. }
  123. /*****************************************************************
  124. **【函数名称】 CreatePduClient
  125. **【函数功能】 创建PDU通讯客户端
  126. **【参数】 a_strFarIp 要连接的服务器IP
  127. a_nFarPort 要连接的服务器端口号
  128. a_nLocalType 本端设备类型
  129. a_nLocalId 本端设备ID
  130. a_nFarType 对端设备类型
  131. a_nFarId 对端设备ID
  132. **【返回值】 TRUE 启动连接成功
  133. FALSE 启动连接失败
  134. ****************************************************************/
  135. BOOL CPduMain::CreatePduClient(CString a_strFarIp, int a_nFarPort, PDU_DEV_TYPE a_nLocalType, int a_nLocalId, PDU_DEV_TYPE a_nFarType, int a_nFarId, bool IsAutoReconnect)
  136. {
  137. // 如果已存在到对端的连接
  138. CPduSocket* pSocket = m_pSocketVector->GetPduSocket(a_nFarType, a_nFarId);
  139. if(pSocket != NULL) return FALSE;
  140. // 开始连接
  141. pSocket = new CPduSocket(this, PDU_LINK_TYPE_CLIENT, IsAutoReconnect);
  142. if(pSocket->ConnectToServer(a_strFarIp, a_nFarPort, a_nFarType, a_nFarId))
  143. {
  144. m_pSocketVector->InsertSocket(pSocket);
  145. SetLocalInfo(a_nLocalType, a_nLocalId);
  146. return TRUE;
  147. }
  148. else
  149. {
  150. delete pSocket;
  151. } // end if
  152. return FALSE;
  153. }
  154. /*****************************************************************
  155. **【函数名称】 ClosePduClient
  156. **【函数功能】 关闭PDU通讯客户端
  157. **【参数】 a_nFarType 对端设备类型
  158. a_nFarId 对端设备ID
  159. **【返回值】 TRUE 启动连接成功
  160. FALSE 启动连接失败
  161. ****************************************************************/
  162. BOOL CPduMain::ClosePduClient(PDU_DEV_TYPE a_nFarType, int a_nFarId)
  163. {
  164. return m_pSocketVector->RemoveSocket(a_nFarType, a_nFarId);
  165. }
  166. /*****************************************************************
  167. **【函数名称】 Send
  168. **【函数功能】 发送PDU命令
  169. **【参数】 a_pCmd 要发送的命令实体
  170. a_nDestType 消息要发送的目标设备类型
  171. a_nDestId 消息要发送的目标设备ID
  172. **【返回值】
  173. ****************************************************************/
  174. BOOL CPduMain::Send(CPduEntity* a_pCmd, PDU_DEV_TYPE a_nDestType, int a_nDestId)
  175. {
  176. CPduSocket* pSocket = NULL;
  177. // 对注册命令的特殊处理
  178. if(a_pCmd->GetCmdType() == PDU_CMD_REG && a_pCmd->GetIsExecReturn())
  179. {
  180. // 查找要发送命令的连接
  181. pSocket = m_pSocketVector->GetPduSocket(a_pCmd->GetAssoSocket());
  182. if(pSocket == NULL) return FALSE;
  183. // 触发注册事件
  184. if(a_pCmd->GetDataBool(0))
  185. {
  186. pSocket->SetPeerDevInfo(a_nDestType, a_nDestId);
  187. OnConnRegistOK(pSocket);
  188. }
  189. else
  190. {
  191. OnConnRegistFailed(pSocket);
  192. } // end if
  193. }
  194. else
  195. {
  196. // 查找要发送命令的连接
  197. pSocket = m_pSocketVector->GetPduSocket(a_nDestType, a_nDestId);
  198. if(pSocket == NULL) return FALSE;
  199. } // end if
  200. // 发送命令
  201. a_pCmd->SetLocalDevInfo(m_nDevType, m_nDevId); // 填充发送方信息
  202. a_pCmd->SetPeerDevInfo(a_nDestType, a_nDestId); // 填充接收方信息
  203. return pSocket->SendDirectly(a_pCmd);
  204. }
  205. /*****************************************************************
  206. **【函数名称】 Send2All
  207. **【函数功能】 向所有注册过的客户端发送PDU
  208. **【参数】
  209. **【返回值】
  210. ****************************************************************/
  211. void CPduMain::Send2All( CPduEntity* a_pCmd )
  212. {
  213. a_pCmd->SetLocalDevInfo(m_nDevType, m_nDevId); // 填充发送方信息
  214. m_pSocketVector->Send2All(a_pCmd);
  215. }
  216. /*****************************************************************
  217. **【函数名称】 GetLinkInfo
  218. **【函数功能】 获取指定连接的网络信息
  219. **【参数】 OUT a_strIp 指定连接的对端IP
  220. OUT a_nPort 指定连接的对端PORT
  221. IN a_nType 要查询的连接类型
  222. IN a_nId 要查询的连接ID
  223. **【返回值】
  224. ****************************************************************/
  225. BOOL CPduMain::GetLinkInfo(CString& a_strIp, int& a_nPort, PDU_DEV_TYPE a_nType, int a_nId)
  226. {
  227. // 查找连接
  228. CPduSocket* pSocket = m_pSocketVector->GetPduSocket(a_nType, a_nId);
  229. if(pSocket == NULL) return FALSE;
  230. // 获取连接的网络信息
  231. pSocket->GetPeerLinkInfo(a_strIp, a_nPort);
  232. return TRUE;
  233. }
  234. /*****************************************************************
  235. **【函数名称】 GetPeerIp
  236. **【函数功能】 获取PDU命令的发送方IP
  237. **【参数】 OUT a_strIp 发送当前PDU命令的对端IP
  238. IN a_pCmd 当前接收到的PDU命令
  239. **【返回值】
  240. ****************************************************************/
  241. BOOL CPduMain::GetPeerIp(CString& a_strIp, CPduEntity* a_pCmd)
  242. {
  243. CPduSocket* pSocket = m_pSocketVector->GetPduSocket(a_pCmd->GetAssoSocket());
  244. if(pSocket == NULL) return FALSE;
  245. // 获取连接的网络信息
  246. int nPort = 0;
  247. pSocket->GetPeerLinkInfo(a_strIp, nPort);
  248. return TRUE;
  249. }
  250. /*****************************************************************
  251. **【函数名称】 RegistPduLinkProc
  252. **【函数功能】 订阅连接管理事件
  253. **【参数】 a_pPduLinkProc 要对事件进行处理的接口类
  254. bIsInsert 添加/删除定阅
  255. **【返回值】
  256. ****************************************************************/
  257. BOOL CPduMain::RegistPduLinkProc(IPduLinkProc* a_pPduLinkProc, BOOL bIsInsert)
  258. {
  259. return m_pEventHost->RegLinkProc(a_pPduLinkProc, bIsInsert);
  260. }
  261. /*****************************************************************
  262. **【函数名称】 RegistPduCommProc
  263. **【函数功能】 订阅消息通知事件
  264. **【参数】 a_pPduCommProc 要对事件进行处理的接口类
  265. bIsInsert 添加/删除定阅
  266. **【返回值】
  267. ****************************************************************/
  268. BOOL CPduMain::RegistPduCommProc(IPduCommProc* a_pPduCommProc, BOOL bIsInsert)
  269. {
  270. return m_pEventHost->RegCommProc(a_pPduCommProc, bIsInsert);
  271. }
  272. /*****************************************************************
  273. **【函数名称】 OnConnEstablished
  274. **【函数功能】 初始连接建立的后续处理
  275. **【参数】
  276. **【返回值】
  277. ****************************************************************/
  278. void CPduMain::OnConnEstablished(CPduSocket* a_pSocket)
  279. {
  280. // 对SOCKET容器的处理
  281. PDU_LINK_STATE nLinkState = m_pSocketVector->OnConnEstablished(a_pSocket);
  282. // 对事件观查者的处理
  283. PduLinkContent linkContent;
  284. SetLinkContent(linkContent, nLinkState, a_pSocket);
  285. m_pEventHost->OnLinkStateChanged(linkContent);
  286. // 对SOCKET监控的处理
  287. a_pSocket->OnConnEstablished();
  288. }
  289. /*****************************************************************
  290. **【函数名称】 OnConnFailed
  291. **【函数功能】 初连接断开/失败的后续处理
  292. **【参数】
  293. **【返回值】
  294. ****************************************************************/
  295. void CPduMain::OnConnFailed(CPduSocket* a_pSocket)
  296. {
  297. // 对SOCKET容器的处理
  298. PDU_LINK_STATE nLinkState = m_pSocketVector->OnConnFailed(a_pSocket);
  299. // 对SOCKET监控的处理
  300. a_pSocket->OnConnFailed();
  301. // 对事件观查者的处理
  302. PduLinkContent linkContent;
  303. SetLinkContent(linkContent, nLinkState, a_pSocket);
  304. m_pEventHost->OnLinkStateChanged(linkContent);
  305. }
  306. /*****************************************************************
  307. **【函数名称】 OnConnRegistOK
  308. **【函数功能】 连接注册成功的后续处理
  309. **【参数】
  310. **【返回值】
  311. ****************************************************************/
  312. void CPduMain::OnConnRegistOK(CPduSocket* a_pSocket)
  313. {
  314. // 对SOCKET容器的处理
  315. PDU_LINK_STATE nLinkState = m_pSocketVector->OnConnRegistOK(a_pSocket);
  316. // 对SOCKET监控的处理
  317. a_pSocket->OnConnRegistOK();
  318. // 对事件观查者的处理
  319. PduLinkContent linkContent;
  320. SetLinkContent(linkContent, nLinkState, a_pSocket);
  321. m_pEventHost->OnLinkStateChanged(linkContent);
  322. }
  323. /*****************************************************************
  324. **【函数名称】 OnConnRegistFailed
  325. **【函数功能】 连接注册失败的后续处理
  326. **【参数】
  327. **【返回值】
  328. ****************************************************************/
  329. void CPduMain::OnConnRegistFailed(CPduSocket* a_pSocket)
  330. {
  331. // 对SOCKET容器的处理
  332. PDU_LINK_STATE nLinkState = m_pSocketVector->OnConnRegistFailed(a_pSocket);
  333. // 对SOCKET监控的处理
  334. a_pSocket->OnConnRegistFailed();
  335. // 对事件观查者的处理
  336. PduLinkContent linkContent;
  337. SetLinkContent(linkContent, nLinkState, a_pSocket);
  338. m_pEventHost->OnLinkStateChanged(linkContent);
  339. }
  340. /*****************************************************************
  341. **【函数名称】 OnRecvCommand
  342. **【函数功能】 命令接收的处理
  343. **【参数】 a_pCmd 当前接收到的命令实体
  344. **【返回值】
  345. ****************************************************************/
  346. void CPduMain::OnRecvCommand(CPduEntity* a_pCmd)
  347. {
  348. // 接收端校验
  349. ASSERT((a_pCmd->GetPeerDevId() == (ULONG)m_nDevId && a_pCmd->GetPeerDevType() == (ULONG)m_nDevType));
  350. // 对注册命令的特殊处理
  351. if(a_pCmd->GetCmdType() == PDU_CMD_REG && a_pCmd->GetIsExecReturn())
  352. {
  353. // 查找要接收命令的连接
  354. CPduSocket* pSocket = m_pSocketVector->GetPduSocket(a_pCmd->GetAssoSocket());
  355. if(pSocket == NULL) return; // 异常处理
  356. // 触发注册事件
  357. if(a_pCmd->GetDataBool(0))
  358. {
  359. OnConnRegistOK(pSocket);
  360. }
  361. else
  362. {
  363. OnConnRegistFailed(pSocket);
  364. } // end if
  365. } // end if
  366. // 对事件观查者的处理
  367. m_pEventHost->OnRecvCommand(a_pCmd);
  368. }
  369. /*****************************************************************
  370. **【函数名称】 StopAll
  371. **【函数功能】 停止
  372. **【参数】
  373. **【返回值】
  374. ****************************************************************/
  375. void CPduMain::StopAll( void )
  376. {
  377. if(m_pSocketServer != NULL)
  378. {
  379. delete m_pSocketServer;
  380. m_pSocketServer = NULL;
  381. }
  382. if(m_pSocketVector != NULL)
  383. {
  384. m_pSocketVector->Release();
  385. delete m_pSocketVector;
  386. m_pSocketVector = NULL;
  387. }
  388. if(m_pEventHost != NULL)
  389. {
  390. delete m_pEventHost;
  391. m_pEventHost = NULL;
  392. }
  393. }