使用网络库提供状态信息的 TCP 服务器
我正在为在线回合制游戏编写 TCP 服务器。我已经使用 php 套接字编写了一个原型,但想转向 C++。我一直在研究流行的网络库(ASIO、ACE、POCO、LibEvent),但目前不清楚哪一个最适合我的需求:
1)连接是持久的(大约几分钟),并且服务器必须能够处理 100 多个并发连接。
2) 连接必须能够维护状态信息(用户登录信息)。 [我的 php 原型当前要求每个客户端请求包含登录信息]
3) 可选且最好是多线程,但单个进程。最好不要每个连接有 1 个线程,而是在所有打开的连接上使用固定数量的线程。
我倾向于 POCO 的 TCPServer 或 Reactor 框架,但不确定它们是否满足我的要求。我认为 Reactor 是单线程的,而 TCPServer 强制执行 1:1 线程/连接。我说得对吗?
无论哪种情况,我都不确定如何执行最重要的任务,将登录信息与随机进出的连接关联到特定连接。
I'm writing a tcp server for an online turn-based game. I've already written a prototype using php sockets, but would like to move to C++. I've been looking at the popular network libraries (ASIO, ACE, POCO, LibEvent), but currently unclear which one would best suit my needs:
1) Connections are persistent (on the order of minutes), and the server must be able to handle 100+ simultaneous connections.
2) Connections must be able to maintain state information (user login info). [my php prototype currently requires each client request to contain the login info]
3) Optionally and preferably multi-threaded, but a single process. Prefer not to have 1 thread per connection, but a fixed number of threads working on all open connections.
I'm leaning towards POCO's TCPServer or Reactor frameworks, but not exactly sure if they meet my requirements. I think the Reactor is single threaded, and the TCPServer enforces 1:1 threading/connection. Am I correct?
In either case case, I'm not exactly sure how to do the most important task of associating login info to a specific connection with connections coming and going at random.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Boost.Asio 应该可以满足您的要求。反应器队列可以由多个线程提供服务。使用异步方法将使您能够设计 为所有连接提供服务的固定线程数。
教程和示例可能是最好的起点。
Boost.Asio should meet your requirements. The reactor queue can be serviced by multiple threads. Using asynchronous methods will enable your design of a fixed number of threads servicing all connections.
The tutorials and examples are probably the best place to start if you are unfamiliar with the library.
您还可以看看 MUSCLE,这是我用它编写的一个多用户网络库和服务器考虑到这种应用程序。它获得 BSD 许可,可处理数百个用户,并包含一个服务器端数据库机制,用于存储和共享您希望客户端相互了解的任何信息。默认情况下,服务器是单线程的,但我在实践中没有发现这是一个问题(如果有必要,可以将服务器扩展为多线程)。
You might also take a look at MUSCLE, a multi-user networking library and server I wrote with this sort of application in mind. It's BSD-licensed, handles hundreds of users, and includes a server-side database mechanism for storing and sharing any information you want the clients to know about each other. The server is single-threaded by default, but I haven't found that to be a problem in practice (and it's possible to extend the server to be multithreaded if that turns out to be necessary).