简体   繁体   English

C ++套接字服务器架构

[英]C++ socket server architecture

I'm designing a TCP socket server for my real-time game and I've come to two approaches to it's architecture. 我正在为我的实时游戏设计一个TCP套接字服务器,我已经采用了两种方法来构建它。 Here they are: 他们来了:

  1. We start two threads. 我们开始两个线程。 One listens for new connections in an indefinite loop and adds new clients to an array. 一个人在无限循环中侦听新连接,并将新客户端添加到数组中。 Second sequentially scans all client sockets from the array and reads data from them. 第二个顺序扫描阵列中的所有客户端套接字并从中读取数据。

  2. We start one thread, which listens for new connections in an indefinite loop and then starts new thread for each client which reads data just from one socket. 我们启动一个线程,它在一个无限循环中侦听新连接,然后为每个只从一个套接字读取数据的客户端启动新线程。

I've made some tests with about 100 clients and I couldn't see difference in performance of both architectures. 我已经对大约100个客户端进行了一些测试,我看不出两种架构的性能差异。 So, I want to ask your opinion, which way is better. 所以,我想问你的意见,哪种方式更好。 Thank you! 谢谢!

This all rather depends on how real-time your game actually is. 这完全取决于你的游戏实际的实时性

In the first approach, you are implementing the demultiplexing of events on all of the open sockets - and presumably using select() or poll() to block. 在第一种方法中,您正在所有打开的套接字上实现事件的多路分解 - 并且可能使用select()poll()来阻止。 Clearly, whilst you can only receive notification of an event whilst blocked and you effectively serialise processing of the each event if several are delivered when you unblock. 显然,虽然您只能在被阻止的情况下收到事件通知,但如果在解除阻止时有多个事件被传递,您可以有效地序列化每个事件的处理。

In the second approach, you have potential to process events in parallel (especially on a multiprocessor system) and also to prioritise connections using thread priority. 在第二种方法中,您有可能并行处理事件(特别是在多处理器系统上),并且还可以使用线程优先级对连接进行优先级排序。 However, this approach uses more memory, and scheduling threads is considerably more expensive than iterating over a list of events in the first approach. 但是,这种方法使用更多内存,而调度线程比在第一种方法中迭代事件列表要昂贵得多。

The questions you need to ask yourself are: 您需要问自己的问题是:

  • Do you actually need to process events in parallel? 你真的需要并行处理事件吗? (are you going to serialise execution in processing anyway?) (你打算在处理中序列化执行吗?)
  • Is the amount of processing on each event significantly more than the cost of scheduling threads? 每个事件的处理量是否明显大于调度线程的成本?
  • Will memory consumption for thread stacks limit scalability? 线程堆栈的内存消耗是否会限制可伸缩性?
  • Are your real-time requirements really that stringent? 您的实时要求是否真的那么严格?

In my opinion, it depends on what you believe is easier for you. 在我看来,这取决于你认为对你更容易的。

Do you use boost? 你用升力吗? If yes, you can look through their examples of using asio for implementing such server architectures. 如果是,您可以查看使用asio实现此类服务器体系结构的示例。 This way you can avoid manual management of thread when listening for connections and processing requests. 这样,您可以在侦听连接和处理请求时避免手动管理线程。

Also there is ZeroMQ , which is a library, made especially for implementing server architectures easily. ZeroMQ也是一个库,专门用于轻松实现服务器架构。 With it, you can test even more detailed cases by explicitly controlling numbers of threads, etc. 有了它,您可以通过显式控制线程数等来测试更详细的案例。

Hope this helps. 希望这可以帮助。

If you want to go WebSocket, here is a quick tip: look at https://github.com/zaphoyd/websocketpp and/or ask the author (Peter). 如果你想去WebSocket,这里有一个快速提示:查看https://github.com/zaphoyd/websocketpp和/或询问作者(Peter)。 WebSocket++ has a very flexible framework regarding what/how you do your WebSocket processing. WebSocket ++有一个非常灵活的框架,关于你的WebSocket处理的内容和方式。

Based on experience I would have a separate thread for the new clients so you can handle the signing in, and have it wait for a good point to join the game. 根据经验,我将为新客户提供一个单独的线程,以便您可以处理登录,并让它等待加入游戏的好点。 I imagine you don't always want to interrupt a game for new players signing in. The one master thread could get pretty messy otherwise. 我想你并不总是想要让新玩家登录游戏中断游戏。一个主线程可能会变得非常混乱。

You may also want multiple existing game threads in a thread group for scalability, such as one per game if you have 8+ cores available. 您可能还希望线程组中的多个现有游戏线程具有可伸缩性,例如,如果您有8个以上的核心可用,则每个游戏一个。 Consider even redirecting from the new game thread to another server for even larger scalability. 考虑甚至从新游戏线程重定向到另一台服务器,以获得更大的可扩展性。

Just use Boost.Asio and be happy! 只需使用Boost.Asio即可开心! Learn some examples and start making actually game and not a network basics. 学习一些示例并开始制作实际游戏,而不是网络基础知识。

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

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