End design of online services: Hack

xiaoxiao2021-03-06  101

?1 Overview

LoginServer? <----->? Gameserver

The server is divided into loginserver and gameserver ,? loginserver makes account authentication,? Gameserver makes the game master logic,

In the middle, you can add a Charserver,? Do people management,? Newly built the character, can you go to GameServer

Starting, LoginServer and Charserver are simpler, slightly.

After the verification of LoginServer, it will be assigned to the client a sessionID, and then communicate with Gameserver or Charserver,

Both this sessionID as the authentication code. • Client is only connected to Gameserver with Gameserver if the correct sessionID is sent.

2.gameserver hierarchy

Gameserver is divided into three layers, network layer <---> logic processing layer <-> database layer

Each layer has a message handling queue, • The message to be processed is stored. The message queue can use the advanced first queue, or

Use a heap or priority queue, how is it to treat the message according to the priority,? Hey, is it a bit similar to QoS?

thought of.

Each layer uses a thread pool technology, which is established in advance, and it is not enough to establish a new thread when it is not enough.

Ensure that there is a specified number of idle threads (MIN / MAX) in the thread pool, and the main thread constantly checks whether the queue has to be processed,?

Having is allocated from the thread pool to a free thread process.

Even under Linux is implemented with pthread_cond_wait and pthread_cond_signal.

2.1. Network layer

This layer can have multiple implementations depending on the operating system. The main function is to establish a TCP connection with the client. Segment the TCP stream into a package,

If there is encryption, decrypt, if there is compression, decompress,? Add the handling queue of the transaction layer? At the same time, send the message to be sent in the process in the process.

Send it, if you want to encrypt, encrypt, if you want to compress it, it is compressed.

Use the IOCP model under Windows? Select / Poll (Epoll) / KQueue under the UNIX-Like system

Even the SELECT mode is used in the even server, and the number of links in the Linux single port is limited, so I have read a plurality of threads to listen to a set of ports.

Make load balancing by loginserver, so that there is no excessive number of port connections.? Whenever there is a new client to log in?

LoginServer judges the number of connections per port, and select the minimum selection to the client.? Even I can make a dynamic approach.

When each port average connection exceeds xxxx, then open a new thread to monitor the new port, and notify loginserver.

2.2. Logic Processing Layer

This layer is the core of Gameserver.?

Assign the message into each sub-module according to the operation code (OPCode). The simplest method is established with continuous OPCODE starting from 0.

An array of processing functions corresponding to opcode,? Opcode as a subscript of an array? This only needs O (1) time to call to the desired function

Even Hash is saved, it is simple and efficient.

Submoduption See Section 7

2.3. Database layer

This layer is used for data storage, essentiallys the data in the memory to the hard disk,? If you are enough, can you use an existing database?

Write your own written algorithm to store text files, but for convenience, in order to improve efficiency, it is better to use a database.

Use MSSQL in Windows or with MySQL,

There is more use under UNIX-Like system. It is best to compatibility with several databases.

MySQL is excellent in performance, and it is a bit more powerful. If you don't need to use a stored procedure,? MySQL is still preferred.

The database layer generally uses a single thread already enough, can you do not need to do an object mutual exclusion, however, it is necessary to pay attention to it?

Database operation must use Transaction, which can effectively prevent copying phenomena, such as: Once transaction operations,

Before Rollback arrived to the transaction? Will not happen to hand over, but there is no situation.

3. Message format definition

3.1. Network Layer <-> Logic Layer Message Format (Network Package Format)

3.2. Logic Layer <-> Database Layer Message Format

4. Game Object Definition (Object)

Object?

?? | ------->? item ?? | ???????????? | ---->? Container (container object, such as warehouse, backpack, etc.)

?? |

?? | ------->? Unit?

?? | ??????????? | ----->? Player

?? | ??????????? | ----->? monster

?? | ??????????? | ----->? NPC

?? | ??????????? | ----->? Corpse (corpse object)

?? |

?? | ------->? GameObj?

?????????????? | ----->? Dynamicobj (temporary object generated by skill)

??

5. Map Scene Management

6. Script system

7. Logic layer modular design

转载请注明原文地址:https://www.9cbs.com/read-123305.html

New Post(0)