123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668 |
- #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"
- BOOL BadUser(CString account)
- {
- for(int i=0; i<g_baduserarray.GetSize (); i++)
- {
- if(account==g_baduserarray.ElementAt (i))return 1;
- }
- return 0;
- }
- static void HandleServerNetEvent(IN SOCKET hSocket, IN ETransportEvent eEvent,
- IN void *pDataBuf, IN unsigned long nDataLen,
- IN int nError, IN void *pContext)
- {
- if( nError != TRANSPORT_OK ) return;
- CMainCtrl *pMainCtrl = (CMainCtrl *)pContext;
- if( NULL == pMainCtrl ) return;
-
- pMainCtrl->processNetEvent(hSocket, eEvent, pDataBuf, nDataLen);
- }
- CMainCtrl::CMainCtrl()
- {
- int nElementSize = sizeof(TUSER_GAME_INFO_STRU);
- m_tClientConnections.Init(nElementSize, "SERVERTUNNEL_CONNECTION_TABLE");
-
- }
- CMainCtrl::~CMainCtrl()
- {
- }
- int CMainCtrl::StartServer()
- {
-
- unsigned short usPort = 8379;
- int nResult = m_tServerTunnel.net_OpenSocket(Transport_Server,
- usPort,
- HandleServerNetEvent, this);
- if( TRANSPORT_OK != nResult )
- {
-
- return nResult;
- }
- return TRANSPORT_OK;
- }
- int CMainCtrl::StopServer()
- {
-
- 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 )
- {
-
- }
- 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);
-
- 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 )
- {
- sendMessageToAllUsers(&tHeader, (void *)&tUserListInfo, dwDataLen);
- }
- else
- {
- 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);
- }
- }
- 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;
-
-
- 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)
- {
- int i = 0;
- if(phoneno.IsEmpty ())return -1;
- for( i=0; i<phoneno.GetLength (); i++)
- {
- if(phoneno.GetAt (i)<'0'||phoneno.GetAt (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<sizeof(mobile)/sizeof(int); i++)
- {
- if(mobile[i]==atoi(phoneno.Left (3)))
- {
- return 0;
- }
- }
- for(i=0; i<sizeof(unicom)/sizeof(int); i++)
- {
- if(unicom[i]==atoi(phoneno.Left (3)))
- {
- return 1;
- }
- }
- for(i=0; i<sizeof(telecom)/sizeof(int); i++)
- {
- if(telecom[i]==atoi(phoneno.Left (3)))
- {
- return 2;
- }
- }
- return -1;
- }
- else if(phoneno.GetAt (0)=='0')
- {
- if(phoneno.GetLength ()>=10 && phoneno.GetLength ()<=12)
- {
- return 3;
- }
- }
- return -1;
- }
- 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)-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)
- {
- int nResult = LOGIN_RESULT_SUC;
- if( NULL == pLoginInfo )
- {
- return LOGIN_RESULT_FAILED;
- }
- TLOGIN_STRU *ptLoginInfo = (TLOGIN_STRU *)pLoginInfo;
- DWORD dwUserID = ptLoginInfo->tCommonMsg.dwConnectionID;
-
- 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; i<str.GetLength(); i++)
- {
- if ( (TBYTE)str[i] < 0x80 )
- {
- leng++;
- continue;
- }
- ucHigh = (TBYTE)str[i];
- if(i+1==str.GetLength())
- {
- leng++;
- break;
- }
- ucLow = (TBYTE)str[i+1];
- if ( ucHigh < 0xa1 || ucLow < 0xa1)
- {
- leng++;
- continue;
- }
- leng++;
- i++;
- }
- return leng;
- }
- int CMainCtrl::GetCount(CString phones, CString content)
- {
- int count=0;
- int leng=GetLengthEx(content);
- int m_nMobile=0;
- int m_nPhone=0;
-
- 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);
- for(int i=0; i<phonearray.GetSize (); i++)
- {
- phones=phonearray.ElementAt (i);
- if(CheckPhoneType(phones)==0 || CheckPhoneType(phones)==1 || CheckPhoneType(phones)==2)
- m_nMobile++;
- else if(CheckPhoneType(phones)==3)
- m_nPhone++;
- }
- if(m_nMobile)
- {
- count+=m_nMobile*(leng/70);
- if(leng%70)
- count+=m_nMobile;
- }
- if(m_nPhone)
- {
- count+=m_nPhone*(leng/56);
- if(leng%56)
- count+=m_nPhone;
- }
- return count;
- }
- BOOL CMainCtrl::IsExist(CString account, CString phones, CString content, CString timestamp)
- {
- CRecordset myset(&g_db);
- CString sql="select count(*) as cot from sendreg where [account]='"+account+"' and [content]='"+content+"' and [phones]='"+phones+"' and [timestamp]='"+timestamp+"'";
- myset.Open (CRecordset::forwardOnly, sql);
- if(!myset.IsEOF())
- {
- myset.GetFieldValue ("cot", sql);
- if(atoi(sql))
- return 1;
- else
- return 0;
- }
- else
- return 0;
- }
- void CMainCtrl::WriteLog(CString account, CString content)
- {
-
- }
|