Skip to content

[Utility] Support online multi-player mode #500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

ljc1301
Copy link
Contributor

@ljc1301 ljc1301 commented Jan 23, 2023

In my implementation, the transmitted information is the users' input data, and all the computations are distributed to the clients.

You can start a server in the app window, or use

./battlegame --server [<port>]

where the default port is 8088.

Currently I've only tested the situation where one player is connected to localhost and the server is run in the command line only. Because it was for the first time that I wrote asynchronous program using multithread, I think it must contains many bugs (, so you are welcomed to rewrite the server/client parts). It seems that the servers and clients sometimes crash.

Something we can do later:

  • Test and debug thoroughly;
  • Document;
  • Support both Big Endian and Little Endian machines (currently conversion from glm::vec2 to bytes is directly using memcpy);
  • Capture user's input in the ImGui window;
  • Support both IPv4 and IPv6;
  • Pure server with less dependencies (i.e. we can build a server without Vulkan and etc.);
  • Any other utilities.

@sshockwave
Copy link

我讲讲我的理解看有没有问题:

  1. 客户端每个tick CaptureInput之后计入input_data_
  2. 客户端每个tick发送一次input_data_
  3. 服务端对每个client都循环接收input_data_
  4. 服务端每个tick读一次client的input_data_并broadcast
  5. 客户端收到broadcast的时候加进input_data_queue_
  6. 每个UpdateDrawCommands() flush一次queue,然后调用Update()

这个实现在当前框架中非常简单直接!Thanks for your great work!

当然,这种实现的主要缺点是对延迟和丢包率要求极高,延迟或丢包会导致input queue不同步,不同客户端渲染不一致。不过在局域网内跑都没问题,之后我们再慢慢整。

Mode mode_{kOffline}, chosen_mode_{kOffline};
bool render_{true};
bool input_data_synced_;
std::queue<std::vector<MessageInputData>> input_data_queue_;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的input_data_queue_因为会被两个线程同时访问,需要加std::mutex。类似的还有input_data_。不过write_messages_应该不用,因为只有io_context进行单线程访问。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看起来问题还有在另一个线程中调用app_->Start()app_->Stop()之类的:sob:我有空多改改

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

现在应该改好了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants