DataSyncSendDlg2.cpp 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. // DataSyncSendDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "DataSyncSend.h"
  5. #include "DataSyncSendDlg2.h"
  6. #include <afxsock.h>//<Winsock2.h>
  7. #include "ConnectDlg.h"
  8. #include "SendDataDlg.h"
  9. #include "AddContact.h"
  10. #include "Lzari.h"
  11. #include "SendReg.h"
  12. #include "zlib.h"
  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. #pragma comment(lib, "ZLib.lib")
  19. // #define CONNTEST 1 //测试
  20. CString g_sendip;
  21. extern void WriteLogin(CString str);
  22. extern DWORD g_nSendCode;
  23. extern BOOL g_bReturned2;
  24. /////////////////////////////////////////////////////////////////////////////
  25. extern HWND g_hSendWnd;
  26. extern HWND g_hConnectWnd;
  27. #define DISCONNECT_TIMER (WM_USER + 101)
  28. #define LOGINFAILED_TIMER (WM_USER + 102)
  29. /////////////////////////////////////////////////////////////////////////////
  30. static void HandleClientNetEvent2(IN SOCKET hSocket, IN ETransportEvent eEvent,
  31. IN void *pDataBuf, IN unsigned long nDataLen,
  32. IN int nError, IN void *pContext)
  33. {
  34. if( nError != TRANSPORT_OK ) return;
  35. CDataSyncSendDlg2 *pDlg = (CDataSyncSendDlg2 *)pContext;
  36. if( NULL == pDlg ) return;
  37. pDlg->ProcessNetEvent(eEvent, pDataBuf, nDataLen);
  38. }
  39. CDataSyncSendDlg2::CDataSyncSendDlg2(CWnd* pParent /*=NULL*/)
  40. : CDialog(CDataSyncSendDlg2::IDD, pParent)
  41. {
  42. //{{AFX_DATA_INIT(CDataSyncSendDlg2)
  43. //}}AFX_DATA_INIT
  44. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  45. }
  46. void CDataSyncSendDlg2::DoDataExchange(CDataExchange* pDX)
  47. {
  48. CDialog::DoDataExchange(pDX);
  49. //{{AFX_DATA_MAP(CDataSyncSendDlg2)
  50. //}}AFX_DATA_MAP
  51. }
  52. BEGIN_MESSAGE_MAP(CDataSyncSendDlg2, CDialog)
  53. //{{AFX_MSG_MAP(CDataSyncSendDlg2)
  54. ON_WM_DESTROY()
  55. ON_WM_TIMER()
  56. //}}AFX_MSG_MAP
  57. END_MESSAGE_MAP()
  58. /////////////////////////////////////////////////////////////////////////////
  59. BOOL CDataSyncSendDlg2::OnInitDialog()
  60. {
  61. CDialog::OnInitDialog();
  62. // Set the icon for this dialog. The framework does this automatically
  63. // when the application's main window is not a dialog
  64. // SetTimer(1, 30000, NULL);
  65. // TODO: Add extra initialization here
  66. return TRUE; // return TRUE unless you set the focus to a control
  67. }
  68. DWORD CDataSyncSendDlg2::GetConnectionID()
  69. {
  70. return m_dwConnectionID;
  71. }
  72. void CDataSyncSendDlg2::SetConnectionID(DWORD dwConnection)
  73. {
  74. m_dwConnectionID = dwConnection;
  75. }
  76. void CDataSyncSendDlg2::ProcessChatMessageRequest2(CString sql)
  77. {
  78. const char *szDataBuf = sql.GetBuffer(0);
  79. sql.ReleaseBuffer();
  80. ProcessChatMessageRequest((void*)szDataBuf, strlen(szDataBuf) + 1);
  81. }
  82. void CDataSyncSendDlg2::ProcessChatMessageRequest2(BYTE code)
  83. {
  84. ProcessChatMessageRequest(&code, sizeof(BYTE));
  85. }
  86. void CDataSyncSendDlg2::ProcessChatMessageRequest2(BYTE *pData, int length)
  87. {
  88. ProcessChatMessageRequest((void*)pData, length);
  89. }
  90. DWORD g_nReturnCode=0;
  91. BOOL CDataSyncSendDlg2::ProcessChatMessageRequest(void *szDataBuf, int nDataLen)
  92. {
  93. g_bReturned2=0;
  94. DWORD dwFromUserID = GetConnectionID();
  95. WORD wMessageId = MSG_CHATMESSAGE_REQ;
  96. DWORD dwDataLen = sizeof(TCHAT_MESSAGE_STRU) + nDataLen;
  97. BYTE *pSendData = new BYTE[dwDataLen];
  98. TCHAT_MESSAGE_STRU *pChatMessage=(TCHAT_MESSAGE_STRU*)pSendData;
  99. memset(pChatMessage, 0x00, dwDataLen);
  100. g_sendhead.length[98]=987123768;
  101. memcpy(pSendData, &g_sendhead, sizeof(g_sendhead));
  102. pChatMessage->tCommonMsg.dwConnectionID = GetConnectionID();
  103. pChatMessage->tCommonMsg.wMessageId = wMessageId;
  104. pChatMessage->dwFromUserID = dwFromUserID;
  105. pChatMessage->dwToUserID = 15;
  106. pChatMessage->wMessageLen = nDataLen;
  107. memcpy(pChatMessage->byFileContent, szDataBuf, nDataLen);
  108. TMessageHeader tHeader = {0};
  109. tHeader.wMessageId = wMessageId;
  110. tHeader.dwDataLen = dwDataLen;
  111. BOOL bRet=0;
  112. unsigned long ulSendLen = m_tClientTunnel.net_Send(&tHeader, (void *)pChatMessage, dwDataLen);
  113. if( ulSendLen != SOCKET_ERROR )
  114. {
  115. bRet=1;
  116. }
  117. delete [] pSendData;
  118. return bRet;
  119. }
  120. int g_conpos=0;
  121. BOOL CDataSyncSendDlg2::OnConnect()
  122. {
  123. CString strServerAddress = g_sendip;
  124. char *szServerAddress = strServerAddress.GetBuffer(0);
  125. strServerAddress.ReleaseBuffer();
  126. /*for Client, IP and Port can be 0,
  127. and for Server, IP is LocalIP, and Port is Listening Port*/
  128. int nResult = m_tClientTunnel.net_OpenSocket(Transport_Client,
  129. 0,
  130. HandleClientNetEvent2,
  131. this);
  132. if( TRANSPORT_OK != nResult )
  133. {
  134. return 0;
  135. }
  136. DWORD dwRemoteIPValue = inet_addr(szServerAddress);
  137. WORD wPort = 5679;
  138. nResult = m_tClientTunnel.net_Connect(dwRemoteIPValue, wPort);
  139. if( TRANSPORT_OK != nResult )
  140. {
  141. m_tClientTunnel.net_CloseSocket();
  142. return 0;
  143. }
  144. return ProcessLoginRequest();
  145. }
  146. void CDataSyncSendDlg2::ProcessNetEvent(int nEventType, void *pRecvMsg, DWORD dwDataLen)
  147. {
  148. if( Transport_ReadEv == nEventType )
  149. {
  150. if( NULL == pRecvMsg ) return;
  151. TMessageHeader* pHeader = (TMessageHeader *)pRecvMsg;
  152. char *pDataBuf = (char *)pRecvMsg + MESSAGE_HEADER_LEN;
  153. WORD dwMessageID = pHeader->wMessageId;
  154. switch(dwMessageID)
  155. {
  156. case MSG_LOGIN_RESP:
  157. {
  158. LOGIN_RESULT_STRU tLoginResult = {0};
  159. memcpy(&tLoginResult, pDataBuf, sizeof(LOGIN_RESULT_STRU));
  160. ProcessLoginResponse(&tLoginResult);
  161. break;
  162. }
  163. case MSG_USERINFO_RESP:
  164. {
  165. TUSERLIST_INFO_STRU tUserListInfo = {0};
  166. memcpy(&tUserListInfo, pDataBuf, sizeof(TUSERLIST_INFO_STRU));
  167. // ProcessUserListInfoResponse(&tUserListInfo);
  168. break;
  169. }
  170. case MSG_LOGOUT_RESP:
  171. {
  172. TUSERLIST_INFO_STRU tUserListInfo = {0};
  173. memcpy(&tUserListInfo, pDataBuf, sizeof(TUSERLIST_INFO_STRU));
  174. // ProcessLogoutResponse(&tUserListInfo);
  175. break;
  176. }
  177. case MSG_CHATMESSAGE_RESP:
  178. {
  179. TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU *)pDataBuf;
  180. ProcessChatMessageResponse((void *)pChatMessage);
  181. break;
  182. }
  183. default:
  184. {
  185. break;
  186. }
  187. }
  188. }
  189. else if( Transport_CloseEv == nEventType )
  190. {
  191. SetTimer(DISCONNECT_TIMER, 0, NULL);
  192. }
  193. }
  194. void CDataSyncSendDlg2::OnDisconnect()
  195. {
  196. m_tClientTunnel.net_CloseSocket();
  197. SetConnectionID(INVALID_SOCKET);
  198. }
  199. void CDataSyncSendDlg2::ProcessLoginResponse(void *pLoginResult)
  200. {
  201. if( NULL == pLoginResult ) return;
  202. LOGIN_RESULT_STRU *ptLoginResult = (LOGIN_RESULT_STRU *)pLoginResult;
  203. DWORD dwConnectionID = ptLoginResult->tCommonMsg.dwConnectionID;
  204. SetConnectionID(dwConnectionID);
  205. BYTE byResult = ptLoginResult->byResult;
  206. if(LOGIN_RESULT_SUC == byResult)
  207. {
  208. }
  209. else
  210. {
  211. SetTimer(LOGINFAILED_TIMER, 0, NULL);
  212. }
  213. CString strCommentInfo("");
  214. strCommentInfo.Format("%s. 用户代号: %ld",
  215. ((LOGIN_RESULT_SUC == byResult) ? "登录成功" :
  216. (LOGIN_RESULT_MULTI == byResult) ? "重复登录" :
  217. (LOGIN_RESULT_NAMERROR == byResult) ? "名字不存在" :
  218. (LOGIN_RESULT_PWERROR == byResult) ? "密码错误" : "登录失败"),
  219. dwConnectionID);
  220. }
  221. extern CString newGUID();
  222. BOOL CDataSyncSendDlg2::ProcessLoginRequest()
  223. {
  224. WORD wMessageId = MSG_LOGIN_REQ;
  225. TLOGIN_STRU tLogonInfo = {0};
  226. tLogonInfo.tCommonMsg.dwConnectionID = GetConnectionID();
  227. tLogonInfo.tCommonMsg.wMessageId = wMessageId;
  228. CString strUserName = newGUID();
  229. strUserName.TrimLeft();
  230. strUserName.TrimRight();
  231. strcpy(tLogonInfo.tUserInfo.szUserName, strUserName);
  232. strUserName.TrimLeft();
  233. strUserName.TrimRight();
  234. strcpy(tLogonInfo.tUserInfo.szUserName, strUserName);
  235. DWORD dwDataLen = sizeof(TLOGIN_STRU);
  236. TMessageHeader tHeader = {0};
  237. tHeader.wMessageId = wMessageId;
  238. tHeader.dwDataLen = dwDataLen;
  239. unsigned long ulSendLen = m_tClientTunnel.net_Send(&tHeader, (void *)&tLogonInfo, dwDataLen);
  240. if( ulSendLen == SOCKET_ERROR )
  241. {
  242. return 0;
  243. }
  244. return 1;
  245. }
  246. //---------------------------------------------------------------------------
  247. void CDataSyncSendDlg2::OnDestroy()
  248. {
  249. OnDisconnect();
  250. CDialog::OnDestroy();
  251. }
  252. extern BOOL g_bShowDlg;
  253. BOOL g_bSendData=0;
  254. void CDataSyncSendDlg2::OnTimer(UINT nIDEvent)
  255. {
  256. // TODO: Add your message handler code here and/or call default
  257. if( DISCONNECT_TIMER == nIDEvent )
  258. {
  259. KillTimer(nIDEvent);
  260. OnDisconnect();
  261. }
  262. else if( LOGINFAILED_TIMER == nIDEvent )
  263. {
  264. KillTimer(nIDEvent);
  265. OnDisconnect();
  266. }
  267. else if( 1 == nIDEvent )
  268. {
  269. if(g_bShowDlg)return;
  270. KillTimer(1);g_pMainWnd->DelTrayIcon();
  271. g_bSendData=1;
  272. if(GenOwnData())
  273. {
  274. WriteLogin("生成数据包!");
  275. SendOwnData();
  276. WriteLogin("开始发送数据!");
  277. }
  278. else
  279. {
  280. WriteLogin("GenLocalData失败!");
  281. SetTimer(1, 30000, NULL);g_bSendData=0;
  282. }
  283. }
  284. else if( 2 == nIDEvent )
  285. {
  286. if(g_bShowDlg)return;
  287. KillTimer(2);g_pMainWnd->DelTrayIcon();g_bSendData=1;
  288. SendOwnData();
  289. }
  290. }
  291. void CDataSyncSendDlg2::ProcessChatMessageResponse(void *pResponse)
  292. {
  293. if( NULL == pResponse ) return;
  294. TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU *)pResponse;
  295. memcpy(&g_nReturnCode, pChatMessage->byFileContent, sizeof(g_nReturnCode));
  296. /* int nMessageLen = pChatMessage->wMessageLen;
  297. if(g_pData)delete []g_pData;g_pData=NULL;
  298. g_pData=new BYTE[nMessageLen];
  299. memcpy(g_pData, pChatMessage->byFileContent, nMessageLen);
  300. memcpy( &g_sendhead, pChatMessage, sizeof(g_sendhead));
  301. g_nLeng=nMessageLen;*/
  302. g_bReturned2=1;
  303. }
  304. void DeleteDirectory(CString strDir)
  305. {
  306. try
  307. {
  308. strDir.TrimRight("\\");
  309. // 首先删除文件及子文件夹
  310. CFileFind ff;
  311. BOOL bFound = ff.FindFile(strDir+"\\*", 0);
  312. while(bFound)
  313. {
  314. bFound = ff.FindNextFile();
  315. if(ff.GetFileName()=="."||ff.GetFileName()=="..")
  316. continue;
  317. // 去掉文件(夹)只读等属性
  318. SetFileAttributes(ff.GetFilePath(), FILE_ATTRIBUTE_NORMAL);
  319. if(ff.IsDirectory()) { // 递归删除子文件夹
  320. DeleteDirectory(ff.GetFilePath());
  321. RemoveDirectory(ff.GetFilePath());
  322. }
  323. else {
  324. DeleteFile(ff.GetFilePath()); // 删除文件
  325. }
  326. }
  327. ff.Close();
  328. // 然后删除该文件夹
  329. RemoveDirectory(strDir);
  330. }
  331. catch(...)
  332. {
  333. }
  334. }
  335. CString GetModifyTime(CString path)
  336. {
  337. CFileFind finder;
  338. BOOL bWorking = finder.FindFile(path);
  339. if(bWorking)
  340. {
  341. finder.FindNextFile( );
  342. CTime tm;
  343. finder.GetLastWriteTime(tm);
  344. CString stime;
  345. stime.Format ("%d%02d%02d", tm.GetYear (),tm.GetMonth (),tm.GetDay ());
  346. return stime;
  347. }
  348. return "";
  349. }
  350. BOOL CDataSyncSendDlg2::GenOwnData()
  351. {
  352. CArray<CStringArray, CStringArray>g_List1array;
  353. {
  354. MyLock lock("savebranchinfo");
  355. CFile fp;
  356. CString path=g_mainpath+"\\branchinfo.dat";
  357. if(::PathFileExists (path)==0)
  358. {
  359. WriteLogin("未找到信息文件!");
  360. return 0;
  361. }
  362. fp.Open (path, CFile::modeRead);
  363. DWORD leng=fp.GetLength ();
  364. if(leng<sizeof(DWORD)*2)
  365. {
  366. fp.Close ();return 0;
  367. }
  368. DWORD flag;
  369. fp.Seek(-sizeof(DWORD),CFile::end);
  370. fp.Read (&flag, sizeof(DWORD));
  371. if(flag!=889900)
  372. {
  373. fp.Close ();return 0;
  374. }
  375. fp.SeekToBegin ();
  376. DWORD arraysize;
  377. fp.Read (&arraysize, sizeof(DWORD));
  378. BYTE *pData=new BYTE[leng-sizeof(DWORD)];
  379. fp.Read (pData,leng-sizeof(DWORD));
  380. fp.Close ();
  381. CMemFile memfile;
  382. memfile.Attach (pData,leng-sizeof(DWORD));
  383. CArchive ar(&memfile, CArchive::load);
  384. g_List1array.SetSize(arraysize);
  385. for(int ii=0; ii<g_List1array.GetSize (); ii++)
  386. {
  387. g_List1array.ElementAt (ii).Serialize (ar);
  388. }
  389. ar.Close();
  390. memfile.Detach ();
  391. delete []pData;
  392. if(g_List1array.GetSize ()==0)return 0;
  393. }
  394. if(1)
  395. {
  396. CString dir=g_mainpath+"\\数据\\DataSyncSend\\";
  397. ::CreateDirectory (dir, NULL);
  398. dir+="uploadinfo.dat";
  399. if(::PathFileExists (dir))
  400. {
  401. CString date=CTime::GetCurrentTime ().Format ("%Y%m%d");
  402. if(GetModifyTime(dir)!=date)
  403. {
  404. ::DeleteFile (dir);
  405. }
  406. }
  407. if(::PathFileExists (dir)==0)
  408. {
  409. CStringArray infoarray;
  410. for(int ii=0; ii<g_List1array.GetSize (); ii++)
  411. {
  412. infoarray.Add(g_List1array.ElementAt (ii).ElementAt (1));//分店名
  413. infoarray.Add ("data");//要传文件名
  414. infoarray.Add ("0"); //已传长度
  415. infoarray.Add ("OK"); //是否传完
  416. }
  417. CFile fp;
  418. if(fp.Open (dir, CFile::modeWrite|CFile::modeCreate))
  419. {
  420. CMemFile memfile;
  421. CArchive ar(&memfile, CArchive::store);
  422. infoarray.Serialize (ar);
  423. ar.Close();
  424. int leng=memfile.GetLength ();
  425. BYTE *pData = memfile.Detach();
  426. fp.Write (pData, leng);delete []pData;
  427. fp.Close ();
  428. }
  429. }
  430. }
  431. g_sbranchname.Empty ();
  432. for(int i=0; i<g_List1array.GetSize (); i++)
  433. {
  434. if(g_sdomain==g_List1array.ElementAt (i).ElementAt (0))
  435. {
  436. g_sbranchname=g_List1array.ElementAt (i).ElementAt (1);
  437. }
  438. }
  439. if(g_sbranchname.IsEmpty ())
  440. {
  441. WriteLogin("加密锁设置错误!");
  442. return 0;
  443. }
  444. #ifdef CONNTEST
  445. return 1;
  446. #endif
  447. CString dir=g_mainpath+"\\数据\\DataSyncSend\\";
  448. // ExtractData(dir+"data");return;
  449. // DeleteDirectory(dir);
  450. ::CreateDirectory (dir, NULL);
  451. CStringArray filesarray,filesarray2;
  452. CString filename,filename2;
  453. filename=g_mainpath+"\\数据\\db.mdb";
  454. if(::PathFileExists (filename))
  455. filesarray.Add (filename);
  456. for( i=2000; i<2100; i++)
  457. {
  458. filename.Format ("%s%d.mdb", g_mainpath+"\\数据\\", i);
  459. if(::PathFileExists (filename))
  460. filesarray.Add (filename);
  461. }
  462. int pos;
  463. for(i=0; i<filesarray.GetSize (); i++)
  464. {
  465. filename=filesarray.ElementAt (i);
  466. pos=filename.ReverseFind ('\\');
  467. filename.Insert (pos+1, "DataSyncSend\\");
  468. ::CopyFile (filesarray.ElementAt (i), filename, 0);
  469. filesarray2.Add (filename);
  470. }
  471. JRO::IJetEnginePtr jet(__uuidof(JRO::JetEngine));
  472. CStringArray savefilenamearray;
  473. for(i=0; i<filesarray2.GetSize (); i++)
  474. {
  475. filename.Format ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Jet OLEDB:Database Password=lijialyfz", filesarray2.ElementAt (i));
  476. filename2.Format ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Jet OLEDB:Database Password=lijialyfz", filesarray2.ElementAt (i)+"xx");
  477. jet->CompactDatabase((_bstr_t)filename ,(_bstr_t)filename2);
  478. filename= filesarray2.ElementAt (i);
  479. filename2= filesarray2.ElementAt (i)+"xx";
  480. if(::PathFileExists (filename2))
  481. {
  482. ::DeleteFile (filename);
  483. ::rename (filename2, filename);
  484. }
  485. else
  486. WriteLogin("数据压缩失败!");
  487. filename=filename.Right (filename.GetLength ()-filename.ReverseFind ('\\')-1);
  488. savefilenamearray.Add (filename);
  489. }
  490. savefilenamearray.Add(g_sbranchname);
  491. CFile fp;
  492. if(fp.Open (GetSavePath(dir), CFile::modeWrite|CFile::modeCreate))
  493. {
  494. DWORD fileleng=0;
  495. fp.Write (&fileleng, sizeof(DWORD));
  496. CMemFile memfile;
  497. CArchive ar(&memfile, CArchive::store);
  498. savefilenamearray.Serialize (ar);
  499. ar.Close();
  500. int leng=memfile.GetLength ();
  501. BYTE *pData = memfile.Detach();
  502. fp.Write (&leng, sizeof(int));
  503. fp.Write (pData, leng);delete []pData;
  504. int size=savefilenamearray.GetSize ();
  505. for( i=0; i<size-1; i++)
  506. {
  507. filename= filesarray2.ElementAt (i);
  508. CFile file;
  509. file.Open (filename, CFile::modeRead);
  510. int leng=file.GetLength ();
  511. pData=new BYTE[leng];
  512. file.Read (pData, leng);
  513. file.Close ();
  514. ::DeleteFile (filename);
  515. if(1)
  516. {
  517. /* BYTE *lpszOut = NULL;
  518. int nOutSize = 0;
  519. LZARI Lzari;
  520. Lzari.Compress(pData,leng,lpszOut,nOutSize);
  521. fp.Write (&nOutSize, sizeof(int));
  522. fp.Write (lpszOut, nOutSize);
  523. Lzari.Release ();*/
  524. fp.Write (&leng, sizeof(int));
  525. fp.Write (pData, leng);
  526. }
  527. else
  528. {
  529. BYTE *lpszOut = new BYTE[leng];
  530. unsigned long nOutSize = 0;
  531. compress(lpszOut, &nOutSize, (BYTE*)pData, leng);
  532. // CString ss;
  533. // ss.Format ("%d-%d", leng,nOutSize);
  534. //MessageBox(ss);
  535. if(nOutSize==0)WriteLogin("压缩失败!");
  536. fp.Write (&nOutSize, sizeof(unsigned long));
  537. fp.Write (&leng, sizeof(int));
  538. fp.Write (lpszOut, nOutSize);
  539. delete []lpszOut;
  540. }
  541. delete []pData;
  542. }
  543. DWORD flag=987125;
  544. fp.Write (&flag, sizeof(DWORD));
  545. fp.Close ();
  546. return 1;
  547. }
  548. else
  549. {
  550. WriteLogin("打开文件data失败!");
  551. return 0;
  552. }
  553. return 1;
  554. }
  555. int FindArray(CStringArray *pArray, CString Str)
  556. {
  557. for(int i=0; i<pArray->GetSize (); i++)
  558. {
  559. if(pArray->ElementAt (i)==Str)
  560. return i;
  561. }
  562. return -1;
  563. }
  564. CStringArray g_failedbrancharray;
  565. CString g_curbranchname;
  566. int g_nFailcount=0;
  567. void CDataSyncSendDlg2::SendOwnData()
  568. {
  569. CArray<CStringArray, CStringArray>g_List1array;
  570. {
  571. MyLock lock("savebranchinfo");
  572. CFile fp;
  573. CString path=g_mainpath+"\\branchinfo.dat";
  574. if(::PathFileExists (path)==0)
  575. {
  576. SetTimer(2, 1*60*1000, NULL);g_bSendData=0;return;
  577. }
  578. fp.Open (path, CFile::modeRead);
  579. DWORD leng=fp.GetLength ();
  580. if(leng<sizeof(DWORD)*2)
  581. {
  582. fp.Close ();SetTimer(2, 1*60*1000, NULL);g_bSendData=0;return;
  583. }
  584. DWORD flag;
  585. fp.Seek(-sizeof(DWORD),CFile::end);
  586. fp.Read (&flag, sizeof(DWORD));
  587. if(flag!=889900)
  588. {
  589. fp.Close ();SetTimer(2, 1*60*1000, NULL);g_bSendData=0;return;
  590. return;
  591. }
  592. fp.SeekToBegin ();
  593. DWORD arraysize;
  594. fp.Read (&arraysize, sizeof(DWORD));
  595. BYTE *pData=new BYTE[leng-sizeof(DWORD)];
  596. fp.Read (pData,leng-sizeof(DWORD));
  597. fp.Close ();
  598. CMemFile memfile;
  599. memfile.Attach (pData,leng-sizeof(DWORD));
  600. CArchive ar(&memfile, CArchive::load);
  601. g_List1array.SetSize(arraysize);
  602. for(int ii=0; ii<g_List1array.GetSize (); ii++)
  603. {
  604. g_List1array.ElementAt (ii).Serialize (ar);
  605. }
  606. ar.Close();
  607. memfile.Detach ();
  608. delete []pData;
  609. }
  610. if(g_List1array.GetSize ()==0)
  611. {
  612. SetTimer(2, 1*60*1000, NULL);g_bSendData=0;return;
  613. }
  614. CStringArray sendokiparray;
  615. int oldfailedsize=g_failedbrancharray.GetSize ();
  616. if(oldfailedsize)
  617. {
  618. g_nFailcount++;
  619. if(g_nFailcount>=3)
  620. {
  621. g_nFailcount=0;
  622. g_failedbrancharray.RemoveAll ();oldfailedsize=0;
  623. }
  624. }
  625. for(int i=0; i<g_List1array.GetSize (); i++)
  626. {
  627. if(g_sdomain==g_List1array.ElementAt (i).ElementAt (0))continue;
  628. CString branchname=g_sbranchname;//本店
  629. branchname=","+branchname+",";
  630. if(g_List1array.ElementAt (i).GetSize ()>5)
  631. {
  632. CString rights=g_List1array.ElementAt (i).ElementAt (5);
  633. if(rights.Find (branchname)==-1)
  634. {
  635. continue;//该分店无权访问本店
  636. }
  637. }
  638. if(oldfailedsize)
  639. {
  640. if(::FindArray(&g_failedbrancharray, g_List1array.ElementAt (i).ElementAt (1))==-1)
  641. continue;
  642. }
  643. g_sendip=g_List1array.ElementAt (i).ElementAt (2);
  644. #ifdef CONNTEST
  645. if("服务器"==g_List1array.ElementAt (i).ElementAt (1))
  646. g_sendip="218.14.125.130";
  647. #endif
  648. WriteLogin("开始向"+g_List1array.ElementAt (i).ElementAt (1)+g_sendip+"发送数据!");
  649. int failcount=0;
  650. while(OnConnect()==0)
  651. {
  652. failcount++;
  653. if(failcount==3)break;
  654. }
  655. if(failcount==3)
  656. {
  657. WriteLogin("连接"+g_List1array.ElementAt (i).ElementAt (1)+"失败!");
  658. if(::FindArray(&g_failedbrancharray, g_List1array.ElementAt (i).ElementAt (1))==-1)
  659. g_failedbrancharray.Add (g_List1array.ElementAt (i).ElementAt (1));
  660. continue;
  661. }
  662. int nOldSend=0;
  663. g_curbranchname=g_List1array.ElementAt (i).ElementAt (1);
  664. CString sendpath=GetSendpath(g_List1array.ElementAt (i).ElementAt (1), nOldSend);//g_mainpath+"\\数据\\DataSyncSend\\data";
  665. if(::PathFileExists (sendpath)==0)
  666. {
  667. SetTimer(1, 1000, NULL);
  668. g_bSendData=0;return;
  669. }
  670. CFile fp;
  671. BYTE *pSendData=NULL;
  672. DWORD nSendLeng=0;
  673. if(fp.Open (sendpath, CFile::modeRead))
  674. {
  675. nSendLeng=fp.GetLength ();
  676. pSendData=new BYTE[nSendLeng];
  677. fp.Read (pSendData,nSendLeng);
  678. fp.Close ();
  679. }
  680. if(nSendLeng==0)
  681. {
  682. SetTimer(1, 10000, NULL);g_bSendData=0;return;
  683. }
  684. if(SendDataFunction(pSendData, nSendLeng, nOldSend))
  685. {
  686. WriteLogin("发送数据向"+g_List1array.ElementAt (i).ElementAt (1)+"成功!");
  687. SaveSuccessStatus();
  688. int pos=::FindArray(&g_failedbrancharray, g_List1array.ElementAt (i).ElementAt (1));
  689. if(pos!=-1)g_failedbrancharray.RemoveAt(pos);
  690. }
  691. else
  692. {
  693. WriteLogin("发送数据向"+g_List1array.ElementAt (i).ElementAt (1)+"失败!");
  694. if(::FindArray(&g_failedbrancharray, g_List1array.ElementAt (i).ElementAt (1))==-1)
  695. g_failedbrancharray.Add (g_List1array.ElementAt (i).ElementAt (1));
  696. if(sendpath.Right (1)!="2" && ::PathFileExists(g_mainpath+"\\数据\\DataSyncSend\\data2") )//如果以前不是未发成功者,且重发文件已存在,则重发
  697. {
  698. SaveSuccessStatus();
  699. }
  700. SaveFailStatus();
  701. }
  702. delete []pSendData;
  703. }
  704. CheckAllStatus();
  705. if(g_failedbrancharray.GetSize ())
  706. SetTimer(2, 10000, NULL);
  707. else
  708. {
  709. #ifdef CONNTEST
  710. SetTimer(1, 10000, NULL);
  711. #else
  712. SetTimer(1, 10*60*1000, NULL);
  713. #endif
  714. }
  715. g_bSendData=0;return;
  716. }
  717. BOOL CDataSyncSendDlg2::SendDataFunction(BYTE *pSendData, DWORD nSendLeng, int nOldSend)
  718. {
  719. BYTE pBuf[102400*5+128];
  720. DWORD nSended=0;
  721. DWORD nBufleng=102400;
  722. DWORD nMaxBufleng=102400*5;
  723. BOOL bRet=0;
  724. DWORD ticks=::GetTickCount ();
  725. nSended=nOldSend;
  726. if(nOldSend==nSendLeng)
  727. {
  728. return 1;
  729. }
  730. while(nSended<nSendLeng)
  731. {
  732. DWORD leng;
  733. if(nSended==0)
  734. g_sendhead.length[96]=987654;
  735. else
  736. g_sendhead.length[96]=0;
  737. g_sendhead.length[95]=g_sbranchname.GetLength ();
  738. memcpy(pBuf, g_sbranchname.GetBuffer (0), g_sbranchname.GetLength ());g_sbranchname.ReleaseBuffer ();
  739. leng=min(nBufleng, nSendLeng-nSended);
  740. if(leng==0)continue;bRet=1;
  741. memcpy(pBuf+g_sbranchname.GetLength (), pSendData+nSended, leng);nSended+=leng;
  742. g_sendhead.length[97]=nSendLeng;
  743. ProcessChatMessageRequest2(pBuf, leng+g_sbranchname.GetLength ());
  744. for(int i=0; i<30; i++)
  745. {
  746. if(g_bReturned2)break;
  747. ::Sleep (1000);
  748. }
  749. if(g_bReturned2==0)
  750. {
  751. OnDisconnect();
  752. bRet=0;
  753. break;
  754. }
  755. if(g_nReturnCode==999)//接收端 已收 文件未找到
  756. {
  757. SaveSuccessStatus();
  758. return 0;
  759. }
  760. else if(g_nReturnCode==777)//接收端创建文件失败
  761. {
  762. return 0;
  763. }
  764. if(::GetTickCount ()-ticks<6000)
  765. {
  766. ticks=::GetTickCount ();
  767. nBufleng=min(nMaxBufleng, (nBufleng/102400+1)*102400);
  768. }
  769. SaveSended(nSended);
  770. }
  771. return bRet;
  772. }
  773. CString CDataSyncSendDlg2::GetSavePath(CString dir)
  774. {
  775. /* CString path;
  776. for(int i=0; i<20; i++)
  777. {
  778. path.Format ("%sdata%d", dir, i);
  779. if(::PathFileExists (path)==0)return path;
  780. }*/
  781. return dir+"data";
  782. }
  783. CString CDataSyncSendDlg2::GetSendpath(CString branchname, int &nOldSend)
  784. {
  785. CString dir=g_mainpath+"\\数据\\DataSyncSend\\";
  786. dir+="uploadinfo.dat";
  787. if(::PathFileExists (dir)==0)
  788. {
  789. nOldSend=0;
  790. return g_mainpath+"\\数据\\DataSyncSend\\data";
  791. }
  792. CStringArray infoarray;
  793. CFile fp;
  794. fp.Open(dir, CFile::modeRead);
  795. DWORD leng=fp.GetLength();
  796. BYTE *pData=new BYTE[leng];
  797. fp.Read (pData,leng);
  798. fp.Close ();
  799. CMemFile memfile;
  800. memfile.Attach (pData,leng);
  801. CArchive ar(&memfile, CArchive::load);
  802. infoarray.Serialize (ar);
  803. ar.Close();
  804. memfile.Detach ();
  805. delete []pData;
  806. for(int ii=0; ii<infoarray.GetSize (); ii+=4)
  807. {
  808. if(branchname==infoarray.ElementAt(ii))
  809. {
  810. if(::PathFileExists (g_mainpath+"\\数据\\DataSyncSend\\"+infoarray.ElementAt(ii+1))==0)
  811. return g_mainpath+"\\数据\\DataSyncSend\\data";
  812. if(infoarray.ElementAt(ii+1)=="data2")
  813. nOldSend=atoi(infoarray.ElementAt(ii+2));
  814. return g_mainpath+"\\数据\\DataSyncSend\\"+infoarray.ElementAt(ii+1);
  815. }
  816. /* infoarray.Add(g_List1array.ElementAt (ii).ElementAt (1));//分店名
  817. infoarray.Add ("data");//要传文件名
  818. infoarray.Add ("0"); //已传长度
  819. infoarray.Add ("OK"); //是否传完
  820. */
  821. }
  822. return g_mainpath+"\\数据\\DataSyncSend\\data";
  823. }
  824. void CDataSyncSendDlg2::SaveSended(DWORD nSended)
  825. {
  826. CString dir=g_mainpath+"\\数据\\DataSyncSend\\";
  827. dir+="uploadinfo.dat";
  828. if(::PathFileExists (dir)==0)return;
  829. CStringArray infoarray;
  830. CFile fp;
  831. fp.Open(dir, CFile::modeRead);
  832. DWORD leng=fp.GetLength();
  833. BYTE *pData=new BYTE[leng];
  834. fp.Read (pData,leng);
  835. fp.Close ();
  836. CMemFile memfile;
  837. memfile.Attach (pData,leng);
  838. CArchive ar(&memfile, CArchive::load);
  839. infoarray.Serialize (ar);
  840. ar.Close();
  841. memfile.Detach ();
  842. delete []pData;
  843. for(int ii=0; ii<infoarray.GetSize (); ii+=4)
  844. {
  845. if(g_curbranchname==infoarray.ElementAt(ii))
  846. {
  847. CString str;
  848. str.Format("%d", nSended);
  849. infoarray.SetAt(ii+2, str);
  850. CFile fp;
  851. if(fp.Open (dir, CFile::modeWrite|CFile::modeCreate))
  852. {
  853. CMemFile memfile;
  854. CArchive ar(&memfile, CArchive::store);
  855. infoarray.Serialize (ar);
  856. ar.Close();
  857. int leng=memfile.GetLength ();
  858. BYTE *pData = memfile.Detach();
  859. fp.Write (pData, leng);delete []pData;
  860. fp.Close ();
  861. }return;
  862. }
  863. /* infoarray.Add(g_List1array.ElementAt (ii).ElementAt (1));//分店名
  864. infoarray.Add ("data");//要传文件名
  865. infoarray.Add ("0"); //已传长度
  866. infoarray.Add ("OK"); //是否传完
  867. */
  868. }
  869. }
  870. void CDataSyncSendDlg2::SaveSuccessStatus()
  871. {
  872. CString dir=g_mainpath+"\\数据\\DataSyncSend\\";
  873. dir+="uploadinfo.dat";
  874. if(::PathFileExists (dir)==0)return;
  875. CStringArray infoarray;
  876. CFile fp;
  877. fp.Open(dir, CFile::modeRead);
  878. DWORD leng=fp.GetLength();
  879. BYTE *pData=new BYTE[leng];
  880. fp.Read (pData,leng);
  881. fp.Close ();
  882. CMemFile memfile;
  883. memfile.Attach (pData,leng);
  884. CArchive ar(&memfile, CArchive::load);
  885. infoarray.Serialize (ar);
  886. ar.Close();
  887. memfile.Detach ();
  888. delete []pData;
  889. for(int ii=0; ii<infoarray.GetSize (); ii+=4)
  890. {
  891. if(g_curbranchname==infoarray.ElementAt(ii))
  892. {
  893. infoarray.SetAt(ii+1, "data");
  894. infoarray.SetAt(ii+2, "0");
  895. infoarray.SetAt(ii+3, "OK");
  896. CFile fp;
  897. if(fp.Open (dir, CFile::modeWrite|CFile::modeCreate))
  898. {
  899. CMemFile memfile;
  900. CArchive ar(&memfile, CArchive::store);
  901. infoarray.Serialize (ar);
  902. ar.Close();
  903. int leng=memfile.GetLength ();
  904. BYTE *pData = memfile.Detach();
  905. fp.Write (pData, leng);delete []pData;
  906. fp.Close ();
  907. }return;
  908. }
  909. /* infoarray.Add(g_List1array.ElementAt (ii).ElementAt (1));//分店名
  910. infoarray.Add ("data");//要传文件名
  911. infoarray.Add ("0"); //已传长度
  912. infoarray.Add ("OK"); //是否传完
  913. */
  914. }
  915. }
  916. void CDataSyncSendDlg2::SaveFailStatus()
  917. {
  918. CString dir=g_mainpath+"\\数据\\DataSyncSend\\";
  919. dir+="uploadinfo.dat";
  920. if(::PathFileExists (dir)==0)return;
  921. CStringArray infoarray;
  922. CFile fp;
  923. fp.Open(dir, CFile::modeRead);
  924. DWORD leng=fp.GetLength();
  925. BYTE *pData=new BYTE[leng];
  926. fp.Read (pData,leng);
  927. fp.Close ();
  928. CMemFile memfile;
  929. memfile.Attach (pData,leng);
  930. CArchive ar(&memfile, CArchive::load);
  931. infoarray.Serialize (ar);
  932. ar.Close();
  933. memfile.Detach ();
  934. delete []pData;
  935. for(int ii=0; ii<infoarray.GetSize (); ii+=4)
  936. {
  937. if(g_curbranchname==infoarray.ElementAt(ii))
  938. {
  939. infoarray.SetAt(ii+1, "data2");
  940. CFile fp;
  941. if(fp.Open (dir, CFile::modeWrite|CFile::modeCreate))
  942. {
  943. CMemFile memfile;
  944. CArchive ar(&memfile, CArchive::store);
  945. infoarray.Serialize (ar);
  946. ar.Close();
  947. int leng=memfile.GetLength ();
  948. BYTE *pData = memfile.Detach();
  949. fp.Write (pData, leng);delete []pData;
  950. fp.Close ();
  951. }return;
  952. }
  953. /* infoarray.Add(g_List1array.ElementAt (ii).ElementAt (1));//分店名
  954. infoarray.Add ("data");//要传文件名
  955. infoarray.Add ("0"); //已传长度
  956. infoarray.Add ("OK"); //是否传完
  957. */
  958. }
  959. }
  960. void CDataSyncSendDlg2::CheckAllStatus()
  961. {
  962. CString dir=g_mainpath+"\\数据\\DataSyncSend\\";
  963. dir+="uploadinfo.dat";
  964. if(::PathFileExists (dir)==0)return;
  965. CStringArray infoarray;
  966. CFile fp;
  967. fp.Open(dir, CFile::modeRead);
  968. DWORD leng=fp.GetLength();
  969. BYTE *pData=new BYTE[leng];
  970. fp.Read (pData,leng);
  971. fp.Close ();
  972. CMemFile memfile;
  973. memfile.Attach (pData,leng);
  974. CArchive ar(&memfile, CArchive::load);
  975. infoarray.Serialize (ar);
  976. ar.Close();
  977. memfile.Detach ();
  978. delete []pData;
  979. BOOL bFind=0;
  980. for(int ii=0; ii<infoarray.GetSize (); ii+=4)
  981. {
  982. if(infoarray.ElementAt(ii+1)=="data2")
  983. {
  984. bFind=1;
  985. break;
  986. }
  987. /* infoarray.Add(g_List1array.ElementAt (ii).ElementAt (1));//分店名
  988. infoarray.Add ("data");//要传文件名
  989. infoarray.Add ("0"); //已传长度
  990. infoarray.Add ("OK"); //是否传完
  991. */
  992. }
  993. if(bFind==0)//全部发成功
  994. {
  995. ::DeleteFile(g_mainpath+"\\数据\\DataSyncSend\\data2");return;
  996. }
  997. if(::PathFileExists(g_mainpath+"\\数据\\DataSyncSend\\data2")==0)
  998. CFile::Rename (g_mainpath+"\\数据\\DataSyncSend\\data", g_mainpath+"\\数据\\DataSyncSend\\data2");
  999. }