123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- // AutoRunDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "AutoRun.h"
- #include "AutoRunDlg2.h"
- #include <afxsock.h>//<Winsock2.h>
- #include "ConnectDlg.h"
- #include "SendDataDlg.h"
- #include "AddContact.h"
- #include "SendReg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- extern DWORD g_nSendCode;
- extern BOOL g_bReturned2;
- /////////////////////////////////////////////////////////////////////////////
- extern HWND g_hSendWnd;
- extern HWND g_hConnectWnd;
- extern WORD g_nSrvType;
- #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;
- CAutoRunDlg2 *pDlg = (CAutoRunDlg2 *)pContext;
- if (NULL == pDlg) return;
- pDlg->ProcessNetEvent(eEvent, pDataBuf, nDataLen);
- }
- CAutoRunDlg2::CAutoRunDlg2(CWnd* pParent /*=NULL*/)
- : CDialog(CAutoRunDlg2::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CAutoRunDlg2)
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- }
- void CAutoRunDlg2::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAutoRunDlg2)
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAutoRunDlg2, CDialog)
- //{{AFX_MSG_MAP(CAutoRunDlg2)
- ON_WM_DESTROY()
- ON_WM_TIMER()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- BOOL CAutoRunDlg2::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- // g_pMainWnd=this;
- /////////////////////////////////
- // TODO: Add extra initialization here
- return TRUE; // return TRUE unless you set the focus to a control
- }
- DWORD CAutoRunDlg2::GetConnectionID()
- {
- return m_dwConnectionID;
- }
- void CAutoRunDlg2::SetConnectionID(DWORD dwConnection)
- {
- m_dwConnectionID = dwConnection;
- }
- void CAutoRunDlg2::ProcessChatMessageRequest2(CString sql)
- {
- const char *szDataBuf = sql.GetBuffer(0);
- sql.ReleaseBuffer();
- ProcessChatMessageRequest((void*)szDataBuf, strlen(szDataBuf) + 1);
- }
- void CAutoRunDlg2::ProcessChatMessageRequest2(BYTE code)
- {
- ProcessChatMessageRequest(&code, sizeof(BYTE));
- }
- void CAutoRunDlg2::ProcessChatMessageRequest2(BYTE *pData, int length)
- {
- ProcessChatMessageRequest((void*)pData, length);
- }
- BOOL CAutoRunDlg2::ProcessChatMessageRequest(void *szDataBuf, int nDataLen)
- {
- g_bReturned2 = 0;
- DWORD dwFromUserID = GetConnectionID();
- WORD wMessageId = 0;
- if (g_nSrvType == 0)
- wMessageId = MSG_CHATMESSAGE_REQ + 0X3FFF;
- else
- 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 CAutoRunDlg2::OnConnectLocalServer()
- {
- OnDisconnect();
- CString strServerAddress = g_server2;
- 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;
- }
- #ifdef _DEBUG
- DWORD dwRemoteIPValue = inet_addr(_T("192.168.1.66"));
- #else
- DWORD dwRemoteIPValue = inet_addr(szServerAddress);
- #endif
- nResult = m_tClientTunnel.net_Connect(dwRemoteIPValue, Global::g_dwSvrPort);
- if (TRANSPORT_OK != nResult)
- {
- m_tClientTunnel.net_CloseSocket();
- if (g_serverarray.GetSize() > 1)
- {
- if (g_conpos < (g_serverarray.GetSize() - 1))
- {
- g_conpos++;
- return OnConnectLocalServer();
- }
- else
- {
- g_conpos = 0;
- return 0;
- }
- }
- return 0;
- }
- return ProcessLoginRequest();
- }
- void CAutoRunDlg2::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:
- case (MSG_LOGIN_RESP + 0X2FFF) :
- {
- 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:
- case (MSG_CHATMESSAGE_RESP + 0X4FFF) :
- {
- 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 CAutoRunDlg2::OnDisconnect()
- {
- m_tClientTunnel.net_CloseSocket();
- SetConnectionID(INVALID_SOCKET);
- }
- void CAutoRunDlg2::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);
- }
- extern void MyGetIPByName(CString &name);
- extern CString newGUID();
- BOOL CAutoRunDlg2::ProcessLoginRequest()
- {
- WORD wMessageId = 0;
- if (g_nSrvType == 0)
- wMessageId = MSG_LOGIN_REQ + 0X1FFF;
- else
- 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 CAutoRunDlg2::OnDestroy()
- {
- OnDisconnect(); CDialog::OnDestroy();
- }
- void CAutoRunDlg2::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();
- }
- }
- extern CArray<CStringArray, CStringArray>g_List1array;
- extern DWORD g_nLeng;
- extern BYTE *g_pData;
- extern DWORD g_ncount2;
- void CAutoRunDlg2::ProcessChatMessageResponse(void *pResponse)
- {
- if (NULL == pResponse) return;
- TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU *)pResponse;
- int nMessageLen = pChatMessage->wMessageLen;
- // if(nMessageLen==0)
- // return;
- 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;
- if (nMessageLen <= sizeof(DWORD))
- {
- memcpy(&g_ncount2, pChatMessage->byFileContent, sizeof(DWORD));
- }
- g_bReturned2 = 1;
- }
|