且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何游戏服务器与提升:短耳异步工作?

更新时间:2022-10-19 07:56:33


  

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


块引用>

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


  

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


块引用>

您应该遵循的boost ::短耳教程启动,并特别注意的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.