A high-concurrency game server architecture
By adopting IO multiplexing technology and Epoll mechanism on the Linux operating system, the high-concurrency game server architecture is designed, which solves the problems of weak concurrency capabilities and resource waste in traditional blocking IO models in high-concurrency scenarios, and realizes high-concurrency task processing and improve resource utilization.
Patent Information
- Application Number
- CN202410956845.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-17
- Publication Date
- 2024-11-05
- Estimated Expiration
- 2044-07-17
AI Technical Summary
Traditional socket-based blocking IO model has weak concurrency capabilities and severe resource waste in high concurrency scenarios, resulting in performance bottlenecks and memory exhaustion.
Using IO multiplexing technology and combining the Epoll mechanism of the Linux operating system, a high-concurrency game server architecture is designed, using a single thread to process multiple client sockets, and network events are distributed through the event bus to achieve high-concurrency task processing.
In high concurrency scenarios, the concurrency processing capability of the server is significantly improved, the operating system resources are fully utilized, the performance overhead brought by context switching is avoided, and the high throughput is achieved.
Smart Images

Figure CN118509466B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer systems and network communication technology, and in particular to a high-concurrency game server architecture. Background Art
[0002] Socket is a programming interface (API) for network communication. It provides a mechanism to enable data transmission between different computers over the network. Socket can use TCP or UDP protocol. It involves two roles, server and client. Network application development can be achieved through socket programming. Socket is widely used in various network application fields and is one of the important technologies for achieving network communication.
[0003] In the development of socket-based server-side network applications in traditional scenarios, using a blocking IO model to implement network communication is a simple and common way. Its main implementation method is divided into the following steps: waiting for the client to connect, receiving the connection request, processing the connection request, and closing the connection.
[0004] The traditional blocking IO model is simple and intuitive to implement, easy to understand and use. However, this processing method has many problems and defects in high-concurrency scenarios.
[0005] First, the traditional blocking IO model has low concurrency. The traditional blocking IO model can only handle one connection at a time, and each connection requires an independent thread or process to handle. When a large number of connections arrive at the same time, it will lead to a large waste of system resources and create performance bottlenecks, greatly limiting the concurrent processing capabilities of the server.
[0006] Secondly, blocking waiting wastes system resources. In the traditional blocking IO model, when the server waits for the client to transmit data, the thread or process may be blocked and unable to perform other tasks. This will lead to low utilization of server resources and inability to fully utilize the server's performance.
[0007] Finally, thread management is complex and consumes a lot of resources. Since each connection requires an independent thread or process to handle, the server needs to manage a large number of threads or processes, including operations such as thread or process creation, destruction, and context switching, which increases the burden on the server CPU. In addition, each process or thread requires a certain amount of memory space to store its context information and data structure. When the number of concurrent connections increases, the memory usage will also increase, which may lead to the exhaustion of server memory resources. Although the thread pool technology can solve the problem of resource waste by fixing the number of threads, the above-mentioned blocking and waiting problems will still occur. Summary of the invention
[0008] The technical problem to be solved by the present invention is to provide a high-concurrency game server architecture in view of the deficiencies of the above-mentioned prior art. In view of the problems of weak concurrency capability, blocking waiting, waste of system resources, etc. when using the traditional blocking IO model in high-concurrency scenarios, the IO multiplexing technology is combined to give full play to the advantages of the Linux operating system in high-concurrency scenarios, and a game server architecture that supports high concurrency is designed and implemented.
[0009] In order to solve the above technical problems, the technical solution adopted by the present invention is:
[0010] A high-concurrency game server architecture, deployed on the Linux operating system, includes a server and a client, wherein the client is a command line application program drawn based on NCurses; the server includes an IO multiplexing module, a protocol parsing module, a command line parameter parsing module, a logging module and a game logic module;
[0011] The IO multiplexing module processes TCP network requests based on the Epoll mechanism provided by the Linux system;
[0012] The protocol parsing module uses an application layer protocol designed based on the TCP protocol as a communication protocol. The protocol content is that event data is transmitted between the server and the client through "messages". The "message" consists of a 4-byte event length, a 4-byte event type and an event body of variable length; an event bus is used to distribute network events;
[0013] The command line parameter parsing module is used to process the parameters passed through the command line at runtime, and can dynamically specify the port and log level parameters;
[0014] The logging module is used to record log information of different levels, output the log information to the standard output and write it into a file for persistent storage;
[0015] The game logic module is used to implement the processing of corresponding specific game logic according to the events triggered by the IO multiplexing module.
[0016] Furthermore, the implementation steps of the IO multiplexing module include:
[0017] Step 1: Initialize the Epoll instance and server socket;
[0018] Step 1.1: Declare function pointer variables client_connected_callback, client_data_callback, and client_disconnected_callback, which are used to expose the client connection event callback function, data arrival callback function, and client connection disconnection callback function, respectively, to decouple the network request processing logic from the application layer protocol processing logic;
[0019] Step 1.2: Create an Epoll instance;
[0020] Step 1.3: Initialize the server socket instance;
[0021] Step 1.4: Add the file descriptor corresponding to the server socket to the previously created Epoll instance and listen for EPOLLIN events;
[0022] Step 2: Loop to accept incoming connections from the client socket;
[0023] Step 2.1: Use the epoll_wait operation to receive a list of events that occurred in the Epoll instance;
[0024] Step 2.2: Process all the events that occurred in sequence; if the file descriptor of the event is not the server socket, execute step 2.4, otherwise execute step 2.3;
[0025] Step 2.3: Add a new client;
[0026] Step 2.4: Process incoming data;
[0027] Step 3: Close the socket connection and release related resources;
[0028] Step 3.1: Close the socket connection corresponding to the file descriptor;
[0029] Step 3.2: Cancel the monitoring of the file descriptor in the Epoll instance;
[0030] Step 3.3: Call the client disconnect callback function to notify the game logic module to handle the event.
[0031] Furthermore, the step 1.3 specifically includes:
[0032] Step 1.3.1: Create a socket of domain AF_INET6 and type SOCK_STREAM;
[0033] Step 1.3.2: Set the socket option SO_REUSEADDR to allow reuse of local addresses and ports;
[0034] Step 1.3.3: Bind the socket to any local IP address and specified port number;
[0035] Step 1.3.4: Listen for incoming socket connections.
[0036] Furthermore, the step 2.3 specifically includes:
[0037] Step 2.3.1: Accept incoming client connections;
[0038] Step 2.3.2: Add the file descriptor corresponding to the client connection to the Epoll instance and listen for EPOLLIN events;
[0039] Step 2.3.3: Call the client connection event callback function to notify the upper-level module to process the event.
[0040] Furthermore, the step 2.4 specifically includes:
[0041] Step 2.4.1: Read the input data of the specified client into the cache; if the read data is empty, execute step 3;
[0042] Step 2.4.2: Call the data arrival callback function to notify the game logic module to process the event.
[0043] Furthermore, the implementation steps of the protocol analysis module include:
[0044] Step P1: declare an event linked list node structure, which includes a callback function whose parameters are the event and socket file descriptor, and a pointer to the next event node;
[0045] Step P2: Create an event bus instance, including:
[0046] Step P2.1: Create an event listener list with a length equal to the number of event types;
[0047] Step P2.2: Set the next value of the head pointer of each listener list to null;
[0048] Step P3: Create a registration event listener function, including:
[0049] Step P3.1: Allocate memory for the next event listener node;
[0050] Step P3.2: Store the incoming listener callback function into the corresponding node;
[0051] Step P4: Create a logout event listener function, including:
[0052] Step P4.1: Traverse all listener nodes in the target event type in turn;
[0053] Step P4.2: If the target listener function is found, delete the node and release the memory;
[0054] Step P5: Create a trigger event function, including:
[0055] Step P5.1: If the event type ID is illegal, ignore the event, otherwise jump to step P5.2;
[0056] Step P5.2: Traverse the listener list in sequence and pass the event instance to the corresponding processing logic through the callback function in the listener;
[0057] Step P6: declare the client information table, which internally stores the client connection information, including file descriptors, player information, room addresses, and receiving buffers, and uses file descriptors for indexing;
[0058] Step P7: Process the client connection, including:
[0059] Step P7.1: Record the client connection information log;
[0060] Step P7.2: Initialize the client information at the corresponding position in the client information table;
[0061] Step P8: Process client input data.
[0062] Furthermore, the step P8 specifically includes:
[0063] Step P8.1: Use the file descriptor in the client information table to obtain the corresponding client information reference;
[0064] Step P8.2: If the number of bytes read into the receive buffer is less than 4, execute step P8.3, otherwise execute P8.4;
[0065] Step P8.3: Read 1 byte into the buffer. If 4 bytes have been read into the buffer, execute P8.5;
[0066] Step P8.4: The remaining length of the data to be processed is reduced by 1 byte, and P8.1 is executed;
[0067] Step P8.5: Read the 4 bytes in the buffer as uint32_t type, convert them to host byte order, and update the number of bytes to be read in the buffer;
[0068] Step P8.6: If the number of bytes read into the buffer is less than the number of bytes to be read, all received data are copied into the buffer and P8.1 is executed; otherwise, step P8.7 is executed;
[0069] Step P8.7: Use the receiving buffer as the event pointer, convert the event type to the host byte order, and then distribute it to the event bus;
[0070] Step P8.8: Clear the buffer. If there is still data to be processed, execute P8.1.
[0071] Furthermore, the implementation steps of the command line parameter parsing module include:
[0072] Step A1: Declare parameter variables and their default values;
[0073] Step A2: Parse command line parameters;
[0074] Step A2.1: Traverse the command line parameter list in sequence;
[0075] Step A2.2: If the command line parameter format is correct, store the result in the corresponding variable; if the format is incorrect, print help information and exit the program.
[0076] The beneficial effect of adopting the above technical solution is that the high-concurrency game server architecture provided by the present invention uses Epoll technology to poll the client socket based on IO multiplexing technology, and only uses a single thread to complete high-concurrency tasks. Test results show that the present invention can withstand high throughput and perform well in high-concurrency scenarios; the feature of using only a single thread also enables the present invention to fully utilize operating system resources and avoid performance overhead caused by context switching. BRIEF DESCRIPTION OF THE DRAWINGS
[0077] Figure 1 A high-concurrency game server architecture design diagram provided for an embodiment of the present invention. DETAILED DESCRIPTION
[0078] The specific implementation of the present invention is further described in detail below in conjunction with the accompanying drawings and examples. The following examples are used to illustrate the present invention, but are not intended to limit the scope of the present invention.
[0079] This embodiment aims to solve the problems of weak concurrency, blocking waiting, and waste of system resources when using the traditional blocking IO model in high-concurrency scenarios. By combining IO multiplexing technology, this embodiment gives full play to the advantages of the Linux operating system in high-concurrency scenarios and designs a game server architecture that supports high concurrency. The high-concurrency game server architecture of this embodiment is as follows: Figure 1 As shown, it is deployed on the Linux operating system and includes a server and a client. The client is a command line application drawn based on NCurses; the server includes an IO multiplexing module, a protocol parsing module, a command line parameter parsing module, a logging module, and a game logic module.
[0080] The IO multiplexing module processes TCP network requests based on the Epoll mechanism provided by the Linux system, including the following steps:
[0081] Step 1: Initialize the Epoll instance and server socket.
[0082] Step 1.1: Declare function pointer variables client_connected_callback, client_data_callback, and client_disconnected_callback, which are used to expose the client connection event callback function, data arrival callback function, and client connection disconnection callback function, respectively, to decouple the network request processing logic from the application layer protocol processing logic;
[0083] Step 1.2: Create an Epoll instance;
[0084] Step 1.3: Initialize the server socket instance;
[0085] Step 1.3.1: Create a socket of domain AF_INET6 and type SOCK_STREAM;
[0086] Step 1.3.2: Set the socket option SO_REUSEADDR to allow reuse of local addresses and ports;
[0087] Step 1.3.3: Bind the socket to any local IP address and specified port number;
[0088] Step 1.3.4: Listen for incoming socket connections;
[0089] Step 1.4: Add the file descriptor corresponding to the server socket to the previously created Epoll instance and listen for EPOLLIN events.
[0090] Step 2: Loop to accept incoming connections from the client socket.
[0091] Step 2.1: Use the epoll_wait operation to receive a list of events that occurred in the Epoll instance;
[0092] Step 2.2: Process all the events that occurred in sequence; if the file descriptor of the event is not the server socket, execute step 2.4, otherwise execute step 2.3;
[0093] Step 2.3: Add a new client;
[0094] Step 2.3.1: Accept incoming client connections;
[0095] Step 2.3.2: Add the file descriptor corresponding to the client connection to the Epoll instance and listen for EPOLLIN events;
[0096] Step 2.3.3: Call the client connection event callback function to notify the upper layer module to process the event;
[0097] Step 2.4: Process incoming data;
[0098] Step 2.4.1: Read the input data of the specified client into the cache. If the read data is empty, execute step 3;
[0099] Step 2.4.2: Call the data arrival callback function to notify the game logic module to process the event.
[0100] Step 3: Close the socket connection and release related resources.
[0101] Step 3.1: Close the socket connection corresponding to the file descriptor;
[0102] Step 3.2: Cancel the monitoring of the file descriptor in the Epoll instance;
[0103] Step 3.3: Call the client disconnect callback function to notify the game logic module to handle the event.
[0104] The protocol parsing module uses an application layer protocol designed based on the TCP protocol as the communication protocol. The protocol content is that the server and the client transmit event data through "messages" and use the event bus to distribute network events. "Messages" are composed of a 4-byte event length, a 4-byte event type, and an event body of variable length.
[0105] The protocol analysis module specifically includes the following steps:
[0106] Step P1: Declare an event linked list node structure, which includes a callback function whose parameters are the event and socket file descriptor, and a pointer to the next event node.
[0107] Step P2: Create an event bus instance.
[0108] Step P2.1: Create an event listener list with a length equal to the number of event types;
[0109] Step P2.2: Set the next value of the head pointer of each listener list to null.
[0110] Step P3: Create a registration event listener function.
[0111] Step P3.1: Allocate memory for the next event listener node;
[0112] Step P3.2: Store the incoming listener callback function into the corresponding node.
[0113] Step P4: Create a logout event listener function.
[0114] Step P4.1: Traverse all listener nodes in the target event type in turn;
[0115] Step P4.2: If the target listener function is found, delete the node and release the memory.
[0116] Step P5: Create a trigger event function.
[0117] Step P5.1: If the event type ID is illegal, ignore the event, otherwise jump to step P5.2;
[0118] Step P5.2: Traverse the listener list in sequence and pass the event instance to the corresponding processing logic through the callback function in the listener.
[0119] Step P6: Declare the client information table, which internally stores the client connection information, including file descriptors, player information, room addresses, and receiving buffers, and uses file descriptors for indexing.
[0120] Step P7: Process client connection.
[0121] Step P7.1: Record the client connection information log;
[0122] Step P7.2: Initialize the client information at the corresponding position in the client information table.
[0123] Step P8: Process client input data.
[0124] Step P8.1: Use the file descriptor in the client information table to obtain the corresponding client information reference;
[0125] Step P8.2: If the number of bytes read into the receive buffer is less than 4, execute step P8.3, otherwise execute P8.4;
[0126] Step P8.3: Read 1 byte into the buffer. If 4 bytes have been read into the buffer, execute P8.5;
[0127] Step P8.4: The remaining length of the data to be processed is reduced by 1 byte, and P8.1 is executed;
[0128] Step P8.5: Read the 4 bytes in the buffer as uint32_t type, convert them to host byte order, and update the number of bytes to be read in the buffer;
[0129] Step P8.6: If the number of bytes read into the buffer is less than the number of bytes to be read, all received data are copied into the buffer and P8.1 is executed; otherwise, step P8.7 is executed;
[0130] Step P8.7: Use the receiving buffer as the event pointer, convert the event type to the host byte order, and then distribute it to the event bus;
[0131] Step P8.8: Clear the buffer. If there is still data to be processed, execute P8.1.
[0132] The command line parameter parsing module is used to process the parameters passed through the command line at runtime, and can dynamically specify parameters such as port and log level, including the following steps:
[0133] Step A1: Declare parameter variables and their default values.
[0134] Step A2: Parse command line arguments.
[0135] Step A2.1: Traverse the command line parameter list in sequence;
[0136] Step A2.2: If the command line parameter format is correct, store the result in the corresponding variable; if the format is incorrect, print help information and exit the program.
[0137] The help information is:
[0138] Use tetris-online-server and parameters to start the program, where the parameters can be --port=[number] to specify the startup port number, the default is 19198; --log-level=[log level] to specify the minimum level of log output, the default is info. The log levels are trace, debug, info, warn, panic, disabled, which correspond to tracing, debugging, information, warning, error and disabled.
[0139] The log recording module is used to record log information of different levels, output the log information to the standard output and write it into a file for persistent storage.
[0140] The game logic module is used to implement the processing of corresponding specific game logic according to the events triggered by the IO multiplexing module.
[0141] The present invention was stress tested using TCP short connection and long connection in a CentOS system, and the results are shown in Table 1 and Table 2.
[0142] It can be seen that the present invention can withstand a higher throughput and performs well in high-concurrency scenarios; the feature of using only a single thread also enables the present invention to fully utilize operating system resources and avoid performance overhead caused by context switching.
[0143] Table 1 Long connection stress test results
[0144] Test Type process Event data size (bytes) Total events Minimum time (milliseconds) Maximum time (milliseconds) Average time (milliseconds) Requests per second Long Connection 1 4096 10000 0 880 708 2987.75 Long Connection 1 8192 10000 0 798 681 1679.26 Long Connection 1 4096 30000 0 897 785 2977.08 Long Connection 1 4096 100000 0 895 815 2968.77
[0145] Table 2 Short connection stress test results
[0146] Test Type process Event data size (bytes) Total events Minimum time (milliseconds) Maximum time (milliseconds) Average time (milliseconds) Requests per second Short connection 1 4096 10000 0 8 0 2375.30 Short connection 10 4096 10000 0 10 3 3194.89 Short connection 100 4096 10000 1 101 33 2946.38 Short connection 200 4096 10000 0 860 33 2457.00 Short connection 100 8192 10000 0 80 58 1693.77 Short connection 100 10240 10000 1 93 68 1451.38 Short connection 100 4096 30000 0 44 31 3139.39 Short connection 100 4096 100000 0 47 31 3132.34
[0147] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the above embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the above embodiments, or replace some or all of the technical features therein with equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope defined by the claims of the present invention.
Claims
1. A high-concurrency game server architecture, characterized by: Deployed on the Linux operating system, it consists of a server and a client. The client is a command-line application based on NCurses. The server includes an IO multiplexing module, a protocol parsing module, a command-line parameter parsing module, a logging module, and a game logic module. The IO multiplexing module processes TCP network requests based on the Epoll mechanism provided by the Linux system; The protocol parsing module uses an application layer protocol designed based on the TCP protocol as a communication protocol. The protocol content is that event data is transmitted between the server and the client through "messages". The "message" consists of a 4-byte event length, a 4-byte event type and an event body of variable length; an event bus is used to distribute network events; The command line parameter parsing module is used to process the parameters passed through the command line at runtime, and can dynamically specify the port and log level parameters; The logging module is used to record log information of different levels, output the log information to the standard output and write it into a file for persistent storage; The game logic module is used to implement the processing of the corresponding specific game logic according to the event triggered by the IO multiplexing module; The implementation steps of the IO multiplexing module include: Step 1: Initialize the Epoll instance and server socket; Step 1.1: Declare function pointer variables client_connected_callback, client_data_callback, and client_disconnected_callback, which are used to expose the client connection event callback function, data arrival callback function, and client connection disconnection callback function, respectively, to decouple the network request processing logic from the application layer protocol processing logic; Step 1.2: Create an Epoll instance; Step 1.3: Initialize the server socket instance; Step 1.4: Add the file descriptor corresponding to the server socket to the previously created Epoll instance and listen for EPOLLIN events; Step 2: Loop to accept incoming connections from the client socket; Step 2.1: Use the epoll_wait operation to receive a list of events that occurred in the Epoll instance; Step 2.2: Process all the events that occurred in sequence; if the file descriptor of the event is not the server socket, execute step 2.4, otherwise execute step 2.3; Step 2.3: Add a new client; specifically includes: Step 2.3.1: Accept incoming client connections; Step 2.3.2: Add the file descriptor corresponding to the client connection to the Epoll instance and listen for EPOLLIN events; Step 2.3.3: Call the client connection event callback function to notify the upper layer module to process the event; Step 2.4: Process the incoming data; specifically: Step 2.4.1: Read the input data of the specified client into the cache; if the read data is empty, execute step 3; Step 2.4.2: Call the data arrival callback function to notify the game logic module to process the event; Step 3: Close the socket connection and release related resources; Step 3.1: Close the socket connection corresponding to the file descriptor; Step 3.2: Cancel the monitoring of the file descriptor in the Epoll instance; Step 3.3: Call the client disconnection callback function to notify the game logic module to handle the event; The implementation steps of the protocol analysis module include: Step P1: declare an event linked list node structure, which includes a callback function whose parameters are the event and socket file descriptor, and a pointer to the next event node; Step P2: Create an event bus instance, including: Step P2.1: Create an event listener list with a length equal to the number of event types; Step P2.2: Set the next value of the head pointer of each listener list to null; Step P3: Create a registration event listener function, including: Step P3.1: Allocate memory for the next event listener node; Step P3.2: Store the incoming listener callback function into the corresponding node; Step P4: Create a logout event listener function, including: Step P4.1: Traverse all listener nodes in the target event type in turn; Step P4.2: If the target listener function is found, delete the node and release the memory; Step P5: Create a trigger event function, including: Step P5.1: If the event type ID is illegal, ignore the event, otherwise jump to step P5.2; Step P5.2: Traverse the listener list in turn, and pass the event instance to the corresponding processing logic through the callback function in the listener; Step P6: declare the client information table, which internally stores the client connection information, including file descriptors, player information, room addresses, and receiving buffers, and uses file descriptors for indexing; Step P7: Process the client connection, including: Step P7.1: Record the client connection information log; Step P7.2: Initialize the client information at the corresponding position in the client information table; Step P8: Processing client input data; specifically including: Step P8.1: Use the file descriptor in the client information table to obtain the corresponding client information reference; Step P8.2: If the number of bytes read into the receive buffer is less than 4, execute step P8.3, otherwise execute P8.4; Step P8.3: Read 1 byte into the buffer. If 4 bytes have been read into the buffer, execute P8.5; Step P8.4: Reduce the length of the data to be processed by 1 byte and execute P8.1; Step P8.5: Read the 4 bytes in the buffer as uint32_t type, convert them to host byte order, and update the number of bytes to be read in the buffer; Step P8.6: If the number of bytes read into the buffer is less than the number of bytes to be read, all received data are copied into the buffer and P8.1 is executed; otherwise, step P8.7 is executed; Step P8.7: Use the receiving buffer as the event pointer, convert the event type to the host byte order, and then distribute it to the event bus; Step P8.8: Clear the buffer. If there is still data to be processed, execute P8.
1.
2. A high-concurrency game server architecture according to claim 1, characterized in that: The step 1.3 specifically includes: Step 1.3.1: Create a socket of domain AF_INET6 and type SOCK_STREAM; Step 1.3.2: Set the socket option SO_REUSEADDR to allow reuse of local addresses and ports; Step 1.3.3: Bind the socket to any local IP address and specified port number; Step 1.3.4: Listen for incoming socket connections.
3. A high-concurrency game server architecture according to claim 1, characterized in that: The implementation steps of the command line parameter parsing module include: Step A1: Declare parameter variables and their default values; Step A2: Parse command line parameters; Step A2.1: Traverse the command line parameter list in sequence; Step A2.2: If the command line parameter format is correct, store the result in the corresponding variable; if the format is incorrect, print help information and exit the program.
