A simple game server framework

Source: Internet
Author: User
Tags xml parser dedicated server

Recently, I was not very busy. I wrote a prototype of my own game server framework, which is not perfect in many places, but it is basically enough to run. Let me start with the upper-layer structure, to the Implementation Details, and write anything when I think of it.

Part 1 server Logic

The server is simply divided into three parts. The client connection first reaches the gateway server. The Gateway has a thread to listen for connections from the client, then, the data is sent to the logic server of the game. On this logic game server, data interaction is through interaction with the data server. RecordServer is used to process connections to databases and query these tasks. Of course, in order for the game server to be able to maximize the number of connections, you must set the number of connections on the gateway server so as not to create a card B like the so-called smooth railway ticket booking network, think about the tragedy of getting a dota card. When we were doing a large online game, these three servers were obviously not enough. Of course, it doesn't matter when you write a small game. Several servers are all set up on your own broken notebook, not just a few programs.

This is almost the case. Let's get to the details.

Part 2 Implementation Details

This is messy, scattered, and casually written. Many of these tools are encapsulated to facilitate your use in projects.

1. make

If there are so many directories in the project and so many sub-directories, you must use tools to compile them. Use aclocal, automake, autoconf, and make to compile our program and compile your own configure. in file, define compilation options, Link Library, and so on, and then write Makefile for each sub-project to be compiled. am, and some need to be compiled into libraries, such as base classes. others need to be compiled into executable files, GatewayServer, LogicalServer, and RecordServer.

2. Socket encapsulation, used by epoll

In linux, we use socket to read and write data on the network. This is very simple. When a client on the gateway connects, we allocate a socket descriptor to it. On the gateway, one thread is used for accept, and one thread is used for data processing. When accept is used for a connection request, it is placed in the Data Processing thread, receives a data, and forwards it directly to the logical server, we use epoll_wait to process the read/write processing of the plug-in. Each n ms processes a loop, and each cycle uses epoll_wait. These socket with events are retrieved at a time.

3. Data Encryption, decryption, compression, and decompression

To ensure security, data on the network must be encrypted and decrypted. This is simple. I will not talk about all the content on the Internet here (I have finished writing all the data and will try again later ). Compressing data can reduce bandwidth throughput, that is, simply calling several zlib functions. I will not elaborate on it. In the zlib usage forwarded earlier, I found myself too lazy, I am too lazy to type.

4. Thread encapsulation, mutex, read/write lock

These are simply using RAII or other methods to encapsulate these items locally. (You should write a thread pool to manage these threads, todo)

5. Database Encapsulation

Using mysql and using mysql's c api functions, this must be encapsulated. Instead of processing data every time, you have to do a lot of things to implement a Field (column) of local data ), record, Table, DataBase, and RecordSet ). Create a database connection handle, such as MysqlHandle, to process and process the database, and implement a HandlerPool. Each time a handle is retrieved from the Pool to query the database, to avoid reconnecting every time.

6. Your own memory pool

In the previous article "memory pool technology details", "write your own memory distributor", and "memory pool applications", we have already explained in detail how to create a memory pool, of course I have made some changes here, but the general idea is that.

7. There is a state machine implementation

This is also recorded in the state machine article I wrote earlier. Haha, I really don't want to continue with the code word, but stick to it.

8. Interaction framework between lua and c ++

This is half done for the time being. When it is all done, let's get it done. It's like npc processing, script processing, and tolua ++.

9. tinyxml encapsulation and regular expression Encapsulation

Tinyxml is a lightweight xml parser, which is easy to use. Regular Expressions Do not enter the c ++ standard, but many existing regular expressions are still processed. The regex. h In the linux library is used directly to compile regular expressions and match the results,

10. log System

How can a project not have its own log system? It is simply logging and writing something into the file. It can be easily done with std: fstream to define the log level: error/debug/fatel/info

11. Time Encapsulation

This is required; otherwise, you can call various functions such as get_clocktime, gmtime, and time each time.

12. Use the Noncopyable and Singleton design methods in the boost library to write our code.

13. define various signal handles and what policies are used when a signal occurs, such as SIGPIPE, for ignore processing.

To be continued ........ (Sleeping)

The game server technology should have been very mature. Compared with the client, its technology update speed is very slow. On the client side, there are a lot of technologies, and various game engines, such as the illusion of 3D, what particle engines, sounds, web game flash, html5, utility, and so on. I want to set up a dedicated server first, and wait for other ones.

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.