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

📄 attemperengine.cpp

📁 游戏框架
💻 CPP
字号:
// ***************************************************************
//  AttemperEngine   version:  1.0
//  -------------------------------------------------------------
//	File Name:	AttemperEngine.cpp
//	Created:	2007/07/18
//	Modified:	2007/07/18   16:49
//	Author:		William.Liang
//  Msn:		lwq49@msn.com
//  Email:		lwq49@21cn.com, lwq49@msn.com
//	Description:
//
//	Purpose:	
//  -------------------------------------------------------------
//  license:
//
//  The contents of this file are subject to the Mozilla Public
//  License Version 1.1 (the "License"); you may not use this file
//  except in compliance with the License. You may obtain a copy
//  of the License at http://www.mozilla.org/MPL/ Software dis-
//  tributed under the License is distributed on an "AS IS" 
//  basis, WITHOUT WARRANTY OF ANY KIND, either express or im-
//  plied. See the License for the specific language governing
//  rights and limitations under the License.
//
//  The Initial Developer of the Original Code is William.Liang .
//  Copyright (C) 2007 - All Rights Reserved.
// ***************************************************************
#include "StdAfx.h"
//#include "EventService.h"
#include "AttemperEngine.h"

//////////////////////////////////////////////////////////////////////////

//************************************
// <p>Description: 构造函数</p>
//************************************
CAttemperEngine::CAttemperEngine(void)
{
	m_bService=false;
	//m_pITCPSocketEngine=NULL;
	m_pIAttemperEngineSink=NULL;
	return;
}

//************************************
// <p>Description: 析构函数</p>
//************************************
CAttemperEngine::~CAttemperEngine(void)
{
}

//************************************
// <p>Description: 接口查询</p>
// <p>Parameters:  </p>
// <p>    const IID & Guid</p>
// <p>    DWORD dwQueryVer</p>
//
// <p>Returns:   void * __cdecl</p>
//************************************
void * __cdecl CAttemperEngine::QueryInterface(const IID & Guid, DWORD dwQueryVer)
{
	QUERYINTERFACE(IAttemperEngine,Guid,dwQueryVer);
	QUERYINTERFACE(IQueueServiceSink,Guid,dwQueryVer);
	QUERYINTERFACE_IUNKNOWNEX(IAttemperEngine,Guid,dwQueryVer);
	return NULL;
}

//************************************
// <p>Description: 启动服务</p>
//
// <p>Returns:   bool __cdecl</p>
//************************************
bool __cdecl CAttemperEngine::StartService()
{
	//判断状态
	if (m_bService==true) 
	{
		//CEventTrace::ShowEventNotify(TEXT("调度引擎重复启动,启动操作忽略"),Level_Warning);
		return true;
	}

	//外挂接口
	////_ASSERT(m_pIAttemperEngineSink==NULL);
	if (m_pIAttemperEngineSink==NULL)
	{
		//CEventTrace::ShowEventNotify(TEXT("调度引擎外挂服务不存在"),Level_Exception);
		return false;
	}

	//设置队列
	bool ts = m_RequestQueueService.SetQueueServiceSink(GET_MYSELF_INTERFACE(IUnknownEx));
	////_ASSERT(ts==false);
	if (ts==false)
	{
		//CEventTrace::ShowEventNotify(TEXT("调度引擎与队列服务绑定失败"),Level_Exception);
		return false;
	}

	//启动外挂
	bool ms = m_pIAttemperEngineSink->StartService(GET_MYSELF_INTERFACE(IUnknownEx));
	////_ASSERT(ms==false);
	if (ms==false)
	{
		//CEventTrace::ShowEventNotify(TEXT("调度引擎外挂服务启动失败"),Level_Exception);
		return false;
	}

	//启动队列
	bool ss = m_RequestQueueService.StartService();
	////_ASSERT(ss == false);
	if (ss==false)
	{
		//CEventTrace::ShowEventNotify(TEXT("调度引擎队列服务启动失败"),Level_Exception);
		return false;
	}

	//设置变量
	m_bService=true;

	return true;
}

