DataSyncSendDlg.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. // DataSyncSendDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "DataSyncSend.h"
  5. #include "DataSyncSendDlg.h"
  6. #include "DataSyncSendDlg2.h"
  7. #include <afxsock.h>//<Winsock2.h>
  8. #include "ConnectDlg.h"
  9. #include "SendDataDlg.h"
  10. #include "AddContact.h"
  11. #include "SendReg.h"
  12. #include <Mmsystem.h>
  13. #include <afxinet.h>
  14. #include "CalendarConvert.h"
  15. #include "AccessRights.h"
  16. #pragma comment(lib, "Winmm.lib")
  17. #ifdef _DEBUG
  18. #define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22. #define DISCONNECT_TIMER (WM_USER + 101)
  23. #define LOGINFAILED_TIMER (WM_USER + 102)
  24. #define IPC_CUSTOM_MSG2 _T("{D4F876E2-w78F-22D5-q98A-Y0B0D07B847C}")
  25. #define WM_ICON_NOTIFY WM_USER+12
  26. UINT g_wmClose = RegisterWindowMessage(IPC_CUSTOM_MSG2);
  27. CString g_enterprisename="";
  28. CDataSyncSendDlg2* g_pMainWnd2=NULL;
  29. DWORD g_nSendCode=0; //g_bSendOK
  30. /////////////////////////////////////////////////////////////////////////////
  31. HWND g_hSendWnd;
  32. CArray<CStringArray, CStringArray> g_List1array;
  33. DWORD g_nLeng=0;
  34. BYTE* g_pData=NULL;
  35. CString g_str="";
  36. BOOL g_bReturned=0;
  37. BOOL g_bReturned2=0;
  38. extern HWND g_hConnectWnd;
  39. extern BOOL g_bSendData;
  40. extern DWORD FindAppProcessID(CString path);
  41. extern void DataToArray(CArray<CStringArray, CStringArray>*List1array);
  42. extern void DataToArray(CArray<CStringArray, CStringArray>*List1array, CArray<CStringArray, CStringArray>*List2array);
  43. void MyGetIPByName(CString &name)
  44. {
  45. CString strIP=name;
  46. name.Empty ();
  47. DWORD dwServerIP=0;
  48. HOSTENT *host = gethostbyname(strIP);
  49. struct in_addr addr;
  50. if (host != NULL)
  51. {
  52. for(int i = 0; host-> h_addr_list[i] != NULL; i ++)
  53. {
  54. memset(&addr, 0, sizeof(addr));
  55. memcpy(&addr.S_un.S_addr, host-> h_addr_list[i], host-> h_length);
  56. dwServerIP = ntohl(addr.S_un.S_addr);
  57. BYTE* pIP = (BYTE*)&dwServerIP;
  58. name.Format(_T("%d.%d.%d.%d"), pIP[3], pIP[2], pIP[1], pIP[0]);
  59. }
  60. }
  61. }
  62. void WriteLogin(CString str)
  63. {
  64. CStdioFile fp;
  65. CString path=g_mainpath+"\\DataSyncSend.log";
  66. if(::PathFileExists (path))
  67. {
  68. fp.Open (path, CFile::modeWrite);
  69. int length=fp.GetLength ();
  70. if(length>1024*1024)
  71. {
  72. fp.Close ();
  73. ::DeleteFile (path);
  74. return;
  75. }
  76. fp.SeekToEnd ();
  77. }
  78. else
  79. fp.Open (path, CFile::modeCreate|CFile::modeWrite);
  80. fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S")+" "+str+"\r\n" );
  81. fp.Close ();
  82. }
  83. unsigned __stdcall SendMsgThread(LPVOID lpParam)
  84. {
  85. BOOL nNeedConn=1;//信息服务器
  86. int i;
  87. CString str;
  88. BYTE *pData;
  89. BOOL bDouble=0;
  90. BOOL bfirst=1;
  91. WriteLogin("服务启动");
  92. while(1)
  93. {
  94. ll: if(g_pMainWnd->m_bTerminate)
  95. break;
  96. if(nNeedConn)
  97. {
  98. WriteLogin("连接服务器");
  99. if(g_pMainWnd->OnConnect()==0)
  100. {
  101. nNeedConn=1;
  102. ::Sleep (1500);
  103. goto ll;
  104. }
  105. else
  106. {
  107. WriteLogin("连接成功");
  108. nNeedConn=0;
  109. }
  110. int aa=100;
  111. while(g_pMainWnd->m_dwConnectionID==INVALID_SOCKET)
  112. {
  113. ::Sleep (50);
  114. aa--;
  115. if(aa==0)break;
  116. }
  117. }
  118. if(bDouble)
  119. {
  120. str=g_sdomain;
  121. pData=new BYTE[str.GetLength ()];
  122. memcpy(pData, str.GetBuffer (0), str.GetLength ());
  123. str.ReleaseBuffer ();
  124. EncryptFile2(pData, str.GetLength (), "888666333");
  125. g_nSendCode=8;
  126. WriteLogin("获取分店信息");
  127. g_pMainWnd->ProcessChatMessageRequest2(pData, str.GetLength ());
  128. for(i=0; i<20; i++)
  129. {
  130. if(g_bReturned)break;
  131. ::Sleep (1500);
  132. }
  133. delete []pData;
  134. if(g_bReturned==0)
  135. {
  136. WriteLogin("获取分店信息失败");
  137. nNeedConn=1;
  138. goto ll;
  139. }
  140. bDouble = !bDouble;
  141. }
  142. else if(g_enterprisename!="")//总店才能传权限
  143. {
  144. CString version = _T ("Version 1.0");
  145. BOOL needupload=AfxGetApp()->GetProfileInt (version, "needupload", 1);
  146. if(needupload)
  147. {
  148. MyLock lock("savebranchrights");
  149. CFile fp;
  150. CString path=g_mainpath+"\\branchrights.dat";
  151. if(fp.Open (path, CFile::modeRead))
  152. {
  153. DWORD leng=fp.GetLength ();
  154. pData=new BYTE[leng];
  155. fp.Read (pData,leng);
  156. fp.Close ();
  157. g_nSendCode=9;
  158. g_pMainWnd->ProcessChatMessageRequest2(pData,leng);
  159. for(i=0; i<20; i++)
  160. {
  161. if(g_bReturned)break;
  162. ::Sleep (1500);
  163. }
  164. delete []pData;
  165. if(g_bReturned==0)
  166. {
  167. nNeedConn=1;
  168. goto ll;
  169. }
  170. else
  171. {
  172. if(g_str != "set ok")
  173. goto ll;
  174. CString version = _T ("Version 1.0");
  175. AfxGetApp()->WriteProfileInt (version, "needupload", 0);
  176. }
  177. }
  178. else
  179. {
  180. CString version = _T ("Version 1.0");
  181. AfxGetApp()->WriteProfileInt (version, "needupload", 0);
  182. bDouble=!bDouble;
  183. goto ll;
  184. }
  185. }
  186. bDouble = !bDouble;
  187. }
  188. else
  189. {
  190. bDouble=!bDouble;
  191. goto ll;
  192. }
  193. if(bfirst)
  194. {
  195. bfirst=0;
  196. goto ll;
  197. }
  198. for(i=0; i<3*60; i++)
  199. {
  200. if(g_pMainWnd->m_bTerminate)
  201. break;
  202. ::Sleep (1000);
  203. }
  204. }
  205. g_pMainWnd->m_bRunning=false;
  206. g_pMainWnd->m_bTerminate=false;
  207. _endthreadex( 0 );
  208. return 0;
  209. }
  210. //MessageBox
  211. /////////////////////////////////////////////////////////////////////////////
  212. static void HandleClientNetEvent(IN SOCKET hSocket, IN ETransportEvent eEvent,
  213. IN void *pDataBuf, IN unsigned long nDataLen,
  214. IN int nError, IN void *pContext)
  215. {
  216. if( nError != TRANSPORT_OK ) return;
  217. CDataSyncSendDlg *pDlg = (CDataSyncSendDlg *)pContext;
  218. if( NULL == pDlg ) return;
  219. pDlg->ProcessNetEvent(eEvent, pDataBuf, nDataLen);
  220. }
  221. CDataSyncSendDlg::CDataSyncSendDlg(CWnd* pParent /*=NULL*/)
  222. : CDialog(CDataSyncSendDlg::IDD, pParent)
  223. {
  224. //{{AFX_DATA_INIT(CDataSyncSendDlg)
  225. //}}AFX_DATA_INIT
  226. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  227. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  228. // g_pMainWnd2=new CDataSyncSendDlg2;
  229. // g_pMainWnd2->Create(IDD_DataSyncSend_DIALOG2, this);
  230. m_bTrayCreate=0;
  231. }
  232. void CDataSyncSendDlg::DoDataExchange(CDataExchange* pDX)
  233. {
  234. CDialog::DoDataExchange(pDX);
  235. //{{AFX_DATA_MAP(CDataSyncSendDlg)
  236. //}}AFX_DATA_MAP
  237. }
  238. BEGIN_MESSAGE_MAP(CDataSyncSendDlg, CDialog)
  239. //{{AFX_MSG_MAP(CDataSyncSendDlg)
  240. ON_WM_PAINT()
  241. ON_WM_QUERYDRAGICON()
  242. ON_WM_DESTROY()
  243. ON_WM_TIMER()
  244. ON_COMMAND(IDM_CLOSE, OnClose)
  245. ON_COMMAND(IDM_DataSyncSend, OnDataSyncSend)
  246. //}}AFX_MSG_MAP
  247. ON_MESSAGE(WM_ICON_NOTIFY, OnTrayNotification)
  248. END_MESSAGE_MAP()
  249. /////////////////////////////////////////////////////////////////////////////
  250. BOOL CDataSyncSendDlg::OnInitDialog()
  251. {
  252. SetWindowPos(&wndNoTopMost,0,0,0,0,SWP_HIDEWINDOW);
  253. ModifyStyleEx(WS_EX_APPWINDOW,WS_EX_TOOLWINDOW);
  254. CDialog::OnInitDialog();
  255. // Set the icon for this dialog. The framework does this automatically
  256. // when the application's main window is not a dialog
  257. SetIcon(m_hIcon, TRUE); // Set big icon
  258. SetIcon(m_hIcon, FALSE); // Set small icon
  259. g_pMainWnd=this;
  260. SetTimer(1, 100, NULL);
  261. /////////////////////////////////
  262. // TODO: Add extra initialization here
  263. return TRUE; // return TRUE unless you set the focus to a control
  264. }
  265. // If you add a minimize button to your dialog, you will need the code below
  266. // to draw the icon. For MFC applications using the document/view model,
  267. // this is automatically done for you by the framework.
  268. void CDataSyncSendDlg::OnPaint()
  269. {
  270. if (IsIconic())
  271. {
  272. CPaintDC dc(this); // device context for painting
  273. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  274. // Center icon in client rectangle
  275. int cxIcon = GetSystemMetrics(SM_CXICON);
  276. int cyIcon = GetSystemMetrics(SM_CYICON);
  277. CRect rect;
  278. GetClientRect(&rect);
  279. int x = (rect.Width() - cxIcon + 1) / 2;
  280. int y = (rect.Height() - cyIcon + 1) / 2;
  281. // Draw the icon
  282. dc.DrawIcon(x, y, m_hIcon);
  283. }
  284. else
  285. {
  286. CDialog::OnPaint();
  287. }
  288. }
  289. // The system calls this to obtain the cursor to display while the user drags
  290. // the minimized window.
  291. HCURSOR CDataSyncSendDlg::OnQueryDragIcon()
  292. {
  293. return (HCURSOR) m_hIcon;
  294. }
  295. DWORD CDataSyncSendDlg::GetConnectionID()
  296. {
  297. return m_dwConnectionID;
  298. }
  299. void CDataSyncSendDlg::SetConnectionID(DWORD dwConnection)
  300. {
  301. m_dwConnectionID = dwConnection;
  302. }
  303. void CDataSyncSendDlg::ProcessChatMessageRequest2(CString sql)
  304. {
  305. const char *szDataBuf = sql.GetBuffer(0);
  306. sql.ReleaseBuffer();
  307. ProcessChatMessageRequest((void*)szDataBuf, strlen(szDataBuf) + 1);
  308. }
  309. void CDataSyncSendDlg::ProcessChatMessageRequest2(BYTE *pData, int length)
  310. {
  311. ProcessChatMessageRequest((void*)pData, length);
  312. }
  313. BOOL CDataSyncSendDlg::ProcessChatMessageRequest(void *szDataBuf, int nDataLen)
  314. {
  315. g_bReturned=0;
  316. DWORD dwFromUserID = GetConnectionID();
  317. WORD wMessageId = MSG_CHATMESSAGE_REQ;
  318. DWORD dwDataLen = sizeof(TCHAT_MESSAGE_STRU) + nDataLen;
  319. BYTE *pSendData = new BYTE[dwDataLen];
  320. TCHAT_MESSAGE_STRU *pChatMessage=(TCHAT_MESSAGE_STRU*)pSendData;
  321. memset(pChatMessage, 0x00, dwDataLen);
  322. g_sendhead.length[98]=987123768;
  323. memcpy(pSendData, &g_sendhead, sizeof(g_sendhead));
  324. pChatMessage->tCommonMsg.dwConnectionID = GetConnectionID();
  325. pChatMessage->tCommonMsg.wMessageId = wMessageId;
  326. pChatMessage->dwFromUserID = dwFromUserID;
  327. pChatMessage->dwToUserID = g_nSendCode;
  328. pChatMessage->wMessageLen = nDataLen;
  329. memcpy(pChatMessage->byFileContent, szDataBuf, nDataLen);
  330. TMessageHeader tHeader = {0};
  331. tHeader.wMessageId = wMessageId;
  332. tHeader.dwDataLen = dwDataLen;
  333. BOOL bRet=0;
  334. unsigned long ulSendLen = m_tClientTunnel.net_Send(&tHeader, (void *)pChatMessage, dwDataLen);
  335. if( ulSendLen != SOCKET_ERROR )
  336. {
  337. bRet=1;
  338. }
  339. delete [] pSendData;
  340. return bRet;
  341. }
  342. BOOL CDataSyncSendDlg::OnConnect()
  343. {
  344. OnDisconnect();
  345. g_server="lijiafz.vicp.net" ;
  346. MyGetIPByName(g_server);
  347. if(g_server.IsEmpty ())return OnConnect2();
  348. CString strServerAddress = g_server;
  349. char *szServerAddress = strServerAddress.GetBuffer(0);
  350. strServerAddress.ReleaseBuffer();
  351. /*for Client, IP and Port can be 0,
  352. and for Server, IP is LocalIP, and Port is Listening Port*/
  353. int nResult = m_tClientTunnel.net_OpenSocket(Transport_Client,
  354. 0,
  355. HandleClientNetEvent,
  356. this);
  357. if( TRANSPORT_OK != nResult )
  358. {
  359. return 0;
  360. }
  361. DWORD dwRemoteIPValue = inet_addr(szServerAddress);
  362. WORD wPort = GetPort();
  363. nResult = m_tClientTunnel.net_Connect(dwRemoteIPValue, wPort);
  364. if( TRANSPORT_OK != nResult )
  365. {
  366. m_tClientTunnel.net_CloseSocket();
  367. return OnConnect2();
  368. }
  369. return ProcessLoginRequest();
  370. }
  371. BOOL CDataSyncSendDlg::OnConnect2()
  372. {
  373. CString server="http://www.lyfz.net/download/ip.txt";
  374. char buf[MAX_PATH];
  375. ::GetTempPath(sizeof(buf),buf);
  376. CString m_strTempDir=buf;
  377. DWORD m_dwHttpRequestFlags=HSR_DOWNLOAD | INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_AUTO_REDIRECT;
  378. INTERNET_PORT m_dwPort;
  379. DWORD dwType;
  380. CInternetSession m_cis;
  381. CHttpConnection *m_pHttp;
  382. CString m_strServer;
  383. CString m_strIniPath;
  384. AfxParseURL(server,dwType,m_strServer,m_strIniPath,m_dwPort);
  385. if(dwType!=AFX_INET_SERVICE_HTTP)
  386. {
  387. return OnConnect3();
  388. }
  389. m_cis.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,5);
  390. m_pHttp=m_cis.GetHttpConnection(m_strServer,m_dwPort);
  391. try
  392. {
  393. m_pHttp=m_cis.GetHttpConnection(m_strServer,m_dwPort);
  394. }
  395. catch(CInternetException* pEx)
  396. {
  397. return OnConnect3();
  398. }
  399. CHttpFile *pFile = m_pHttp->OpenRequest(CHttpConnection::HTTP_VERB_GET,
  400. m_strIniPath, NULL, 1, NULL, NULL, m_dwHttpRequestFlags);
  401. try
  402. {
  403. pFile->SendRequest();
  404. }
  405. catch(CInternetException* pEx)
  406. {
  407. pFile->Close();
  408. return OnConnect3();
  409. }
  410. if(pFile)
  411. {
  412. CStdioFile csf;
  413. csf.Open(m_strTempDir+"\\ip.txt",CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
  414. char buf[2048];
  415. int n;
  416. while((n=pFile->Read(buf,2048))>0)
  417. csf.Write(buf,n);
  418. csf.Close();
  419. pFile->Close();
  420. }
  421. CStdioFile fp;
  422. fp.Open (m_strTempDir+"\\ip.txt", CFile::modeRead);
  423. fp.ReadString (g_server);
  424. fp.Close ();
  425. CString strServerAddress = g_server;
  426. char *szServerAddress = strServerAddress.GetBuffer(0);
  427. strServerAddress.ReleaseBuffer();
  428. /*for Client, IP and Port can be 0,
  429. and for Server, IP is LocalIP, and Port is Listening Port*/
  430. int nResult = m_tClientTunnel.net_OpenSocket(Transport_Client,
  431. 0,
  432. HandleClientNetEvent,
  433. this);
  434. if( TRANSPORT_OK != nResult )
  435. {
  436. return 0;
  437. }
  438. DWORD dwRemoteIPValue = inet_addr(szServerAddress);
  439. WORD wPort = GetPort();
  440. nResult = m_tClientTunnel.net_Connect(dwRemoteIPValue, wPort);
  441. if( TRANSPORT_OK != nResult )
  442. {
  443. m_tClientTunnel.net_CloseSocket();
  444. return OnConnect3();
  445. }
  446. return ProcessLoginRequest();
  447. }
  448. BOOL CDataSyncSendDlg::OnConnect3()
  449. {
  450. CString server="http://photo.bjlyfz.com/download/ip.txt";
  451. char buf[MAX_PATH];
  452. ::GetTempPath(sizeof(buf),buf);
  453. CString m_strTempDir=buf;
  454. DWORD m_dwHttpRequestFlags=HSR_DOWNLOAD | INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_AUTO_REDIRECT;
  455. INTERNET_PORT m_dwPort;
  456. DWORD dwType;
  457. CInternetSession m_cis;
  458. CHttpConnection *m_pHttp;
  459. CString m_strServer;
  460. CString m_strIniPath;
  461. AfxParseURL(server,dwType,m_strServer,m_strIniPath,m_dwPort);
  462. if(dwType!=AFX_INET_SERVICE_HTTP)
  463. {
  464. return FALSE;
  465. }
  466. m_cis.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,5);
  467. m_pHttp=m_cis.GetHttpConnection(m_strServer,m_dwPort);
  468. try
  469. {
  470. m_pHttp=m_cis.GetHttpConnection(m_strServer,m_dwPort);
  471. }
  472. catch(CInternetException* pEx)
  473. {
  474. return false;
  475. }
  476. CHttpFile *pFile = m_pHttp->OpenRequest(CHttpConnection::HTTP_VERB_GET,
  477. m_strIniPath, NULL, 1, NULL, NULL, m_dwHttpRequestFlags);
  478. try
  479. {
  480. pFile->SendRequest();
  481. }
  482. catch(CInternetException* pEx)
  483. {
  484. pFile->Close();
  485. return false;
  486. }
  487. if(pFile)
  488. {
  489. CStdioFile csf;
  490. csf.Open(m_strTempDir+"\\ip.txt",CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
  491. char buf[2048];
  492. int n;
  493. while((n=pFile->Read(buf,2048))>0)
  494. csf.Write(buf,n);
  495. csf.Close();
  496. pFile->Close();
  497. }
  498. CStdioFile fp;
  499. fp.Open (m_strTempDir+"\\ip.txt", CFile::modeRead);
  500. fp.ReadString (g_server);
  501. fp.Close ();
  502. CString strServerAddress = g_server;
  503. char *szServerAddress = strServerAddress.GetBuffer(0);
  504. strServerAddress.ReleaseBuffer();
  505. /*for Client, IP and Port can be 0,
  506. and for Server, IP is LocalIP, and Port is Listening Port*/
  507. int nResult = m_tClientTunnel.net_OpenSocket(Transport_Client,
  508. 0,
  509. HandleClientNetEvent,
  510. this);
  511. if( TRANSPORT_OK != nResult )
  512. {
  513. return 0;
  514. }
  515. DWORD dwRemoteIPValue = inet_addr(szServerAddress);
  516. WORD wPort = GetPort();
  517. nResult = m_tClientTunnel.net_Connect(dwRemoteIPValue, wPort);
  518. if( TRANSPORT_OK != nResult )
  519. {
  520. m_tClientTunnel.net_CloseSocket();
  521. return 0;
  522. }
  523. return ProcessLoginRequest();
  524. }
  525. void CDataSyncSendDlg::ProcessNetEvent(int nEventType, void *pRecvMsg, DWORD dwDataLen)
  526. {
  527. if( Transport_ReadEv == nEventType )
  528. {
  529. if( NULL == pRecvMsg ) return;
  530. TMessageHeader* pHeader = (TMessageHeader *)pRecvMsg;
  531. char *pDataBuf = (char *)pRecvMsg + MESSAGE_HEADER_LEN;
  532. WORD dwMessageID = pHeader->wMessageId;
  533. switch(dwMessageID)
  534. {
  535. case MSG_LOGIN_RESP:
  536. {
  537. LOGIN_RESULT_STRU tLoginResult = {0};
  538. memcpy(&tLoginResult, pDataBuf, sizeof(LOGIN_RESULT_STRU));
  539. ProcessLoginResponse(&tLoginResult);
  540. break;
  541. }
  542. case MSG_USERINFO_RESP:
  543. {
  544. TUSERLIST_INFO_STRU tUserListInfo = {0};
  545. memcpy(&tUserListInfo, pDataBuf, sizeof(TUSERLIST_INFO_STRU));
  546. // ProcessUserListInfoResponse(&tUserListInfo);
  547. break;
  548. }
  549. case MSG_LOGOUT_RESP:
  550. {
  551. TUSERLIST_INFO_STRU tUserListInfo = {0};
  552. memcpy(&tUserListInfo, pDataBuf, sizeof(TUSERLIST_INFO_STRU));
  553. // ProcessLogoutResponse(&tUserListInfo);
  554. break;
  555. }
  556. case MSG_CHATMESSAGE_RESP:
  557. {
  558. TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU *)pDataBuf;
  559. ProcessChatMessageResponse((void *)pChatMessage);
  560. break;
  561. }
  562. default:
  563. {
  564. break;
  565. }
  566. }
  567. }
  568. else if( Transport_CloseEv == nEventType )
  569. {
  570. SetTimer(DISCONNECT_TIMER, 0, NULL);
  571. }
  572. ///////
  573. }
  574. void CDataSyncSendDlg::OnDisconnect()
  575. {
  576. m_tClientTunnel.net_CloseSocket();
  577. SetConnectionID(INVALID_SOCKET);
  578. }
  579. void CDataSyncSendDlg::ProcessLoginResponse(void *pLoginResult)
  580. {
  581. if( NULL == pLoginResult ) return;
  582. LOGIN_RESULT_STRU *ptLoginResult = (LOGIN_RESULT_STRU *)pLoginResult;
  583. DWORD dwConnectionID = ptLoginResult->tCommonMsg.dwConnectionID;
  584. SetConnectionID(dwConnectionID);
  585. BYTE byResult = ptLoginResult->byResult;
  586. if(LOGIN_RESULT_SUC == byResult)
  587. {
  588. }
  589. else
  590. {
  591. SetTimer(LOGINFAILED_TIMER, 0, NULL);
  592. }
  593. CString strCommentInfo("");
  594. strCommentInfo.Format("%s. 用户代号: %ld",
  595. ((LOGIN_RESULT_SUC == byResult) ? "登录成功" :
  596. (LOGIN_RESULT_MULTI == byResult) ? "重复登录" :
  597. (LOGIN_RESULT_NAMERROR == byResult) ? "名字不存在" :
  598. (LOGIN_RESULT_PWERROR == byResult) ? "密码错误" : "登录失败"),
  599. dwConnectionID);
  600. // MessageBox((CString)strCommentInfo);
  601. }
  602. CString newGUID()
  603. {
  604. CString str;
  605. GUID guid;
  606. // CoInitialize(NULL);
  607. if (S_OK == ::CoCreateGuid(&guid))
  608. {
  609. str.Format (
  610. "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
  611. guid.Data1,
  612. guid.Data2,
  613. guid.Data3,
  614. guid.Data4[0], guid.Data4[1],
  615. guid.Data4[2], guid.Data4[3],
  616. guid.Data4[4], guid.Data4[5],
  617. guid.Data4[6], guid.Data4[7]);
  618. }
  619. // CoUninitialize();
  620. return str.Left (28);
  621. }
  622. BOOL CDataSyncSendDlg::ProcessLoginRequest()
  623. {
  624. WORD wMessageId = MSG_LOGIN_REQ;
  625. TLOGIN_STRU tLogonInfo = {0};
  626. tLogonInfo.tCommonMsg.dwConnectionID = GetConnectionID();
  627. tLogonInfo.tCommonMsg.wMessageId = wMessageId;
  628. CString strUserName = newGUID();
  629. strUserName.TrimLeft();
  630. strUserName.TrimRight();
  631. strcpy(tLogonInfo.tUserInfo.szUserName, strUserName);
  632. DWORD dwDataLen = sizeof(TLOGIN_STRU);
  633. TMessageHeader tHeader = {0};
  634. tHeader.wMessageId = wMessageId;
  635. tHeader.dwDataLen = dwDataLen;
  636. unsigned long ulSendLen = m_tClientTunnel.net_Send(&tHeader, (void *)&tLogonInfo, dwDataLen);
  637. if( ulSendLen == SOCKET_ERROR )
  638. {
  639. return 0;
  640. }
  641. return 1;
  642. }
  643. //---------------------------------------------------------------------------
  644. void CDataSyncSendDlg::OnDestroy()
  645. {
  646. // g_pMainWnd2->DestroyWindow ();
  647. // delete g_pMainWnd2;
  648. OnDisconnect();
  649. Transport_UnInit();
  650. CDialog::OnDestroy();
  651. }
  652. void CDataSyncSendDlg::OnTimer(UINT nIDEvent)
  653. {
  654. // TODO: Add your message handler code here and/or call default
  655. if( DISCONNECT_TIMER == nIDEvent )
  656. {
  657. KillTimer(nIDEvent);
  658. OnDisconnect();
  659. }
  660. else if( LOGINFAILED_TIMER == nIDEvent )
  661. {
  662. KillTimer(nIDEvent);
  663. OnDisconnect();
  664. }
  665. else if( 1 == nIDEvent )
  666. {
  667. KillTimer(1);
  668. m_bTerminate=false;
  669. m_bRunning=false;
  670. m_hThread = NULL;
  671. m_bRunning=true;
  672. m_hThread = (HANDLE)_beginthreadex(NULL,0, SendMsgThread,(LPVOID)this,0/* CREATE_SUSPENDED*/,&m_dwThreadID);
  673. }
  674. }
  675. void CDataSyncSendDlg::OnClose()
  676. {
  677. // TODO: Add your command handler code here
  678. TerminateThread();
  679. CDialog::OnCancel ();
  680. }
  681. BOOL CDataSyncSendDlg::TerminateThread()
  682. {
  683. try
  684. {
  685. if ( !m_bRunning )
  686. return TRUE;
  687. m_bTerminate=true;
  688. for( ; ; )
  689. {
  690. if ( ::WaitForSingleObject(m_hThread, 0) == WAIT_OBJECT_0 )
  691. break;
  692. MSG msg;
  693. while (::PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
  694. {
  695. if (!AfxGetApp()->PumpMessage())
  696. break;
  697. }
  698. }
  699. ::CloseHandle(m_hThread);
  700. m_bRunning=false;
  701. return TRUE;
  702. }
  703. catch(...)
  704. {
  705. }
  706. }
  707. void CDataSyncSendDlg::ProcessChatMessageResponse(void *pResponse)
  708. {
  709. if( NULL == pResponse ) return;
  710. TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU *)pResponse;
  711. int nMessageLen = pChatMessage->wMessageLen;
  712. DWORD dwToUserID = pChatMessage->dwToUserID;
  713. memcpy( &g_sendhead, pChatMessage, sizeof(g_sendhead));
  714. g_bReturned=1;
  715. g_str = pChatMessage->byFileContent;
  716. if(g_str=="set ok")
  717. {
  718. WriteLogin("设置访问权限成功");
  719. return;
  720. }
  721. if(nMessageLen > 10)
  722. {
  723. if(g_str.Find ("没有此企业的资料")!=-1)
  724. {
  725. WriteLogin("没有此企业的资料");
  726. return;
  727. }
  728. WriteLogin("获取分店信息成功");
  729. MyLock lock("savebranchinfo");
  730. CFile fp;
  731. CString path=g_mainpath+"\\branchinfo.dat";
  732. fp.Open (path, CFile::modeCreate|CFile::modeWrite);
  733. DWORD arraysize=g_sendhead.count[0];
  734. fp.Write (&arraysize, sizeof(DWORD));
  735. fp.Write(pChatMessage->byFileContent, nMessageLen);
  736. DWORD flag=889900;
  737. fp.Write (&flag, sizeof(DWORD));
  738. fp.Close ();
  739. if(g_pData)
  740. delete [] g_pData;
  741. g_pData=NULL;
  742. g_pData = new BYTE[nMessageLen];
  743. memcpy(g_pData, pChatMessage->byFileContent, nMessageLen);
  744. g_nLeng = nMessageLen;
  745. DataToArray(&g_List1array);
  746. g_enterprisename.Empty ();
  747. for(int i=0; i<g_List1array.GetSize(); i++)
  748. {
  749. if(g_sdomain == g_List1array.ElementAt (i).ElementAt (0))
  750. {
  751. if("总店" == g_List1array.ElementAt (i).ElementAt (3) )
  752. {
  753. g_enterprisename=g_List1array.ElementAt (i).ElementAt (4);
  754. if(g_enterprisename!="")
  755. {
  756. if(m_bTrayCreate == 0 && g_bSendData == 0)
  757. {
  758. m_bTrayCreate = 1;
  759. m_TrayIcon.Create(this,WM_ICON_NOTIFY, "数据同步服务" , m_hIcon, IDR_MENUtray); //构造
  760. }
  761. }
  762. }
  763. else if(m_bTrayCreate)
  764. {
  765. m_bTrayCreate=0;
  766. m_TrayIcon.RemoveIcon();
  767. }
  768. else
  769. m_bTrayCreate=0;
  770. }
  771. }
  772. }
  773. }
  774. LRESULT CDataSyncSendDlg::OnTrayNotification(WPARAM wParam,LPARAM lParam)
  775. {
  776. return m_TrayIcon.OnTrayNotification(wParam,lParam);
  777. }
  778. BOOL g_bShowDlg=0;
  779. void CDataSyncSendDlg::OnDataSyncSend()
  780. {
  781. // TODO: Add your command handler code here
  782. {
  783. MyLock lock("savebranchinfo");
  784. CFile fp;
  785. CString path=g_mainpath+"\\branchinfo.dat";
  786. if(::PathFileExists (path)==0)return;
  787. fp.Open (path, CFile::modeRead);
  788. DWORD leng=fp.GetLength ();
  789. if(leng<sizeof(DWORD)*2)
  790. {
  791. fp.Close ();return;
  792. }
  793. DWORD flag;
  794. fp.Seek(-sizeof(DWORD),CFile::end);
  795. fp.Read (&flag, sizeof(DWORD));
  796. if(flag!=889900)
  797. {
  798. fp.Close ();return;
  799. }
  800. fp.SeekToBegin ();
  801. DWORD arraysize;
  802. fp.Read (&arraysize, sizeof(DWORD));
  803. BYTE *pData=new BYTE[leng-sizeof(DWORD)];
  804. fp.Read (pData,leng-sizeof(DWORD));
  805. fp.Close ();
  806. CMemFile memfile;
  807. memfile.Attach (pData,leng-sizeof(DWORD));
  808. CArchive ar(&memfile, CArchive::load);
  809. g_List1array.SetSize(arraysize);
  810. for(int ii=0; ii<g_List1array.GetSize (); ii++)
  811. {
  812. g_List1array.ElementAt (ii).Serialize (ar);
  813. }
  814. ar.Close();
  815. memfile.Detach ();
  816. delete []pData;
  817. if(g_List1array.GetSize ()==0)return;
  818. }
  819. g_sbranchname.Empty ();
  820. for(int i=0; i<g_List1array.GetSize (); i++)
  821. {
  822. if(g_sdomain==g_List1array.ElementAt (i).ElementAt (0))
  823. {
  824. g_sbranchname=g_List1array.ElementAt (i).ElementAt (1);
  825. }
  826. }
  827. if(g_sbranchname.IsEmpty ())
  828. {
  829. return;
  830. }
  831. g_bShowDlg=1;
  832. AccessRights dlg;
  833. dlg.DoModal();
  834. g_bShowDlg=0;
  835. }
  836. void CDataSyncSendDlg::DelTrayIcon()
  837. {
  838. if(m_bTrayCreate)
  839. {
  840. m_bTrayCreate=0;
  841. m_TrayIcon.RemoveIcon();
  842. }
  843. }
  844. int CDataSyncSendDlg::GetPort()
  845. {
  846. static int aaa=-1;
  847. if(aaa==-1)
  848. {
  849. aaa=CTime::GetCurrentTime ().GetSecond ()%7;
  850. }
  851. aaa++;
  852. if(aaa>=7)aaa=0;
  853. return 8387+aaa;
  854. }