MarioTCP: Multi Libvent TCP Server a high-performance TCP Server

Source: Internet
Author: User

I have built this open source project in sourceforge, introduce the project first in csdn: http://blog.csdn.net/everlastinging/article/details/10894493 did not expect everyone to evaluate is good, so again, I hope more people look at him, he can be used as the underlying layer of large-scale game servers and P2P servers, and he can also learn this technology experience quickly without graduation from a master's degree in computer science.


MrioTCP, Super Mario, as the name suggests, is not only efficient, but also super simple and fun. At the same time, it can be a simple Linux C Development Learning Project. It is no exaggeration to say that if you master this project all, you will become a good guy in Linux C. Of course, you can also use the source code package's mario. c (maritcp Server Sample program) to learn, you can get started quickly for Linux C development.

After more than two months of testing and compiling c ++ client testing and tuning system parameters, the test results show that the maximum bandwidth throughput of a single machine is 1000 MB, and the maximum TCP persistent connection is 1 million, the number of connections processed per second reaches 40 thousand, and the system load value is very low. In short, it can take full advantage of the maximum capacity of a server to provide the highest-performance services. It has been fully tested and runs stably and occupies a very small amount of system resources.

He is an open-source project built on Sourceforge, initiated by source code author JohnFong. The source code can be downloaded on Sourceforge.

Sourceforge download: https://sourceforge.net/projects/mariotcp/files/latest/download

Csdn download: http://download.csdn.net/detail/everlastinging/6195605

Download 51cto: http://down.51cto.com/data/935913

Getting Started


It's easy to use MarioTCP to build a TCP server with powerful performance!

The engineering source code package is a very concise example, and a tcp server program: maritcp is generated.
In the source code package:
Mario. c is a simple example of the main program. Direct make can compile maritcp, a tcp server, and the business logic has only one function: count the number of online sockets at the same time, and output every 1 minute.
The mario folder, the core code of MarioTCP, and make can directly compile static libmario.. The core architecture of MarioTCP will be introduced later.
The test folder is a slightly simple client test program. By establishing a connection with the server and sending a LOGIN package to log on to the server, the maritcp server will add 1 online at the same time, when the client is disconnected, the number of online servers decreases by 1.

Now let's talk about how to customize a tcp server with its own business logic in five steps:
1. initialize the SERVER
SERVER * server = init_server (conf-> port, conf-> workernum, conf-> connnum, conf-> timeout, conf-> timeout );
The input parameters are:
Server Listen port, number of worker threads, number of connections supported by each thread, read timeout, and write timeout.
Workernum * connum is the number of persistent connections supported by the server. A worker can easily support 0.1 million persistent connections.

2. Implement and register business logic functions
For specific business logic functions, see the Function module. You can use the "regist _ *" function defined in mario. h to register a registry.

