AutoRunDlg2.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // AutoRunDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "AutoRun.h"
  5. #include "AutoRunDlg2.h"
  6. #include <afxsock.h>//<Winsock2.h>
  7. #include "ConnectDlg.h"
  8. #include "SendDataDlg.h"
  9. #include "AddContact.h"
  10. #include "SendReg.h"
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. extern DWORD g_nSendCode;
  17. extern BOOL g_bReturned2;
  18. /////////////////////////////////////////////////////////////////////////////
  19. extern HWND g_hSendWnd;
  20. extern HWND g_hConnectWnd;
  21. extern WORD g_nSrvType;
  22. #define DISCONNECT_TIMER (WM_USER + 101)
  23. #define LOGINFAILED_TIMER (WM_USER + 102)
  24. /////////////////////////////////////////////////////////////////////////////
  25. static void HandleClientNetEvent2(IN SOCKET hSocket, IN ETransportEvent eEvent,
  26. IN void *pDataBuf, IN unsigned long nDataLen,
  27. IN int nError, IN void *pContext)
  28. {
  29. if (nError != TRANSPORT_OK) return;
  30. CAutoRunDlg2 *pDlg = (CAutoRunDlg2 *)pContext;
  31. if (NULL == pDlg) return;
  32. pDlg->ProcessNetEvent(eEvent, pDataBuf, nDataLen);
  33. }
  34. CAutoRunDlg2::CAutoRunDlg2(CWnd* pParent /*=NULL*/)
  35. : CDialog(CAutoRunDlg2::IDD, pParent)
  36. {
  37. //{{AFX_DATA_INIT(CAutoRunDlg2)
  38. //}}AFX_DATA_INIT
  39. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  40. }
  41. void CAutoRunDlg2::DoDataExchange(CDataExchange* pDX)
  42. {
  43. CDialog::DoDataExchange(pDX);
  44. //{{AFX_DATA_MAP(CAutoRunDlg2)
  45. //}}AFX_DATA_MAP
  46. }
  47. BEGIN_MESSAGE_MAP(CAutoRunDlg2, CDialog)
  48. //{{AFX_MSG_MAP(CAutoRunDlg2)
  49. ON_WM_DESTROY()
  50. ON_WM_TIMER()
  51. //}}AFX_MSG_MAP
  52. END_MESSAGE_MAP()
  53. /////////////////////////////////////////////////////////////////////////////
  54. BOOL CAutoRunDlg2::OnInitDialog()
  55. {
  56. CDialog::OnInitDialog();
  57. // Set the icon for this dialog. The framework does this automatically
  58. // when the application's main window is not a dialog
  59. // g_pMainWnd=this;
  60. /////////////////////////////////
  61. // TODO: Add extra initialization here
  62. return TRUE; // return TRUE unless you set the focus to a control
  63. }
  64. DWORD CAutoRunDlg2::GetConnectionID()
  65. {
  66. return m_dwConnectionID;
  67. }
  68. void CAutoRunDlg2::SetConnectionID(DWORD dwConnection)
  69. {
  70. m_dwConnectionID = dwConnection;
  71. }
  72. void CAutoRunDlg2::ProcessChatMessageRequest2(CString sql)
  73. {
  74. const char *szDataBuf = sql.GetBuffer(0);
  75. sql.ReleaseBuffer();
  76. ProcessChatMessageRequest((void*)szDataBuf, strlen(szDataBuf) + 1);
  77. }
  78. void CAutoRunDlg2::ProcessChatMessageRequest2(BYTE code)
  79. {
  80. ProcessChatMessageRequest(&code, sizeof(BYTE));
  81. }
  82. void CAutoRunDlg2::ProcessChatMessageRequest2(BYTE *pData, int length)
  83. {
  84. ProcessChatMessageRequest((void*)pData, length);
  85. }
  86. BOOL CAutoRunDlg2::ProcessChatMessageRequest(void *szDataBuf, int nDataLen)
  87. {
  88. g_bReturned2 = 0;
  89. DWORD dwFromUserID = GetConnectionID();
  90. WORD wMessageId = 0;
  91. if (g_nSrvType == 0)
  92. wMessageId = MSG_CHATMESSAGE_REQ + 0X3FFF;
  93. else
  94. wMessageId = MSG_CHATMESSAGE_REQ;
  95. DWORD dwDataLen = sizeof(TCHAT_MESSAGE_STRU) + nDataLen;
  96. BYTE *pSendData = new BYTE[dwDataLen];
  97. TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU*)pSendData;
  98. memset(pChatMessage, 0x00, dwDataLen);
  99. g_sendhead.length[98] = 987123768;
  100. memcpy(pSendData, &g_sendhead, sizeof(g_sendhead));
  101. pChatMessage->tCommonMsg.dwConnectionID = GetConnectionID();
  102. pChatMessage->tCommonMsg.wMessageId = wMessageId;
  103. pChatMessage->dwFromUserID = dwFromUserID;
  104. pChatMessage->dwToUserID = 15;
  105. pChatMessage->wMessageLen = nDataLen;
  106. memcpy(pChatMessage->byFileContent, szDataBuf, nDataLen);
  107. TMessageHeader tHeader = { 0 };
  108. tHeader.wMessageId = wMessageId;
  109. tHeader.dwDataLen = dwDataLen;
  110. BOOL bRet = 0;
  111. unsigned long ulSendLen = m_tClientTunnel.net_Send(&tHeader, (void *)pChatMessage, dwDataLen);
  112. if (ulSendLen != SOCKET_ERROR)
  113. {
  114. bRet = 1;
  115. }
  116. delete[] pSendData;
  117. return bRet;
  118. }
  119. int g_conpos = 0;
  120. BOOL CAutoRunDlg2::OnConnectLocalServer()
  121. {
  122. OnDisconnect();
  123. CString strServerAddress = g_server2;
  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. #ifdef _DEBUG
  137. DWORD dwRemoteIPValue = inet_addr(_T("192.168.1.66"));
  138. #else
  139. DWORD dwRemoteIPValue = inet_addr(szServerAddress);
  140. #endif
  141. nResult = m_tClientTunnel.net_Connect(dwRemoteIPValue, Global::g_dwSvrPort);
  142. if (TRANSPORT_OK != nResult)
  143. {
  144. m_tClientTunnel.net_CloseSocket();
  145. if (g_serverarray.GetSize() > 1)
  146. {
  147. if (g_conpos < (g_serverarray.GetSize() - 1))
  148. {
  149. g_conpos++;
  150. return OnConnectLocalServer();
  151. }
  152. else
  153. {
  154. g_conpos = 0;
  155. return 0;
  156. }
  157. }
  158. return 0;
  159. }
  160. return ProcessLoginRequest();
  161. }
  162. void CAutoRunDlg2::ProcessNetEvent(int nEventType, void *pRecvMsg, DWORD dwDataLen)
  163. {
  164. if (Transport_ReadEv == nEventType)
  165. {
  166. if (NULL == pRecvMsg) return;
  167. TMessageHeader* pHeader = (TMessageHeader *)pRecvMsg;
  168. char *pDataBuf = (char *)pRecvMsg + MESSAGE_HEADER_LEN;
  169. WORD dwMessageID = pHeader->wMessageId;
  170. switch (dwMessageID)
  171. {
  172. case MSG_LOGIN_RESP:
  173. case (MSG_LOGIN_RESP + 0X2FFF) :
  174. {
  175. LOGIN_RESULT_STRU tLoginResult = { 0 };
  176. memcpy(&tLoginResult, pDataBuf, sizeof(LOGIN_RESULT_STRU));
  177. ProcessLoginResponse(&tLoginResult);
  178. break;
  179. }
  180. case MSG_USERINFO_RESP:
  181. {
  182. TUSERLIST_INFO_STRU tUserListInfo = { 0 };
  183. memcpy(&tUserListInfo, pDataBuf, sizeof(TUSERLIST_INFO_STRU));
  184. // ProcessUserListInfoResponse(&tUserListInfo);
  185. break;
  186. }
  187. case MSG_LOGOUT_RESP:
  188. {
  189. TUSERLIST_INFO_STRU tUserListInfo = { 0 };
  190. memcpy(&tUserListInfo, pDataBuf, sizeof(TUSERLIST_INFO_STRU));
  191. // ProcessLogoutResponse(&tUserListInfo);
  192. break;
  193. }
  194. case MSG_CHATMESSAGE_RESP:
  195. case (MSG_CHATMESSAGE_RESP + 0X4FFF) :
  196. {
  197. TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU *)pDataBuf;
  198. ProcessChatMessageResponse((void *)pChatMessage);
  199. break;
  200. }
  201. default:
  202. {
  203. break;
  204. }
  205. }
  206. }
  207. else if (Transport_CloseEv == nEventType)
  208. {
  209. SetTimer(DISCONNECT_TIMER, 0, NULL);
  210. }
  211. ///////
  212. }
  213. void CAutoRunDlg2::OnDisconnect()
  214. {
  215. m_tClientTunnel.net_CloseSocket();
  216. SetConnectionID(INVALID_SOCKET);
  217. }
  218. void CAutoRunDlg2::ProcessLoginResponse(void *pLoginResult)
  219. {
  220. if (NULL == pLoginResult) return;
  221. LOGIN_RESULT_STRU *ptLoginResult = (LOGIN_RESULT_STRU *)pLoginResult;
  222. DWORD dwConnectionID = ptLoginResult->tCommonMsg.dwConnectionID;
  223. SetConnectionID(dwConnectionID);
  224. BYTE byResult = ptLoginResult->byResult;
  225. if (LOGIN_RESULT_SUC == byResult)
  226. {
  227. }
  228. else
  229. {
  230. SetTimer(LOGINFAILED_TIMER, 0, NULL);
  231. }
  232. CString strCommentInfo("");
  233. strCommentInfo.Format("%s. 用户代号: %ld",
  234. ((LOGIN_RESULT_SUC == byResult) ? "登录成功" :
  235. (LOGIN_RESULT_MULTI == byResult) ? "重复登录" :
  236. (LOGIN_RESULT_NAMERROR == byResult) ? "名字不存在" :
  237. (LOGIN_RESULT_PWERROR == byResult) ? "密码错误" : "登录失败"),
  238. dwConnectionID);
  239. // MessageBox((CString)strCommentInfo);
  240. }
  241. extern void MyGetIPByName(CString &name);
  242. extern CString newGUID();
  243. BOOL CAutoRunDlg2::ProcessLoginRequest()
  244. {
  245. WORD wMessageId = 0;
  246. if (g_nSrvType == 0)
  247. wMessageId = MSG_LOGIN_REQ + 0X1FFF;
  248. else
  249. wMessageId = MSG_LOGIN_REQ;
  250. TLOGIN_STRU tLogonInfo = { 0 };
  251. tLogonInfo.tCommonMsg.dwConnectionID = GetConnectionID();
  252. tLogonInfo.tCommonMsg.wMessageId = wMessageId;
  253. CString strUserName = newGUID();
  254. strUserName.TrimLeft();
  255. strUserName.TrimRight();
  256. strcpy(tLogonInfo.tUserInfo.szUserName, strUserName);
  257. strUserName.TrimLeft();
  258. strUserName.TrimRight();
  259. strcpy(tLogonInfo.tUserInfo.szUserName, strUserName);
  260. DWORD dwDataLen = sizeof(TLOGIN_STRU);
  261. TMessageHeader tHeader = { 0 };
  262. tHeader.wMessageId = wMessageId;
  263. tHeader.dwDataLen = dwDataLen;
  264. unsigned long ulSendLen = m_tClientTunnel.net_Send(&tHeader, (void *)&tLogonInfo, dwDataLen);
  265. if (ulSendLen == SOCKET_ERROR)
  266. {
  267. return 0;
  268. }
  269. return 1;
  270. }
  271. //---------------------------------------------------------------------------
  272. void CAutoRunDlg2::OnDestroy()
  273. {
  274. OnDisconnect(); CDialog::OnDestroy();
  275. }
  276. void CAutoRunDlg2::OnTimer(UINT nIDEvent)
  277. {
  278. // TODO: Add your message handler code here and/or call default
  279. if (DISCONNECT_TIMER == nIDEvent)
  280. {
  281. KillTimer(nIDEvent);
  282. OnDisconnect();
  283. }
  284. else if (LOGINFAILED_TIMER == nIDEvent)
  285. {
  286. KillTimer(nIDEvent);
  287. OnDisconnect();
  288. }
  289. }
  290. extern CArray<CStringArray, CStringArray>g_List1array;
  291. extern DWORD g_nLeng;
  292. extern BYTE *g_pData;
  293. extern DWORD g_ncount2;
  294. void CAutoRunDlg2::ProcessChatMessageResponse(void *pResponse)
  295. {
  296. if (NULL == pResponse) return;
  297. TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU *)pResponse;
  298. int nMessageLen = pChatMessage->wMessageLen;
  299. // if(nMessageLen==0)
  300. // return;
  301. if (g_pData)delete[]g_pData; g_pData = NULL;
  302. g_pData = new BYTE[nMessageLen];
  303. memcpy(g_pData, pChatMessage->byFileContent, nMessageLen);
  304. memcpy(&g_sendhead, pChatMessage, sizeof(g_sendhead));
  305. g_nLeng = nMessageLen;
  306. if (nMessageLen <= sizeof(DWORD))
  307. {
  308. memcpy(&g_ncount2, pChatMessage->byFileContent, sizeof(DWORD));
  309. }
  310. g_bReturned2 = 1;
  311. }