MainWndProc.cpp
上传用户:szopptop
上传日期:2013-04-23
资源大小:1047k
文件大小:6k
- #include "stdafx.h"
- #include "../def/dbmgr.h"
- LPARAM OnServerSockMsg(WPARAM wParam, LPARAM lParam);
- LPARAM OnClientSockMsg(WPARAM wParam, LPARAM lParam);
- BOOL jRegSetKey(LPCTSTR pSubKeyName, LPCTSTR pValueName, DWORD dwFlags, LPBYTE pValue, DWORD nValueSize);
- BOOL jRegGetKey(LPCTSTR pSubKeyName, LPCTSTR pValueName, LPBYTE pValue);
- UINT WINAPI ProcessDBMsg(LPVOID lpParameter);
- UINT WINAPI ProcessGateMsg(LPVOID lpParameter);
- BOOL InitGateCommSocket(SOCKET &s, SOCKADDR_IN* addr, UINT nMsgID, int nPort, long lEvent);
- LPARAM OnGateCommSockMsg(WPARAM wParam, LPARAM lParam);
- BOOL CALLBACK ConfigDlgFunc(HWND hWndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
- extern HINSTANCE g_hInst;
- extern HWND g_hMainWnd;
- extern HWND g_hLogMsgWnd;
- extern HWND g_hToolBar;
- extern HWND g_hStatusBar;
- BOOL g_fTerminated = FALSE;
- SOCKET g_ssock = INVALID_SOCKET;
- SOCKADDR_IN g_saddr;
- SOCKET g_gssock = INVALID_SOCKET;
- SOCKADDR_IN g_gsaddr;
- CWHList<GAMESERVERINFO*> g_xGameServerList;
- void SwitchMenuItem(BOOL fFlag)
- {
- HMENU hMainMenu = GetMenu(g_hMainWnd);
- HMENU hMenu = GetSubMenu(hMainMenu, 0);
- if (fFlag)
- {
- EnableMenuItem(hMenu, IDM_STARTSERVICE, MF_GRAYED|MF_BYCOMMAND);
- EnableMenuItem(hMenu, IDM_STOPSERVICE, MF_ENABLED|MF_BYCOMMAND);
- SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STARTSERVICE, (LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0));
- SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STOPSERVICE, (LPARAM)MAKELONG(TBSTATE_ENABLED, 0));
- InsertLogMsg(IDS_STARTSERVICE);
- SendMessage(g_hStatusBar, SB_SETTEXT, MAKEWORD(0, 0), (LPARAM)_T("Ready"));
- }
- else
- {
- EnableMenuItem(hMenu, IDM_STARTSERVICE, MF_ENABLED|MF_BYCOMMAND);
- EnableMenuItem(hMenu, IDM_STOPSERVICE, MF_GRAYED|MF_BYCOMMAND);
- SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STARTSERVICE, (LPARAM)MAKELONG(TBSTATE_ENABLED, 0));
- SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STOPSERVICE, (LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0));
- InsertLogMsg(IDS_STOPSERVICE);
- }
- }
- void LoadCharacterRecords()
- {
- InsertLogMsg(IDS_LOADACCOUNTRECORDS);
- CRecordset *pRec = GetDBManager()->CreateRecordset();
- if ( pRec->Execute( "SELECT * FROM TBL_GAMEGATEINFO" ) )
- {
- GAMESERVERINFO *pServerInfo;
- while ( pRec->Fetch() )
- {
- pServerInfo = new GAMESERVERINFO;
- if ( !pServerInfo )
- break;
- pServerInfo->index = atoi( pRec->Get( "FLD_SERVERIDX" ) );
- strcpy( pServerInfo->name, pRec->Get( "FLD_SERVERNAME" ) );
- strcpy( pServerInfo->ip, pRec->Get( "FLD_SERVERIP" ) );
- pServerInfo->connCnt = 0;
- g_xGameServerList.AddNewNode( pServerInfo );
- }
- }
- GetDBManager()->DestroyRecordset( pRec );
- }
- void OnCommand(WPARAM wParam, LPARAM lParam)
- {
- int nPort;
- switch (LOWORD(wParam))
- {
- case IDM_STARTSERVICE:
- {
- g_fTerminated = FALSE;
- if (!jRegGetKey(_DB_SERVER_REGISTRY, _TEXT("LocalPort"), (LPBYTE)&nPort))
- nPort = 5000;
-
- LoadCharacterRecords();
- InitServerSocket(g_ssock, &g_saddr, _IDM_GATECOMMSOCK_MSG, 6000, 1);
- InitGateCommSocket(g_gssock, &g_gsaddr, _IDM_GATECOMMSOCK_MSG, 5100, FD_ACCEPT|FD_READ|FD_CLOSE);
- UINT dwThreadIDForMsg = 0;
- unsigned long hThreadForMsg = 0;
-
- // if (hThreadForMsg = _beginthreadex(NULL, 0, ProcessUserHuman, NULL, 0, &dwThreadIDForMsg))
- hThreadForMsg = _beginthreadex(NULL, 0, ProcessDBMsg, NULL, 0, &dwThreadIDForMsg);
- hThreadForMsg = _beginthreadex(NULL, 0, ProcessGateMsg, NULL, 0, &dwThreadIDForMsg);
- SwitchMenuItem(TRUE);
- return;
- }
- case IDM_STOPSERVICE:
- {
- g_fTerminated = TRUE;
- SwitchMenuItem(FALSE);
- return;
- }
- case IDM_CONFIG:
- {
- DialogBox(g_hInst, MAKEINTRESOURCE(IDD_CONFIGDLG), NULL, (DLGPROC)ConfigDlgFunc);
- return;
- }
- }
- }
- // **************************************************************************************
- //
- //
- //
- // **************************************************************************************
- LPARAM APIENTRY MainWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
- {
- switch (nMsg)
- {
- case _IDM_GATECOMMSOCK_MSG:
- return OnGateCommSockMsg(wParam, lParam);
- case WM_COMMAND:
- OnCommand(wParam, lParam);
- break;
- case WM_SIZE:
- {
- if (g_hToolBar && g_hMainWnd && g_hStatusBar)
- {
- RECT rcToolBar, rcMain, rcStatusBar;
- GetWindowRect(g_hToolBar, &rcToolBar);
- GetClientRect(g_hMainWnd, &rcMain);
- GetWindowRect(g_hStatusBar, &rcStatusBar);
- MoveWindow(g_hToolBar, 0, 0, LOWORD(lParam), (rcToolBar.bottom - rcToolBar.top), TRUE);
- MoveWindow(g_hStatusBar, 0, rcMain.bottom - (rcStatusBar.bottom - rcStatusBar.top),
- LOWORD(lParam), (rcStatusBar.bottom - rcStatusBar.top), TRUE);
- MoveWindow(g_hLogMsgWnd, 0, (rcToolBar.bottom - rcToolBar.top) - 2, (rcMain.right - rcMain.left),
- (rcMain.bottom - rcMain.top) - (rcToolBar.bottom - rcToolBar.top) - (rcStatusBar.bottom - rcStatusBar.top) + 2,
- TRUE);
- int nStatusPartsWidths[_NUMOFMAX_STATUS_PARTS];
- int nCnt = 0;
- for (int i = _NUMOFMAX_STATUS_PARTS - 1; i >= 0 ; i--)
- nStatusPartsWidths[nCnt++] = (rcStatusBar.right - rcStatusBar.left) - (90 * i);
- SendMessage(g_hStatusBar, SB_SETPARTS, _NUMOFMAX_STATUS_PARTS, (LPARAM)nStatusPartsWidths);
- }
- break;
- }
- case WM_CLOSE:
- {
- TCHAR szMsg[128];
- TCHAR szTitle[128];
- LoadString(g_hInst, IDS_PROGRAM_QUIT, szMsg, sizeof(szMsg));
- LoadString(g_hInst, IDS_PROGRAM_TITLE, szTitle, sizeof(szTitle));
- if (MessageBox(g_hMainWnd, szMsg, szTitle, MB_ICONSTOP|MB_YESNO) == IDYES)
- {
- if (SendMessage(g_hToolBar, TB_GETSTATE, (WPARAM)IDM_STARTSERVICE, (LPARAM)0L) == TBSTATE_INDETERMINATE)
- OnCommand(IDM_STOPSERVICE, 0L);
- ClearSocket(g_ssock);
- WSACleanup();
- CoUninitialize();
- PostQuitMessage(0);
- }
- return 0L;
- }
- }
- return (DefWindowProc(hWnd, nMsg, wParam, lParam));
- }