ServerSocket.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  1. #include "stdafx.h"
  2. //#include "stdafx.h"
  3. #include <comdef.h>
  4. #include <atlbase.h>
  5. //#include "resource.h"
  6. //#include "AboutBox.h"
  7. #include "ThreadPool.hpp"
  8. #include <strsafe.h>
  9. #include "SysLib.h"
  10. #include "ascprocess.h"
  11. #include "rtuprocess.h"
  12. #include "tcpprocess.h"
  13. #include "icpdasprocess.h"
  14. #include "SnmpProcess.h"
  15. #include "upsParadigmProcess.h"
  16. #include "stulzprocess.h"
  17. #include "DaiKinProcess.h"
  18. #include "PowerwareProcess.h"
  19. #include "greeprocess.h"
  20. #include "CarelProcess.h"
  21. #include "MostDevice.h"
  22. #include "NoticeQueue.h"
  23. #include "MainPro.h"
  24. const int SOCK_TCP = SOCK_STREAM-1;
  25. const int SOCK_UDP = SOCK_DGRAM-1;
  26. const int AF_IPV4 = 0;
  27. const int AF_IPV6 = 1;
  28. #pragma warning(disable : 4995)
  29. namespace ServerSocket
  30. {
  31. CServerSocket* g_pServerSocket[MAX_SERVER_LISTEN] = {0};
  32. CServerSocket::CServerSocket(void)
  33. {
  34. m_nSocketIndex = 0;
  35. m_nSockType = SOCK_TCP;
  36. m_nMode = AF_IPV4;
  37. InitializeCriticalSection( &m_csProcessData );
  38. m_SocketServer.SetInterface(this);
  39. }
  40. CServerSocket::~CServerSocket(void)
  41. {
  42. DeleteCriticalSection( &m_csProcessData );
  43. }
  44. ///////////////////////////////////////////////////////////////////////////////
  45. void CServerSocket::OnThreadBegin(CSocketHandle* pSH)
  46. {
  47. ASSERT( pSH == m_SocketServer );
  48. (pSH);
  49. CString strAddr;
  50. SockAddrIn sockAddr;
  51. m_SocketServer->GetSockName(sockAddr);
  52. GetAddress( sockAddr, strAddr );
  53. }
  54. void CServerSocket::OnThreadExit(CSocketHandle* pSH)
  55. {
  56. ASSERT( pSH == m_SocketServer );
  57. (pSH);
  58. }
  59. void CServerSocket::OnConnectionFailure(CSocketHandle* pSH, SOCKET newSocket)
  60. {
  61. ASSERT( pSH == m_SocketServer );
  62. (pSH);
  63. CString strAddr;
  64. CSocketHandle sockHandle;
  65. SockAddrIn sockAddr;
  66. if (newSocket != INVALID_SOCKET)
  67. {
  68. sockHandle.Attach( newSocket );
  69. sockHandle.GetPeerName( sockAddr );
  70. GetAddress( sockAddr, strAddr );
  71. sockHandle.Close();
  72. LOG4C((LOG_NOTICE, _T("Connection abandoned: %s\r\n"), strAddr ));
  73. }
  74. else
  75. {
  76. LOG4C((LOG_NOTICE, _T("Connection abandoned. Not a valid socket.\r\n"), strAddr ));
  77. }
  78. }
  79. void CServerSocket::OnAddConnection(CSocketHandle* pSH, SOCKET newSocket)
  80. {
  81. ASSERT( pSH == m_SocketServer );
  82. (pSH);
  83. }
  84. void CServerSocket::OnDataReceived(CSocketHandle* pSH, const BYTE* pbData, DWORD dwCount, const SockAddrIn& addr)
  85. {
  86. //ASSERT( g_pTcpServer != NULL );
  87. ASSERT( pSH == m_SocketServer );
  88. (pSH);
  89. CString strAddr, strText;
  90. USES_CONVERSION;
  91. LPTSTR pszText = strText.GetBuffer(dwCount+1);
  92. ::StringCchCopyN(pszText, dwCount+1, A2CT(reinterpret_cast<LPCSTR>(pbData)), dwCount);
  93. strText.ReleaseBuffer();
  94. GetAddress( addr, strAddr );
  95. int nResult = 0;
  96. if (m_nSockType == SOCK_TCP)
  97. {
  98. // unsafe access to Socket list!
  99. #ifdef SOCKHANDLE_USE_OVERLAPPED
  100. const SocketBufferList& sl = m_SocketServer.GetSocketList();
  101. for(SocketBufferList::const_iterator citer = sl.begin(); citer != sl.end(); ++citer)
  102. #else
  103. const SocketList& sl = m_SocketServer.GetSocketList();
  104. for(SocketList::const_iterator citer = sl.begin(); citer != sl.end(); ++citer)
  105. #endif
  106. {
  107. EnterCriticalSection( &m_csProcessData );
  108. CSocketHandle sockHandle;
  109. sockHandle.m_nPendingSize = 0;
  110. memset(sockHandle.m_PendingBuffer, 0, SOCKET_BUFFSIZE);
  111. sockHandle.Attach( (*citer) );
  112. SockAddrIn sockAddr;
  113. sockHandle.GetPeerName(sockAddr);
  114. if( sockAddr == addr )
  115. {
  116. ProcessData( sockHandle, pbData, dwCount );
  117. }
  118. sockHandle.Detach();
  119. LeaveCriticalSection( &m_csProcessData );
  120. }
  121. }
  122. else
  123. {
  124. SockAddrIn servAddr, sockAddr;
  125. m_SocketServer->GetSockName(servAddr);
  126. GetDestination(sockAddr);
  127. if ( servAddr != sockAddr ) {
  128. //m_SocketServer.Write((const LPBYTE)(T2CA(strMsg)), strMsg.GetLength(), sockAddr);
  129. } else {
  130. //AppendText( _T("Please change the port number to send message to a client.\r\n") );
  131. }
  132. }
  133. }
  134. void CServerSocket::OnConnectionDropped(CSocketHandle* pSH)
  135. {
  136. ASSERT( pSH == m_SocketServer );
  137. (pSH);
  138. }
  139. void CServerSocket::OnConnectionError(CSocketHandle* pSH, DWORD dwError)
  140. {
  141. ASSERT( pSH == m_SocketServer );
  142. (pSH);
  143. _com_error err(dwError);
  144. LOG4C((LOG_NOTICE, _T("Communication Error:\r\n%s\r\n"), err.ErrorMessage() ));
  145. }
  146. int CServerSocket::OnCmdProcess(CSocketHandle &sockHandle, void *pData)
  147. {
  148. ProtocolHeader *pHeader = (ProtocolHeader *)pData;
  149. if( pHeader == NULL ) return -1;
  150. if( pHeader->nLen < 0 || pHeader->nLen > 65535 ) return -1;
  151. unsigned int tmp = CalcCheckSum(pHeader, pHeader->nLen);
  152. if(tmp != pHeader->Verify)
  153. {
  154. return -1;
  155. }
  156. switch (pHeader->nCmd)
  157. {
  158. case CMD_DEV_ONLINE: //读设备在线状态
  159. ProcessResDevOnline(sockHandle, pHeader);
  160. break;
  161. case CMD_SINGLE_VAR_ONLINE: //读单个变量在线状态命令
  162. ProcessResSingleVarOnline(sockHandle, pHeader);
  163. break;
  164. case CMD_MUL_VAR_ONLINE: //读多个变量在线状态命令
  165. ProcessResMulVarOnline(sockHandle, pHeader);
  166. break;
  167. case CMD_READ_SINGLE_VAR_DATA: //读单个变量内容或报警状态
  168. ProcessResReadSingleVarData(sockHandle, pHeader);
  169. break;
  170. case CMD_READ_MUL_VAR_DATA: //读多个变量内容或报警状态
  171. ProcessResReadMulVarData(sockHandle, pHeader);
  172. break;
  173. case CMD_WRITE_SINGLE_VAR_DATA: //设置单个变量值
  174. ProcessResWriteSingleVarData(sockHandle, pHeader);
  175. break;
  176. case CMD_WRITE_MUL_VAR_DATA: //设置多个变量值
  177. PrcocessResWriteMulVarData(sockHandle, pHeader);
  178. break;
  179. case CMD_DOG_DATA: //软件狗数据
  180. ProcessResSoftDog(sockHandle, pHeader);
  181. break;
  182. case CMD_ALARM_CONFIRM: //报警确认
  183. ProcessResAlarmConfirm(sockHandle, pHeader);
  184. break;
  185. //请求读特殊变量的数据
  186. case CMD_READ_SPECIAL_SINGLE_VAR_DATA:
  187. ProcessResReadSpecSingleVarData(sockHandle, pHeader);
  188. break;
  189. case CMD_HEART: //心跳包响应
  190. #if 0
  191. ProcessHeart(sockHandle, pHeader);
  192. #endif
  193. break;
  194. case CMD_NOTICE_READ_DB_VAR: //通知服务器再一次从数据库读取变量参数
  195. ProcessNoticeReadDBVar(sockHandle, pHeader);
  196. break;
  197. case CMD_NOTICE_READ_DB_DEV: //通知服务器再一次从数据库读取设备参数
  198. ProcessNoticeReadDBDev(sockHandle, pHeader);
  199. break;
  200. case CMD_NOTICE_READ_DB_USER: //通知服务器再一次从数据库读取用户参数
  201. ProcessNoticeReadDBUser(sockHandle, pHeader);
  202. break;
  203. case CMD_NOTICE_TEST_ALARM:
  204. ProcessNoticeTestAlarm(sockHandle, pHeader);
  205. break;
  206. default: return 0;
  207. }
  208. return 0;
  209. }
  210. void CServerSocket::ProcessData(CSocketHandle &sockHandle, const BYTE* pData, DWORD nLen)
  211. {
  212. DWORD nBuffIndex = 0;
  213. while( nBuffIndex < nLen )
  214. {
  215. //LOG4C((LOG_NOTICE, "接收长度=%d, 处理长度=%d, PendingSize=%d", nLen, nBuffIndex, sockHandle.m_nPendingSize));
  216. ProtocolHeader *pHeader; //当前协议包头
  217. DWORD nProcessedLen = 0; //当前循环处理了多少个字节
  218. if( sockHandle.m_nPendingSize > 0 ) // 开始组包
  219. {
  220. //LOG4C((LOG_NOTICE, "开始组包"));
  221. pHeader = (ProtocolHeader *)sockHandle.m_PendingBuffer;
  222. if( sockHandle.m_nPendingSize < sizeof(ProtocolHeader) ) //上一次接收到的长度小于包头
  223. {
  224. DWORD nLinkHeaderLen = sizeof( ProtocolHeader ) - sockHandle.m_nPendingSize;
  225. if( nLinkHeaderLen < nLen ) //这次可以收完包头
  226. {
  227. memcpy( &sockHandle.m_PendingBuffer[ sockHandle.m_nPendingSize ], pData, nLinkHeaderLen ); //这里已经收完Header
  228. nProcessedLen = pHeader->nLen - sockHandle.m_nPendingSize;
  229. if( nProcessedLen <= nLen ) //如果所需处理的长度小于等于当前包长度
  230. {
  231. memcpy( &sockHandle.m_PendingBuffer[ sizeof( ProtocolHeader ) ],
  232. & ( ( char *) pData )[ nLinkHeaderLen ],
  233. pHeader->nLen - sizeof( ProtocolHeader ) );
  234. //LOG4C((LOG_NOTICE, "收完所需的包"));
  235. }
  236. else
  237. {
  238. int nTemp = nLen - nLinkHeaderLen; //除去头剩余部分的长度
  239. if ( nTemp > 0 ) //刚好是Header的长度,不用拷贝内存,所以这里加了>0的判断
  240. {
  241. memcpy( &sockHandle.m_PendingBuffer[ sizeof( ProtocolHeader ) ],
  242. & ( ( char *) pData )[ nLinkHeaderLen ],
  243. nTemp );
  244. }
  245. sockHandle.m_nPendingSize += nLen;
  246. }
  247. }
  248. else //这次还是没有收完包头, 继续Pending
  249. {
  250. //LOG4C((LOG_NOTICE, "这次还是没有收完包头, 继续Pending"));
  251. memcpy( &sockHandle.m_PendingBuffer[ sockHandle.m_nPendingSize ], pData, nLen );
  252. sockHandle.m_nPendingSize += nLen;
  253. nProcessedLen = nLen;
  254. }
  255. }
  256. else //Header部分已经在阻塞的缓冲区中
  257. {
  258. nProcessedLen = pHeader->nLen - sockHandle.m_nPendingSize;
  259. if ( nProcessedLen <= nLen ) //如果需要处理的长度小于现有包的长度
  260. {
  261. memcpy( &sockHandle.m_PendingBuffer[ sockHandle.m_nPendingSize ], pData, nProcessedLen );
  262. sockHandle.m_nPendingSize = 0;
  263. }
  264. else //否则要继续阻塞
  265. {
  266. memcpy( &sockHandle.m_PendingBuffer[ sockHandle.m_nPendingSize ], pData, nLen );
  267. sockHandle.m_nPendingSize += nLen;
  268. }
  269. }
  270. }
  271. else //第一次接包
  272. {
  273. //LOG4C((LOG_NOTICE, "第一次接包"));
  274. pHeader = (ProtocolHeader *)&( (unsigned char *)pData )[nBuffIndex];
  275. if( nLen - nBuffIndex < sizeof(ProtocolHeader) ) // 没有收够包头,先记录当前收到的Buffer
  276. {
  277. //如果第一次接包就没有收够包头,认为是非法包,扔掉,就是说已处理的长度nProcessedLen = 0
  278. sockHandle.m_nPendingSize = nLen - nBuffIndex;
  279. memcpy(sockHandle.m_PendingBuffer, pHeader, sockHandle.m_nPendingSize);
  280. //LOG4C((LOG_NOTICE, "第一次接包就没有收够包头,认为是非法包,扔掉"));
  281. }
  282. else
  283. {
  284. nProcessedLen = pHeader->nLen;
  285. if( (int)pHeader->nLen > nLen - nBuffIndex )
  286. {
  287. memcpy(sockHandle.m_PendingBuffer, pHeader, nLen - nBuffIndex);
  288. //如果第一次接包,pHeader->nLen大于当前包的总长,认为是非法包,扔掉
  289. if( nBuffIndex == 0 )
  290. {
  291. //组包错误,则扔掉当前包
  292. //LOG4C((LOG_NOTICE, "第一次接包,服务器pHeader->nLen大于当前包的总长,认为是非法包,扔掉\r\n"));
  293. }
  294. sockHandle.m_nPendingSize = nLen - nBuffIndex;
  295. nProcessedLen = nLen - nBuffIndex;
  296. }
  297. else
  298. {
  299. //LOG4C((LOG_NOTICE, "正常包"));
  300. sockHandle.m_nPendingSize = 0;
  301. }
  302. }
  303. }
  304. if ( nProcessedLen == 0 )
  305. {
  306. // 没有收够包头,认为是非法包,扔掉
  307. //LOG4C((LOG_NOTICE, "没有收够包头,认为是非法包,扔掉"));
  308. break;
  309. }
  310. if ( sockHandle.m_nPendingSize == 0 )
  311. {
  312. if ( pHeader->nLen > SOCKET_BUFFSIZE )
  313. {
  314. // 包长度超过限制
  315. //LOG4C((LOG_NOTICE, "pHeader->nLen超过限制"));
  316. }
  317. if(-1 == OnCmdProcess( sockHandle, pHeader ))
  318. {
  319. LOG4C((LOG_NOTICE, "Error OnCmdProcess"));
  320. break;
  321. }
  322. }
  323. nBuffIndex += nProcessedLen;
  324. //LOG4C((LOG_NOTICE, "处理长度=%d, PendingSize=%d", nBuffIndex, sockHandle.m_nPendingSize));
  325. }
  326. }
  327. void CServerSocket::SocketStop()
  328. {
  329. m_SocketServer.Terminate();
  330. }
  331. int CServerSocket::SvrStart(CString strPort)
  332. {
  333. int nFamily = (m_nMode == AF_IPV4) ? AF_INET : AF_INET6;
  334. if (!m_SocketServer.StartServer(NULL, strPort, nFamily, (m_nSockType+1) ))
  335. {
  336. LOG4C((LOG_NOTICE, _T("Failed to start server.")));
  337. };
  338. return 0;
  339. }
  340. bool CServerSocket::GetDestination(SockAddrIn& addrIn) const
  341. {
  342. CString strPort;
  343. //GetDlgItemText(IDC_SVR_PORT, strPort);
  344. int nFamily = (m_nMode == AF_IPV4) ? AF_INET : AF_INET6;
  345. return addrIn.CreateFrom(NULL, strPort, nFamily);
  346. }
  347. void CServerSocket::SocketSend(CSocketHandle &sockHandle, unsigned char *pMsg, int iLength)
  348. {
  349. if ( m_SocketServer.IsOpen() )
  350. {
  351. USES_CONVERSION;
  352. if (m_nSockType == SOCK_TCP)
  353. {
  354. // unsafe access to Socket list!
  355. const LPBYTE lpbData = (const LPBYTE)(pMsg);
  356. sockHandle.Write(lpbData, iLength, NULL);
  357. }
  358. else
  359. {
  360. SockAddrIn servAddr, sockAddr;
  361. m_SocketServer->GetSockName(servAddr);
  362. GetDestination(sockAddr);
  363. if ( servAddr != sockAddr ) {
  364. m_SocketServer.Write((const LPBYTE)*pMsg, iLength, sockAddr);
  365. } else {
  366. }
  367. }
  368. }
  369. else
  370. {
  371. }
  372. }
  373. void CServerSocket::GetAddress(const SockAddrIn& addrIn, CString& rString) const
  374. {
  375. TCHAR szIPAddr[MAX_PATH] = { 0 };
  376. CSocketHandle::FormatIP(szIPAddr, MAX_PATH, addrIn);
  377. rString.Format(_T("%s : %d"), szIPAddr, static_cast<int>(static_cast<UINT>(ntohs(addrIn.GetPort()))) );
  378. }
  379. #if 0
  380. int CServerSocket::ProcessHeart(CSocketHandle &sockHandle, void *pBuffer)
  381. {
  382. ProtocolHeader *pHeader = (ProtocolHeader *)pBuffer;
  383. int nClientIndex = 0;
  384. CSocketHandle sockTmpHandle;
  385. #ifdef SOCKHANDLE_USE_OVERLAPPED
  386. const SocketBufferList& sl = g_pTcpServer->m_SocketServer.GetSocketList();
  387. for(SocketBufferList::const_iterator citer = sl.begin(); citer != sl.end(); ++citer)
  388. #else
  389. const SocketList& sl = g_pTcpServer->m_SocketServer.GetSocketList();
  390. for(SocketList::const_iterator citer = sl.begin(); citer != sl.end(); ++citer)
  391. #endif
  392. {
  393. sockTmpHandle.Attach( (*citer) );
  394. SockAddrIn sockTmpAddr, sockAddr;
  395. sockTmpHandle.GetPeerName(sockTmpAddr);
  396. sockHandle.GetPeerName(sockAddr);
  397. if( memcmp(&sockTmpAddr, &sockAddr, sizeof(SockAddrIn)) == 0 && sockTmpHandle.IsOpen() )
  398. {
  399. g_dwClientOnlineTick[nClientIndex] = GetTickCount();
  400. }
  401. sockTmpHandle.Detach();
  402. nClientIndex++;
  403. }
  404. return 0;
  405. }
  406. #endif
  407. int CServerSocket::ProcessResDevOnline(CSocketHandle &sockHandle, void *pBuffer)
  408. {
  409. DevOnline *tmpDevOnline = (DevOnline *)pBuffer;
  410. ReqDevOnline tmpReqline = {0};
  411. tmpReqline.Header.nCmd = tmpDevOnline->Header.nCmd;
  412. tmpReqline.Header.nCmdType = tmpDevOnline->Header.nCmdType;
  413. strcpy( (char *)tmpReqline.DevUid, (char *)tmpDevOnline->DevUid );
  414. tmpReqline.nVarID = tmpDevOnline->nVarID;
  415. //if( 17 == tmpReqline.nVarID )
  416. //{
  417. // LOG4C((LOG_NOTICE, "receive dev communication var"));
  418. //}
  419. int nDeviceIndex = -1, nVarIndex = -1;
  420. BOOL bFind = FindVar((char *)tmpReqline.DevUid, tmpReqline.nVarID, nDeviceIndex, nVarIndex);
  421. if( bFind == FALSE )
  422. {
  423. //LOG4C((LOG_NOTICE, "FindVar == FALSE"));
  424. return -1;
  425. }
  426. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  427. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  428. tmpReqline.nOnline = pBaseVar->m_nStatus;
  429. tmpReqline.nStatus = pBaseVar->m_nStatus;
  430. if( tmpReqline.nOnline == -1 )
  431. {
  432. tmpReqline.nOnline = 1;
  433. tmpReqline.nStatus = 1;
  434. //LOG4C((LOG_NOTICE, "tmpReqline.nOnline == -1"));
  435. //EnterCriticalSection( &g_csTcpReadOneData );
  436. //pBaseVar->m_nStatus = 1;
  437. //LeaveCriticalSection( &g_csTcpReadOneData );
  438. }
  439. tmpReqline.Header.Verify = 0;
  440. tmpReqline.Header.nLen = 44;
  441. int nSendLen = tmpReqline.Header.nLen;
  442. tmpReqline.Header.Verify = CalcCheckSum( &tmpReqline, nSendLen );
  443. #if 0
  444. if( g_pTcpServer != NULL )
  445. {
  446. g_pTcpServer->SocketSend(sockHandle, (unsigned char *)&tmpReqline, nSendLen);
  447. }
  448. #else
  449. SocketSend(sockHandle, (unsigned char *)&tmpReqline, nSendLen);
  450. //LOG4C_HEX_DUMP((LOG_NOTICE, (char *)&tmpReqline, nSendLen));
  451. #endif
  452. return 0;
  453. }
  454. //单个变量应答设备在线状态
  455. int CServerSocket::ProcessResSingleVarOnline(CSocketHandle &sockHandle, void *pBuffer)
  456. {
  457. SingleVarOnline *tmpSnVarline = (SingleVarOnline *)pBuffer;
  458. ReqSingleVarOnline tmpReqSnVarline;
  459. memset(&tmpReqSnVarline, 0, sizeof(ReqSingleVarOnline) );
  460. tmpReqSnVarline.Header.nCmd = tmpSnVarline->Header.nCmd;
  461. tmpReqSnVarline.Header.nCmdType = tmpSnVarline->Header.nCmdType;
  462. strcpy((char *)tmpReqSnVarline.DevUid, (char *)tmpSnVarline->DevUid);
  463. tmpReqSnVarline.nOnline = 0x01;
  464. tmpReqSnVarline.nStatus = 0x02;
  465. tmpReqSnVarline.iVarid = tmpSnVarline->iVarid;
  466. tmpReqSnVarline.Header.Verify = 0;
  467. tmpReqSnVarline.Header.nLen = sizeof(ReqSingleVarOnline);
  468. int nSendLen = tmpReqSnVarline.Header.nLen;
  469. tmpReqSnVarline.Header.Verify = CalcCheckSum( &tmpReqSnVarline, nSendLen );
  470. #if 0
  471. if( g_pTcpServer != NULL )
  472. {
  473. g_pTcpServer->SocketSend(sockHandle, (unsigned char *)&tmpReqSnVarline, nSendLen);
  474. }
  475. #else
  476. SocketSend(sockHandle, (unsigned char *)&tmpReqSnVarline, nSendLen);
  477. #endif
  478. return 0;
  479. }
  480. //应答多个变量在线状态
  481. int CServerSocket::ProcessResMulVarOnline(CSocketHandle &sockHandle, void *pBuffer)
  482. {
  483. return 0;
  484. }
  485. //读单个变量内容或报警状态
  486. int CServerSocket::ProcessResReadSingleVarData(CSocketHandle &sockHandle, void *pBuffer)
  487. {
  488. ReadSingleVarData *pReadSnVar = (ReadSingleVarData *)pBuffer;
  489. ReqSingleVarData tmpReqSnVar;
  490. memset(&tmpReqSnVar, 0, sizeof(ReqSingleVarData));
  491. tmpReqSnVar.Header.nCmd = pReadSnVar->Header.nCmd;
  492. tmpReqSnVar.Header.nCmdType = pReadSnVar->Header.nCmdType;
  493. strcpy((char *)tmpReqSnVar.DevUid, (char *)pReadSnVar->DevUid);
  494. tmpReqSnVar.nVarID = pReadSnVar->nVarID;
  495. int nDeviceIndex = -1, nVarIndex = -1;
  496. BOOL bFind = FindVar((char *)pReadSnVar->DevUid, pReadSnVar->nVarID, nDeviceIndex, nVarIndex);
  497. if( bFind == FALSE ) return -1;
  498. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  499. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  500. if( pBaseVar->m_nVarTypeID != SNMP_NPM_TYPE &&
  501. pBaseVar->m_nVarTypeID != SNMP_STRING_ID )
  502. {
  503. if( (int)pBaseVar->m_dbData < pBaseVar->m_nReserved2 ||
  504. (int)pBaseVar->m_dbData > pBaseVar->m_nReserved3 )
  505. {
  506. return -1;
  507. }
  508. }
  509. tmpReqSnVar.nStatus = pBaseVar->m_nStatus;
  510. tmpReqSnVar.Data = pBaseVar->m_dbData;
  511. tmpReqSnVar.Header.Verify = 0;
  512. tmpReqSnVar.Header.nLen = sizeof(ReqSingleVarData);
  513. int nSendLen = tmpReqSnVar.Header.nLen;
  514. tmpReqSnVar.Header.Verify = CalcCheckSum( &tmpReqSnVar, nSendLen );
  515. #if 0
  516. if( g_pTcpServer != NULL )
  517. {
  518. g_pTcpServer->SocketSend(sockHandle, (unsigned char *)&tmpReqSnVar, nSendLen);
  519. }
  520. #else
  521. SocketSend(sockHandle, (unsigned char *)&tmpReqSnVar, nSendLen);
  522. #endif
  523. return 0;
  524. }
  525. //读多个变量内容或报警状态
  526. int CServerSocket::ProcessResReadMulVarData(CSocketHandle &sockHandle, void *pBuffer)
  527. {
  528. return 0;
  529. }
  530. //设置单个变量值
  531. int CServerSocket::ProcessResWriteSingleVarData(CSocketHandle &sockHandle, void *pBuffer)
  532. {
  533. WrSingleVarData *pWrSnVarData = (WrSingleVarData*)pBuffer;//请求数据包
  534. ReqWrSingleVarData tmpReqWrSnVar;//应答数据包
  535. //设置命令、命令类型、设备UID、变量UID
  536. memset(&tmpReqWrSnVar, 0, sizeof(ReqWrSingleVarData));
  537. tmpReqWrSnVar.Header.nCmd = pWrSnVarData->Header.nCmd;
  538. tmpReqWrSnVar.Header.nCmdType = pWrSnVarData->Header.nCmdType;
  539. strcpy((char *)tmpReqWrSnVar.DevUid, (char *)pWrSnVarData->DevUid);
  540. tmpReqWrSnVar.iVarid = pWrSnVarData->iVarid;
  541. int nDeviceIndex = -1, nVarIndex = -1;
  542. BOOL bFind = FindVar((char *)pWrSnVarData->DevUid, pWrSnVarData->iVarid, nDeviceIndex, nVarIndex);
  543. if( bFind == FALSE ) return -1;
  544. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  545. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  546. SETBASEPARAM SetBasePara;
  547. memset( &SetBasePara, 0, sizeof(SETBASEPARAM) );
  548. //网络设备
  549. char chDllName[MAX_PATH] = {0};
  550. strcpy(chDllName, (char *)(LPCTSTR)pDev->m_strProtocoldllname);
  551. StrLower(chDllName,chDllName);
  552. char szIniName[MAX_PATH] = {0};
  553. strcpy(szIniName, (char *)(LPCTSTR)pDev->m_strIniName);
  554. StrLower(szIniName, szIniName);
  555. if (pDev->m_bySpecialdevice == 0) // modbus rtu 设备
  556. {
  557. for( int i = RTU_THREAD_START; i < g_nRtuThreadIndex; i++ )
  558. {
  559. if( NULL != g_hRequestDataThread[i] )
  560. {
  561. SuspendThread(g_hRequestDataThread[i]);
  562. }
  563. }
  564. int nSetCount = 0;
  565. // 写3次串口,如果不成功则放弃
  566. do
  567. {
  568. if( nSetCount > 3 ) break;
  569. if( RtuRequestWrData( (char *)tmpReqWrSnVar.DevUid,
  570. tmpReqWrSnVar.iVarid, pWrSnVarData->Data) == 0)
  571. {
  572. tmpReqWrSnVar.nStatus = 0;
  573. break;
  574. }
  575. else
  576. {
  577. tmpReqWrSnVar.nStatus = -1;
  578. }
  579. nSetCount++;
  580. }while( WaitForSingleObject( g_hRunObject, 1250L ) == WAIT_TIMEOUT );
  581. for( int i = RTU_THREAD_START; i < g_nRtuThreadIndex; i++ )
  582. {
  583. if( NULL != g_hRequestDataThread[i] )
  584. {
  585. ResumeThread(g_hRequestDataThread[i]);
  586. }
  587. }
  588. }
  589. else if( pDev->m_bySpecialdevice == 1 ) // modbus Ascii 设备
  590. {
  591. for( int i = ASC_THREAD_START; i < g_nAscThreadIndex; i++ )
  592. {
  593. if( NULL != g_hRequestDataThread[i] )
  594. {
  595. SuspendThread(g_hRequestDataThread[i]);
  596. }
  597. }
  598. int nSetCount = 0;
  599. // 写3次串口,如果不成功则放弃
  600. do
  601. {
  602. if( nSetCount > 3 ) break;
  603. if (AscRequestWrData((char*)tmpReqWrSnVar.DevUid,
  604. tmpReqWrSnVar.iVarid, pWrSnVarData->Data) == 0)
  605. {
  606. AscSingleResponseWriteData((char*)tmpReqWrSnVar.DevUid,tmpReqWrSnVar.iVarid);
  607. if (pBaseVar->m_dbData != pWrSnVarData->Data)//判断设置数据是否一致
  608. tmpReqWrSnVar.nStatus = 0;
  609. break;
  610. }
  611. else
  612. {
  613. tmpReqWrSnVar.nStatus = -1;
  614. }
  615. nSetCount++;
  616. }while( WaitForSingleObject( g_hRunObject, 1250L ) == WAIT_TIMEOUT );
  617. for( int i = ASC_THREAD_START; i < g_nAscThreadIndex; i++ )
  618. {
  619. if( NULL != g_hRequestDataThread[i] )
  620. {
  621. ResumeThread(g_hRequestDataThread[i]);
  622. }
  623. }
  624. }
  625. else if( pDev->m_bySpecialdevice == 2 ) // modbus tcp 设备
  626. {
  627. int nSetCount = 0;
  628. // 写3次串口,如果不成功则放弃
  629. do
  630. {
  631. if( nSetCount > 3 ) break;
  632. //g_hRequestDataThread[g_nIcpdasIndex]
  633. #if 1
  634. ResetEvent( pDev->m_hSemNet );
  635. Sleep(5 * 1000); //等待5S,防止重置信号量之前,已经有变量在请求,保证变量已经读完
  636. #endif
  637. if( TcpRequestWriteData(pDev->m_iIpport,
  638. pDev->m_iDevideaddr,
  639. (char *)(LPCTSTR)pDev->m_strIp,
  640. pBaseVar->m_iRegisterNum,
  641. pBaseVar->m_nRegStartAddr,
  642. pBaseVar->m_iFuncID,
  643. (int)pWrSnVarData->Data) == 0 )
  644. {
  645. g_nIndex--;
  646. if( g_nIndex != 0 )
  647. {
  648. TRACE("ModbusTcp数据不同步!\r\n");
  649. }
  650. //TRACE(">>>>>>>>>>结束请求一次数据\r\n");
  651. #if 1
  652. SetEvent( pDev->m_hSemNet );
  653. #endif
  654. //SetEvent( g_hSemNet );
  655. //TRACE("变量ID=%d, 请求寄存器地址:%d, 变量值=%.1f\r\n", pReadSnVar->iVarid, atoi(chStartAddr), g_TcpReadOneData.dbData);
  656. //
  657. tmpReqWrSnVar.nStatus = 0;
  658. break;
  659. #if 0
  660. int nRet = TcpSingleResponseWriteData((char *)(LPCSTR)pBaseVar->m_strVarUID, pBaseVar->m_nVarID);
  661. if( nRet == 0 )
  662. {
  663. g_nIndex--;
  664. if( g_nIndex != 0 )
  665. {
  666. TRACE("ModbusTcp数据不同步!\r\n");
  667. }
  668. //TRACE(">>>>>>>>>>结束请求一次数据\r\n");
  669. SetEvent( g_hSemNet );
  670. //TRACE("变量ID=%d, 请求寄存器地址:%d, 变量值=%.1f\r\n", pReadSnVar->iVarid, atoi(chStartAddr), g_TcpReadOneData.dbData);
  671. //
  672. tmpReqWrSnVar.ReqVarData.nStatus = 0;
  673. break;
  674. }
  675. else
  676. {
  677. g_nIndex--;
  678. //TRACE("ModbusTcp响应数据错误!\r\n");
  679. SetEvent( g_hSemNet );
  680. tmpReqWrSnVar.ReqVarData.nStatus = -1;
  681. }
  682. #endif
  683. }
  684. #if 1
  685. else
  686. {
  687. SetEvent( pDev->m_hSemNet );
  688. }
  689. #endif
  690. nSetCount++;
  691. }while( WaitForSingleObject( g_hRunObject, 1250L ) == WAIT_TIMEOUT );
  692. }
  693. else if( pDev->m_bySpecialdevice == 3 ) // snmp 设备
  694. {
  695. CString strSetValue;
  696. strSetValue.Format("%.1f", pWrSnVarData->Data);
  697. if( pSnmpDllSetValue(
  698. (char *)(LPCTSTR)pDev->m_strIp,
  699. (char *)(LPCTSTR)pBaseVar->m_strFields,
  700. (char *)(LPCTSTR)pBaseVar->m_strSnmpoi,
  701. "int",
  702. 1,
  703. 300,
  704. (char *)(LPCTSTR)strSetValue
  705. ) == 0 )
  706. {
  707. tmpReqWrSnVar.nStatus = 0;
  708. }
  709. else
  710. {
  711. tmpReqWrSnVar.nStatus = -1;
  712. }
  713. }
  714. else if( pDev->m_bySpecialdevice == 4 ) // RS232 设备
  715. {
  716. if (strcmp(chDllName, "icpdas.dll") ==0)
  717. {
  718. for( int i = ICPDAS_THREAD_START; i < g_nIcpdasIndex; i++ )
  719. {
  720. if( NULL != g_hRequestDataThread[i] )
  721. {
  722. int nRet = SuspendThread(g_hRequestDataThread[i]);
  723. LOG4C((LOG_NOTICE, "SuspendThread return %d", nRet));
  724. }
  725. }
  726. int nSetCount = 0;
  727. // 写3次串口,如果不成功则放弃
  728. do
  729. {
  730. if( nSetCount > 3 ) break;
  731. if (RequestWrIcpdasData((char*)tmpReqWrSnVar.DevUid,
  732. tmpReqWrSnVar.iVarid, pWrSnVarData->Data) == 0)
  733. {
  734. tmpReqWrSnVar.nStatus = 0;
  735. break;
  736. }
  737. else
  738. {
  739. tmpReqWrSnVar.nStatus = -1;
  740. }
  741. nSetCount++;
  742. }while( WaitForSingleObject( g_hRunObject, 1250L ) == WAIT_TIMEOUT );
  743. for( int i = ICPDAS_THREAD_START; i < g_nIcpdasIndex; i++ )
  744. {
  745. if( NULL != g_hRequestDataThread[i] )
  746. {
  747. int nRet = ResumeThread(g_hRequestDataThread[i]);
  748. LOG4C((LOG_NOTICE, "ResumeThread return %d", nRet));
  749. }
  750. }
  751. }
  752. else if (strcmp(chDllName, "stulz.dll") ==0)
  753. {
  754. int nSetCount = 0;
  755. // 写3次串口,如果不成功则放弃
  756. do
  757. {
  758. if( nSetCount > 3 ) break;
  759. if (RequestWrStulzData((char*)tmpReqWrSnVar.DevUid,
  760. tmpReqWrSnVar.iVarid, pWrSnVarData->Data) == 0)
  761. {
  762. tmpReqWrSnVar.nStatus = 0;
  763. break;
  764. }
  765. else
  766. {
  767. tmpReqWrSnVar.nStatus = -1;
  768. }
  769. nSetCount++;
  770. }while( WaitForSingleObject( g_hRunObject, 1250L ) == WAIT_TIMEOUT );
  771. }
  772. else if( strcmp(chDllName, "daikin.dll") == 0 )
  773. {
  774. int nSetCount = 0;
  775. // 写3次串口,如果不成功则放弃
  776. do
  777. {
  778. if( nSetCount > 3 ) break;
  779. if (RequestDaiKinWrData( (char *)tmpReqWrSnVar.DevUid,
  780. tmpReqWrSnVar.iVarid, pWrSnVarData->Data, (char *)(LPCTSTR)pBaseVar->m_strRs232cmd ) == 0 )
  781. {
  782. tmpReqWrSnVar.nStatus = 0;
  783. break;
  784. }
  785. else
  786. {
  787. tmpReqWrSnVar.nStatus = -1;
  788. }
  789. nSetCount++;
  790. }while( WaitForSingleObject( g_hRunObject, 1250L ) == WAIT_TIMEOUT );
  791. }
  792. else if( strcmp(chDllName, "gree.dll") == 0 )
  793. {
  794. int nSetCount = 0;
  795. // 写10次串口,如果不成功则放弃
  796. do
  797. {
  798. if( nSetCount > 10 ) break;
  799. if (RequestGreeWrData( (char *)tmpReqWrSnVar.DevUid,
  800. tmpReqWrSnVar.iVarid, pWrSnVarData->Data, (char *)(LPCTSTR)pBaseVar->m_strRs232cmd ) == 0 )
  801. {
  802. tmpReqWrSnVar.nStatus = 0;
  803. break;
  804. }
  805. else
  806. {
  807. tmpReqWrSnVar.nStatus = -1;
  808. }
  809. nSetCount++;
  810. }while( WaitForSingleObject( g_hRunObject, 1250L ) == WAIT_TIMEOUT );
  811. }
  812. else if( strcmp(chDllName, "carel.dll") == 0 )
  813. {
  814. int nSetCount = 0;
  815. // 写3次串口,如果不成功则放弃
  816. do
  817. {
  818. if( nSetCount > 3 ) break;
  819. if (CarelControlRequest( (char *)tmpReqWrSnVar.DevUid,
  820. tmpReqWrSnVar.iVarid, pWrSnVarData->Data, (char *)(LPCTSTR)pBaseVar->m_strRs232cmd ) == 0 )
  821. {
  822. tmpReqWrSnVar.nStatus = 0;
  823. break;
  824. }
  825. else
  826. {
  827. tmpReqWrSnVar.nStatus = -1;
  828. }
  829. nSetCount++;
  830. }while( WaitForSingleObject( g_hRunObject, 1250L ) == WAIT_TIMEOUT );
  831. }
  832. else if ( strcmp(szIniName, "upsuh31.ini") == 0 ||
  833. strcmp(szIniName, "emerson_cm.ini") == 0 ||
  834. strcmp(szIniName, "dme3000.ini") == 0 ||
  835. strcmp(szIniName, "paradigm200a_rectifier.ini") == 0 ||
  836. strcmp(szIniName, "paradigm200a_inverter.ini") == 0 ||
  837. strcmp(szIniName, "paradigm200b.ini") == 0 ||
  838. strcmp(szIniName, "hipulseigmnet.ini") == 0 ||
  839. strcmp(szIniName, "hipulsecdt.ini") == 0 ||
  840. strcmp(szIniName, "prostar.ini") == 0 ||
  841. strcmp(szIniName, "joton.ini") == 0 ||
  842. strcmp(szIniName, "hiross.ini") == 0 ||
  843. strcmp(szIniName, "upslink.ini") == 0 ||
  844. strcmp(szIniName, "upsapcsilcon.ini") == 0 ||
  845. strcmp(szIniName, "upsuh11.ini") == 0 ||
  846. strcmp(szIniName, "weibo.ini") == 0 ||
  847. strcmp(szIniName, "daikin.ini") == 0 ||
  848. strcmp(szIniName, "scantak_3c.ini") == 0 ||
  849. strcmp(szIniName, "delta.ini") == 0 ||
  850. strcmp(szIniName, "sda_ii.ini") == 0 ||
  851. strcmp(szIniName, "m84c5z.ini") == 0 ||
  852. strcmp(szIniName, "batteryadu.ini") == 0 ||
  853. strcmp(szIniName, "upsmonitorlan.ini") == 0 ||
  854. strcmp(szIniName, "sk6000.ini") == 0 ||
  855. strcmp(szIniName, "batteryadu2000.ini") == 0 ||
  856. strcmp(szIniName, "santak_shiwei.ini") == 0 ||
  857. strcmp(szIniName, "santak_q4.ini") == 0 ||
  858. strcmp(szIniName, "ups_nxl.ini") == 0 ||
  859. strcmp(szIniName, "twoul33.ini") == 0 ||
  860. strcmp(szIniName, "oumai.ini") == 0 ||
  861. strcmp(szIniName, "santak_3a3120ks.ini") == 0 ||
  862. strcmp(szIniName, "tmaster.ini") == 0 ||
  863. strcmp(szIniName, "jb1501a64p.ini") == 0 ||
  864. strcmp(szIniName, "st_3c340ks.ini") == 0 ||
  865. strcmp(szIniName, "delta_v1.54.ini") == 0||
  866. strcmp(szIniName, "icp_i7017.ini") == 0 ||
  867. strcmp(szIniName, "fad.ini") == 0 ||
  868. strcmp(szIniName, "itrustadapt.ini" ) == 0 ||
  869. strcmp(szIniName, "uha3r_0200l.ini") == 0)
  870. {
  871. for( int i = MOSTDEVICE_THREAD_START; i < g_nMostDeviceIndex; i++ )
  872. {
  873. if( NULL != g_hRequestDataThread[i] )
  874. {
  875. SuspendThread(g_hRequestDataThread[i]);
  876. }
  877. }
  878. int nSetCount = 0;
  879. char szMsg[MAX_VAR_MSG] = {0};
  880. //memcpy(szMsg, &pWrSnVarData->VarData.Data, sizeof(double));
  881. itoa((int)pWrSnVarData->Data, szMsg, 16);
  882. ByteToUpper(szMsg);
  883. // 写3次串口,如果不成功则放弃
  884. do
  885. {
  886. if( nSetCount > 3 ) break;
  887. if (MostDeviceRequestSet(pDev->m_iPort, pDev->m_iDevideaddr,
  888. (char*)(LPCTSTR)tmpReqWrSnVar.DevUid, tmpReqWrSnVar.iVarid,
  889. (char*)(LPCTSTR)pBaseVar->m_strRs232cmd,
  890. szIniName, szMsg) == 0)
  891. //if (CarelControlRequest( (char *)tmpReqWrSnVar.DevUid,
  892. // tmpReqWrSnVar.ReqVarData.iVarid, pWrSnVarData->VarData.Data, (char *)(LPCTSTR)pBaseVar->m_strRs232cmd ) == 0 )
  893. {
  894. tmpReqWrSnVar.nStatus = 0;
  895. break;
  896. }
  897. else
  898. {
  899. tmpReqWrSnVar.nStatus = -1;
  900. }
  901. nSetCount++;
  902. }while( WaitForSingleObject( g_hRunObject, 1250L ) == WAIT_TIMEOUT );
  903. for( int i = MOSTDEVICE_THREAD_START; i < g_nMostDeviceIndex; i++ )
  904. {
  905. if( NULL != g_hRequestDataThread[i] )
  906. {
  907. ResumeThread(g_hRequestDataThread[i]);
  908. }
  909. }
  910. }
  911. }
  912. tmpReqWrSnVar.Header.Verify = 0;//校验码
  913. tmpReqWrSnVar.Header.nLen = sizeof(ReqWrSingleVarData);//数据包长度
  914. int nSendLen = tmpReqWrSnVar.Header.nLen;
  915. tmpReqWrSnVar.Header.Verify = CalcCheckSum( &tmpReqWrSnVar, nSendLen );//进行校验
  916. #if 0
  917. if( g_pTcpServer != NULL )
  918. {
  919. g_pTcpServer->SocketSend(sockHandle, (unsigned char *)&tmpReqWrSnVar, nSendLen);
  920. }
  921. #else
  922. SocketSend(sockHandle, (unsigned char *)&tmpReqWrSnVar, nSendLen);
  923. #endif
  924. return 0;
  925. }
  926. int CServerSocket::PrcocessResWriteMulVarData(CSocketHandle &sockHandle, void *pBuffer)
  927. {
  928. return 0;
  929. }
  930. int CServerSocket::ProcessResSoftDog(CSocketHandle &sockHandle, void *pBuffer)
  931. {
  932. Dog_Data *pDogData = (Dog_Data *)pBuffer;
  933. ReqDogData tmpReqDogData;
  934. int nSendLen = sizeof(ReqDogData);
  935. memset( &tmpReqDogData, 0, nSendLen);
  936. tmpReqDogData.Header.nCmd = pDogData->Header.nCmd;
  937. tmpReqDogData.Header.nCmdType = pDogData->Header.nCmdType;
  938. tmpReqDogData.Header.nLen = nSendLen;
  939. tmpReqDogData.nUseLimit = g_nUseTimeLimit;
  940. tmpReqDogData.Header.Verify = CalcCheckSum( &tmpReqDogData, nSendLen );
  941. #if 0
  942. if( g_pTcpServer != NULL )
  943. {
  944. g_pTcpServer->SocketSend(sockHandle, (unsigned char *)&tmpReqDogData, nSendLen);
  945. }
  946. #else
  947. SocketSend(sockHandle, (unsigned char *)&tmpReqDogData, nSendLen);
  948. #endif
  949. return 0;
  950. }
  951. int CServerSocket::ProcessResAlarmConfirm(CSocketHandle &sockHandle, void *pBuffer)
  952. {
  953. AlarmConfirm *tag = (AlarmConfirm *)pBuffer;
  954. DeleteSendNoticeList( (char *)tag->DevUid,tag->iVarid,tag->nAlarmIndex );
  955. DeleteSendSmsList( (char *)tag->DevUid,tag->iVarid,tag->nAlarmIndex );
  956. return 0;
  957. }
  958. //请求读特殊变量的数据
  959. int CServerSocket::ProcessResReadSpecSingleVarData(CSocketHandle &sockHandle, void *pBuffer)
  960. {
  961. ReadSingleSpecialVarData *pReadSpecSnVar = (ReadSingleSpecialVarData *)pBuffer;
  962. ReqSingleSpecialVarData tmpReqSpecSnVar;
  963. memset(&tmpReqSpecSnVar, 0, sizeof(ReqSingleSpecialVarData));
  964. tmpReqSpecSnVar.Header.nCmd = pReadSpecSnVar->Header.nCmd;
  965. tmpReqSpecSnVar.Header.nCmdType = pReadSpecSnVar->Header.nCmdType;
  966. strcpy((char *)tmpReqSpecSnVar.DevUid, (char *)pReadSpecSnVar->DevUid);
  967. tmpReqSpecSnVar.iSpecialFlag = pReadSpecSnVar->iSpecialFlag;
  968. tmpReqSpecSnVar.nVarID = pReadSpecSnVar->nVarID;
  969. int nDeviceIndex = -1, nVarIndex = -1;
  970. BOOL bFind = FindVar((char *)pReadSpecSnVar->DevUid, pReadSpecSnVar->nVarID, nDeviceIndex, nVarIndex);
  971. if( bFind == FALSE )
  972. {
  973. return -1;
  974. }
  975. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  976. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  977. if( pBaseVar->m_nVarTypeID != SNMP_NPM_TYPE &&
  978. pBaseVar->m_nVarTypeID != SNMP_STRING_ID )
  979. {
  980. if( (int)pBaseVar->m_dbData < pBaseVar->m_nReserved2 ||
  981. (int)pBaseVar->m_dbData > pBaseVar->m_nReserved3 )
  982. {
  983. return -1;
  984. }
  985. }
  986. tmpReqSpecSnVar.nStatus = pBaseVar->m_nStatus;
  987. tmpReqSpecSnVar.Data = pBaseVar->m_dbData;
  988. tmpReqSpecSnVar.Header.Verify = 0;
  989. tmpReqSpecSnVar.Header.nLen = sizeof(ReqSingleSpecialVarData);
  990. int nSendLen = tmpReqSpecSnVar.Header.nLen;
  991. tmpReqSpecSnVar.Header.Verify = CalcCheckSum( &tmpReqSpecSnVar, nSendLen );
  992. #if 0
  993. if( g_pTcpServer != NULL )
  994. {
  995. g_pTcpServer->SocketSend(sockHandle, (unsigned char *)&tmpReqSpecSnVar, nSendLen);
  996. }
  997. #else
  998. SocketSend(sockHandle, (unsigned char *)&tmpReqSpecSnVar, nSendLen);
  999. #endif
  1000. return 0;
  1001. }
  1002. //请求读特殊变量的数据
  1003. int CServerSocket::ProcessResReadSpecMulVarData(CSocketHandle &sockHandle, void *pBuffer)
  1004. {
  1005. return 0;
  1006. }
  1007. //重新读取数据库通知
  1008. int CServerSocket::ProcessNoticeReadDBVar(CSocketHandle &sockHandle, void *pBuffer)
  1009. {
  1010. LOG4C((LOG_NOTICE, "Enter ProcessNoticeReadDBVar 再次读取变量!"));
  1011. NoticeReadDBVar *pNoticeReadDBVar = (NoticeReadDBVar *)pBuffer;
  1012. int nDeviceIndex = -1, nVarIndex = -1;
  1013. g_pDevicesManager->FindDevByUid((CString)pNoticeReadDBVar->DevUid, nDeviceIndex);
  1014. if( -1 == nDeviceIndex ) return 0;
  1015. //LOG4C((LOG_NOTICE, "nDeviceIndex = %d", nDeviceIndex));
  1016. g_pDevicesManager->FindVar(nDeviceIndex, pNoticeReadDBVar->nVarID, nVarIndex);
  1017. //LOG4C((LOG_NOTICE, "nVarIndex = %d", nVarIndex));
  1018. if( -1 == nVarIndex ) return 0;
  1019. switch( pNoticeReadDBVar->nOperate )
  1020. {
  1021. case NOTICE_NEW:
  1022. break;
  1023. case NOTICE_EDIT:
  1024. EnterCriticalSection( &m_csProcessData );
  1025. //LOG4C((LOG_NOTICE, "Uid = %s, varid = %d", pNoticeReadDBVar->DevUid, pNoticeReadDBVar->nVarID));
  1026. if( strcmp((char *)pNoticeReadDBVar->DevUid, "") != 0 && pNoticeReadDBVar->nVarID > 0 )
  1027. {
  1028. CDBInterface::GetInstancePtr()->EditVarInfo(nDeviceIndex, nVarIndex, (char *)(LPCTSTR)pNoticeReadDBVar->DevUid, pNoticeReadDBVar->nVarID);
  1029. }
  1030. LeaveCriticalSection( &m_csProcessData );
  1031. break;
  1032. case NOTICE_DEL:
  1033. break;
  1034. }
  1035. return 0;
  1036. }
  1037. int CServerSocket::ProcessNoticeReadDBDev(CSocketHandle &sockHandle, void *pBuffer)
  1038. {
  1039. LOG4C((LOG_NOTICE, "Enter ProcessNoticeReadDBDev 再次读取设备!"));
  1040. NoticeReadDBDev *pNoticeReadDBDev = (NoticeReadDBDev *)pBuffer;
  1041. switch( pNoticeReadDBDev->nOperate )
  1042. {
  1043. case NOTICE_NEW:
  1044. break;
  1045. case NOTICE_EDIT:
  1046. EnterCriticalSection( &m_csProcessData );
  1047. LeaveCriticalSection( &m_csProcessData );
  1048. break;
  1049. case NOTICE_DEL:
  1050. break;
  1051. }
  1052. return 0;
  1053. }
  1054. int CServerSocket::ProcessNoticeReadDBUser(CSocketHandle &sockHandle, void *pBuffer)
  1055. {
  1056. EnterCriticalSection( &m_csProcessData );
  1057. CDBInterface::GetInstancePtr()->UnInitUserRoleInfo();
  1058. CDBInterface::GetInstancePtr()->InitUserRoleInfo();
  1059. LeaveCriticalSection( &m_csProcessData );
  1060. return 0;
  1061. }
  1062. int CServerSocket::ProcessNoticeTestAlarm(CSocketHandle &sockHandle, void *pBuffer)
  1063. {
  1064. char chTel[MAX_TEL_LENGTH] = {0};
  1065. char chMobile[MAX_TEL_LENGTH] = {0};
  1066. char chFax[MAX_TEL_LENGTH] = {0};
  1067. char chEmail[MAX_EMAIL_LENGTH] = {0};
  1068. list<string> list_tel, list_mobile, list_fax, list_email;
  1069. list<string>::iterator it_tel, it_mobile, it_fax, it_email;
  1070. int nUserCount = CDBInterface::GetInstancePtr()->GetUserInfoList(list_tel, list_mobile, list_fax, list_email);
  1071. if( 0 == nUserCount )
  1072. {
  1073. LOG4C((LOG_NOTICE, "0 == nUserCount"));
  1074. return -1;
  1075. }
  1076. if( 0 == list_tel.size() )
  1077. {
  1078. LOG4C((LOG_NOTICE, "0 == list_tel.size()"));
  1079. return -1;
  1080. }
  1081. char szResultMsg[MAX_EQUIP_DESC + 1] = {0};
  1082. int nResult = -1;
  1083. memset(szResultMsg, 0, sizeof(szResultMsg));
  1084. for( it_tel = list_tel.begin(), it_mobile = list_mobile.begin(), it_fax = list_fax.begin(), it_email = list_email.begin();
  1085. it_tel != list_tel.end(), it_mobile != list_mobile.end(), it_fax != list_fax.end(), it_email != list_email.end(); )
  1086. {
  1087. strcpy(chTel, (*it_tel++).c_str());
  1088. strcpy(chMobile, (*it_mobile++).c_str());
  1089. strcpy(chFax, (*it_fax++).c_str());
  1090. strcpy(chEmail, (*it_email++).c_str());
  1091. if( g_nSendSms==1 )
  1092. {
  1093. if( strcmp(chMobile, "") != 0 )
  1094. {
  1095. CString strMobileTel;
  1096. strMobileTel.Format("%s",chMobile );
  1097. if( strMobileTel.GetLength()!=11 ) continue; //判断是不是手机号码 只根据长度
  1098. if ( pSmsDllSendSms && g_bSuccesOpen)
  1099. {
  1100. nResult = pSmsDllSendSms(chMobile, (char *)(LPCTSTR)g_strHintSysRun, szResultMsg);
  1101. }
  1102. if( nResult != 0 ) // 发送失败
  1103. {
  1104. }
  1105. }
  1106. }
  1107. if( g_nSendNotice == 1 )
  1108. {
  1109. if( strcmp(chTel, "") != 0 )
  1110. {
  1111. //LOG4C((LOG_NOTICE, "chTel = %s insertSendNoticeList", chTel));
  1112. // 继续加入语音发送list
  1113. EnterCriticalSection( &g_csVarNotice );
  1114. InsertSendNoticeList("0.0.0.0", 1, chTel, (char *)(LPCTSTR)g_strHintSysRun,0);
  1115. LeaveCriticalSection( &g_csVarNotice );
  1116. }
  1117. #if 0 //移动电话 不打电话
  1118. if( strcmp(chMobile, "") != 0 )
  1119. {
  1120. //LOG4C((LOG_NOTICE, "chMobile = %s insertSendNoticeList", chMobile));
  1121. // 继续加入语音发送list
  1122. EnterCriticalSection( &g_csVarNotice );
  1123. InsertSendNoticeList("0.0.0.0", 1, chMobile, (char *)(LPCTSTR)g_strHintSysRun);
  1124. LeaveCriticalSection( &g_csVarNotice );
  1125. }
  1126. #endif
  1127. }
  1128. }
  1129. for( it_tel = list_tel.begin(), it_mobile = list_mobile.begin(), it_fax = list_fax.begin(), it_email = list_email.begin();
  1130. it_tel != list_tel.end(), it_mobile != list_mobile.end(), it_fax != list_fax.end(), it_email != list_email.end(); )
  1131. {
  1132. list_tel.erase(it_tel++);
  1133. list_mobile.erase(it_mobile++);
  1134. list_fax.erase(it_fax++);
  1135. list_email.erase(it_email++);
  1136. }
  1137. return 0;
  1138. }
  1139. void CServerSocket::AutoSendDataToAllClient( CBaseVar *pBaseVar )
  1140. {
  1141. int nResult = 0;
  1142. if (m_nSockType == SOCK_TCP)
  1143. {
  1144. // unsafe access to Socket list!
  1145. #ifdef SOCKHANDLE_USE_OVERLAPPED
  1146. const SocketBufferList& sl = m_SocketServer.GetSocketList();
  1147. for(SocketBufferList::const_iterator citer = sl.begin(); citer != sl.end(); ++citer)
  1148. #else
  1149. const SocketList& sl = m_SocketServer.GetSocketList();
  1150. for(SocketList::const_iterator citer = sl.begin(); citer != sl.end(); ++citer)
  1151. #endif
  1152. {
  1153. //LOG4C((LOG_NOTICE, "Socket number = %d ", sl.size()));
  1154. EnterCriticalSection( &m_csProcessData );
  1155. CSocketHandle sockHandle;
  1156. sockHandle.m_nPendingSize = 0;
  1157. memset(sockHandle.m_PendingBuffer, 0, SOCKET_BUFFSIZE);
  1158. sockHandle.Attach( (*citer) );
  1159. //if( sockAddr == addr )
  1160. {
  1161. ProcessAutoSendVarDataToClient( sockHandle,pBaseVar );
  1162. }
  1163. sockHandle.Detach();
  1164. LeaveCriticalSection( &m_csProcessData );
  1165. }
  1166. }
  1167. else
  1168. {
  1169. SockAddrIn servAddr, sockAddr;
  1170. m_SocketServer->GetSockName(servAddr);
  1171. GetDestination(sockAddr);
  1172. if ( servAddr != sockAddr ) {
  1173. //m_SocketServer.Write((const LPBYTE)(T2CA(strMsg)), strMsg.GetLength(), sockAddr);
  1174. } else {
  1175. //AppendText( _T("Please change the port number to send message to a client.\r\n") );
  1176. }
  1177. }
  1178. }
  1179. //主动发变量的值给客户端 没有简单变量,特殊变量之分
  1180. int CServerSocket::ProcessAutoSendVarDataToClient(CSocketHandle &sockHandle, CBaseVar *pBaseVar )
  1181. {
  1182. ReqSingleVarData tmpReqSnVar;
  1183. memset(&tmpReqSnVar, 0, sizeof(ReqSingleVarData));
  1184. tmpReqSnVar.Header.nCmd = CMD_READ_SINGLE_VAR_DATA;
  1185. tmpReqSnVar.Header.nCmdType = 0;
  1186. strcpy((char *)tmpReqSnVar.DevUid, (char *)(LPCTSTR)pBaseVar->m_strVarUID);
  1187. tmpReqSnVar.nVarID = pBaseVar->m_nVarID;
  1188. tmpReqSnVar.nStatus = pBaseVar->m_nStatus;
  1189. tmpReqSnVar.Data = pBaseVar->m_dbData;
  1190. tmpReqSnVar.Header.Verify = 0;
  1191. tmpReqSnVar.Header.nLen = sizeof(ReqSingleVarData);
  1192. int nSendLen = tmpReqSnVar.Header.nLen;
  1193. tmpReqSnVar.Header.Verify = CalcCheckSum( &tmpReqSnVar, nSendLen );
  1194. #if 0
  1195. if( g_pTcpServer != NULL )
  1196. {
  1197. g_pTcpServer->SocketSend(sockHandle, (unsigned char *)&tmpReqSnVar, nSendLen);
  1198. }
  1199. #else
  1200. SocketSend(sockHandle, (unsigned char *)&tmpReqSnVar, nSendLen);
  1201. #endif
  1202. return 0;
  1203. }
  1204. //有报警,自动发给客户端
  1205. int CServerSocket::ProcessAutoSendAlarmDataToClient( CSocketHandle &sockHandle,CHAR *pDevUid, int iVarID, int iStatus,CHAR *pContent
  1206. ,double dbParaValue, int nUpperLimit,int nLowerLimit, int nNormalState,int nAlarmState,int nAlarmIndex )
  1207. {
  1208. AutoSendAlarmData tmpAlarmData;
  1209. memset(&tmpAlarmData, 0, sizeof(AutoSendAlarmData));
  1210. tmpAlarmData.Header.nCmd = CMD_ALARM_VAR_DATA;
  1211. tmpAlarmData.Header.nCmdType = 0;
  1212. strcpy( (char *)tmpAlarmData.DevUid, pDevUid );
  1213. tmpAlarmData.nVarID = iVarID;
  1214. strcpy( (char *)tmpAlarmData.sContent, pContent );
  1215. tmpAlarmData.nStatus = iStatus;
  1216. tmpAlarmData.Data = dbParaValue;
  1217. tmpAlarmData.nUpperLimit = nUpperLimit;
  1218. tmpAlarmData.nLowerLimit = nLowerLimit;
  1219. tmpAlarmData.nNormalState = nNormalState;
  1220. tmpAlarmData.nAlarmState = nAlarmState;
  1221. tmpAlarmData.nAlarmIndex = nAlarmIndex;
  1222. tmpAlarmData.Header.Verify = 0;
  1223. tmpAlarmData.Header.nLen = sizeof(AutoSendAlarmData);
  1224. int nSendLen = tmpAlarmData.Header.nLen;
  1225. tmpAlarmData.Header.Verify = CalcCheckSum( &tmpAlarmData, nSendLen );
  1226. SocketSend(sockHandle, (unsigned char *)&tmpAlarmData, nSendLen);
  1227. return 0;
  1228. }
  1229. void CServerSocket::AutoSendAlarmDataToClient( CHAR *pDevUid, int iVarID, int iStatus,CHAR *pContent,double dbParaValue,
  1230. int nUpperLimit,int nLowerLimit, int nNormalState,int nAlarmState,int nAlarmIndex )
  1231. {
  1232. int nResult = 0;
  1233. if (m_nSockType == SOCK_TCP)
  1234. {
  1235. // unsafe access to Socket list!
  1236. #ifdef SOCKHANDLE_USE_OVERLAPPED
  1237. const SocketBufferList& sl = m_SocketServer.GetSocketList();
  1238. for(SocketBufferList::const_iterator citer = sl.begin(); citer != sl.end(); ++citer)
  1239. #else
  1240. const SocketList& sl = m_SocketServer.GetSocketList();
  1241. for(SocketList::const_iterator citer = sl.begin(); citer != sl.end(); ++citer)
  1242. #endif
  1243. {
  1244. //LOG4C((LOG_NOTICE, "Socket number = %d ", sl.size()));
  1245. EnterCriticalSection( &m_csProcessData );
  1246. CSocketHandle sockHandle;
  1247. sockHandle.m_nPendingSize = 0;
  1248. memset(sockHandle.m_PendingBuffer, 0, SOCKET_BUFFSIZE);
  1249. sockHandle.Attach( (*citer) );
  1250. //if( sockAddr == addr )
  1251. {
  1252. ProcessAutoSendAlarmDataToClient( sockHandle,pDevUid,iVarID,iStatus,pContent,dbParaValue, nUpperLimit,nLowerLimit, nNormalState,nAlarmState,nAlarmIndex );
  1253. }
  1254. sockHandle.Detach();
  1255. LeaveCriticalSection( &m_csProcessData );
  1256. }
  1257. }
  1258. else
  1259. {
  1260. SockAddrIn servAddr, sockAddr;
  1261. m_SocketServer->GetSockName(servAddr);
  1262. GetDestination(sockAddr);
  1263. if ( servAddr != sockAddr ) {
  1264. //m_SocketServer.Write((const LPBYTE)(T2CA(strMsg)), strMsg.GetLength(), sockAddr);
  1265. } else {
  1266. //AppendText( _T("Please change the port number to send message to a client.\r\n") );
  1267. }
  1268. }
  1269. }
  1270. };