Files
Client/Server/RylServerProject/BaseLibrary/Pattern/CommandQueue.cpp
LGram16 dd97ddec92 Restructure repository to include all source folders
Move git root from Client/ to src/ to track all source code:
- Client: Game client source (moved to Client/Client/)
- Server: Game server source
- GameTools: Development tools
- CryptoSource: Encryption utilities
- database: Database scripts
- Script: Game scripts
- rylCoder_16.02.2008_src: Legacy coder tools
- GMFont, Game: Additional resources

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-29 20:17:20 +09:00

107 lines
2.0 KiB
C++

#include "stdafx.h"
#include "CommandQueue.h"
#include "Command.h"
#include <Thread/ThreadMgr.h>
#include <Log/ServerLog.h>
CCommandQueueThread::CCommandQueueThread(long nMaxQueueSize)
{
MsgQueueLock::Syncronize sync(m_Lock);
m_hHandles[StopperIndex] = CreateEvent(0, TRUE, FALSE, 0);
m_hHandles[SemaphoreIndex] = CreateSemaphore(0, 0, nMaxQueueSize, 0);
}
CCommandQueueThread::~CCommandQueueThread()
{
CThreadMgr::Stop(this);
MsgQueueLock::Syncronize sync(m_Lock);
CommandList::iterator pos = m_CommandList.begin();
CommandList::iterator end = m_CommandList.end();
for(;pos != end; ++pos)
{
(*pos)->Destroy();
}
m_CommandList.clear();
CloseHandle(m_hHandles[StopperIndex]);
CloseHandle(m_hHandles[SemaphoreIndex]);
}
bool CCommandQueueThread::IsValid()
{
MsgQueueLock::Syncronize sync(m_Lock);
return INVALID_HANDLE_VALUE != m_hHandles[StopperIndex] &&
INVALID_HANDLE_VALUE != m_hHandles[SemaphoreIndex];
}
bool CCommandQueueThread::Add(CCommand* lpCommand)
{
MsgQueueLock::Syncronize sync(m_Lock);
m_CommandList.push_back(lpCommand);
BOOL bResult = ReleaseSemaphore(m_hHandles[SemaphoreIndex], 1, 0);
if(!bResult)
{
m_CommandList.pop_back();
}
return 0 != bResult;
}
unsigned int CCommandQueueThread::Run()
{
bool bExit = false;
while(!bExit)
{
CCommand* lpCommand = 0;
switch(WaitForMultipleObjects(MaxIndex, m_hHandles, FALSE, INFINITE))
{
case StopperIndex:
bExit = true;
break;
case SemaphoreIndex:
{
MsgQueueLock::Syncronize sync(m_Lock);
if(!m_CommandList.empty())
{
lpCommand = m_CommandList.front();
m_CommandList.pop_front();
}
}
if(0 != lpCommand)
{
lpCommand->DoProcess();
lpCommand->Destroy();
lpCommand = 0;
}
break;
case WAIT_FAILED:
ERRLOG1(g_Log, "Err:%d/Error from WaitForMultipleObject", GetLastError());
bExit = true;
break;
}
}
return 0;
}
BOOL CCommandQueueThread::End()
{
return SetEvent(m_hHandles[StopperIndex]);
}