//************************************
// <p>Description: 停止服务</p>
//
// <p>Returns:   bool __cdecl</p>
//************************************
bool __cdecl CAttemperEngine::StopService()
{
	//设置变量
	m_bService=false;

	//停止请求队列
	m_RequestQueueService.StopService();

	//停止外挂
	if (m_pIAttemperEngineSink!=NULL)
	{
		m_pIAttemperEngineSink->StopService(GET_MYSELF_INTERFACE(IUnknownEx));
	}

	return true;
}

//************************************
// <p>Description: 设置网络</p>
// <p>Parameters:  </p>
// <p>    IUnknownEx * pIUnknownEx</p>
//
// <p>Returns:   bool __cdecl</p>
//************************************
bool __cdecl CAttemperEngine::SetSocketEngine(IUnknownEx * pIUnknownEx)
{
	////_ASSERT(pIUnknownEx!=NULL);
	//m_pITCPSocketEngine=GET_OBJECTPTR_INTERFACE(pIUnknownEx,ITCPSocketEngine);
	////_ASSERT(m_pITCPSocketEngine!=NULL);
	//return (m_pITCPSocketEngine!=NULL);
	return true;
}

//************************************
// <p>Description: 注册钩子</p>
// <p>Parameters:  </p>
// <p>    IUnknownEx * pIUnknownEx</p>
//
// <p>Returns:   bool __cdecl</p>
//************************************
bool __cdecl CAttemperEngine::SetAttemperEngineSink(IUnknownEx * pIUnknownEx)
{
	//效验参数
	//_ASSERT(pIUnknownEx!=NULL);
	//_ASSERT(m_pIAttemperEngineSink==NULL);
	if (pIUnknownEx==NULL) return false;
	if (m_pIAttemperEngineSink!=NULL) return false;

	//查询接口
	m_pIAttemperEngineSink=GET_OBJECTPTR_INTERFACE(pIUnknownEx,IAttemperEngineSink);
	if (m_pIAttemperEngineSink==NULL)
	{
		//CEventTrace::ShowEventNotify(TEXT("调度引擎外挂服务接口获取失败,挂接操作失败"),Level_Exception);
		return false;
	}

	return true;
}

//************************************
// <p>Description: 获取接口</p>
// <p>Parameters:  </p>
// <p>    const IID & Guid</p>
// <p>    DWORD dwQueryVer</p>
//
// <p>Returns:   void * __cdecl</p>
//************************************
void * __cdecl CAttemperEngine::GetQueueService(const IID & Guid, DWORD dwQueryVer)
{
	return m_RequestQueueService.QueryInterface(Guid,dwQueryVer);
}

