// DataSyncSendDlg.cpp : implementation file // #include "stdafx.h" #include "DataSyncSend.h" #include "DataSyncSendDlg.h" #include "DataSyncSendDlg2.h" #include // #include "ConnectDlg.h" #include "SendDataDlg.h" #include "AddContact.h" #include "SendReg.h" #include #include #include "CalendarConvert.h" #include "AccessRights.h" #pragma comment(lib, "Winmm.lib") #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define DISCONNECT_TIMER (WM_USER + 101) #define LOGINFAILED_TIMER (WM_USER + 102) #define IPC_CUSTOM_MSG2 _T("{D4F876E2-w78F-22D5-q98A-Y0B0D07B847C}") #define WM_ICON_NOTIFY WM_USER+12 UINT g_wmClose = RegisterWindowMessage(IPC_CUSTOM_MSG2); CString g_enterprisename=""; CDataSyncSendDlg2* g_pMainWnd2=NULL; DWORD g_nSendCode=0; //g_bSendOK ///////////////////////////////////////////////////////////////////////////// HWND g_hSendWnd; CArray g_List1array; DWORD g_nLeng=0; BYTE* g_pData=NULL; CString g_str=""; BOOL g_bReturned=0; BOOL g_bReturned2=0; extern HWND g_hConnectWnd; extern BOOL g_bSendData; extern DWORD FindAppProcessID(CString path); extern void DataToArray(CArray*List1array); extern void DataToArray(CArray*List1array, CArray*List2array); void MyGetIPByName(CString &name) { CString strIP=name; name.Empty (); DWORD dwServerIP=0; HOSTENT *host = gethostbyname(strIP); struct in_addr addr; if (host != NULL) { for(int i = 0; host-> h_addr_list[i] != NULL; i ++) { memset(&addr, 0, sizeof(addr)); memcpy(&addr.S_un.S_addr, host-> h_addr_list[i], host-> h_length); dwServerIP = ntohl(addr.S_un.S_addr); BYTE* pIP = (BYTE*)&dwServerIP; name.Format(_T("%d.%d.%d.%d"), pIP[3], pIP[2], pIP[1], pIP[0]); } } } void WriteLogin(CString str) { CStdioFile fp; CString path=g_mainpath+"\\DataSyncSend.log"; if(::PathFileExists (path)) { fp.Open (path, CFile::modeWrite); int length=fp.GetLength (); if(length>1024*1024) { fp.Close (); ::DeleteFile (path); return; } fp.SeekToEnd (); } else fp.Open (path, CFile::modeCreate|CFile::modeWrite); fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S")+" "+str+"\r\n" ); fp.Close (); } unsigned __stdcall SendMsgThread(LPVOID lpParam) { BOOL nNeedConn=1;//信息服务器 int i; CString str; BYTE *pData; BOOL bDouble=0; BOOL bfirst=1; WriteLogin("服务启动"); while(1) { ll: if(g_pMainWnd->m_bTerminate) break; if(nNeedConn) { WriteLogin("连接服务器"); if(g_pMainWnd->OnConnect()==0) { nNeedConn=1; ::Sleep (1500); goto ll; } else { WriteLogin("连接成功"); nNeedConn=0; } int aa=100; while(g_pMainWnd->m_dwConnectionID==INVALID_SOCKET) { ::Sleep (50); aa--; if(aa==0)break; } } if(bDouble) { str=g_sdomain; pData=new BYTE[str.GetLength ()]; memcpy(pData, str.GetBuffer (0), str.GetLength ()); str.ReleaseBuffer (); EncryptFile2(pData, str.GetLength (), "888666333"); g_nSendCode=8; WriteLogin("获取分店信息"); g_pMainWnd->ProcessChatMessageRequest2(pData, str.GetLength ()); for(i=0; i<20; i++) { if(g_bReturned)break; ::Sleep (1500); } delete []pData; if(g_bReturned==0) { WriteLogin("获取分店信息失败"); nNeedConn=1; goto ll; } bDouble = !bDouble; } else if(g_enterprisename!="")//总店才能传权限 { CString version = _T ("Version 1.0"); BOOL needupload=AfxGetApp()->GetProfileInt (version, "needupload", 1); if(needupload) { MyLock lock("savebranchrights"); CFile fp; CString path=g_mainpath+"\\branchrights.dat"; if(fp.Open (path, CFile::modeRead)) { DWORD leng=fp.GetLength (); pData=new BYTE[leng]; fp.Read (pData,leng); fp.Close (); g_nSendCode=9; g_pMainWnd->ProcessChatMessageRequest2(pData,leng); for(i=0; i<20; i++) { if(g_bReturned)break; ::Sleep (1500); } delete []pData; if(g_bReturned==0) { nNeedConn=1; goto ll; } else { if(g_str != "set ok") goto ll; CString version = _T ("Version 1.0"); AfxGetApp()->WriteProfileInt (version, "needupload", 0); } } else { CString version = _T ("Version 1.0"); AfxGetApp()->WriteProfileInt (version, "needupload", 0); bDouble=!bDouble; goto ll; } } bDouble = !bDouble; } else { bDouble=!bDouble; goto ll; } if(bfirst) { bfirst=0; goto ll; } for(i=0; i<3*60; i++) { if(g_pMainWnd->m_bTerminate) break; ::Sleep (1000); } } g_pMainWnd->m_bRunning=false; g_pMainWnd->m_bTerminate=false; _endthreadex( 0 ); return 0; } //MessageBox ///////////////////////////////////////////////////////////////////////////// static void HandleClientNetEvent(IN SOCKET hSocket, IN ETransportEvent eEvent, IN void *pDataBuf, IN unsigned long nDataLen, IN int nError, IN void *pContext) { if( nError != TRANSPORT_OK ) return; CDataSyncSendDlg *pDlg = (CDataSyncSendDlg *)pContext; if( NULL == pDlg ) return; pDlg->ProcessNetEvent(eEvent, pDataBuf, nDataLen); } CDataSyncSendDlg::CDataSyncSendDlg(CWnd* pParent /*=NULL*/) : CDialog(CDataSyncSendDlg::IDD, pParent) { //{{AFX_DATA_INIT(CDataSyncSendDlg) //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); // g_pMainWnd2=new CDataSyncSendDlg2; // g_pMainWnd2->Create(IDD_DataSyncSend_DIALOG2, this); m_bTrayCreate=0; } void CDataSyncSendDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDataSyncSendDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDataSyncSendDlg, CDialog) //{{AFX_MSG_MAP(CDataSyncSendDlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_WM_DESTROY() ON_WM_TIMER() ON_COMMAND(IDM_CLOSE, OnClose) ON_COMMAND(IDM_DataSyncSend, OnDataSyncSend) //}}AFX_MSG_MAP ON_MESSAGE(WM_ICON_NOTIFY, OnTrayNotification) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// BOOL CDataSyncSendDlg::OnInitDialog() { SetWindowPos(&wndNoTopMost,0,0,0,0,SWP_HIDEWINDOW); ModifyStyleEx(WS_EX_APPWINDOW,WS_EX_TOOLWINDOW); CDialog::OnInitDialog(); // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon g_pMainWnd=this; SetTimer(1, 100, NULL); ///////////////////////////////// // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CDataSyncSendDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CDataSyncSendDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } DWORD CDataSyncSendDlg::GetConnectionID() { return m_dwConnectionID; } void CDataSyncSendDlg::SetConnectionID(DWORD dwConnection) { m_dwConnectionID = dwConnection; } void CDataSyncSendDlg::ProcessChatMessageRequest2(CString sql) { const char *szDataBuf = sql.GetBuffer(0); sql.ReleaseBuffer(); ProcessChatMessageRequest((void*)szDataBuf, strlen(szDataBuf) + 1); } void CDataSyncSendDlg::ProcessChatMessageRequest2(BYTE *pData, int length) { ProcessChatMessageRequest((void*)pData, length); } BOOL CDataSyncSendDlg::ProcessChatMessageRequest(void *szDataBuf, int nDataLen) { g_bReturned=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 = g_nSendCode; 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; } BOOL CDataSyncSendDlg::OnConnect() { OnDisconnect(); g_server="lijiafz.vicp.net" ; MyGetIPByName(g_server); if(g_server.IsEmpty ())return OnConnect2(); CString strServerAddress = g_server; 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, HandleClientNetEvent, this); if( TRANSPORT_OK != nResult ) { return 0; } DWORD dwRemoteIPValue = inet_addr(szServerAddress); WORD wPort = GetPort(); nResult = m_tClientTunnel.net_Connect(dwRemoteIPValue, wPort); if( TRANSPORT_OK != nResult ) { m_tClientTunnel.net_CloseSocket(); return OnConnect2(); } return ProcessLoginRequest(); } BOOL CDataSyncSendDlg::OnConnect2() { CString server="http://www.lyfz.net/download/ip.txt"; char buf[MAX_PATH]; ::GetTempPath(sizeof(buf),buf); CString m_strTempDir=buf; DWORD m_dwHttpRequestFlags=HSR_DOWNLOAD | INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_AUTO_REDIRECT; INTERNET_PORT m_dwPort; DWORD dwType; CInternetSession m_cis; CHttpConnection *m_pHttp; CString m_strServer; CString m_strIniPath; AfxParseURL(server,dwType,m_strServer,m_strIniPath,m_dwPort); if(dwType!=AFX_INET_SERVICE_HTTP) { return OnConnect3(); } m_cis.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,5); m_pHttp=m_cis.GetHttpConnection(m_strServer,m_dwPort); try { m_pHttp=m_cis.GetHttpConnection(m_strServer,m_dwPort); } catch(CInternetException* pEx) { return OnConnect3(); } CHttpFile *pFile = m_pHttp->OpenRequest(CHttpConnection::HTTP_VERB_GET, m_strIniPath, NULL, 1, NULL, NULL, m_dwHttpRequestFlags); try { pFile->SendRequest(); } catch(CInternetException* pEx) { pFile->Close(); return OnConnect3(); } if(pFile) { CStdioFile csf; csf.Open(m_strTempDir+"\\ip.txt",CFile::modeCreate|CFile::modeWrite|CFile::typeBinary); char buf[2048]; int n; while((n=pFile->Read(buf,2048))>0) csf.Write(buf,n); csf.Close(); pFile->Close(); } CStdioFile fp; fp.Open (m_strTempDir+"\\ip.txt", CFile::modeRead); fp.ReadString (g_server); fp.Close (); CString strServerAddress = g_server; 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, HandleClientNetEvent, this); if( TRANSPORT_OK != nResult ) { return 0; } DWORD dwRemoteIPValue = inet_addr(szServerAddress); WORD wPort = GetPort(); nResult = m_tClientTunnel.net_Connect(dwRemoteIPValue, wPort); if( TRANSPORT_OK != nResult ) { m_tClientTunnel.net_CloseSocket(); return OnConnect3(); } return ProcessLoginRequest(); } BOOL CDataSyncSendDlg::OnConnect3() { CString server="http://photo.bjlyfz.com/download/ip.txt"; char buf[MAX_PATH]; ::GetTempPath(sizeof(buf),buf); CString m_strTempDir=buf; DWORD m_dwHttpRequestFlags=HSR_DOWNLOAD | INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_AUTO_REDIRECT; INTERNET_PORT m_dwPort; DWORD dwType; CInternetSession m_cis; CHttpConnection *m_pHttp; CString m_strServer; CString m_strIniPath; AfxParseURL(server,dwType,m_strServer,m_strIniPath,m_dwPort); if(dwType!=AFX_INET_SERVICE_HTTP) { return FALSE; } m_cis.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,5); m_pHttp=m_cis.GetHttpConnection(m_strServer,m_dwPort); try { m_pHttp=m_cis.GetHttpConnection(m_strServer,m_dwPort); } catch(CInternetException* pEx) { return false; } CHttpFile *pFile = m_pHttp->OpenRequest(CHttpConnection::HTTP_VERB_GET, m_strIniPath, NULL, 1, NULL, NULL, m_dwHttpRequestFlags); try { pFile->SendRequest(); } catch(CInternetException* pEx) { pFile->Close(); return false; } if(pFile) { CStdioFile csf; csf.Open(m_strTempDir+"\\ip.txt",CFile::modeCreate|CFile::modeWrite|CFile::typeBinary); char buf[2048]; int n; while((n=pFile->Read(buf,2048))>0) csf.Write(buf,n); csf.Close(); pFile->Close(); } CStdioFile fp; fp.Open (m_strTempDir+"\\ip.txt", CFile::modeRead); fp.ReadString (g_server); fp.Close (); CString strServerAddress = g_server; 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, HandleClientNetEvent, this); if( TRANSPORT_OK != nResult ) { return 0; } DWORD dwRemoteIPValue = inet_addr(szServerAddress); WORD wPort = GetPort(); nResult = m_tClientTunnel.net_Connect(dwRemoteIPValue, wPort); if( TRANSPORT_OK != nResult ) { m_tClientTunnel.net_CloseSocket(); return 0; } return ProcessLoginRequest(); } void CDataSyncSendDlg::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 CDataSyncSendDlg::OnDisconnect() { m_tClientTunnel.net_CloseSocket(); SetConnectionID(INVALID_SOCKET); } void CDataSyncSendDlg::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); // MessageBox((CString)strCommentInfo); } CString newGUID() { CString str; GUID guid; // CoInitialize(NULL); if (S_OK == ::CoCreateGuid(&guid)) { str.Format ( "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]); } // CoUninitialize(); return str.Left (28); } BOOL CDataSyncSendDlg::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); 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 CDataSyncSendDlg::OnDestroy() { // g_pMainWnd2->DestroyWindow (); // delete g_pMainWnd2; OnDisconnect(); Transport_UnInit(); CDialog::OnDestroy(); } void CDataSyncSendDlg::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 ) { KillTimer(1); m_bTerminate=false; m_bRunning=false; m_hThread = NULL; m_bRunning=true; m_hThread = (HANDLE)_beginthreadex(NULL,0, SendMsgThread,(LPVOID)this,0/* CREATE_SUSPENDED*/,&m_dwThreadID); } } void CDataSyncSendDlg::OnClose() { // TODO: Add your command handler code here TerminateThread(); CDialog::OnCancel (); } BOOL CDataSyncSendDlg::TerminateThread() { try { if ( !m_bRunning ) return TRUE; m_bTerminate=true; for( ; ; ) { if ( ::WaitForSingleObject(m_hThread, 0) == WAIT_OBJECT_0 ) break; MSG msg; while (::PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)) { if (!AfxGetApp()->PumpMessage()) break; } } ::CloseHandle(m_hThread); m_bRunning=false; return TRUE; } catch(...) { } } void CDataSyncSendDlg::ProcessChatMessageResponse(void *pResponse) { if( NULL == pResponse ) return; TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU *)pResponse; int nMessageLen = pChatMessage->wMessageLen; DWORD dwToUserID = pChatMessage->dwToUserID; memcpy( &g_sendhead, pChatMessage, sizeof(g_sendhead)); g_bReturned=1; g_str = pChatMessage->byFileContent; if(g_str=="set ok") { WriteLogin("设置访问权限成功"); return; } if(nMessageLen > 10) { if(g_str.Find ("没有此企业的资料")!=-1) { WriteLogin("没有此企业的资料"); return; } WriteLogin("获取分店信息成功"); MyLock lock("savebranchinfo"); CFile fp; CString path=g_mainpath+"\\branchinfo.dat"; fp.Open (path, CFile::modeCreate|CFile::modeWrite); DWORD arraysize=g_sendhead.count[0]; fp.Write (&arraysize, sizeof(DWORD)); fp.Write(pChatMessage->byFileContent, nMessageLen); DWORD flag=889900; fp.Write (&flag, sizeof(DWORD)); fp.Close (); if(g_pData) delete [] g_pData; g_pData=NULL; g_pData = new BYTE[nMessageLen]; memcpy(g_pData, pChatMessage->byFileContent, nMessageLen); g_nLeng = nMessageLen; DataToArray(&g_List1array); g_enterprisename.Empty (); for(int i=0; i=7)aaa=0; return 8387+aaa; }