//mainCtrl.cpp TCHAT_MESSAGE_STRU #include "stdafx.h" /////////////////////////////////////////////////////////////////////////////// #include "mainCtrl.h" #include "mysqldata.h" #include "LYFZIPManage.h" extern CString g_mainpath; extern CDatabase g_db; CStringArray g_baduserarray; #include "MyTimer.h" ////////////////////////////////////////clientip BOOL BadUser(CString account) { for(int i=0; iprocessNetEvent(hSocket, eEvent, pDataBuf, nDataLen); } /////////////////////////////////////////////////////////////////////////////// CMainCtrl::CMainCtrl() { int nElementSize = sizeof(TUSER_GAME_INFO_STRU); m_tClientConnections.Init(nElementSize, "SERVERTUNNEL_CONNECTION_TABLE"); // g_baduserarray.Add ("564531"); } CMainCtrl::~CMainCtrl() { } int CMainCtrl::StartServer() { /*for Client, Port can be 0, and for Server, Port is Listening Port*/ unsigned short usPort = 8379; int nResult = m_tServerTunnel.net_OpenSocket(Transport_Server, usPort, HandleServerNetEvent, this); if( TRANSPORT_OK != nResult ) { // printf("net_OpenSocket failed!\n"); return nResult; } // printf("Server is ready! Port = %d...\n", usPort); return TRANSPORT_OK; } int CMainCtrl::StopServer() { //printf("\nNow is Stopping Server, please wait..."); m_tServerTunnel.net_CloseSocket(); m_tClientConnections.End(); return TRANSPORT_OK; } CTableInfoMgr *CMainCtrl::GetClientConnectionTable() { return &m_tClientConnections; } /////////////////////////////////////////////////////////////////////////////// void CMainCtrl::processNetEvent(IN SOCKET hSocket, IN ETransportEvent eEvent, IN void *pRecvMsg, IN unsigned long nDataLen) { unsigned long ulUserID = hSocket; if( Transport_AcceptEv == eEvent ) { // printf("Accept one new connection. Socket = %d\n", hSocket); } else if( Transport_ReadEv == eEvent ) { if( NULL == pRecvMsg ) return; TMessageHeader* pHeader = (TMessageHeader *)pRecvMsg; char *pDataBuf = (char *)pRecvMsg + MESSAGE_HEADER_LEN; WORD dwMessageID = pHeader->wMessageId; switch(dwMessageID) { case MSG_LOGIN_REQ: { TLOGIN_STRU tLoginInfo = {0}; memcpy(&tLoginInfo, pDataBuf, sizeof(TLOGIN_STRU)); DWORD dwConnectionID = tLoginInfo.tCommonMsg.dwConnectionID; if( dwConnectionID != hSocket ) { dwConnectionID = hSocket; tLoginInfo.tCommonMsg.dwConnectionID = dwConnectionID; } processLoginRequest(&tLoginInfo); break; } case MSG_CHATMESSAGE_REQ: { TCHAT_MESSAGE_STRU *pChatMessage = (TCHAT_MESSAGE_STRU *)pDataBuf; DWORD dwConnectionID = pChatMessage->tCommonMsg.dwConnectionID; if( dwConnectionID != hSocket ) { dwConnectionID = hSocket; pChatMessage->tCommonMsg.dwConnectionID = dwConnectionID; } processChatMessageRequest((void *)pChatMessage); break; } } } else if( Transport_CloseEv == eEvent ) { unsigned long ulUserID = hSocket; processDiconnection(ulUserID); } } void CMainCtrl::processLoginRequest(void *pLoginInfo) { if( NULL == pLoginInfo ) return; TLOGIN_STRU *ptLoginInfo = (TLOGIN_STRU *)pLoginInfo; DWORD dwUserID = ptLoginInfo->tCommonMsg.dwConnectionID; int nResult = VerifyUserLoginInfo((void *)ptLoginInfo); DWORD dwConnectionID = (LOGIN_RESULT_SUC == nResult) ? dwUserID : INVALID_SOCKET; WORD wMessageId = MSG_LOGIN_RESP; LOGIN_RESULT_STRU tLoginResult = {0}; tLoginResult.tCommonMsg.dwConnectionID = dwConnectionID; tLoginResult.tCommonMsg.wMessageId = wMessageId; tLoginResult.byResult = nResult; tLoginResult.dwUserID = dwConnectionID; tLoginResult.byStatus = (LOGIN_RESULT_SUC == nResult) ? USER_STATUS_ONLINE : USER_STATUS_OFFLINE; CTableInfoMgr *pConnectionTable = GetClientConnectionTable(); long lCount = pConnectionTable->GetItemCount(); tLoginResult.wUserCount = (WORD)lCount; DWORD dwDataLen = sizeof(LOGIN_RESULT_STRU); TMessageHeader tHeader = {0}; tHeader.wMessageId = wMessageId; tHeader.dwDataLen = dwDataLen; SOCKET hSocket = (SOCKET)dwUserID; m_tServerTunnel.net_Send(hSocket, &tHeader, (void *)&tLoginResult, dwDataLen); /*Send User Info*/ TUSERLIST_INFO_STRU tUserListInfo = {0}; dwDataLen = sizeof(TUSERLIST_INFO_STRU); memset(&tHeader, 0x00, sizeof(TMessageHeader)); tHeader.wMessageId = MSG_USERINFO_RESP; tHeader.dwDataLen = dwDataLen; for(long lIndex = 0; lIndex < lCount; lIndex++) { TUSER_GAME_INFO_STRU *pUserInfo = (TUSER_GAME_INFO_STRU *)pConnectionTable->GetItemValue(lIndex); if( NULL == pUserInfo ) continue; memset(&tUserListInfo, 0x00, sizeof(TUSERLIST_INFO_STRU)); tUserListInfo.dwConnectionID = pUserInfo->dwConnectionID; tUserListInfo.byStatus = pUserInfo->tUserInfo.byStatus; strcpy(tUserListInfo.szUserName, pUserInfo->tUserInfo.szUserName); if( dwUserID == pUserInfo->dwConnectionID ) /*The owner*/ { sendMessageToAllUsers(&tHeader, (void *)&tUserListInfo, dwDataLen); } else /*The Others*/ { m_tServerTunnel.net_Send(hSocket, &tHeader, (void *)&tUserListInfo, dwDataLen); } } } void CMainCtrl::processDiconnection(unsigned long ulUserID) { CTableInfoMgr *pConnectionTable = GetClientConnectionTable(); TUSER_GAME_INFO_STRU *pUserInfo = (TUSER_GAME_INFO_STRU *)pConnectionTable->Find(ulUserID); if( NULL != pUserInfo ) { TUSERLIST_INFO_STRU tUserListInfo = {0}; DWORD dwDataLen = sizeof(TUSERLIST_INFO_STRU); TMessageHeader tHeader = {0}; memset(&tHeader, 0x00, sizeof(TMessageHeader)); tHeader.wMessageId = MSG_LOGOUT_RESP; tHeader.dwDataLen = dwDataLen; tUserListInfo.dwConnectionID = pUserInfo->dwConnectionID; tUserListInfo.byStatus = USER_STATUS_OFFLINE; strcpy(tUserListInfo.szUserName, pUserInfo->tUserInfo.szUserName); pConnectionTable->Delete(ulUserID); sendMessageToAllUsers(&tHeader, (void *)&tUserListInfo, dwDataLen); } // printf("Disconnect one connection. Socket = %ld\n", ulUserID); } void FillHeader(BYTE *pSendData, WORD wMessageId, DWORD dwDataLen) { TMessageHeader *pMessageHeader =(TMessageHeader*)pSendData; pMessageHeader->byVersion = 101; pMessageHeader->wHeaderFlag = MESSAGE_HEADER_FLAG; pMessageHeader->wMessageId = wMessageId; pMessageHeader->wMessageSubId = 0; pMessageHeader->dwDataLen = dwDataLen; pMessageHeader->wReserve = 0; /* convert network word */ htons(pMessageHeader->wHeaderFlag); htons(pMessageHeader->wMessageId); htons(pMessageHeader->wMessageSubId); htonl(pMessageHeader->dwDataLen); htons(pMessageHeader->wCheckSum); htonl(pMessageHeader->wReserve); } BOOL CheckUser(CString account, CString psw) { CRstClientInfo rsSt; rsSt.m_strFilter ="[account]='"+account+"' and [psw]='"+psw+"'"; rsSt.Open(); if(!rsSt.IsEOF()) { rsSt.Close(); return 1; } rsSt.Close(); return 0; } int CheckPhoneType(CString phoneno)//移动:0 联通:1 电信:2 小灵通:3 未知:-1 { int i = 0; if(phoneno.IsEmpty ())return -1; for( i=0; i'9')return -1; } if(phoneno.GetAt (0)=='1') { if(phoneno.GetLength ()!=11) return -1; int mobile[]={139,138,137,136,135,134,159,158,152,151,150,157,188,187,144,182}; int unicom[]={130,131,132,155,156,186,185}; int telecom[]={133,153,189,180,181}; for(i=0; i=10 && phoneno.GetLength ()<=12) { return 3; } } return -1; } /* 移动: 139,138,137,136,135,134,159,158,152,151,150,157,188,187,144 联通: 130,131,132,155,156,186,185 电信: 133,153,189,180 */ extern void ReCalAccount(CString account); extern void WriteLogin(CString str); int GetBalance(CString account) { CString count1=""; CString count2=""; CString count3=""; try { CRecordset myset(&g_db); CString sql="select sum([count]) as cot from recharge where account='"+account+"'"; myset.Open (CRecordset::forwardOnly, sql); if(!myset.IsEOF()) myset.GetFieldValue ("cot", count1); myset.Close(); sql="select sum([msgcount]) as cot from sendreg where account='"+account+"'"; myset.Open (CRecordset::forwardOnly, sql); if(!myset.IsEOF()) myset.GetFieldValue ("cot", count2); myset.Close(); sql="select sum([msgcount2]) as cot from sendreg where account='"+account+"'"; myset.Open (CRecordset::forwardOnly, sql); if(!myset.IsEOF()) myset.GetFieldValue ("cot", count3); myset.Close(); // return atoi(count1)-min(atoi(count2),atoi(count3)); return atoi(count1)-atoi(count2); } catch(...) { return -1; } } BOOL g_bWork=0; void CMainCtrl::processChatMessageRequest(void *pChatMsg) { } void CMainCtrl::sendMessageToAllUsers(void *pHeader, void *pDataBuf, unsigned long ulDataLen) { if( (NULL == pHeader) || (NULL == pDataBuf) || (0 >= ulDataLen) ) return; CTableInfoMgr *pConnectionTable = GetClientConnectionTable(); long lCount = pConnectionTable->GetItemCount(); for(long lIndex = 0; lIndex < lCount; lIndex++) { TUSER_GAME_INFO_STRU *pUserInfo = (TUSER_GAME_INFO_STRU *)pConnectionTable->GetItemValue(lIndex); if( NULL == pUserInfo ) continue; SOCKET hSocket = (SOCKET)pUserInfo->dwConnectionID; m_tServerTunnel.net_Send(hSocket, pHeader, pDataBuf, ulDataLen); } } /////////////////////////////////////////////////////////////////////////////// int CMainCtrl::VerifyUserLoginInfo(void *pLoginInfo) { /* static ticks=::GetTickCount (); if(::GetTickCount ()-ticks<500) { ticks=::GetTickCount (); return LOGIN_RESULT_FAILED; } ticks=::GetTickCount ();*/ int nResult = LOGIN_RESULT_SUC; if( NULL == pLoginInfo ) { return LOGIN_RESULT_FAILED; } TLOGIN_STRU *ptLoginInfo = (TLOGIN_STRU *)pLoginInfo; DWORD dwUserID = ptLoginInfo->tCommonMsg.dwConnectionID; /* CString ip; struct sockaddr_in sa; int salen = sizeof(sa); SOCKET s=dwUserID; ::getpeername( s, (struct sockaddr*)&sa, &salen ); ip.Format ("%d.%d.%d.%d", (BYTE)sa.sin_addr.S_un.S_un_b.s_b1 , (BYTE)sa.sin_addr.S_un.S_un_b.s_b2 , (BYTE)sa.sin_addr.S_un.S_un_b.s_b3 , (BYTE)sa.sin_addr.S_un.S_un_b.s_b4); CRecordset myset(&g_db); CString sql="select count(*) as cot from noregisterdomain where [ip]='"+ip+"'"; myset.Open (CRecordset::forwardOnly, sql); myset.GetFieldValue ("cot", sql); myset.Close(); if(atoi(sql)) { WriteLogin2(ip+"-此IP未绑定"); shutdown(s,SD_BOTH); closesocket(s); return LOGIN_RESULT_FAILED; }*/ /*verify the user information*/ CTableInfoMgr *pConnectionTable = GetClientConnectionTable(); long lCount = pConnectionTable->GetItemCount(); for(long lIndex = 0; lIndex < lCount; lIndex++) { TUSER_GAME_INFO_STRU *pUserInfo = (TUSER_GAME_INFO_STRU *)pConnectionTable->GetItemValue(lIndex); if( NULL == pUserInfo ) continue; if( (0 == stricmp(pUserInfo->tUserInfo.szUserName, ptLoginInfo->tUserInfo.szUserName)) && (USER_STATUS_ONLINE == pUserInfo->tUserInfo.byStatus) ) { return LOGIN_RESULT_MULTI; } } TUSER_GAME_INFO_STRU *pUserGameInfo = new TUSER_GAME_INFO_STRU; memset(pUserGameInfo, 0x00, sizeof(TUSER_GAME_INFO_STRU)); pUserGameInfo->dwConnectionID = dwUserID; pUserGameInfo->tUserInfo.dwUserID = dwUserID; strcpy(pUserGameInfo->tUserInfo.szUserName, ptLoginInfo->tUserInfo.szUserName); strcpy(pUserGameInfo->tUserInfo.szPassword, ptLoginInfo->tUserInfo.szPassword); pUserGameInfo->tUserInfo.byStatus = USER_STATUS_ONLINE; pConnectionTable->Add(pUserGameInfo->dwConnectionID, (void *)pUserGameInfo); return nResult; } int CMainCtrl::GetLengthEx(CString str) { int leng=0; TBYTE ucHigh, ucLow; for (int i=0; ileng[a]) { while(GetLengthEx(contenttemp)>leng[a]) { for(int pos=leng[a]; posAdd (contenttemp); contenttemp.Empty (); break; } else { pArray[a]->Add (contenttemp.Left (pos)); contenttemp=contenttemp.Right (contenttemp.GetLength ()-pos); } } if(!contenttemp.IsEmpty ()) pArray[a]->Add (contenttemp); } else pArray[a]->Add (content); } CStringArray phonearray; int pos=phones.Find (","); if(pos!=-1) { phonearray.Add(phones.Left (pos)); phones=phones.Right (phones.GetLength ()-pos-1); pos=phones.Find (","); while(pos!=-1) { phonearray.Add (phones.Left (pos)); phones=phones.Right (phones.GetLength ()-pos-1); pos=phones.Find (","); } phonearray.Add (phones); } else phonearray.Add (phones); int phonearraysize1,phonearraysize2,phonearraysize3,phonearraysize4; CArraymultiphonearray1; CArraymultiphonearray2; CArraymultiphonearray3; CArraymultiphonearray4; phonearraysize1=phonearraysize2=phonearraysize3=phonearraysize4=1; multiphonearray1.SetSize(phonearraysize1);multiphonearray1.ElementAt (0).RemoveAll (); multiphonearray2.SetSize(phonearraysize2);multiphonearray2.ElementAt (0).RemoveAll (); multiphonearray3.SetSize(phonearraysize3);multiphonearray3.ElementAt (0).RemoveAll (); multiphonearray4.SetSize(phonearraysize4);multiphonearray4.ElementAt (0).RemoveAll (); int size[4]={1,1,1,1}; CArray *pPhoneArray[4]={&multiphonearray1,&multiphonearray2,&multiphonearray3,&multiphonearray4}; for(int i=0; iElementAt (size[pos]-1).GetSize ()>=1000) { size[pos]++; pPhoneArray[pos]->SetSize(size[pos]); pPhoneArray[pos]->ElementAt (size[pos]-1).RemoveAll (); pPhoneArray[pos]->ElementAt (size[pos]-1).Add (phones); } else { pPhoneArray[pos]->ElementAt (size[pos]-1).Add (phones); } } /// for(i=0; i<4; i++) { for(int j=0; jGetSize (); j++) { if(!pPhoneArray[i]->ElementAt(j).GetSize ())continue; phones.Empty (); for(int n=0; nElementAt (j).GetSize (); n++) { phones+=pPhoneArray[i]->ElementAt (j).ElementAt (n); phones+=","; } phones.TrimRight (","); if(i<3) { for(int m=0; m