如何游戏服务器与提升:短耳异步工作? [英] How game servers with Boost:Asio work asynchronously?

查看:106
本文介绍了如何游戏服务器与提升:短耳异步工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个游戏服务器,目前,我与线程使得它。每个对象(球员,怪物),都有自己的线程与同时(1)周期,巫执行特定的功能。

和服务器基本上是这样的:

 的main(){//一些初始化而(1)
{
//读取客户包
//指示数据包信息,以一个特定的对象
//对象执行某些功能
//然后服务器返回结果数据包发送回客户端
睡眠(1);
}

我听说,效率不高,使用这样的线程服务器,
我应该考虑使用升压::短耳,使功能异步工作。
但我不知道服务器将如何工作,然后。我将不胜感激,如果有人可以解释这样的服务器基本上是如何工作的。


解决方案

  

每一个对象(一个玩家,怪物),都有自己的线程。
  我听说,效率不高,使使用线程服务器
  像


您是正确的,这不是一个可扩展的设计。考虑一个大的游戏,你可能有10,000个对象,甚至上百万。当您需要为每个对象的线程这样的设计很快分崩离析。这就是所谓的 C10K问题


  

我应该考虑使用升压::短耳,使职能的工作
  异步。但我不知道服务器将如何工作,然后。
  我将不胜感激,如果有人会解释如何等基本
  服务器的工作。


您应该遵循的boost ::短耳教程启动,并特别注意的<一个href=\"http://www.boost.org/doc/libs/release/doc/html/boost_asio/tutorial/tutdaytime3.html\">Asynchronous TCP daytime服务器。你了解你的程序的流程反转后相比,同步编程异步编程的概念并不难。从一个高的水平,你的游戏服务器将有一个事件循环是由升压驱动:: ASIO :: io_service对象。过于简化,它看起来像这样

  INT
主要()
{
    提高:: ASIO :: io_service对象io_service对象;
    //一些工作添加到io_service对象    io_service.run(); //启动事件循环    //不应该到这里来
}

这是从事件循环链运营调用一起回调处理程序。也就是说,一旦你从客户端读取数据调用回调函数,处理程序将启动另一个异步操作。

这种设计的优点在于它能够消除并发线程。考虑长时间运行操作中的游戏服务器,诸如从客户机读取的数据。使用异步方法,你的游戏服务器不需要等待操作完成。当操作完成代表的内核将被通报。

I am trying to create a game server, and currently, I am making it with threads. Every object( a player , monster ), has its own thread with while(1) cycle , in witch particular functions are performed.

And the server basically works like this:

main(){

//some initialization

while(1)
{
//reads clients packet
//directs packet info to a particular object
//object performs some functions
//then server returns result packet back to client
Sleep(1);
}

I have heard that is not efficient to make the server using threads like that, and I should consider to use Boost::Asio, and make the functions work asynchronously. But I don't know how then the server would work. I would be grateful if someone would explain how basically such servers work.

解决方案

Every object( a player , monster ), has its own thread. I have heard that is not efficient to make the server using threads like that

You are correct, this is not a scalable design. Consider a large game where you may have 10,000 objects or even a million. Such a design quickly falls apart when you require a thread per object. This is known as the C10K problem.

I should consider to use Boost::Asio, and make the functions work asynchronously. But I don't know how then the server would work. I would be grateful if someone would explain how basically such servers work.

You should start by following the Boost::Asio tutorials, and pay specific attention to the Asynchronous TCP daytime server. The concept of asynchronous programming compared to synchronous programming is not difficult after you understand that the flow of your program is inverted. From a high level, your game server will have an event loop that is driven by a boost::asio::io_service. Overly simplified, it will look like this

int
main()
{
    boost::asio::io_service io_service;
    // add some work to the io_service

    io_service.run(); // start event loop

    // should never get here
}

The callback handlers that are invoked from the event loop will chain operations together. That is, once your callback for reading data from a client is invoked, the handler will initiate another asynchronous operation.

The beauty of this design is that it decouples threading from concurrency. Consider a long running operation in your game server, such as reading data from a client. Using asynchronous methods, your game server does not need to wait for the operation to complete. It will be notified when the operation has completed on behalf of the kernel.

这篇关于如何游戏服务器与提升:短耳异步工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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