📄 server_handler.cpp
字号:
// ============================================================================
// Handle messages received from the server
//
// (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 "report_error.h"
#include "socket.h"
#include <string>
using namespace std;
struct Command {
char * command;
void (* function)();
};
static void process_commands ();
static void shut_down ();
static void new_board ();
static void update_board ();
namespace {
Socket inbound(0);
Command command_list[] = {
{"new-board", new_board},
{"update-board", update_board},
{"shut-down", shut_down}
};
const int num_commands (sizeof(command_list) / sizeof(Command));
}
// ============================================================================
// Entry point (New Thread)
// ============================================================================
DWORD WINAPI server_handler(LPVOID parameter)
{
try {
enable_fp_exceptions();
port_number = inbound.get_number();
PostMessage(main_window, MY_THREAD_RUNNING, 0, 0);
inbound.listen();
process_commands();
}
catch (Exception & e) {
report_text(e.get_error().c_str());
}
catch (...) {
report_text("Exception thrown from server handler");
}
return 0;
}
// ============================================================================
// Process server commands
// ============================================================================
static void process_commands()
{
while (true) {
string input;
inbound >> input;
if (inbound.fail() || inbound.bad()) {
Sock_exception e("Lost socket IO stream");
RAISE(e);
}
for (int i = 0; i < num_commands; i++) {
if (input == command_list[i].command) {
command_list[i].function();
}
}
}
}
// ============================================================================
// New board arriving, reset our copy of the board
// ============================================================================
static void new_board()
{
board.receive(&inbound);
PostMessage(main_window, WM_PAINT, 0, 0);
}
// ============================================================================
// An update to the game has arrived
// ============================================================================
static void update_board()
{
board.get_updates(&inbound);
}
// ============================================================================
// Shut down this handler
// ============================================================================
static void shut_down()
{
port_number = 0;
ExitThread(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -