글로벌로 Job을 관리해보자
class GlobalQueue
{
public:
GlobalQueue();
~GlobalQueue();
void Push(JobQueueRef jobQueue);
JobQueueRef Pop();
private:
LockQueue<JobQueueRef> _jobQueues;
};
GlobalQueue::GlobalQueue()
{
}
GlobalQueue::~GlobalQueue()
{
}
void GlobalQueue::Push(JobQueueRef jobQueue)
{
_jobQueues.Push(jobQueue);
}
JobQueueRef GlobalQueue::Pop()
{
return _jobQueues.Pop();
}
이 Job의 처리는?
void ThreadManager::DoGlobalQueueWork()
{
while (true)
{
uint64 now = ::GetTickCount64();
if (now > LEndTickCount)
break;
JobQueueRef jobQueue = GGlobalQueue->Pop();
if (jobQueue == nullptr)
break;
jobQueue->Execute();
}
}
enum
{
WORKER_TICK = 64
};
void DoWorkerJob(ServerServiceRef& service)
{
while (true)
{
LEndTickCount = ::GetTickCount64() + WORKER_TICK;
// 네트워크 입출력 처리 -> 인게임 로직까지 (패킷 핸들러에 의해)
service->GetIocpCore()->Dispatch(10);
// 글로벌 큐
ThreadManager::DoGlobalQueueWork();
}
}
int main()
{
ClientPacketHandler::Init();
ServerServiceRef service = MakeShared<ServerService>(
NetAddress(L"127.0.0.1", 7777),
MakeShared<IocpCore>(),
MakeShared<GameSession>, // TODO : SessionManager 등
100);
ASSERT_CRASH(service->Start());
for (int32 i = 0; i < 5; i++)
{
GThreadManager->Launch([&service]()
{
DoWorkerJob(service);
});
}
// Main Thread
DoWorkerJob(service);
GThreadManager->Join();
}