What am I thinking when I design a game server? (3)

Source: Internet
Author: User

With a single-process server, when I want to join the first game features, I again tangled, this goods is not the last thing I want Ah, I even use a single process to write the whole game, it is not this ah, I want a multi-process server, and the process should be able to communicate, The process can also be dynamic additions and deletions. So that I can carry the pressure I think will appear.


So I began to think about how I need to communicate between the process, the data how to transfer, the server process communication with the server and the client's communication is not the same, with the client's communication, need to ensure security, while the amount of data needed to be as small as possible, and the server process is usually not the existence of such a problem, Because most of the server process is usually on the same machine, even on different machines, it will be under the protection of the external network, so the internal communication basically does not need to consider security issues, of course, this is my word, I think so, so I want a fast, efficient, and simple way of communication, I want the two sides of the communication to be able to enter or leave casually, I want the process change does not affect the existing logic, and I do not want to limit the boot sequence of the process, because that will inevitably involve the state of the process need to synchronize, and I also want to pomelo such a stateless process, This makes it easy to change the server's load-carrying capacity by adding or deleting processes.


I found him in the ZMQ 1100 degrees. A shiny building block. Although the LGPL protocol gives it a bit of black, it still obscures the brilliance of the design, which basically satisfies all my ideas.


ZMQ's official documentation is very interesting, it is recommended that everyone go to see, I found on GitHub a very good Chinese translation:https://github.com/anjuke/zguide-cn

Basically the contents of this document all read, to ZMQ on the understanding, which is also its simple embodiment, nodejs ZMQ binding There are some problems, but are not too big problem, I use the use of the solution out. I first designed the model through ZMQ is the REQ-REP model, but also the simplest, most basic model, of course, between req and rep, I use the Route-dealer component to do the distribution of messages, and because at this time I have thought about the game of the various function modules, so I also abstract Out different server processes, and then allow them to send messages to each other, the whole design looks like this:


650) this.width=650; "Src=" Https://app.yinxiang.com/shard/s12/res/e03c7b14-3db1-4dac-8574-db96ea577e76/%E6%9C%8D %e5%8a%a1%e5%99%a8route-dealer%e6%9e%b6%e6%9e%84.png "Name=" e03c7b14-3db1-4dac-8574-db96ea577e76 "class=" En-media "style=" Margin:.857412em 0px 1.286em;padding:0px;border:0px;height:auto; "/>

The first thing that looks like a daisy is my original design, all process logic processes are brokered through Router-dealer, each process has a number of request links req, there are several response link reps, because there are many different processes, So each process needs to save the ZMQ link to communicate with other processes, ZMQ protocol, not a req can correspond to multiple reps, but req and rep number need to be consistent, at the beginning I think this is OK, can write to find that the goods are obviously too complex, and it seems to have not played a zmq the most Big power, so I started to read the ZMQ document. RTFM may be a painful process, but ZMQ's documentation is very good, pleasing, I also found an unexpected surprise, zmq asynchronous Butler mode, this mode is actually using Dealer link instead of req and rep link, so you can take a one-to-many way to distribute the message, Will not be like I did before the maintenance of a chrysanthemum. NPM Library has another foreigner to write the ZMQ Butler mode:Https://www.npmjs.com/package/pigato, but he wrote not satisfied with my needs, I do my own, with JS wrote a set of my taste of the asynchronous Butler mode, Later, a background, he introduced me immediately after the understanding, and quickly hands-on function to add, prove that I write is still good:)


With this dispenser, I quickly changed the above chrysanthemum more beautiful, and I also designed their communication protocol, loaded support a lot of data types, in fact, no use, and then use LUA to write a protocol resolver, according to the protocol configuration file, I use an INI file, to generate the corresponding Protocol code for LUA version and JS version:

650) this.width=650; "Src=" https://app.yinxiang.com/shard/s12/res/ed1c0052-f6cd-4aa5-9496-afc781d0c162/QQ%E5%9B% Be%e7%89%8720150713204103.png "Name=" ed1c0052-f6cd-4aa5-9496-afc781d0c162 "class=" En-media "style=" margin:. 857412em 0px 1.286em;padding:0px;border:0px;height:auto; "/>

Then I designed a command-line process to manage my set of server process systems, and the diagram became like this:


650) this.width=650; "Src=" https://app.yinxiang.com/shard/s12/res/6f2a6e3f-d839-4290-a05d-92cd4fc5830e/QQ%E5%9B% Be%e7%89%8720150713203911.png "class=" En-media "style=" Margin:.857412em 0px 1.286em;padding:0px;border:0px;height: auto; "/>

Each process provides a request link and a work link that sends request information from the request link, which is used to receive request information from another process and process the return. The request and the work reply are forwarded by the broker, and the pub and sub links are prepared to listen for messages from the command-line process. So that the basic framework of the server is finished, I also long out of breath.


The appearance of Chrysanthemum design shows that I do not fully understand the ZMQ behavior pattern, but also shows that my understanding of the TCP protocol is still less than the heat. I will continue to improve the design in the next article.


This article is from "rookie surfaced" blog, please be sure to keep this source http://rangercyh.blog.51cto.com/1444712/1673932

What am I thinking when I design a game server? (3)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.