C++游戏服务器编程-LogServer

本文固定链接
http://blog.csdn.net/u013108312/article/details/78693896
程序的入口还是从main函数开始,首先执行的当然是游戏的初始化了,让我们进入到LogHandle.h去看看都初始化了哪些内容。

支付宝捐赠

打赏红包

// LogServer.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "Net/LogNetSessionMgr.h"
#include "LogHandler.h"
int main()
{
	auto res = CLogHandler::GetInstance().Init();
	if (res){
		while(true){
			INetSessionMgr::GetInstance()->Update();
			Sleep(1);
		}
	}

	return 0;
}

#LogHandler.h

#include "StdAfx.h"
#include "LogHandler.h"
#include "DBMgr.h"
#include "CSDataSource.h" 
#include "Net/LogNetSessionMgr.h"

CLogHandler* CLogHandler::m_pCLogHandler = nullptr;
CLogHandler::CLogHandler(void):m_pDBActiveWrapper(NULL)
{
}


CLogHandler::~CLogHandler(void)
{
}

CLogHandler& CLogHandler::GetInstance(){
	if (NULL == m_pCLogHandler){
		m_pCLogHandler = new CLogHandler;
	}
	return *m_pCLogHandler;
}

void CLogHandler::HandleLogMsg(const char* pMsg, int n32MsgLength){
	ELOG(LOG_DBBUG, "");
	ToLog::WriteLog sMsg;
	if (!ParseProtoMsg(pMsg, n32MsgLength, sMsg)){
		return;
	}

	m_pDBActiveWrapper->EncodeAndSendToDBThread(sMsg, sMsg.msgid());
}

void CLogHandler::AsynHanlder(Buffer*& pBuffer){
	if (pBuffer->m_LogLevel == ToLog::eMsgToLog_WriteLog){
		ToLog::WriteLog sMsg;
		if (!ParseProtoMsg(pBuffer->GetDataHeader(), pBuffer->GetDataLength(), sMsg)){
			return;
		}

		/*MysqlDWrapper::*/IDBConnector *pConn = m_pDBActiveWrapper->GetDBConnector();
		if (NULL == pConn){
			ELOG(LOG_ERROR, "");
			return;
		}
		INT32 nRet = pConn->ExecQuery(sMsg.log_str() );
		if (nRet == eNormal)
		{ 
			pConn->CloseQuery(); 
		}
	}
}

bool CLogHandler::Init(){
	const char * szDBCfg = "./LGCfg/DBConfig.xml"; 
	TiXmlDocument doc(szDBCfg); 
	if(doc.LoadFile())
	{
		TiXmlHandle dochandle(&doc);
		TiXmlElement *father =  dochandle.FirstChild("DBCfg").FirstChild("DB").ToElement();

		auto port =  atoi(dochandle.FirstChild("DBCfg").FirstChildElement("port").ToElement()->GetText());
		CLogNetSessionMgr* pSessionMgr = new CLogNetSessionMgr;
		INetSessionMgr::GetInstance()->CreateListener(port, 102400, 102400, 0, &gClientSessionFactory);

		SetDBData(father, m_DbCfg);

		m_pDBActiveWrapper = new DBActiveWrapper(std::bind(&CLogHandler::AsynHanlder, this, std::placeholders::_1), m_DbCfg);
		m_pDBActiveWrapper->Start();
		ELOG(LOG_INFO, "Init Success!");
	}
	else{
		ELOG(LOG_ERROR, "Init Failed!");
		return false;
	}

	return true;
}


void CLogHandler::SetDBData(TiXmlElement * father, SDBCfg &sDBCfg){
	TiXmlElement *ip = father->FirstChildElement("ip")->ToElement();
	strcpy(sDBCfg.aszDBHostIP , ip->GetText() );

	TiXmlElement *port = father->FirstChildElement("Port")->ToElement();
	sDBCfg.un32DBHostPort =  atoi(port->GetText());

	TiXmlElement *user = father->FirstChildElement("user")->ToElement();
	strcpy(sDBCfg.aszDBUserName , user->GetText());

	TiXmlElement *pwd = father->FirstChildElement("pwd")->ToElement();
	strcpy(sDBCfg.aszDBUserPwd , pwd->GetText());

	TiXmlElement *dbname = father->FirstChildElement("dbname")->ToElement();
	strcpy(sDBCfg.aszDBName , dbname->GetText());
}

在初始化的时候,首先去读取DBConfig.xml这个配置文件,获取到IP,Port,user,pwd,dbname等。初始化成功之后服务器程序进入到一个死循环,等待网络消息。
本文固定链接
http://blog.csdn.net/u013108312/article/details/78693896

发布了181 篇原创文章 · 获赞 129 · 访问量 78万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 精致技术 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览