⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 msgserverdlg.cpp

📁 网络游戏魔域源代码 测试可以完整变异
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MsgServerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "protocol.h"
#include "common.h"
#include "IniFile.h"
#include "LogFile.h"
#include "I_Shell.h"
#include "SocketThread.h"
#include "WorldThread.h"
#include "MapGroupThread.h"
#include "MessagePort.h"
#include "P_ServerManager.h"
#include "basetype.h"
#include "array.h"
#include "mystack.h"
#include "I_MessagePort.h"

#include "MsgServer.h"
#include "MsgServerDlg.h"

typedef	CMyStack<const char*>	DEADLOOP;
typedef	Array<DEADLOOP>		DEADLOOP_SET;
DEADLOOP_SET	g_setDeadLoop;
inline DEADLOOP_SET&	DeadLoopSet() { return g_setDeadLoop; }
/////////////////////////////////////////////////////////////////////////////
ST_CONFIG		CONFIG;	

/////////////////////////////////////////////////////////////////////////////
struct STAT_STRUCT	g_stat = {0};
long				g_nRestart		= false;
long				g_nServerClosed = false;

long				s_nDatabaseTimeSum;

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMsgServerDlg dialog

CMsgServerDlg::CMsgServerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMsgServerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMsgServerDlg)
	m_sKernelState = _T("");
	m_sShellState = _T("");
	m_sTalk = _T("");
	m_sText = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_nState		= SHELLSTATE_NONE;
	m_nTextLines	= 0;
	m_pMsgPort		= NULL;
	m_pInterPort		= NULL;
	m_pSocketThread	= NULL;
	m_pWorldThread	= NULL;

	m_hMutexThread	= NULL;

	m_nAllPlayers	= 0;
	m_nMaxPlayers	= 0;
}

void CMsgServerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMsgServerDlg)
	DDX_Text(pDX, IDC_KERNEL, m_sKernelState);
	DDX_Text(pDX, IDC_SHELL, m_sShellState);
	DDX_Text(pDX, IDC_TALK, m_sTalk);
	DDX_Text(pDX, IDC_TEXT, m_sText);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMsgServerDlg, CDialog)
	//{{AFX_MSG_MAP(CMsgServerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_SEND, OnSend)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMsgServerDlg message handlers

BOOL CMsgServerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
#if	!defined(PALED_DEBUG)
#ifdef _DEBUG
	this->MessageBox("这个版本的服务器端,只可以在内部运行,绝对禁止在外部运行!", "Warning", MB_OK|MB_ICONEXCLAMATION);
#endif
#endif

	memset(&g_stat, 0, sizeof(g_stat));
	DateTime(m_szStartServer, time(NULL));

	m_hMutexServer = ::CreateMutex(NULL, false, "ConquerServer");
	if (m_hMutexServer)
	{
		if (ERROR_ALREADY_EXISTS == ::GetLastError())
		{
			::ReleaseMutex(m_hMutexServer);
			::CloseHandle(m_hMutexServer);
			m_hMutexServer = NULL;
			MessageBox("Repeat run game server!");
			this->EndDialog(-1);
			return false;
		}
	}
	else
	{
		MessageBox("Create mutex failed!");
		this->EndDialog(-1);
		return false;
	}

#ifdef	DEBUG_MULTITHREAD
	m_hMutexThread    =::CreateMutex(NULL, false, "FW_DEBUG_MULTITHREAD");
	if(!m_hMutexThread)
	{
		PrintText("Create mutex handle failed!");
		return false;
	}
#endif
	if(!LoadConfigIni())
		MessageBox("Load config.ini failed!");
	else if(!CMessagePort::InitPortSet(MSGPORT_MAPGROUP_FIRST + CONFIG.MAPGROUP_SIZE))
		MessageBox("Initial intra message port failed!");
	else
	{
		m_pMsgPort = CMessagePort::GetInterface(MSGPORT_SHELL);
		m_pMsgPort->Open();
		m_nState	= SHELLSTATE_INIT;

		if(CONFIG.CURRENT_PORTID)
		{
			m_pInterPort = CInternetPort::CreateNew(CONFIG.CURRENT_PORTID, CONFIG.PORT_SIZE, CONFIG.MASTER_IP, CONFIG.MASTER_PORT, CONFIG.LOGIN_KEY);
			if(m_pInterPort)
				m_pInterPort->Open();
		}
	}

	// dead loop init
	for(int i = 0; i < CONFIG.MAPGROUP_SIZE + MSGPORT_MAPGROUP_FIRST; i++)
		DeadLoopSet().Push(DEADLOOP());

	// get game title
	CIniFile	ini("shell.ini", "AccountServer");
	ini.GetString(m_szServer, "SERVERNAME", _MAX_NAMESIZE);

	// windows title
	CString strTitle;
	strTitle.Format("%s - %s (%s %s)", GAME_TITLE, m_szServer, __DATE__, __TIME__);
	SetWindowText(strTitle);

	// init log file
	CreateDirectory(LOGFILE_DIR, NULL);
	InitLog(strTitle, LOGFILE_FILENAME, time(NULL));
	LOGMSG("\n\n\n=================================================================");
	LOGMSG(strTitle);
	LOGMSG("=================================================================");

	SetTimer(1, 200, NULL);				// 句柄为1,刷新间隔为200MS

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMsgServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else if ((nID & 0xFFF0) == SC_CLOSE)
	{
		PostMessage(WM_COMMAND, IDCANCEL);
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMsgServerDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMsgServerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMsgServerDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
#ifdef	DEBUG_MULTITHREAD
	if(::WaitForSingleObject(m_hMutexThread, INFINITE) == WAIT_ABANDONED)
		return ;
#endif

	static	int nLock = 0;
	if(nIDEvent == 1 && nLock <= 0)
	{
		nLock++;	//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		//!!! Can't use 'return' in this block

		if(m_pMsgPort)
			Process();

		DEBUG_TRY		// VVVVVVVVVVVVVVVVVVVVVVVVVVVV
		if(m_pInterPort)
			ProcessInterMsg();
		DEBUG_CATCH("ProcessInterMsg()")		// AAAAAAAAAAAAAAAAAAAAAAAAAAAAA

		extern long				g_nRestart;
		switch(m_nState)
		{
		case	SHELLSTATE_NONE:
			{
				m_nState	= SHELLSTATE_END;
			}
			break;
		case	SHELLSTATE_INIT:
			{
				m_nState	= SHELLSTATE_CLOSING;		// 失败时退出

				PrintText("1、Init world thread...");
				m_pWorldThread = new CWorldThread(CMessagePort::GetInterface(MSGPORT_WORLD));
				if(!m_pWorldThread)
					break;
				if(!m_pWorldThread->CreateThread(false))
					break;

				PrintText("2、Init map group thread...");
				for(int i = 0; i < CONFIG.MAPGROUP_SIZE; i++)
				{
					CMapGroupThread* pThread = new CMapGroupThread(CMessagePort::GetInterface(i + MSGPORT_MAPGROUP_FIRST));
					if(!pThread)
						break; // for
					m_setMapGroupThread.push_back(pThread);
					if(!pThread->CreateThread(false))
						break; // for
				}
				if(i < CONFIG.MAPGROUP_SIZE)
					break;

				PrintText("3、Init socket thread...");
				m_pSocketThread = new CSocketThread(CMessagePort::GetInterface(MSGPORT_SOCKET));
				if(!m_pSocketThread)
					break;
				if(!m_pSocketThread->CreateThread(false))
					break;
				PrintText("Init OK。");

				m_pWorldThread->ResumeThread();
				for( i = 0; i < CONFIG.MAPGROUP_SIZE; i++)
					m_setMapGroupThread[i]->ResumeThread();
				m_pSocketThread->ResumeThread();
				PrintText("All thread start OK。");

				m_nState	= SHELLSTATE_RUNNING;
			}
			break;
		case	SHELLSTATE_RUNNING:
			{
				// 状态刷新
				const int nSecsPerLogin = 5*60;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -