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

📄 game_handler.cpp

📁 通用网络游戏开发框架
💻 CPP
字号:
// ============================================================================
// Game server
//
// Deal with accepting connections from clients
//
// (c) 2003 Ken Reed
//
// This is free software. You can redistribute it and/or modify it under the
// terms of the GNU General Public License version 2 as published by the Free
// Software Foundation.
// ============================================================================

#include "stdafx.h"

#include "global.h"
#include "socket.h"
#include "report_error.h"

extern DWORD WINAPI client_handler (LPVOID parameter);


// ============================================================================
// Game Handler (New Thread)
//
// Accept client connections and start a separate thread to handle each one.
// ============================================================================

DWORD WINAPI game_handler(LPVOID parameter)
{
   try {
      enable_fp_exceptions();

      Socket server(server_port);

      while (true) {
         server.listen();

         for (int i = 0; i < max_players; i++) {
            if (! clients[i].in_use) {
               clients[i].connection = server.get_connection();
               clients[i].in_use     = true;

               DWORD thread_id (0);
               CreateThread(0, 0, client_handler, 
                            reinterpret_cast<LPVOID>(i), 0, &thread_id);
               break;
            }
         }
      }
   }
   catch (Exception & e) {
      report_text(e.get_error().c_str());
   }
   catch (...) {
      report_text("Exception thrown from Game Handler");
   }

   return 0;
}

⌨️ 快捷键说明

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