处理传入消息的最佳方法是什么? [英] What's the best way to handle incoming messages?

查看:82
本文介绍了处理传入消息的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个用于在线游戏的服务器,该服务器最终应能够处理1,000-2,000个客户端.我发现执行此操作的3种方法基本上是:

I'm writing a server for an online game, that should be able to handle 1,000-2,000 clients in the end. The 3 ways I found to do this were basically:

  1. 1个线程/连接(阻塞)
  2. 列出客户列表,并在它们之间循环(非阻塞)
  3. 选择(基本上是一次针对所有客户端的阻塞语句,并带有可选的超时时间?)

过去我使用1,但是众所周知,它的扩展性不好. 2还可以,但是我对一个客户在技术上能够使其他所有人冻结的感受却参差不齐. 3个听起来很有趣(比2个更好),但是我听说它不适用于太多的连接. 那么,什么是最好的方法(在D中)?还有其他选择吗?

In the past I've used 1, but as we all know, it doesn't scale well. 2 is okay, but I have mixed feelings, about one client technically being able to make everyone else freeze. 3 sounds interesting (a bit better than 2), but I've heard it's not suitable for too many connections. So, what would be the best way to do it (in D)? Are there other options?

推荐答案

通常的方法最接近3:具有更高性能的select替代方案(例如pollepoll系统调用)的异步编程Linux,Windows上的IOCP或包装它们的更高级别的库. D不直接支持它们,但是您可以找到D绑定或为它们提供支持的第三方D库(例如Tango).

The usual approach is closest to 3: asynchronous programming with a higher-performance select alternative, such as the poll or epoll system calls on Linux, IOCP on Windows, or higher-level libraries wrapping them. D does not support them directly, but you can find D bindings or 3rd-party D libraries (e.g. Tango) providing support for them.

高性能服务器(例如nginx)每个CPU内核使用一个线程/进程,并在该线程/进程内使用异步事件处理.

Higher-performance servers (e.g. nginx) use one thread/process per CPU core, and use asynchronous event handling within that thread/process.

这篇关于处理传入消息的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