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

📄 client_handler.cpp

📁 通用网络游戏开发框架
💻 CPP
字号:
// ============================================================================
// Client Handler
//
// Deal with commands from a client.
//
// (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 verclientn 2 as published by the
// Free Software Foundation.
// ============================================================================

#include "stdafx.h"

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

#include <string>
#include <sstream>

using namespace std;

static void process_mouse     (Socket & socket, int player);
static void process_key       (Socket & socket, int player);
static void connect_client    (Socket & socket, int player);
static void disconnect_client (Socket & socket, int player);

struct Command {
   char *  command;
   void   (* function)(Socket & socket, int player);
};


namespace {
   int joined_players (0);

   Command command_list[] = {
      {"connect",      connect_client},
      {"disconnect",   disconnect_client},
      {"key",          process_key},
      {"mouse",        process_mouse},
   };

   const int num_commands = sizeof(command_list) / sizeof(Command);
}


// ============================================================================
// Client Handler (New Thread)
//
// Process client commands. One copy of this function runs for every client
// connected.
// ============================================================================

extern DWORD WINAPI client_handler(LPVOID parameter)
{
   int      player (reinterpret_cast<int>(parameter));
   Socket   client;
   
   enable_fp_exceptions();
   client.set_connection(clients[player].connection);

   try {
      while (true) {
         string command;
         client >> command;
         if (client.fail()) {
            clients[player].in_use = false;
            return 0;
         }

         for (int i = 0; i < num_commands; i++) {
            if (command == command_list[i].command) {
               command_list[i].function(client, player);
            }
         }
      }
   }
   catch (Exception & e) {
      report_text(e.get_error().c_str());
   }
   catch (...) {
      report_text("Exception thrown from Client Handler");
   }
   
   return 0;
}


// ============================================================================
// Connect a new client
// ============================================================================

static void connect_client(Socket & client, int player)
{
   int    port;
   string hostname;

   client >> hostname >> port;
   client << player << "\n";

   Socket * socket = (new Socket());

   socket->connect(hostname.c_str(), port);

   clients[player].socket = socket;

   joined_players++;
   ostringstream s;
   s << joined_players;
   SetWindowText(player_count, s.str().c_str());
}


// ============================================================================
// Disconnect a client
// ============================================================================

static void disconnect_client(Socket & client, int player) 
{
   Socket * socket = (clients[player].socket);

   (*socket) << "shut-down\n";
   Sleep(100);

   client.close();

   socket->close();
   free(socket);
   clients[player].in_use = false;

   joined_players--;
   ostringstream s;
   s << joined_players;
   SetWindowText(player_count, s.str().c_str());

   ExitThread(0);
}


// ============================================================================
// Process mouse events
// ============================================================================

static void process_mouse(Socket & client, int player)
{
   Point position;
   int   command;

   client >> command >> position.x >> position.y;
   board.mouse(player, static_cast<Mouse_Command>(command), position);
}


// ============================================================================
// Process key presses
// ============================================================================

static void process_key(Socket & client, int player)
{
   Point position;
   int   key;

   client >> key >> position.x >> position.y;
   board.key(player, key, position);
}

⌨️ 快捷键说明

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