1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099 |
- // DataSyncSendDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "DataSyncSend.h"
- #include "DataSyncSendDlg2.h"
- #include <afxsock.h>//<Winsock2.h>
- #include "ConnectDlg.h"
- #include "SendDataDlg.h"
- #include "AddContact.h"
- #include "Lzari.h"
- #include "SendReg.h"
- #include "zlib.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #pragma comment(lib, "ZLib.lib")
- // #define CONNTEST 1 //测试
- CString g_sendip;
- extern void WriteLogin(CString str);
- extern DWORD g_nSendCode;
- extern BOOL g_bReturned2;
- /////////////////////////////////////////////////////////////////////////////
- extern HWND g_hSendWnd;
- extern HWND g_hConnectWnd;
- #define DISCONNECT_TIMER (WM_USER + 101)
- #define LOGINFAILED_TIMER (WM_USER + 102)
- /////////////////////////////////////////////////////////////////////////////
- static void HandleClientNetEvent2(IN SOCKET hSocket, IN ETransportEvent eEvent,
- IN void *pDataBuf, IN unsigned long nDataLen,
- IN int nError, IN void *pContext)
- {
- if( nError != TRANSPORT_OK ) return;
- CDataSyncSendDlg2 *pDlg = (CDataSyncSendDlg2 *)pContext;
- if( NULL == pDlg ) return;
- pDlg->ProcessNetEvent(eEvent, pDataBuf, nDataLen);
- }
- CDataSyncSendDlg2::CDataSyncSendDlg2(CWnd* pParent /*=NULL*/)
- : CDialog(CDataSyncSendDlg2::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CDataSyncSendDlg2)
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- }
- void CDataSyncSendDlg2::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CDataSyncSendDlg2)
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CDataSyncSendDlg2, CDialog)
- //{{AFX_MSG_MAP(CDataSyncSendDlg2)
- ON_WM_DESTROY()
- ON_WM_TIMER()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- BOOL CDataSyncSendDlg2::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- // SetTimer(1, 30000, NULL);
-
- // TODO: Add extra initialization here
- return TRUE; // return TRUE unless you set the focus to a control
- }
- DWORD CDataSyncSendDlg2::GetConnectionID()
- {
- return m_dwConnectionID;
- }
- void CDataSyncSendDlg2::SetConnectionID(DWORD dwConnection)
- {
- m_dwConnectionID = dwConnection;
- }
- void CDataSyncSendDlg2::ProcessChatMessageRequest2(CString sql)
- {
- const char *szDataBuf = sql.GetBuffer(0);
- sql.ReleaseBuffer();
- ProcessChatMessageRequest((void*)szDataBuf, strlen(szDataBuf) + 1);
- }
- void CDataSyncSendDlg2::ProcessChatMessageRequest2(BYTE code)
- {
- ProcessChatMessageRequest(&code, sizeof(BYTE));
- }
- void CDataSyncSendDlg2::ProcessChatMessageRequest2(BYTE *pData, int length)
- {
- ProcessChatMessageRequest((void*)pData, length);
- }
- DWORD g_nReturnCode=0;
- BOOL CDataSyncSendDlg2::ProcessChatMessageRequest(void *szDataBuf, int nDataLen)
- {
- g_bReturned2=0;
- DWORD dwFromUserID = GetConnectionID();
- WORD wMessageId = MSG_CHATMESSAGE_REQ;
- DWORD dwDataLen = sizeof(TCHAT_MESSAGE_STRU) + nDataLen;
- BYTE *pSendData = new BYTE[dwDataLen];
- TCHAT_MESSAGE_STRU *pChatMessage=(TCHAT_MESSAGE_STRU*)pSendData;
- memset(pChatMessage, 0x00, dwDataLen);
- g_sendhead.length[98]=987123768;
- memcpy(pSendData, &g_sendhead, sizeof(g_sendhead));
- pChatMessage->tCommonMsg.dwConnectionID = GetConnectionID();
- pChatMessage->tCommonMsg.wMessageId = wMessageId;
- pChatMessage->dwFromUserID = dwFromUserID;
- pChatMessage->dwToUserID = 15;
- pChatMessage->wMessageLen = nDataLen;
- memcpy(pChatMessage->byFileContent, szDataBuf, nDataLen);
-
- TMessageHeader tHeader = {0};
- tHeader.wMessageId = wMessageId;
- tHeader.dwDataLen = dwDataLen;
- BOOL bRet=0;
- unsigned long ulSendLen = m_tClientTunnel.net_Send(&tHeader, (void *)pChatMessage, dwDataLen);
- if( ulSendLen != SOCKET_ERROR )
- {
- bRet=1;
- }
- delete [] pSendData;
- return bRet;
- }
- int g_conpos=0;
- BOOL CDataSyncSendDlg2::OnConnect()
- {
- CString strServerAddress = g_sendip;
- char *szServerAddress = strServerAddress.GetBuffer(0);
- strServerAddress.ReleaseBuffer();
- /*for Client, IP and Port can be 0,
- and for Server, IP is LocalIP, and Port is Listening Port*/
- int nResult = m_tClientTunnel.net_OpenSocket(Transport_Client,
- 0,
- HandleClientNetEvent2,
- this);
- if( TRANSPORT_OK != nResult )
- {
- return 0;
- }
- DWORD dwRemoteIPValue = inet_addr(szServerAddress);
- WORD wPort = 5679;
- nResult = m_tClientTunnel.net_Connect(dwRemoteIPValue, wPort);
- if( TRANSPORT_OK != nResult )
- {
- m_tClientTunnel.net_CloseSocket();
- return 0;
- }
- return ProcessLoginRequest();
- }
- void CDataSyncSendDlg2::ProcessNetEvent(int nEventType, void *pRecvMsg, DWORD dwDataLen)
- {
- if( Transport_ReadEv == nEventType )
- {
- if( NULL == pRecvMsg ) return;
- TMessageHeader* pHeader = (TMessageHeader *)pRecvMsg;
- char *pDataBuf = (char *)pRecvMsg + MESSAGE_HEADER_LEN;
- WORD dwMessageID = pHeader->wMessageId;
- switch(dwMessageID)
- {
- case MSG_LOGIN_RESP:
- {
- LOGIN_RESULT_STRU tLoginResult = {0};
- memcpy(&tLoginResult, pDataBuf, sizeof(LOGIN_RESULT_STRU));
-
- ProcessLoginResponse(&tLoginResult);
- break;
- }
- case MSG_USERINFO_RESP:
- {
- TUSERLIST_INFO_STRU tUserListInfo = {0};
- memcpy(&tUserListInfo, pDataBuf, sizeof(TUSERLIST_INFO_STRU));
- // ProcessUserListInfoResponse(&tUserListInfo);
- break;
- }
- case MSG_LOGOUT_RESP:
- {
- TUSERLIST_INFO_STRU tUserListInfo = {0};
- memcpy(&tUserListInfo, pDataBuf, sizeof(TUSERLIST_INFO_STRU));
- // ProcessLogoutResponse(&tUserListInfo);
- break;
- }
- case MSG_CHATMESSAGE_RESP:
- {
- TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU *)pDataBuf;
- ProcessChatMessageResponse((void *)pChatMessage);
- break;
- }
- default:
- {
- break;
- }
- }
- }
- else if( Transport_CloseEv == nEventType )
- {
- SetTimer(DISCONNECT_TIMER, 0, NULL);
- }
- }
- void CDataSyncSendDlg2::OnDisconnect()
- {
- m_tClientTunnel.net_CloseSocket();
- SetConnectionID(INVALID_SOCKET);
- }
- void CDataSyncSendDlg2::ProcessLoginResponse(void *pLoginResult)
- {
- if( NULL == pLoginResult ) return;
- LOGIN_RESULT_STRU *ptLoginResult = (LOGIN_RESULT_STRU *)pLoginResult;
- DWORD dwConnectionID = ptLoginResult->tCommonMsg.dwConnectionID;
- SetConnectionID(dwConnectionID);
- BYTE byResult = ptLoginResult->byResult;
- if(LOGIN_RESULT_SUC == byResult)
- {
- }
- else
- {
- SetTimer(LOGINFAILED_TIMER, 0, NULL);
- }
- CString strCommentInfo("");
- strCommentInfo.Format("%s. 用户代号: %ld",
- ((LOGIN_RESULT_SUC == byResult) ? "登录成功" :
- (LOGIN_RESULT_MULTI == byResult) ? "重复登录" :
- (LOGIN_RESULT_NAMERROR == byResult) ? "名字不存在" :
- (LOGIN_RESULT_PWERROR == byResult) ? "密码错误" : "登录失败"),
- dwConnectionID);
- }
- extern CString newGUID();
- BOOL CDataSyncSendDlg2::ProcessLoginRequest()
- {
- WORD wMessageId = MSG_LOGIN_REQ;
- TLOGIN_STRU tLogonInfo = {0};
- tLogonInfo.tCommonMsg.dwConnectionID = GetConnectionID();
- tLogonInfo.tCommonMsg.wMessageId = wMessageId;
- CString strUserName = newGUID();
-
- strUserName.TrimLeft();
- strUserName.TrimRight();
- strcpy(tLogonInfo.tUserInfo.szUserName, strUserName);
-
- strUserName.TrimLeft();
- strUserName.TrimRight();
- strcpy(tLogonInfo.tUserInfo.szUserName, strUserName);
-
- DWORD dwDataLen = sizeof(TLOGIN_STRU);
- TMessageHeader tHeader = {0};
- tHeader.wMessageId = wMessageId;
- tHeader.dwDataLen = dwDataLen;
- unsigned long ulSendLen = m_tClientTunnel.net_Send(&tHeader, (void *)&tLogonInfo, dwDataLen);
- if( ulSendLen == SOCKET_ERROR )
- {
- return 0;
- }
- return 1;
- }
- //---------------------------------------------------------------------------
- void CDataSyncSendDlg2::OnDestroy()
- {
- OnDisconnect();
- CDialog::OnDestroy();
- }
- extern BOOL g_bShowDlg;
- BOOL g_bSendData=0;
- void CDataSyncSendDlg2::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- if( DISCONNECT_TIMER == nIDEvent )
- {
- KillTimer(nIDEvent);
- OnDisconnect();
- }
- else if( LOGINFAILED_TIMER == nIDEvent )
- {
- KillTimer(nIDEvent);
- OnDisconnect();
- }
- else if( 1 == nIDEvent )
- {
- if(g_bShowDlg)return;
- KillTimer(1);g_pMainWnd->DelTrayIcon();
- g_bSendData=1;
- if(GenOwnData())
- {
- WriteLogin("生成数据包!");
- SendOwnData();
- WriteLogin("开始发送数据!");
- }
- else
- {
- WriteLogin("GenLocalData失败!");
- SetTimer(1, 30000, NULL);g_bSendData=0;
- }
- }
- else if( 2 == nIDEvent )
- {
- if(g_bShowDlg)return;
- KillTimer(2);g_pMainWnd->DelTrayIcon();g_bSendData=1;
- SendOwnData();
- }
- }
-
- void CDataSyncSendDlg2::ProcessChatMessageResponse(void *pResponse)
- {
- if( NULL == pResponse ) return;
- TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU *)pResponse;
- memcpy(&g_nReturnCode, pChatMessage->byFileContent, sizeof(g_nReturnCode));
-
- /* int nMessageLen = pChatMessage->wMessageLen;
- if(g_pData)delete []g_pData;g_pData=NULL;
- g_pData=new BYTE[nMessageLen];
- memcpy(g_pData, pChatMessage->byFileContent, nMessageLen);
- memcpy( &g_sendhead, pChatMessage, sizeof(g_sendhead));
- g_nLeng=nMessageLen;*/
- g_bReturned2=1;
- }
- void DeleteDirectory(CString strDir)
- {
- try
- {
- strDir.TrimRight("\\");
- // 首先删除文件及子文件夹
- CFileFind ff;
- BOOL bFound = ff.FindFile(strDir+"\\*", 0);
- while(bFound)
- {
- bFound = ff.FindNextFile();
- if(ff.GetFileName()=="."||ff.GetFileName()=="..")
- continue;
- // 去掉文件(夹)只读等属性
- SetFileAttributes(ff.GetFilePath(), FILE_ATTRIBUTE_NORMAL);
- if(ff.IsDirectory()) { // 递归删除子文件夹
- DeleteDirectory(ff.GetFilePath());
- RemoveDirectory(ff.GetFilePath());
- }
- else {
- DeleteFile(ff.GetFilePath()); // 删除文件
- }
- }
- ff.Close();
- // 然后删除该文件夹
- RemoveDirectory(strDir);
- }
- catch(...)
- {
- }
- }
- CString GetModifyTime(CString path)
- {
- CFileFind finder;
- BOOL bWorking = finder.FindFile(path);
- if(bWorking)
- {
- finder.FindNextFile( );
- CTime tm;
- finder.GetLastWriteTime(tm);
- CString stime;
- stime.Format ("%d%02d%02d", tm.GetYear (),tm.GetMonth (),tm.GetDay ());
- return stime;
- }
- return "";
- }
- BOOL CDataSyncSendDlg2::GenOwnData()
- {
- CArray<CStringArray, CStringArray>g_List1array;
- {
- MyLock lock("savebranchinfo");
- CFile fp;
- CString path=g_mainpath+"\\branchinfo.dat";
- if(::PathFileExists (path)==0)
- {
- WriteLogin("未找到信息文件!");
- return 0;
- }
- fp.Open (path, CFile::modeRead);
- DWORD leng=fp.GetLength ();
- if(leng<sizeof(DWORD)*2)
- {
- fp.Close ();return 0;
- }
- DWORD flag;
- fp.Seek(-sizeof(DWORD),CFile::end);
- fp.Read (&flag, sizeof(DWORD));
- if(flag!=889900)
- {
- fp.Close ();return 0;
- }
- fp.SeekToBegin ();
-
- DWORD arraysize;
- fp.Read (&arraysize, sizeof(DWORD));
- BYTE *pData=new BYTE[leng-sizeof(DWORD)];
- fp.Read (pData,leng-sizeof(DWORD));
- fp.Close ();
- CMemFile memfile;
- memfile.Attach (pData,leng-sizeof(DWORD));
- CArchive ar(&memfile, CArchive::load);
- g_List1array.SetSize(arraysize);
- for(int ii=0; ii<g_List1array.GetSize (); ii++)
- {
- g_List1array.ElementAt (ii).Serialize (ar);
- }
- ar.Close();
- memfile.Detach ();
- delete []pData;
- if(g_List1array.GetSize ()==0)return 0;
- }
- if(1)
- {
- CString dir=g_mainpath+"\\数据\\DataSyncSend\\";
- ::CreateDirectory (dir, NULL);
- dir+="uploadinfo.dat";
- if(::PathFileExists (dir))
- {
- CString date=CTime::GetCurrentTime ().Format ("%Y%m%d");
- if(GetModifyTime(dir)!=date)
- {
- ::DeleteFile (dir);
- }
- }
- if(::PathFileExists (dir)==0)
- {
- CStringArray infoarray;
- for(int ii=0; ii<g_List1array.GetSize (); ii++)
- {
- infoarray.Add(g_List1array.ElementAt (ii).ElementAt (1));//分店名
- infoarray.Add ("data");//要传文件名
- infoarray.Add ("0"); //已传长度
- infoarray.Add ("OK"); //是否传完
- }
- CFile fp;
- if(fp.Open (dir, CFile::modeWrite|CFile::modeCreate))
- {
- CMemFile memfile;
- CArchive ar(&memfile, CArchive::store);
- infoarray.Serialize (ar);
- ar.Close();
- int leng=memfile.GetLength ();
- BYTE *pData = memfile.Detach();
- fp.Write (pData, leng);delete []pData;
- fp.Close ();
- }
- }
-
- }
- g_sbranchname.Empty ();
- for(int i=0; i<g_List1array.GetSize (); i++)
- {
- if(g_sdomain==g_List1array.ElementAt (i).ElementAt (0))
- {
- g_sbranchname=g_List1array.ElementAt (i).ElementAt (1);
- }
- }
- if(g_sbranchname.IsEmpty ())
- {
- WriteLogin("加密锁设置错误!");
- return 0;
- }
- #ifdef CONNTEST
- return 1;
- #endif
- CString dir=g_mainpath+"\\数据\\DataSyncSend\\";
- // ExtractData(dir+"data");return;
- // DeleteDirectory(dir);
- ::CreateDirectory (dir, NULL);
- CStringArray filesarray,filesarray2;
- CString filename,filename2;
- filename=g_mainpath+"\\数据\\db.mdb";
- if(::PathFileExists (filename))
- filesarray.Add (filename);
- for( i=2000; i<2100; i++)
- {
- filename.Format ("%s%d.mdb", g_mainpath+"\\数据\\", i);
- if(::PathFileExists (filename))
- filesarray.Add (filename);
- }
- int pos;
- for(i=0; i<filesarray.GetSize (); i++)
- {
- filename=filesarray.ElementAt (i);
- pos=filename.ReverseFind ('\\');
- filename.Insert (pos+1, "DataSyncSend\\");
- ::CopyFile (filesarray.ElementAt (i), filename, 0);
- filesarray2.Add (filename);
- }
- JRO::IJetEnginePtr jet(__uuidof(JRO::JetEngine));
- CStringArray savefilenamearray;
- for(i=0; i<filesarray2.GetSize (); i++)
- {
- filename.Format ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Jet OLEDB:Database Password=lijialyfz", filesarray2.ElementAt (i));
- filename2.Format ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Jet OLEDB:Database Password=lijialyfz", filesarray2.ElementAt (i)+"xx");
- jet->CompactDatabase((_bstr_t)filename ,(_bstr_t)filename2);
- filename= filesarray2.ElementAt (i);
- filename2= filesarray2.ElementAt (i)+"xx";
- if(::PathFileExists (filename2))
- {
- ::DeleteFile (filename);
- ::rename (filename2, filename);
- }
- else
- WriteLogin("数据压缩失败!");
- filename=filename.Right (filename.GetLength ()-filename.ReverseFind ('\\')-1);
- savefilenamearray.Add (filename);
- }
- savefilenamearray.Add(g_sbranchname);
-
- CFile fp;
- if(fp.Open (GetSavePath(dir), CFile::modeWrite|CFile::modeCreate))
- {
- DWORD fileleng=0;
- fp.Write (&fileleng, sizeof(DWORD));
- CMemFile memfile;
- CArchive ar(&memfile, CArchive::store);
- savefilenamearray.Serialize (ar);
- ar.Close();
- int leng=memfile.GetLength ();
- BYTE *pData = memfile.Detach();
- fp.Write (&leng, sizeof(int));
- fp.Write (pData, leng);delete []pData;
- int size=savefilenamearray.GetSize ();
- for( i=0; i<size-1; i++)
- {
- filename= filesarray2.ElementAt (i);
- CFile file;
- file.Open (filename, CFile::modeRead);
- int leng=file.GetLength ();
- pData=new BYTE[leng];
- file.Read (pData, leng);
- file.Close ();
- ::DeleteFile (filename);
- if(1)
- {
- /* BYTE *lpszOut = NULL;
- int nOutSize = 0;
- LZARI Lzari;
- Lzari.Compress(pData,leng,lpszOut,nOutSize);
- fp.Write (&nOutSize, sizeof(int));
- fp.Write (lpszOut, nOutSize);
- Lzari.Release ();*/
-
- fp.Write (&leng, sizeof(int));
- fp.Write (pData, leng);
-
- }
- else
- {
- BYTE *lpszOut = new BYTE[leng];
- unsigned long nOutSize = 0;
- compress(lpszOut, &nOutSize, (BYTE*)pData, leng);
- // CString ss;
- // ss.Format ("%d-%d", leng,nOutSize);
- //MessageBox(ss);
- if(nOutSize==0)WriteLogin("压缩失败!");
- fp.Write (&nOutSize, sizeof(unsigned long));
- fp.Write (&leng, sizeof(int));
- fp.Write (lpszOut, nOutSize);
- delete []lpszOut;
- }
- delete []pData;
- }
- DWORD flag=987125;
- fp.Write (&flag, sizeof(DWORD));
- fp.Close ();
- return 1;
- }
- else
- {
- WriteLogin("打开文件data失败!");
- return 0;
- }
- return 1;
- }
- int FindArray(CStringArray *pArray, CString Str)
- {
- for(int i=0; i<pArray->GetSize (); i++)
- {
- if(pArray->ElementAt (i)==Str)
- return i;
- }
- return -1;
- }
- CStringArray g_failedbrancharray;
- CString g_curbranchname;
- int g_nFailcount=0;
- void CDataSyncSendDlg2::SendOwnData()
- {
- CArray<CStringArray, CStringArray>g_List1array;
- {
- MyLock lock("savebranchinfo");
- CFile fp;
- CString path=g_mainpath+"\\branchinfo.dat";
- if(::PathFileExists (path)==0)
- {
- SetTimer(2, 1*60*1000, NULL);g_bSendData=0;return;
- }
- fp.Open (path, CFile::modeRead);
- DWORD leng=fp.GetLength ();
- if(leng<sizeof(DWORD)*2)
- {
- fp.Close ();SetTimer(2, 1*60*1000, NULL);g_bSendData=0;return;
- }
- DWORD flag;
- fp.Seek(-sizeof(DWORD),CFile::end);
- fp.Read (&flag, sizeof(DWORD));
- if(flag!=889900)
- {
- fp.Close ();SetTimer(2, 1*60*1000, NULL);g_bSendData=0;return;
- return;
- }
- fp.SeekToBegin ();
-
- DWORD arraysize;
- fp.Read (&arraysize, sizeof(DWORD));
- BYTE *pData=new BYTE[leng-sizeof(DWORD)];
- fp.Read (pData,leng-sizeof(DWORD));
- fp.Close ();
- CMemFile memfile;
- memfile.Attach (pData,leng-sizeof(DWORD));
- CArchive ar(&memfile, CArchive::load);
- g_List1array.SetSize(arraysize);
- for(int ii=0; ii<g_List1array.GetSize (); ii++)
- {
- g_List1array.ElementAt (ii).Serialize (ar);
- }
- ar.Close();
- memfile.Detach ();
- delete []pData;
- }
- if(g_List1array.GetSize ()==0)
- {
- SetTimer(2, 1*60*1000, NULL);g_bSendData=0;return;
- }
- CStringArray sendokiparray;
-
- int oldfailedsize=g_failedbrancharray.GetSize ();
- if(oldfailedsize)
- {
- g_nFailcount++;
- if(g_nFailcount>=3)
- {
- g_nFailcount=0;
- g_failedbrancharray.RemoveAll ();oldfailedsize=0;
- }
- }
- for(int i=0; i<g_List1array.GetSize (); i++)
- {
- if(g_sdomain==g_List1array.ElementAt (i).ElementAt (0))continue;
- CString branchname=g_sbranchname;//本店
- branchname=","+branchname+",";
- if(g_List1array.ElementAt (i).GetSize ()>5)
- {
- CString rights=g_List1array.ElementAt (i).ElementAt (5);
- if(rights.Find (branchname)==-1)
- {
- continue;//该分店无权访问本店
- }
- }
- if(oldfailedsize)
- {
- if(::FindArray(&g_failedbrancharray, g_List1array.ElementAt (i).ElementAt (1))==-1)
- continue;
- }
- g_sendip=g_List1array.ElementAt (i).ElementAt (2);
- #ifdef CONNTEST
- if("服务器"==g_List1array.ElementAt (i).ElementAt (1))
- g_sendip="218.14.125.130";
- #endif
- WriteLogin("开始向"+g_List1array.ElementAt (i).ElementAt (1)+g_sendip+"发送数据!");
- int failcount=0;
- while(OnConnect()==0)
- {
- failcount++;
- if(failcount==3)break;
- }
- if(failcount==3)
- {
- WriteLogin("连接"+g_List1array.ElementAt (i).ElementAt (1)+"失败!");
- if(::FindArray(&g_failedbrancharray, g_List1array.ElementAt (i).ElementAt (1))==-1)
- g_failedbrancharray.Add (g_List1array.ElementAt (i).ElementAt (1));
- continue;
- }
- int nOldSend=0;
- g_curbranchname=g_List1array.ElementAt (i).ElementAt (1);
- CString sendpath=GetSendpath(g_List1array.ElementAt (i).ElementAt (1), nOldSend);//g_mainpath+"\\数据\\DataSyncSend\\data";
-
- if(::PathFileExists (sendpath)==0)
- {
- SetTimer(1, 1000, NULL);
- g_bSendData=0;return;
- }
- CFile fp;
- BYTE *pSendData=NULL;
- DWORD nSendLeng=0;
- if(fp.Open (sendpath, CFile::modeRead))
- {
- nSendLeng=fp.GetLength ();
- pSendData=new BYTE[nSendLeng];
- fp.Read (pSendData,nSendLeng);
- fp.Close ();
- }
- if(nSendLeng==0)
- {
- SetTimer(1, 10000, NULL);g_bSendData=0;return;
- }
- if(SendDataFunction(pSendData, nSendLeng, nOldSend))
- {
- WriteLogin("发送数据向"+g_List1array.ElementAt (i).ElementAt (1)+"成功!");
- SaveSuccessStatus();
- int pos=::FindArray(&g_failedbrancharray, g_List1array.ElementAt (i).ElementAt (1));
- if(pos!=-1)g_failedbrancharray.RemoveAt(pos);
- }
- else
- {
- WriteLogin("发送数据向"+g_List1array.ElementAt (i).ElementAt (1)+"失败!");
- if(::FindArray(&g_failedbrancharray, g_List1array.ElementAt (i).ElementAt (1))==-1)
- g_failedbrancharray.Add (g_List1array.ElementAt (i).ElementAt (1));
- if(sendpath.Right (1)!="2" && ::PathFileExists(g_mainpath+"\\数据\\DataSyncSend\\data2") )//如果以前不是未发成功者,且重发文件已存在,则重发
- {
- SaveSuccessStatus();
- }
- SaveFailStatus();
- }
- delete []pSendData;
- }
-
- CheckAllStatus();
- if(g_failedbrancharray.GetSize ())
- SetTimer(2, 10000, NULL);
- else
- {
- #ifdef CONNTEST
- SetTimer(1, 10000, NULL);
- #else
- SetTimer(1, 10*60*1000, NULL);
- #endif
- }
- g_bSendData=0;return;
- }
- BOOL CDataSyncSendDlg2::SendDataFunction(BYTE *pSendData, DWORD nSendLeng, int nOldSend)
- {
- BYTE pBuf[102400*5+128];
- DWORD nSended=0;
- DWORD nBufleng=102400;
- DWORD nMaxBufleng=102400*5;
- BOOL bRet=0;
- DWORD ticks=::GetTickCount ();
- nSended=nOldSend;
- if(nOldSend==nSendLeng)
- {
- return 1;
- }
- while(nSended<nSendLeng)
- {
- DWORD leng;
- if(nSended==0)
- g_sendhead.length[96]=987654;
- else
- g_sendhead.length[96]=0;
- g_sendhead.length[95]=g_sbranchname.GetLength ();
- memcpy(pBuf, g_sbranchname.GetBuffer (0), g_sbranchname.GetLength ());g_sbranchname.ReleaseBuffer ();
- leng=min(nBufleng, nSendLeng-nSended);
- if(leng==0)continue;bRet=1;
- memcpy(pBuf+g_sbranchname.GetLength (), pSendData+nSended, leng);nSended+=leng;
- g_sendhead.length[97]=nSendLeng;
- ProcessChatMessageRequest2(pBuf, leng+g_sbranchname.GetLength ());
- for(int i=0; i<30; i++)
- {
- if(g_bReturned2)break;
- ::Sleep (1000);
- }
- if(g_bReturned2==0)
- {
- OnDisconnect();
- bRet=0;
- break;
- }
- if(g_nReturnCode==999)//接收端 已收 文件未找到
- {
- SaveSuccessStatus();
- return 0;
- }
- else if(g_nReturnCode==777)//接收端创建文件失败
- {
- return 0;
- }
- if(::GetTickCount ()-ticks<6000)
- {
- ticks=::GetTickCount ();
- nBufleng=min(nMaxBufleng, (nBufleng/102400+1)*102400);
- }
- SaveSended(nSended);
- }
- return bRet;
- }
- CString CDataSyncSendDlg2::GetSavePath(CString dir)
- {
- /* CString path;
- for(int i=0; i<20; i++)
- {
- path.Format ("%sdata%d", dir, i);
- if(::PathFileExists (path)==0)return path;
- }*/
- return dir+"data";
- }
- CString CDataSyncSendDlg2::GetSendpath(CString branchname, int &nOldSend)
- {
- CString dir=g_mainpath+"\\数据\\DataSyncSend\\";
- dir+="uploadinfo.dat";
- if(::PathFileExists (dir)==0)
- {
- nOldSend=0;
- return g_mainpath+"\\数据\\DataSyncSend\\data";
- }
-
- CStringArray infoarray;
- CFile fp;
- fp.Open(dir, CFile::modeRead);
- DWORD leng=fp.GetLength();
- BYTE *pData=new BYTE[leng];
- fp.Read (pData,leng);
- fp.Close ();
- CMemFile memfile;
- memfile.Attach (pData,leng);
- CArchive ar(&memfile, CArchive::load);
- infoarray.Serialize (ar);
- ar.Close();
- memfile.Detach ();
- delete []pData;
- for(int ii=0; ii<infoarray.GetSize (); ii+=4)
- {
- if(branchname==infoarray.ElementAt(ii))
- {
- if(::PathFileExists (g_mainpath+"\\数据\\DataSyncSend\\"+infoarray.ElementAt(ii+1))==0)
- return g_mainpath+"\\数据\\DataSyncSend\\data";
- if(infoarray.ElementAt(ii+1)=="data2")
- nOldSend=atoi(infoarray.ElementAt(ii+2));
- return g_mainpath+"\\数据\\DataSyncSend\\"+infoarray.ElementAt(ii+1);
- }
- /* infoarray.Add(g_List1array.ElementAt (ii).ElementAt (1));//分店名
- infoarray.Add ("data");//要传文件名
- infoarray.Add ("0"); //已传长度
- infoarray.Add ("OK"); //是否传完
- */
- }
- return g_mainpath+"\\数据\\DataSyncSend\\data";
- }
- void CDataSyncSendDlg2::SaveSended(DWORD nSended)
- {
- CString dir=g_mainpath+"\\数据\\DataSyncSend\\";
- dir+="uploadinfo.dat";
- if(::PathFileExists (dir)==0)return;
-
- CStringArray infoarray;
- CFile fp;
- fp.Open(dir, CFile::modeRead);
- DWORD leng=fp.GetLength();
- BYTE *pData=new BYTE[leng];
- fp.Read (pData,leng);
- fp.Close ();
- CMemFile memfile;
- memfile.Attach (pData,leng);
- CArchive ar(&memfile, CArchive::load);
- infoarray.Serialize (ar);
- ar.Close();
- memfile.Detach ();
- delete []pData;
- for(int ii=0; ii<infoarray.GetSize (); ii+=4)
- {
- if(g_curbranchname==infoarray.ElementAt(ii))
- {
- CString str;
- str.Format("%d", nSended);
- infoarray.SetAt(ii+2, str);
- CFile fp;
- if(fp.Open (dir, CFile::modeWrite|CFile::modeCreate))
- {
- CMemFile memfile;
- CArchive ar(&memfile, CArchive::store);
- infoarray.Serialize (ar);
- ar.Close();
- int leng=memfile.GetLength ();
- BYTE *pData = memfile.Detach();
- fp.Write (pData, leng);delete []pData;
- fp.Close ();
- }return;
- }
- /* infoarray.Add(g_List1array.ElementAt (ii).ElementAt (1));//分店名
- infoarray.Add ("data");//要传文件名
- infoarray.Add ("0"); //已传长度
- infoarray.Add ("OK"); //是否传完
- */
- }
- }
- void CDataSyncSendDlg2::SaveSuccessStatus()
- {
- CString dir=g_mainpath+"\\数据\\DataSyncSend\\";
- dir+="uploadinfo.dat";
- if(::PathFileExists (dir)==0)return;
-
- CStringArray infoarray;
- CFile fp;
- fp.Open(dir, CFile::modeRead);
- DWORD leng=fp.GetLength();
- BYTE *pData=new BYTE[leng];
- fp.Read (pData,leng);
- fp.Close ();
- CMemFile memfile;
- memfile.Attach (pData,leng);
- CArchive ar(&memfile, CArchive::load);
- infoarray.Serialize (ar);
- ar.Close();
- memfile.Detach ();
- delete []pData;
- for(int ii=0; ii<infoarray.GetSize (); ii+=4)
- {
- if(g_curbranchname==infoarray.ElementAt(ii))
- {
- infoarray.SetAt(ii+1, "data");
- infoarray.SetAt(ii+2, "0");
- infoarray.SetAt(ii+3, "OK");
- CFile fp;
- if(fp.Open (dir, CFile::modeWrite|CFile::modeCreate))
- {
- CMemFile memfile;
- CArchive ar(&memfile, CArchive::store);
- infoarray.Serialize (ar);
- ar.Close();
- int leng=memfile.GetLength ();
- BYTE *pData = memfile.Detach();
- fp.Write (pData, leng);delete []pData;
- fp.Close ();
- }return;
- }
- /* infoarray.Add(g_List1array.ElementAt (ii).ElementAt (1));//分店名
- infoarray.Add ("data");//要传文件名
- infoarray.Add ("0"); //已传长度
- infoarray.Add ("OK"); //是否传完
- */
- }
- }
- void CDataSyncSendDlg2::SaveFailStatus()
- {
- CString dir=g_mainpath+"\\数据\\DataSyncSend\\";
- dir+="uploadinfo.dat";
- if(::PathFileExists (dir)==0)return;
-
- CStringArray infoarray;
- CFile fp;
- fp.Open(dir, CFile::modeRead);
- DWORD leng=fp.GetLength();
- BYTE *pData=new BYTE[leng];
- fp.Read (pData,leng);
- fp.Close ();
- CMemFile memfile;
- memfile.Attach (pData,leng);
- CArchive ar(&memfile, CArchive::load);
- infoarray.Serialize (ar);
- ar.Close();
- memfile.Detach ();
- delete []pData;
- for(int ii=0; ii<infoarray.GetSize (); ii+=4)
- {
- if(g_curbranchname==infoarray.ElementAt(ii))
- {
- infoarray.SetAt(ii+1, "data2");
- CFile fp;
- if(fp.Open (dir, CFile::modeWrite|CFile::modeCreate))
- {
- CMemFile memfile;
- CArchive ar(&memfile, CArchive::store);
- infoarray.Serialize (ar);
- ar.Close();
- int leng=memfile.GetLength ();
- BYTE *pData = memfile.Detach();
- fp.Write (pData, leng);delete []pData;
- fp.Close ();
- }return;
- }
- /* infoarray.Add(g_List1array.ElementAt (ii).ElementAt (1));//分店名
- infoarray.Add ("data");//要传文件名
- infoarray.Add ("0"); //已传长度
- infoarray.Add ("OK"); //是否传完
- */
- }
- }
- void CDataSyncSendDlg2::CheckAllStatus()
- {
- CString dir=g_mainpath+"\\数据\\DataSyncSend\\";
- dir+="uploadinfo.dat";
- if(::PathFileExists (dir)==0)return;
-
- CStringArray infoarray;
- CFile fp;
- fp.Open(dir, CFile::modeRead);
- DWORD leng=fp.GetLength();
- BYTE *pData=new BYTE[leng];
- fp.Read (pData,leng);
- fp.Close ();
- CMemFile memfile;
- memfile.Attach (pData,leng);
- CArchive ar(&memfile, CArchive::load);
- infoarray.Serialize (ar);
- ar.Close();
- memfile.Detach ();
- delete []pData;
- BOOL bFind=0;
- for(int ii=0; ii<infoarray.GetSize (); ii+=4)
- {
-
- if(infoarray.ElementAt(ii+1)=="data2")
- {
- bFind=1;
- break;
- }
- /* infoarray.Add(g_List1array.ElementAt (ii).ElementAt (1));//分店名
- infoarray.Add ("data");//要传文件名
- infoarray.Add ("0"); //已传长度
- infoarray.Add ("OK"); //是否传完
- */
- }
- if(bFind==0)//全部发成功
- {
- ::DeleteFile(g_mainpath+"\\数据\\DataSyncSend\\data2");return;
- }
- if(::PathFileExists(g_mainpath+"\\数据\\DataSyncSend\\data2")==0)
- CFile::Rename (g_mainpath+"\\数据\\DataSyncSend\\data", g_mainpath+"\\数据\\DataSyncSend\\data2");
- }
|