NetWork_Alone.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*******************************************************************************
  2. 版权声明: Copyright (C), 2024-2034, Yunda Tech. Co., Ltd.
  3. 文件名 : NetWork_Alone.cpp
  4. 描 述 : 作为动态库输出时网络实现类
  5. 其 他 : 无
  6. 历史记录:
  7. 1. 时间: 2024-05-22
  8. 作者: masq
  9. 修改描述: 新创建
  10. 2. ...
  11. *******************************************************************************/
  12. #include "pch.h"
  13. #include <YD_Public_OCC.h>
  14. #include "NetWork_Alone.h"
  15. /*******************************************************
  16. 函数名称: ThreadFun_TCP_Send
  17. 函数功能: TCP数据发送线程函数
  18. 输入参数: lpParameter - 线程参数
  19. 输出参数: NULL
  20. 返 回 值: void
  21. *******************************************************/
  22. void CNetWork_Alone::ThreadFun_TCP_Send()
  23. {
  24. m_SendTCPThreadOut = false;
  25. try
  26. {
  27. while ( !m_SendTCPThreadOut )
  28. {
  29. if ( !DealWith_TCP_Send() )
  30. {
  31. Thread_Sleep(TIMESPAN_LVT);
  32. }
  33. }
  34. }
  35. catch (...)
  36. {
  37. //DWORD error = GetLastError();
  38. std::string errorstr = Str_Format("DisPatchServer NetMsgSendDealThread Have a Error errorcode");
  39. //lpthis->AddLogItem(errorstr, LOG_LEVEL_ERROR);
  40. }
  41. }
  42. /*******************************************************
  43. 函数名称: UDP_Send_Thread
  44. 函数功能: UDP数据发送线程函数
  45. 输入参数: lpParameter - 线程参数
  46. 输出参数: NULL
  47. 返 回 值: void
  48. *******************************************************/
  49. void CNetWork_Alone::ThreadFun_UDP_Send()
  50. {
  51. m_SendTCPThreadOut = false;
  52. try
  53. {
  54. while ( !m_SendTCPThreadOut )
  55. {
  56. if ( !DealWith_UDP_Send() )
  57. {
  58. Thread_Sleep(TIMESPAN_LVT);
  59. }
  60. }
  61. }
  62. catch (...)
  63. {
  64. //DWORD error = GetLastError();
  65. std::string errorstr = Str_Format("DisPatchServer NetMsgSendDealThread Have a Error errorcode");
  66. //lpthis->AddLogItem(errorstr, LOG_LEVEL_ERROR);
  67. }
  68. }
  69. /*******************************************************
  70. 函数名称: Group_Send_Thread
  71. 函数功能: 组播数据发送线程函数
  72. 输入参数: lpParameter - 线程参数
  73. 输出参数: NULL
  74. 返 回 值: void
  75. *******************************************************/
  76. void CNetWork_Alone::ThreadFun_Group_Send()
  77. {
  78. m_SendTCPThreadOut = false;
  79. try
  80. {
  81. while ( !m_SendTCPThreadOut )
  82. {
  83. if ( !DealWith_Group_Send() )
  84. {
  85. Thread_Sleep(TIMESPAN_LVT);
  86. }
  87. }
  88. }
  89. catch (...)
  90. {
  91. //DWORD error = GetLastError();
  92. std::string errorstr = Str_Format("DisPatchServer NetMsgSendDealThread Have a Error errorcode");
  93. //lpthis->AddLogItem(errorstr, LOG_LEVEL_ERROR);
  94. }
  95. }
  96. //构造函数
  97. CNetWork_Alone::CNetWork_Alone( CDispatchMng_I* pDispatchMng )
  98. {
  99. m_pSendList_TCP = nullptr;
  100. m_pSendList_UDP = nullptr;
  101. m_pSendList_Group = nullptr;
  102. m_pRecvList_TCP = nullptr;
  103. m_pRecvList_UDP = nullptr;
  104. m_pRecvList_Group = nullptr;
  105. m_pTCPSendCallBack = nullptr;
  106. m_pUDPSendCallBack = nullptr;
  107. m_SendTCPThreadID = 0; //线程ID-TCP数据发送
  108. m_SendUDPThreadID = 0; //线程ID-UDP数据发送
  109. m_SendGroupThreadID = 0; //线程ID-组播数据发送
  110. m_SendTCPThreadOut = true; //线程退出标志-TCP数据发送
  111. m_SendUDPThreadOut = true; //线程退出标志-UDP数据发送
  112. m_SendGroupThreadOut= true; //线程退出标志-组播数据发送
  113. InitAlone_NetWork(pDispatchMng);
  114. }
  115. //析构函数
  116. CNetWork_Alone::~CNetWork_Alone(void)
  117. {
  118. }
  119. /*******************************************************
  120. 函数名称: _IGet_System_ID
  121. 函数功能: 获取服务器的系统号
  122. 输入参数: NULL
  123. 输出参数: NULL
  124. 返 回 值: int - 系统号
  125. *******************************************************/
  126. int CNetWork_Alone::_IGet_System_ID()
  127. {
  128. return -1;
  129. }
  130. /*******************************************************
  131. 函数名称: _IGetTrainID
  132. 函数功能: 获取训练号
  133. 输入参数: NULL
  134. 输出参数: NULL
  135. 返 回 值: int - 训练号
  136. *******************************************************/
  137. int CNetWork_Alone::_IGetTrainID()
  138. {
  139. return -1;
  140. }
  141. /*******************************************************
  142. 函数名称: _IConnectToTech
  143. 函数功能: 接口-连接到教员
  144. 输入参数: NULL
  145. 输出参数: NULL
  146. 返 回 值: int - 结果
  147. *******************************************************/
  148. int CNetWork_Alone::_IConnectToTech()
  149. {
  150. return -1;
  151. }
  152. /*******************************************************
  153. 函数名称: _IClose
  154. 函数功能: 关闭网络
  155. 输入参数: NULL
  156. 输出参数: NULL
  157. 返 回 值: bool - 结果
  158. *******************************************************/
  159. bool CNetWork_Alone::_IClose()
  160. {
  161. //1、数据发送线程关闭-TCP
  162. m_SendTCPThreadOut = true;
  163. if ( m_ThreadSend_TCP.joinable() )
  164. {
  165. m_ThreadSend_TCP.join();
  166. }
  167. //2、数据发送线程关闭-UDP
  168. m_SendUDPThreadOut = true;
  169. if ( m_ThreadSend_UDP.joinable() )
  170. {
  171. m_ThreadSend_UDP.join();
  172. }
  173. //3、数据发送线程关闭-GROUP
  174. m_SendGroupThreadOut = true;
  175. if ( m_ThreadSend_Group.joinable() )
  176. {
  177. m_ThreadSend_Group.join();
  178. }
  179. //4、其他状态指针赋值初始化
  180. m_pSendList_TCP = nullptr;
  181. m_pSendList_UDP = nullptr;
  182. m_pSendList_Group = nullptr;
  183. m_pRecvList_TCP = nullptr;
  184. m_pRecvList_UDP = nullptr;
  185. m_pRecvList_Group = nullptr;
  186. m_pTCPSendCallBack = nullptr;
  187. m_pUDPSendCallBack = nullptr;
  188. return false;
  189. }
  190. /*******************************************************
  191. 函数名称: _ICreate_TCP_Connect
  192. 函数功能: 创建TCP连接
  193. 输入参数: NULL
  194. 输出参数: NULL
  195. 返 回 值: void
  196. *******************************************************/
  197. void CNetWork_Alone::_ICreate_TCP_Connect( int nSysID )
  198. {
  199. }
  200. /*******************************************************
  201. 函数名称: _ISend_TCPData_RightNow
  202. 函数功能: 立即发送-TCP数据
  203. 输入参数: NetCmd - 网络命令
  204. 输出参数: NULL
  205. 返 回 值: void
  206. *******************************************************/
  207. void CNetWork_Alone::_ISend_TCPData_RightNow( OCCCOMMAND NetCmd )
  208. {
  209. //1、判断是否服务器自身数据
  210. BOOL bOwnerSelfCmd = FALSE;
  211. switch ( NetCmd.DesSysType )
  212. {
  213. case OCC_SYS_LS_SVR:
  214. case OCC_SYS_XD_SVR:
  215. case OCC_SYS_DD_SVR:
  216. case OCC_SYS_HD_SVR:
  217. case OCC_SYS_FS_SVR:
  218. case OCC_SYS_TC_SVR:
  219. case OCC_SYS_JT_SVR:
  220. case OCC_SYS_TS_SVR:
  221. case OCC_SYS_KC_SVR:
  222. bOwnerSelfCmd = TRUE;
  223. break;
  224. default:
  225. break;
  226. }
  227. //2、发送服务器自身数据
  228. if ( bOwnerSelfCmd && NULL != m_pRecvList_TCP )
  229. {
  230. m_pRecvList_TCP->push(NetCmd);
  231. }
  232. //3、发送非服务器自身数据
  233. if ( !bOwnerSelfCmd && NULL != m_pTCPSendCallBack )
  234. {
  235. m_pTCPSendCallBack((char*)&NetCmd, sizeof(NetCmd));
  236. }
  237. }
  238. /*******************************************************
  239. 函数名称: _ISend_UDPData_RightNow
  240. 函数功能: 立即发送-UDP数据
  241. 输入参数: NetCmd - 网络命令
  242. 输出参数: NULL
  243. 返 回 值: void
  244. *******************************************************/
  245. void CNetWork_Alone::_ISend_UDPData_RightNow( OCCCOMMAND NetCmd )
  246. {
  247. //1、判断是否服务器自身数据
  248. BOOL bOwnerSelfCmd = FALSE;
  249. switch (NetCmd.DesSysType)
  250. {
  251. case OCC_SYS_LS_SVR:
  252. case OCC_SYS_XD_SVR:
  253. case OCC_SYS_DD_SVR:
  254. case OCC_SYS_HD_SVR:
  255. case OCC_SYS_FS_SVR:
  256. case OCC_SYS_TC_SVR:
  257. case OCC_SYS_JT_SVR:
  258. case OCC_SYS_TS_SVR:
  259. case OCC_SYS_KC_SVR:
  260. bOwnerSelfCmd = TRUE;
  261. break;
  262. default:
  263. break;
  264. }
  265. //2、发送服务器自身数据
  266. if ( bOwnerSelfCmd && NULL != m_pRecvList_UDP )
  267. {
  268. m_pRecvList_UDP->push(NetCmd);
  269. }
  270. //3、发送非服务器自身数据
  271. if ( !bOwnerSelfCmd && NULL != m_pUDPSendCallBack )
  272. {
  273. m_pUDPSendCallBack((char*)&NetCmd, sizeof(NetCmd));
  274. }
  275. }
  276. /*******************************************************
  277. 函数名称: _ISetRepeat_MainCtrl_SysID
  278. 函数功能: 设置复示显示的系统号
  279. 输入参数: nSysID - 系统号
  280. 输出参数: NULL
  281. 返 回 值: void
  282. *******************************************************/
  283. void CNetWork_Alone::_ISetRepeat_MainCtrl_SysID( int nSysID )
  284. {
  285. }
  286. /*******************************************************
  287. 函数名称: _ISetRepeat_MainCtrl_ClientID
  288. 函数功能: 设置复示显示客户端的系统号
  289. 输入参数: nSysID - 系统号
  290. 输出参数: NULL
  291. 返 回 值: void
  292. *******************************************************/
  293. void CNetWork_Alone::_ISetRepeat_MainCtrl_ClientID( int nSysID )
  294. {
  295. }
  296. /*******************************************************
  297. 函数名称: _ISetTechID
  298. 函数功能: 设置教员系统号
  299. 输入参数: nTeachSysID - 教员系统号
  300. 输出参数: NULL
  301. 返 回 值: void
  302. *******************************************************/
  303. void CNetWork_Alone::_ISetTechID(int nTeachSysID)
  304. {
  305. }
  306. /*******************************************************
  307. 函数名称: _ISetTrainID
  308. 函数功能: 设置训练号
  309. 输入参数: nTrainingID - 训练号
  310. 输出参数: NULL
  311. 返 回 值: void
  312. *******************************************************/
  313. void CNetWork_Alone::_ISetTrainID(int nTrainingID)
  314. {
  315. }
  316. /*******************************************************
  317. 函数名称: _IRecord_Training_Sys
  318. 函数功能: 记录参与训练的系统号
  319. 输入参数: nSysType - 系统类型
  320. 输出参数: NULL
  321. 返 回 值: void
  322. *******************************************************/
  323. void CNetWork_Alone::_IRecord_Training_Sys(int nSysType)
  324. {
  325. }
  326. /*******************************************************
  327. 函数名称: _IRecord_Key_System
  328. 函数功能: 记录关键系统
  329. 输入参数: nSysType - 系统类型
  330. nSysID - 系统号
  331. 输出参数: NULL
  332. 返 回 值: void
  333. *******************************************************/
  334. void CNetWork_Alone::_IRecord_Key_System(int nSysType, int nSysID)
  335. {
  336. }
  337. /*******************************************************
  338. 函数名称: _ISetNetData_MonitorS
  339. 函数功能: 设置网络数据监视开关
  340. 输入参数: bNetData_MonitorS - OCC网络监视开关
  341. 输出参数: NULL
  342. 返 回 值: void
  343. *******************************************************/
  344. void CNetWork_Alone::_ISetNetData_MonitorS( BOOL bNetData_MonitorS )
  345. {
  346. }
  347. /*******************************************************
  348. 函数名称: _IStart_Training
  349. 函数功能: 开始训练
  350. 输入参数: nTrainingID - 训练号
  351. nCourseID - 课程号
  352. nSimTime - 仿真时间
  353. 输出参数: NULL
  354. 返 回 值: void
  355. *******************************************************/
  356. void CNetWork_Alone::_IStart_Training( int nTrainingID, int nCourseID, int nSimTime )
  357. {
  358. }
  359. /*******************************************************
  360. 函数名称: _IEnd_Training
  361. 函数功能: 结束训练
  362. 输入参数: nTrainingID - 训练号
  363. nCourseID - 课程号
  364. nSimTime - 仿真时间
  365. 输出参数: NULL
  366. 返 回 值: void
  367. *******************************************************/
  368. void CNetWork_Alone::_IEnd_Training(int nTrainingID, int nCourseID, int nSimTime)
  369. {
  370. }
  371. /*******************************************************
  372. 函数名称: _ISend_TCP_OtherCmd
  373. 函数功能: 数据发送 - TCP非OCC封装
  374. 输入参数: OtherCmd_TCP - 数据
  375. 输出参数: NULL
  376. 返 回 值: void
  377. *******************************************************/
  378. void CNetWork_Alone::_ISend_TCP_OtherCmd(OTHER_CMD_TCP OtherCmd_TCP)
  379. {
  380. }
  381. /*******************************************************
  382. 函数名称: _ISend_UDP_OtherCmd
  383. 函数功能: 数据发送 - TCP非OCC封装
  384. 输入参数: OtherCmd_UDP - 数据
  385. 输出参数: NULL
  386. 返 回 值: void
  387. *******************************************************/
  388. void CNetWork_Alone::_ISend_UDP_OtherCmd(OTHER_CMD_UDP OtherCmd_UDP)
  389. {
  390. }
  391. /*******************************************************
  392. 函数名称: InitAlone_NetWork
  393. 函数功能: 初始化独立网络
  394. 输入参数: pDispatchMng - 调度管理
  395. 输出参数: NULL
  396. 返 回 值: void
  397. *******************************************************/
  398. void CNetWork_Alone::InitAlone_NetWork( CDispatchMng_I* pDispatchMng )
  399. {
  400. m_pSendList_TCP = (CommandQueue*)pDispatchMng->_IGetPtr_CmdQueue(CmdQueueType_Send_TCP);
  401. m_pSendList_UDP = (CommandQueue*)pDispatchMng->_IGetPtr_CmdQueue(CmdQueueType_Send_UDP);
  402. m_pSendList_Group = (CommandQueue*)pDispatchMng->_IGetPtr_CmdQueue(CmdQueueType_Send_GROUP);
  403. m_pRecvList_TCP = (CommandQueue*)pDispatchMng->_IGetPtr_CmdQueue(CmdQueueType_Recv_TCP);
  404. m_pRecvList_UDP = (CommandQueue*)pDispatchMng->_IGetPtr_CmdQueue(CmdQueueType_Recv_UDP);
  405. m_pRecvList_Group = (CommandQueue*)pDispatchMng->_IGetPtr_CmdQueue(CmdQueueType_Recv_GROUP);
  406. m_pTCPSendCallBack = (NETDATA_CALLBACK)pDispatchMng->_IGetPtr_TCPSend_CallBack();
  407. m_pUDPSendCallBack = (NETDATA_CALLBACK)pDispatchMng->_IGetPtr_UDPSend_CallBack();
  408. m_ThreadSend_TCP = std::thread(std::bind(&CNetWork_Alone::ThreadFun_TCP_Send, this));
  409. m_ThreadSend_UDP = std::thread(std::bind(&CNetWork_Alone::ThreadFun_UDP_Send, this));
  410. m_ThreadSend_Group = std::thread(std::bind(&CNetWork_Alone::ThreadFun_Group_Send, this));
  411. }
  412. /*******************************************************
  413. 函数名称: DealWith_TCP_Send
  414. 函数功能: 处理数据发送-TCP
  415. 输入参数: NULL
  416. 输出参数: NULL
  417. 返 回 值: BOOL - TRUE:成功,FALSE:失败
  418. *******************************************************/
  419. BOOL CNetWork_Alone::DealWith_TCP_Send()
  420. {
  421. //1、队列弹出网络命令
  422. OCCCOMMAND NetCmd;
  423. if ( !m_pSendList_TCP->pop(NetCmd) )
  424. {
  425. return FALSE;
  426. }
  427. //2、判断是否服务器自身命令
  428. BOOL bOwnerSelfCmd = FALSE;
  429. switch ( NetCmd.DesSysType )
  430. {
  431. case OCC_SYS_LS_SVR:
  432. case OCC_SYS_XD_SVR:
  433. case OCC_SYS_DD_SVR:
  434. case OCC_SYS_HD_SVR:
  435. case OCC_SYS_FS_SVR:
  436. case OCC_SYS_TC_SVR:
  437. case OCC_SYS_JT_SVR:
  438. case OCC_SYS_TS_SVR:
  439. case OCC_SYS_KC_SVR:
  440. bOwnerSelfCmd = TRUE;
  441. break;
  442. default:
  443. break;
  444. }
  445. //3、发送服务器自身命令
  446. if ( bOwnerSelfCmd && NULL != m_pRecvList_TCP )
  447. {
  448. m_pRecvList_TCP->push(NetCmd);
  449. }
  450. //4、发送非服务器自身命令
  451. if ( !bOwnerSelfCmd && NULL != m_pTCPSendCallBack )
  452. {
  453. m_pTCPSendCallBack((char*)&NetCmd, sizeof(NetCmd));
  454. }
  455. return TRUE;
  456. }
  457. /*******************************************************
  458. 函数名称: DealWith_UDP_Send
  459. 函数功能: 处理数据发送-UDP
  460. 输入参数: NULL
  461. 输出参数: NULL
  462. 返 回 值: BOOL - TRUE:成功,FALSE:失败
  463. *******************************************************/
  464. BOOL CNetWork_Alone::DealWith_UDP_Send()
  465. {
  466. //1、队列弹出网络命令
  467. OCCCOMMAND NetCmd;
  468. if ( !m_pSendList_UDP->pop(NetCmd) )
  469. {
  470. return FALSE;
  471. }
  472. //2、判断是否服务器自身命令
  473. BOOL bOwnerSelfCmd = FALSE;
  474. switch ( NetCmd.DesSysType )
  475. {
  476. case OCC_SYS_LS_SVR:
  477. case OCC_SYS_XD_SVR:
  478. case OCC_SYS_DD_SVR:
  479. case OCC_SYS_HD_SVR:
  480. case OCC_SYS_FS_SVR:
  481. case OCC_SYS_TC_SVR:
  482. case OCC_SYS_JT_SVR:
  483. case OCC_SYS_TS_SVR:
  484. case OCC_SYS_KC_SVR:
  485. bOwnerSelfCmd = TRUE;
  486. break;
  487. default:
  488. break;
  489. }
  490. //3、发送服务器自身命令
  491. if ( bOwnerSelfCmd && NULL != m_pRecvList_UDP )
  492. {
  493. m_pRecvList_UDP->push(NetCmd);
  494. }
  495. //4、发送非服务器自身命令
  496. if ( !bOwnerSelfCmd && NULL != m_pUDPSendCallBack )
  497. {
  498. m_pUDPSendCallBack((char*)&NetCmd, sizeof(NetCmd));
  499. }
  500. return TRUE;
  501. }
  502. /*******************************************************
  503. 函数名称: DealWith_Group_Send
  504. 函数功能: 处理数据发送-组播
  505. 输入参数: NULL
  506. 输出参数: NULL
  507. 返 回 值: BOOL - TRUE:成功,FALSE:失败
  508. *******************************************************/
  509. BOOL CNetWork_Alone::DealWith_Group_Send()
  510. {
  511. OCCCOMMAND NetCmd;
  512. if ( !m_pSendList_Group->pop(NetCmd) )
  513. {
  514. return FALSE;
  515. }
  516. //NOTE:组播数据即将淘汰,这里只将数据弹出,不做其他处理
  517. return TRUE;
  518. }