GateInfo.cpp
上传用户:szopptop
上传日期:2013-04-23
资源大小:1047k
文件大小:15k
- #include "stdafx.h"
- #include "../def/dbmgr.h"
- #include <stdio.h>
- extern CWHList< GAMESERVERINFO* > g_xGameServerList;
- extern char g_szServerList[1024];
- int CompareDBString( char *str1, char *str2 )
- {
- ChangeSpaceToNull( str1 );
- ChangeSpaceToNull( str2 );
- return strcmp( str1, str2 );
- }
- int GetCertification()
- {
- static long g_nCertification = 30;
- InterlockedIncrement(&g_nCertification);
- if (g_nCertification >= 0x7FFFFFFF)
- g_nCertification = 30;
-
- return g_nCertification;
- }
- /* **************************************************************************************
- Close
-
- PURPOSE : Send packet to login gate server.
-
- NOTE
-
- 1. Packet construction : Packet % + client socket handle + / # + packet + ! + $
- ************************************************************************************** */
- void CGateInfo::Close()
- {
- PLISTNODE pListNode;
- CUserInfo* pUserInfo;
- if (xUserInfoList.GetCount())
- {
- pListNode = xUserInfoList.GetHead();
- while (pListNode)
- {
- pUserInfo = xUserInfoList.GetData(pListNode);
- if (pUserInfo)
- {
- delete pUserInfo;
- pUserInfo = NULL;
- pListNode = xUserInfoList.RemoveNode(pListNode);
- continue;
- }
- pListNode = xUserInfoList.GetNext(pListNode);
- }
- }
- closesocket(sock);
- sock = INVALID_SOCKET;
- }
- /* **************************************************************************************
- SendToGate
-
- PURPOSE : Send packet to login gate server.
-
- NOTE
-
- 1. Packet construction : Packet % + client socket handle + / # + packet + ! + $
- ************************************************************************************** */
- void CGateInfo::SendToGate(SOCKET cSock, char *pszPacket)
- {
- char szData[256];
- WSABUF buf;
- DWORD dwSendBytes;
-
- // wsprintf(szData, _TEXT("%%%d/#%s!$"), (int)cSock, pszPacket);
-
- int nLen = memlen(pszPacket) - 1;
- szData[0] = '%';
-
- char *pszNext = ValToAnsiStr((int)cSock, &szData[1]);
-
- *pszNext++ = '/';
- *pszNext++ = '#';
- memmove(pszNext, pszPacket, nLen);
- pszNext += nLen;
- *pszNext++ = '!';
- *pszNext++ = '$';
- *pszNext++ = ' ';
- buf.len = pszNext - szData;
- buf.buf = szData;
- WSASend(sock, &buf, 1, &dwSendBytes, 0, NULL, NULL);
- }
- /* **************************************************************************************
- MakeNewUser
-
- PURPOSE :
-
- NOTE
- ************************************************************************************** */
- void CGateInfo::MakeNewUser(char *pszPacket)
- {
- char szDecodeMsg[256];
- char szEncodeMsg[32];
- char *pszID, *pszName, *pszPassword;
- _TDEFAULTMESSAGE DefMsg;
- fnDecodeMessageA(&DefMsg, pszPacket);
- if (DefMsg.wIdent == CM_ADDNEWUSER)
- {
- int nPos = fnDecode6BitBufA((pszPacket + DEFBLOCKSIZE), szDecodeMsg, sizeof(szDecodeMsg));
- szDecodeMsg[nPos] = ' ';
- pszID = &szDecodeMsg[0];
-
- pszName = (char *)memchr(szDecodeMsg, '/', memlen(szDecodeMsg) - 1);
- *pszName = ' ';
- pszName++;
- pszPassword = (char *)memchr(pszName, '/', memlen(pszName) - 1);
- *pszPassword = ' ';
- pszPassword++;
- if ((memlen(pszID) - 1) || (memlen(pszName) - 1) || (memlen(pszPassword) - 1))
- {
- char szQuery[1024];
- sprintf( szQuery,
- "INSERT TBL_ACCOUNT( FLD_LOGINID, FLD_PASSWORD, FLD_USERNAME, FLD_CERTIFICATION ) "
- "VALUES( '%s', '%s', '%s', 0 )",
- pszID, pszPassword, pszName );
- CRecordset *pRec = GetDBManager()->CreateRecordset();
- if ( pRec->Execute( szQuery ) && pRec->GetRowCount() )
- fnMakeDefMessageA( &DefMsg, SM_NEWID_SUCCESS, 0, 0, 0, 0 );
- else
- fnMakeDefMessageA( &DefMsg, SM_NEWID_FAIL, 0, 0, 0, 0 );
- GetDBManager()->DestroyRecordset( pRec );
- // -----------------------------------------------------------------------------------
- }
- else
- fnMakeDefMessageA(&DefMsg, SM_NEWID_FAIL, 0, 0, 0, 0);
- fnEncodeMessageA(&DefMsg, szEncodeMsg, sizeof(szEncodeMsg));
- szDecodeMsg[0] = '#';
- memmove(&szDecodeMsg[1], szEncodeMsg, DEFBLOCKSIZE);
- szDecodeMsg[DEFBLOCKSIZE + 1] = '!';
- szDecodeMsg[DEFBLOCKSIZE + 2] = ' ';
- send(sock, szDecodeMsg, DEFBLOCKSIZE + 2, 0);
- }
- }
- /* **************************************************************************************
- ReceiveServerMsg
-
- PURPOSE :
-
- NOTE
- ************************************************************************************** */
- void CGateInfo::ReceiveServerMsg(char *pszPacket)
- {
- char *pszPos;
- int nCertification;
- int nLen = memlen(pszPacket);
- if (pszPos = (char *)memchr(pszPacket, '/', nLen))
- {
- *pszPos++ = ' ';
- nCertification = AnsiStrToVal(pszPos);
- char szQuery[256];
- sprintf( szQuery,
- "UPDATE TBL_ACCOUNT SET FLD_CERTIFICATION=%d WHERE FLD_LOGINID='%s'",
- nCertification, pszPacket );
- CRecordset *pRec = GetDBManager()->CreateRecordset();
- pRec->Execute( szQuery );
- GetDBManager()->DestroyRecordset( pRec );
- }
- }
- void CGateInfo::ReceiveOpenUser(char *pszPacket)
- {
- char *pszPos;
- int nSocket;
- int nLen = memlen(pszPacket);
- if (pszPos = (char *)memchr(pszPacket, '/', nLen))
- {
- nSocket = AnsiStrToVal(pszPacket);
- pszPos++;
- CUserInfo* pUserInfo = new CUserInfo;
- if (pUserInfo)
- {
- MultiByteToWideChar(CP_ACP, 0, pszPacket, -1, pUserInfo->szSockHandle, sizeof(pUserInfo->szSockHandle)/sizeof(TCHAR));
- MultiByteToWideChar(CP_ACP, 0, pszPos, -1, pUserInfo->szAddress, sizeof(pUserInfo->szAddress)/sizeof(TCHAR));
- pUserInfo->sock = nSocket;
- pUserInfo->nCertification = 0;
- pUserInfo->nClientVersion = 0;
- pUserInfo->fSelServerOk = FALSE;
- ZeroMemory(pUserInfo->szUserID, sizeof(pUserInfo->szUserID));
- xUserInfoList.AddNewNode(pUserInfo);
- InsertLogMsgParam(IDS_OPEN_USER, pUserInfo->szAddress);
- }
- }
- }
- /* **************************************************************************************
- ReceiveCloseUser
-
- PURPOSE :
-
- NOTE
- ************************************************************************************** */
- void CGateInfo::ReceiveCloseUser(char *pszPacket)
- {
- int nSocket = AnsiStrToVal(pszPacket);
- }
- /* **************************************************************************************
- ReceiveSendUser
-
- PURPOSE :
-
- NOTE
- ************************************************************************************** */
- void CGateInfo::ReceiveSendUser(char *pszPacket)
- {
- char *pszPos;
- int nSocket;
- int nLen = memlen(pszPacket);
- if ((pszPos = (char *)memchr(pszPacket, '/', nLen)))
- {
- nSocket = AnsiStrToVal(pszPacket);
- pszPos++;
- _LPTSENDBUFF lpSendUserData = new _TSENDBUFF;
- lpSendUserData->sock = (SOCKET)nSocket;
- memmove(lpSendUserData->szData, pszPos, memlen(pszPos));
- g_SendToGateQ.PushQ((BYTE *)lpSendUserData);
- }
- }
- /* **************************************************************************************
- ProcSelectServer
-
- PURPOSE :
-
- NOTE
- ************************************************************************************** */
- void CGateInfo::ProcSelectServer(SOCKET s, WORD wServerIndex)
- {
- _TDEFAULTMESSAGE DefMsg;
- char szEncodePacket[128];
- char szEncodeAllPacket[256];
- char szEncodeMsg[24];
- char *pServerIP;
- GAMESERVERINFO *pServerInfo;
- PLISTNODE pListNode = xUserInfoList.GetHead();
- while (pListNode)
- {
- CUserInfo *pUserInfo = xUserInfoList.GetData(pListNode);
- if (pUserInfo->sock == s)
- {
- if (!pUserInfo->fSelServerOk)
- {
- fnMakeDefMessageA(&DefMsg, SM_SELECTSERVER_OK, 0, pUserInfo->nCertification, 0, 0);
- int nPos = fnEncodeMessageA(&DefMsg, szEncodeMsg, sizeof(szEncodePacket));
- szEncodeMsg[nPos] = ' ';
- for ( PLISTNODE pNode = g_xGameServerList.GetHead(); pNode; pNode = g_xGameServerList.GetNext( pNode ) )
- {
- pServerInfo = g_xGameServerList.GetData( pNode );
-
- if ( pServerInfo->index == wServerIndex )
- {
- pServerIP = pServerInfo->ip;
- pServerInfo->connCnt++;
- break;
- }
- }
- if ( !pServerIP )
- break;
- pUserInfo->nServerID = wServerIndex;
- int nPos2 = fnEncode6BitBufA((unsigned char *)pServerIP, szEncodePacket, memlen(pServerIP), sizeof(szEncodePacket));
- szEncodePacket[nPos2] = ' ';
- memmove(szEncodeAllPacket, szEncodeMsg, nPos);
- memmove(&szEncodeAllPacket[nPos], szEncodePacket, nPos2);
- szEncodeAllPacket[nPos + nPos2] = ' ';
- SendToGate(s, szEncodeAllPacket);
- pUserInfo->fSelServerOk = TRUE;
- pListNode = xUserInfoList.RemoveNode(pListNode);
- }
- }
- else
- pListNode = xUserInfoList.GetNext(pListNode);
- }
- }
- /* **************************************************************************************
- ParseUserEntry
-
- PURPOSE :
-
- NOTE
- ************************************************************************************** */
- bool CGateInfo::ParseUserEntry( char *buf, _AUSERENTRYINFO *userInfo )
- {
- char seps[] = "