//************************************
// <p>Description: 队列接口</p>
// <p>Parameters:  </p>
// <p>    WORD wIdentifier</p>
// <p>    void * pBuffer</p>
// <p>    WORD wDataSize</p>
// <p>    DWORD dwInsertTime</p>
//
// <p>Returns:   void __cdecl</p>
//************************************
void __cdecl CAttemperEngine::OnQueueServiceSink(WORD wIdentifier, void * pBuffer, WORD wDataSize, DWORD dwInsertTime)
{
	//内核事件
	//_ASSERT(m_pIAttemperEngineSink!=NULL);
	switch (wIdentifier)
	{
	case EVENT_TIMER:			//定时器事件
		{
			//效验参数
			_ASSERT(wDataSize==sizeof(NTY_TimerEvent));
			if (wDataSize!=sizeof(NTY_TimerEvent)) return;

			//处理消息
			NTY_TimerEvent * pTimerEvent=(NTY_TimerEvent *)pBuffer;
			m_pIAttemperEngineSink->OnEventTimer(pTimerEvent->wTimerID,pTimerEvent->wBindParam);

			return;
		}
	case EVENT_ACTION:			//动作事件
		{
			//效验参数
			_ASSERT(wDataSize==sizeof(NTY_ActionEvent));
			if (wDataSize!=sizeof(NTY_ActionEvent)) return;

			//处理消息
			NTY_ActionEvent * pActionEvent=(NTY_ActionEvent *)pBuffer;
			m_pIAttemperEngineSink->OnEventAction(pActionEvent->pIGameObject, pActionEvent->wActionID, pActionEvent->dwRepeatTimes, pActionEvent->wBindParam);

			return;
		}
	case EVENT_DATABASE:		//数据库事件
		{
			//效验参数
			_ASSERT(wDataSize>=sizeof(NTY_DataBaseEvent));
			if (wDataSize<sizeof(NTY_DataBaseEvent)) return;

			//处理消息
			NTY_DataBaseEvent * pDataBaseEvent=(NTY_DataBaseEvent *)pBuffer;
			m_pIAttemperEngineSink->OnEventDataBase(pDataBaseEvent+1,wDataSize-sizeof(NTY_DataBaseEvent),pDataBaseEvent);

			return;
		}
	case EVENT_SOCKET_ACCEPT:	//网络应答事件
		{
			//效验大小
			_ASSERT(wDataSize==sizeof(NTY_SocketAcceptEvent));
			if (wDataSize!=sizeof(NTY_SocketAcceptEvent)) return;

			//处理消息
			NTY_SocketAcceptEvent * pSocketAcceptEvent=(NTY_SocketAcceptEvent *)pBuffer;
			m_pIAttemperEngineSink->OnEventSocketAccept(pSocketAcceptEvent);

			return;
		}
	case EVENT_SOCKET_READ:		//网络读取事件
		{
			//效验大小
			NTY_SocketReadEvent * pSocketReadEvent=(NTY_SocketReadEvent *)pBuffer;
			_ASSERT(wDataSize>=sizeof(NTY_SocketReadEvent));
			_ASSERT(wDataSize==(sizeof(NTY_SocketReadEvent)+pSocketReadEvent->wDataSize));
			if (wDataSize<sizeof(NTY_SocketReadEvent)) return;
			if (wDataSize!=(sizeof(NTY_SocketReadEvent)+pSocketReadEvent->wDataSize)) return;

			//处理消息
			bool bSuccess=false;
			try 
			{ 
				bSuccess=m_pIAttemperEngineSink->OnEventSocketRead(pSocketReadEvent->Command,pSocketReadEvent+1,pSocketReadEvent->wDataSize,pSocketReadEvent);
			}
			catch (...)	{ }
			//if (bSuccess==false) m_pITCPSocketEngine->CloseSocket(pSocketReadEvent->wIndex,pSocketReadEvent->wRoundID);

			return;
		}
	case EVENT_SOCKET_CLOSE:	//网络关闭事件
		{
			//效验大小
			_ASSERT(wDataSize==sizeof(NTY_SocketCloseEvent));
			if (wDataSize!=sizeof(NTY_SocketCloseEvent)) return;

			//处理消息
			NTY_SocketCloseEvent * pSocketCloseEvent=(NTY_SocketCloseEvent *)pBuffer;
			m_pIAttemperEngineSink->OnEventSocketClose(pSocketCloseEvent);

			return;
		}
	case EVENT_CLIENT_SOCKET_CONNECT:
		{
			//效验大小
			_ASSERT(wDataSize==sizeof(NTY_SocketConnectEvent));
			if (wDataSize!=sizeof(NTY_SocketConnectEvent)) return;

			//处理消息
			NTY_SocketConnectEvent * pSocketConnectEvent=(NTY_SocketConnectEvent *)pBuffer;
			m_pIAttemperEngineSink->OnEventClientSocketConnect(pSocketConnectEvent->iErrorCode, pSocketConnectEvent->szErrorDesc);

			return;
		}
	case EVENT_CLIENT_SOCKET_READ:
		{
			//效验大小
			NTY_SocketReadEvent * pSocketReadEvent=(NTY_SocketReadEvent *)pBuffer;
			_ASSERT(wDataSize>=sizeof(NTY_SocketReadEvent));
			_ASSERT(wDataSize==(sizeof(NTY_SocketReadEvent)+pSocketReadEvent->wDataSize));
			if (wDataSize<sizeof(NTY_SocketReadEvent)) return;
			if (wDataSize!=(sizeof(NTY_SocketReadEvent)+pSocketReadEvent->wDataSize)) return;

			m_pIAttemperEngineSink->OnEventClientSocketRead(pSocketReadEvent->Command,pSocketReadEvent+1,pSocketReadEvent->wDataSize);

			return;
		}
	case EVENT_CLIENT_SOCKET_CLOSE:
		{
			m_pIAttemperEngineSink->OnEventClientSocketClose();

			return;
		}
	}

	//其他事件
	m_pIAttemperEngineSink->OnAttemperEvent(wIdentifier,pBuffer,wDataSize,dwInsertTime); 

	return;
}

//////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

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