/*
* Register a business processing function
/
Void regist_akg_func (uint16 id, FUNC_PTR func );
The id can be any number between 0 and. This id is encapsulated in the MarioTCP protocol. See the end of this Article ).
The range of IDS can be customized according to the business logic. For example, maritcp is set through the CMD struct defined in protocol. h:
Typedef enum _ CMD {
Performance_function_base = 0x6100,
Performance_function_login = 0x6101
} CMD;

If you want to add a "say_hello" service for maritcp, you can do this:
1) Add: CMD_FUNCTION_SAY_HELLO = 0x602 in CMD.
2) Add a function to the function:
Sint32 say_hello (CONN c ){
I) Use CONN to parse the parameters of the request sent from the client
II) Set "hello" to c-> out_buf
III) bufferevent_write (c-> bufev, c-> out_buf, hrsp-> pkglen );
IV) return 0;
}
3) add regist_akg_func (performance_function_say_hello, say_hello) to mario. c );

How is it? It's easy and efficient to customize your business logic!

3. Start the log thread start_log_thread ()
The log function encapsulation of MarioTCP is not good enough. Continue to discuss it on the go on 1.0.0 page...

4. Start the server start_server (void *) server );
OK, a TCP server that supports 1 million or more connections is born!


Go On 1.0.0


The first release version is 0.9.9. Using this package, you can create a stable and efficient TCP server that customizes your business logic in just a few minutes, but there are still a lot of places to be improved MarioTCP, let us solve the following problems as soon as possible, let the MarioTCP-1.0.0 release as soon as possible!


1. How to optimize the MarioTCP Protocol
To ensure the security of MarioTCP, a simple MarioTCP protocol is defined. After three handshakes to connect to the MarioTCP client, the format of the next packet must be "HEAD + Data, the HEAD struct is defined in mario_akg.h:
Typedef struct _ HEAD {
Uint32 stx;
Uint16 pkglen;
Uint16 akg_id;
} HEAD;
Pkglen is the length of the entire package, that is, "HEAD + Data ".
Akg_id and the id corresponding to the custom business logic function, for example, pai_function_say_hello on the "Getting Started" Page
Stx is the ciphertext of your custom protocol. It is registered through regist_stx (uint32 stx) (see mario. c)

Although the MarioTCP protocol is simple enough, and the ciphertext at the beginning of the Protocol can be customized, can it be simpler or have no protocol to maximize the convenience of development and use, we need your suggestions and help!

2. The log system is too rigid
MarioTCP has a set of self-contained log functions, but it is obscure.
Next, expand...

3. Business Logic Stability Support
MarioTCP is highly efficient and stable for network connection and read/write.
However, the thread pool of MarioTCP is fixed and globally unique for initialization. Dead threads cannot be restarted. The Master thread assigned to network tasks does not have the function of listening to worker, A thread is dead, but the task is still allocated, resulting in service accumulation and not processing. This problem occurs if the business logic is complex and inefficient.
In large online projects, where MarioTCP is used, the above problems are avoided through monitoring, alarms, and automatic processing of the business logic module. This function has not been abstracted into MarioTCP due to time issues.

I will deal with this matter in the near future. I also hope you will have suggestions and help !!


Why Supper


I. Why is it super efficient?
1. All struct and memory used by the network service are initialized when the program is started. No destruction or recovery is required.
No destruction is easy to understand and not explained.
No recovery refers to the out-of-the-box use of all memory units, use up and availability, and do not need to perform reset operations.
2. One master thread performs accept
After testing, it is found that the accept of a multi-process or thread is the same as that of a process or thread. The difference is not big under the extreme pressure.
A master node does not need to use locks to solve synchronization problems better than multiple masters.
3. Single producer consumer mode for master and worker, completely unlocked Communication
Not only is accept unlocked, but connection allocation and subsequent conncetion processing are lockless.
Even the business logic can be found in the example of the online statistics function of maritcp.) The log system of MarioTCP is also one reason why the log system is not abstract enough. The previous design was too dependent on the overall architecture) they are all unlocked!
4. A worker libevent Environment
Libevent handles 0.1 million persistent network read/write events, maximizing its performance.
Each worker has an independent set of libevent. This structure has been tested and found to have low overhead and high performance.

2. How to test the performance of a single machine with millions of persistent connections and 40 thousand cps connections per second
1. Set the maximum number of files in the system to unlimited.
2. Set the system's tcp memory Kernel Parameter to over MB.
3. Set the system ip address to 15, so the number of long connections that can be served is theoretically at least 15*(65535-1024)
4. Use epoll or libevent to open a client program that can connect to 1000 at the same time. The program also needs to randomly select 1000 connections per second and then create new connections. In addition, thousands of connections are randomly selected to send packets.
At the same time, 20 clients are enabled on multiple machines, that is, persistent connections, connections are disconnected every second, new connections are added, and several packages are sent.
5. Set the server to reuse the SYN_WAIT connection. The client is disconnected to prevent client program port accumulation)
In short, it was a tough test. The test was completed more than two months ago and later.
The above content is written by memory, and may contain errors or omissions. In order to announce the test code and test results, we will re-develop, test, and adjust the above content.


Related Article

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.