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

📄 queueservice.cpp

📁 游戏框架
💻 CPP
字号:
// ***************************************************************
//  QueueService   version:  1.0
//  -------------------------------------------------------------
//	File Name:	QueueService.cpp
//	Created:	2007/07/18
//	Modified:	2007/07/18   16:45
//	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 "QueueService.h"

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

//************************************
// <p>Description: 构造函数</p>
//************************************
CQueueServiceThread::CQueueServiceThread(void)
{
	m_hCompletionPort=NULL;
	memset(m_cbBuffer,0,sizeof(m_cbBuffer));
}

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

//************************************
// <p>Description: 配置函数</p>
// <p>Parameters:  </p>
// <p>    HANDLE hCompletionPort</p>
//
// <p>Returns:   bool</p>
//************************************
bool CQueueServiceThread::InitThread(HANDLE hCompletionPort)
{
	//效验参数
	_ASSERT(IsRuning()==false);
	_ASSERT(m_hCompletionPort==NULL);

	//设置变量
	m_hCompletionPort=hCompletionPort;
	memset(m_cbBuffer,0,sizeof(m_cbBuffer));

	return true;
}

//************************************
// <p>Description: 取消配置</p>
//
// <p>Returns:   bool</p>
//************************************
bool CQueueServiceThread::UnInitThread()
{
	//效验参数
	_ASSERT(IsRuning()==false);

	//设置变量
	m_hCompletionPort=NULL;
	memset(m_cbBuffer,0,sizeof(m_cbBuffer));

	return true;
}

//************************************
// <p>Description: 运行函数</p>
// <p>Parameters:  </p>
//
// <p>Returns:   bool</p>
//************************************
bool CQueueServiceThread::RepetitionRun()
{
	//效验参数
	_ASSERT(m_hCompletionPort!=NULL);

	//变量定义
	DWORD dwThancferred=0;
	OVERLAPPED * pOverLapped=NULL;
	CQueueService * pQueueService=NULL;

	//等待完成端口
	if (GetQueuedCompletionStatus(m_hCompletionPort,&dwThancferred,(PULONG_PTR)&pQueueService,&pOverLapped,INFINITE))
	{
		//判断退出
		if (pQueueService==NULL) return false;

		//获取数据
		tagDataHead DataHead;
		bool bSuccess=pQueueService->GetData(DataHead,m_cbBuffer,sizeof(m_cbBuffer));
		_ASSERT(bSuccess==true);

		//处理数据
		if (bSuccess==true) pQueueService->OnQueueServiceThread(DataHead,m_cbBuffer,DataHead.wDataSize);

		return true;
	}

	return false;
}

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


//************************************
// <p>Description: 构造函数</p>
//************************************
CQueueService::CQueueService(void)
{
	m_bService=false;
	m_hCompletionPort=NULL;
	m_pIQueueServiceSink=NULL;
}

//************************************
// <p>Description: 析构函数</p>
//************************************
CQueueService::~CQueueService(void)
{
	//停止服务
	StopService();

	return;
}

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

//************************************
// <p>Description: 设置接口</p>
// <p>Parameters:  </p>
// <p>    IUnknownEx * pIUnknownEx</p>
//
// <p>Returns:   bool __cdecl</p>
//************************************
bool __cdecl CQueueService::SetQueueServiceSink(IUnknownEx * pIUnknownEx)
{
	_ASSERT(pIUnknownEx!=NULL);
	m_pIQueueServiceSink=GET_OBJECTPTR_INTERFACE(pIUnknownEx,IQueueServiceSink);
	_ASSERT(m_pIQueueServiceSink!=NULL);
	return (m_pIQueueServiceSink!=NULL);
}

//************************************
// <p>Description: 负荷信息</p>
// <p>Parameters:  </p>
// <p>    tagBurthenInfo & BurthenInfo</p>
//
// <p>Returns:   bool __cdecl</p>
//************************************
bool __cdecl CQueueService::GetBurthenInfo(tagBurthenInfo & BurthenInfo)
{
	CThreadLockHandle LockHandle(&m_ThreadLock);
	return m_DataStorage.GetBurthenInfo(BurthenInfo);
}

//************************************
// <p>Description: 加入数据</p>
// <p>Parameters:  </p>
// <p>    WORD wIdentifier</p>
// <p>    void * const pBuffer</p>
// <p>    WORD wDataSize</p>
//
// <p>Returns:   bool __cdecl</p>
//************************************
bool __cdecl CQueueService::AddToQueue(WORD wIdentifier, void * const pBuffer, WORD wDataSize)
{
	CThreadLockHandle LockHandle(&m_ThreadLock);
	m_DataStorage.AddData(wIdentifier,pBuffer,wDataSize);
	PostQueuedCompletionStatus(m_hCompletionPort,wDataSize,(ULONG_PTR)this,NULL);
	return true;
}

//************************************
// <p>Description: 开始服务</p>
//
// <p>Returns:   bool __cdecl</p>
//************************************
bool __cdecl CQueueService::StartService()
{
	//效验参数
	_ASSERT(m_bService==false);
	_ASSERT(m_hCompletionPort==NULL);
	_ASSERT(m_pIQueueServiceSink!=NULL);

	//建立完成端口
	m_hCompletionPort=CreateIoCompletionPort(INVALID_HANDLE_VALUE,NULL,NULL,1);
	if (m_hCompletionPort==NULL) throw TEXT("队列对象完成端口创建失败");

	//启动线程
	if (m_QueueServiceThread.InitThread(m_hCompletionPort)==false) throw TEXT("队列对象线程初始化失败");
	if (m_QueueServiceThread.StartThead()==false) throw TEXT("队列对象线程启动失败");

	//设置参数
	m_bService=true;

	return true;
}

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

	//停止线程
	if (m_hCompletionPort!=NULL) PostQueuedCompletionStatus(m_hCompletionPort,0,NULL,NULL);
	m_QueueServiceThread.StopThread();
	m_QueueServiceThread.UnInitThread();

	//关闭完成端口
	if (m_hCompletionPort!=NULL)
	{
		CloseHandle(m_hCompletionPort);
		m_hCompletionPort=NULL;
	}

	//设置数据
	m_DataStorage.RemoveData(false);

	return true;
}

//************************************
// <p>Description: 提取数据</p>
// <p>Parameters:  </p>
// <p>    tagDataHead & DataHead</p>
// <p>    void * pBuffer</p>
// <p>    WORD wBufferSize</p>
//
// <p>Returns:   bool</p>
//************************************
bool CQueueService::GetData(tagDataHead & DataHead, void * pBuffer, WORD wBufferSize)
{
	CThreadLockHandle LockHandle(&m_ThreadLock);
	return m_DataStorage.GetData(DataHead,pBuffer,wBufferSize);
}

//************************************
// <p>Description: 数据消息</p>
// <p>Parameters:  </p>
// <p>    const tagDataHead & DataHead</p>
// <p>    void * pBuffer</p>
// <p>    WORD wDataSize</p>
//
// <p>Returns:   void</p>
//************************************
void CQueueService::OnQueueServiceThread(const tagDataHead & DataHead, void * pBuffer, WORD wDataSize)
{
	_ASSERT(m_pIQueueServiceSink!=NULL);
	try	
	{ 
		m_pIQueueServiceSink->OnQueueServiceSink(DataHead.wIdentifier,pBuffer,DataHead.wDataSize,DataHead.dwInsertTime); 
	}
	catch (...) {}
	return;
}

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

⌨️ 快捷键说明

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