JobThreadF.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include "VMSCommLibF.h"
  4. #pragma hdrstop
  5. #include "JobThreadF.h"
  6. #include "ClientSessionF.h"
  7. #include "ClientSessionManagerF.h"
  8. #include "CDSCtlrF.h"
  9. #include "VmsProtocolF.h"
  10. //---------------------------------------------------------------------------
  11. #pragma package(smart_init)
  12. TJobThread *JobThread = NULL;
  13. //---------------------------------------------------------------------------
  14. TJobThread::TJobThread()
  15. {
  16. FPnlState = NULL;
  17. FTmrDb = NULL;
  18. }
  19. //---------------------------------------------------------------------------
  20. TJobThread::~TJobThread()
  21. {
  22. }
  23. //---------------------------------------------------------------------------
  24. void __fastcall TJobThread::updateLedColor(TColor AColor)
  25. {
  26. try {
  27. if (FPnlState) {
  28. PostMessage((HWND)g_AppCfg.lMainWinHandle, WM_PANEL_REFRESH, 3, (LPARAM)AColor);
  29. //FPnlState->Color = AColor;
  30. //FPnlState->Refresh();
  31. }
  32. } catch(Exception &e) {}
  33. }
  34. //---------------------------------------------------------------------------
  35. DWORD __fastcall TJobThread::Process(LPVOID AParam)
  36. {
  37. TClientSession *pSession;
  38. IPC_JOB_MESSAGE *pMsg;
  39. DWORD dwJob;
  40. int nRcvSize;
  41. char *pRcvBuff;
  42. OverlappedIO overlappedIO;
  43. while(!IsTerm() && !g_AppCfg.bThrExit)
  44. {
  45. updateLedColor(clGreen);
  46. if (!g_jobQ.PopBlocking(dwJob))
  47. {
  48. continue;
  49. }
  50. if (dwJob == Q_CLOSE)
  51. {
  52. break;
  53. }
  54. else
  55. if (dwJob == Q_TICK)
  56. {
  57. updateLedColor(clRed);
  58. ClientSessionManager->SendFlushAllClient();
  59. ClientSessionManager->CheckCommandTimeoutSessions();
  60. #if USE_POOL
  61. #else
  62. ClientSessionManager->CollectGarbageSessions();
  63. #endif
  64. continue;
  65. }
  66. else
  67. if (dwJob == eTcpCloseAll)
  68. {
  69. updateLedColor(clLime);
  70. ClientSessionManager->CloseAll();
  71. continue;
  72. }
  73. else
  74. if (dwJob == eVmsFormDownload)
  75. {
  76. // 정주기 시나리오 다운로드
  77. updateLedColor(clLime);
  78. SendVmsFormDownload();
  79. continue;
  80. }
  81. else
  82. if (dwJob == eVmsStatusReqAll)
  83. {
  84. // 정주기 상태정보 요청
  85. updateLedColor(clLime);
  86. SendVmsStatusReq();
  87. continue;
  88. }
  89. updateLedColor(clLime);
  90. pMsg = (IPC_JOB_MESSAGE*)dwJob;
  91. try
  92. {
  93. pSession = (TClientSession*)pMsg->ObjPtr;
  94. if (pSession == NULL)
  95. {
  96. MERROR("JOBT Unknown Event Null session: %d", pMsg->Type);
  97. }
  98. else
  99. {
  100. switch(pMsg->Type)
  101. {
  102. case eTcpConnect:
  103. if (pSession->OnConnect())
  104. {
  105. pSession->SendFlush();
  106. }
  107. break;
  108. case eTcpErrorEvent:
  109. case eTcpErrorRecv:
  110. case eTcpErrorSend:
  111. case eTcpClose:
  112. pSession->Disconnect();
  113. break;
  114. case eTcpRecv:
  115. nRcvSize = pSession->GetFreeSpaceSize();
  116. pRcvBuff = pSession->GetBuffer();
  117. if (nRcvSize >= pMsg->Len && pRcvBuff)
  118. {
  119. memcpy(pRcvBuff, pMsg->Buff, pMsg->Len);
  120. overlappedIO.FSession = pSession;
  121. RecvCompletion(0, pMsg->Len, (LPWSAOVERLAPPED)&overlappedIO, 0);
  122. }
  123. break;
  124. // 폼 다운로드 시작
  125. case eVmsScenarioDownload: // 개별 명령에 의한 시나리오 다운로드
  126. if (pSession->download_form_schedule(false) > 0)
  127. {
  128. pSession->SendFlush();
  129. }
  130. break;
  131. case eVmsStatusReq:
  132. if (pSession->ReqStauts())
  133. {
  134. pSession->SendFlush();
  135. }
  136. break;
  137. case eVmsPowerModuleStautsReq:
  138. if (pSession->ReqPowerModuleStatus())
  139. {
  140. pSession->SendFlush();
  141. }
  142. break;
  143. case eVmsDisplayModuleStatusReq:
  144. if (pSession->ReqDisplayModuelStatus())
  145. {
  146. pSession->SendFlush();
  147. }
  148. break;
  149. case eVmsParamReq:
  150. if (pSession->ReqParameter() > 0)
  151. {
  152. pSession->SendFlush();
  153. }
  154. break;
  155. case eVmsPixelImage:
  156. if (pSession->ReqPixelImage(pMsg->Buff) > 0)
  157. {
  158. pSession->SendFlush();
  159. }
  160. break;
  161. case eVmsUploadCurrentForm:
  162. if (pSession->ReqUploadCurrentForm() > 0)
  163. {
  164. pSession->SendFlush();
  165. }
  166. break;
  167. case eVmsDispalyDefaultForm:
  168. if (pSession->ReqDisplayDefaultForm() > 0)
  169. {
  170. pSession->SendFlush();
  171. }
  172. break;
  173. case eVmsUploadScheduleForm:
  174. if (pSession->ReqUploadScheduleForm() > 0)
  175. {
  176. pSession->SendFlush();
  177. }
  178. break;
  179. case eVmsDisplayFormId:
  180. if (pSession->ReqDisplayFormId(pMsg->Buff) > 0)
  181. {
  182. pSession->SendFlush();
  183. }
  184. break;
  185. case eVmsStatusControl:
  186. if (pSession->ReqStatusControl(pMsg->Buff, pMsg->Len) > 0)
  187. {
  188. pSession->SendFlush();
  189. }
  190. break;
  191. case eVmsSendPacket:
  192. if (pSession->ReqSendPacket(pMsg->Buff, pMsg->Len) > 0)
  193. {
  194. pSession->SendFlush();
  195. }
  196. break;
  197. }
  198. if (pSession->IsConnected)
  199. {
  200. pSession->SendFlush();
  201. }
  202. }
  203. }
  204. __finally
  205. {
  206. //SAFE_DELETE(pMsg);
  207. }
  208. }
  209. Term();
  210. return 0;
  211. }
  212. //---------------------------------------------------------------------------
  213. int __fastcall TJobThread::SendVmsFormDownload()
  214. {
  215. CtlrItr it;
  216. for(it=CDSCtlrManager->FLists.FObjects.begin(); it != CDSCtlrManager->FLists.FObjects.end(); ++it)
  217. {
  218. TCDSCtlr *pObj = (TCDSCtlr*)it->second;
  219. if (!pObj->Used) continue;
  220. if (!pObj->FProvide) continue;
  221. try
  222. {
  223. TClientSession *pSession = pObj->FSession;
  224. if (pSession)
  225. {
  226. if (pSession->download_form_schedule(true) > 0)
  227. {
  228. pSession->SendFlush();
  229. }
  230. }
  231. // 제어기로 전송 시퀀스를 호출한 후에 타임아웃을 체크하도록 한다.
  232. pObj->CTRLMODE->SaveFlag = false;
  233. pObj->CTRLMODE->pTimer = Now();
  234. }
  235. catch(Exception &e)
  236. {
  237. }
  238. }
  239. return 0;
  240. }
  241. //---------------------------------------------------------------------------
  242. int __fastcall TJobThread::SendVmsStatusReq()
  243. {
  244. CtlrItr it;
  245. for(it=CDSCtlrManager->FLists.FObjects.begin(); it != CDSCtlrManager->FLists.FObjects.end(); ++it)
  246. {
  247. TCDSCtlr *pObj = (TCDSCtlr*)it->second;
  248. if (!pObj->Used) continue;
  249. try
  250. {
  251. TClientSession *pSession = pObj->FSession;
  252. if (pSession)
  253. {
  254. if (pSession->ReqStauts() > 0)
  255. {
  256. pSession->SendFlush();
  257. }
  258. }
  259. }
  260. catch(Exception &e)
  261. {
  262. }
  263. }
  264. return 0;
  265. }
  266. //---------------------------------------------------------------------------