简体   繁体   English

从服务器接收数据时如何在后台检测键盘事件(C++ TCP 客户端)

[英]How do I detect keyboard event in background while receiving data from server (C++ TCP Client)

I'm making a multiplayer game with TCP connection between client and server in C++.我正在使用 C++ 中客户端和服务器之间的 TCP 连接制作多人游戏。 The client is ment to detect keypresses (arrowkeys) from the console and send data to the server.客户端需要从控制台检测按键(箭头键)并将数据发送到服务器。 The server is supposed to send data to all connected clients about player position etc. This means I need to receive data from the server at anytime whilst detecting whenever an arrowkey is pressed in order to send data back to the server.服务器应该向所有连接的客户端发送有关玩家位置等的数据。这意味着我需要随时从服务器接收数据,同时检测何时按下箭头键以便将数据发送回服务器。 How do I listen for keyevent and receive data at the same time?如何同时监听 keyevent 和接收数据?

I've tried the following, however the loop gets stuck at _getch();我尝试了以下方法,但是循环卡在了 _getch(); which waits for input.等待输入。

int keyListener() {

int keypress = _getch();

return keypress;

} }

int main() { int main() {

int c = 0;
bool run = true;
while (run) {

    cout << "Listening to socket" << endl;
    
        c = keyListener();
        switch (c) {
        case KEY_UP:
            cout << "UP" << endl;
            break;
        case KEY_DOWN:
            cout << "DOWN" << endl;
            break;
        case KEY_LEFT:
            cout << "LEFT" << endl;
            break;
        case KEY_RIGHT:
            cout << "RIGHT" << endl;
            break;
        default:
            cout << "Listening for keypress" << endl;
            break;
        }

}

return 0;

} }

You would need more than one book to learn game development.您需要不止一本书来学习游戏开发。 I assume this is just a C++ coding exercise with a "game" theme.我认为这只是一个带有“游戏”主题的 C++ 编码练习。

I suggest you create two threads: one to listen to keyboard input and another to handle TCP.我建议您创建两个线程:一个用于侦听键盘输入,另一个用于处理 TCP。 You start those threads at the beginning of your game and join them on exit.您在游戏开始时启动这些线程并在退出时加入它们。

The overly simplified game skeleton may look like this:过度简化的游戏骨架可能如下所示:

#include <atomic>
#include <thread>
#include <queue>
#include <mutex>
#include <conio.h>

std::atomic<bool> run = true;
std::queue<int> input;
std::mutex guard;  // to protects input queue

void keyboard()
{
  while (run)
  {
    int keypress = _getch();
    // TODO: if keypress indicates Exit - set run = false;
    // Lock the queue for safe multi-thread access
    {
      const std::lock_guard<std::mutex> lock(guard);
      input.push(keypress);
    }
  }
}

int main()
{
  std::thread keyListener(keyboard);
  // TODO: start your TCP thread
  while (run)
  {
    // 1. Process input (keyboard, mouse, voice, etc.)
    {
      // Lock the queue for safe multi-thread access
      const std::lock_guard<std::mutex> lock(guard);
      // Pop all collected keys and process them
      while (!input.empty())
      {
        int keypress = input.front();
        input.pop();
        // TODO: Send game-related keys to the TCP thread
      }
    }

    // 2. Process TCP input, probably with another queue

    // 3. Update the Game World

    // 4. Display the Game World
  }
  keyListener.join();
  // TODO: join your TCP thread
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM