Chaos Is Linux Platform , Reactor Network event Library , Currently only TCP Transmission Protocol , Only in X86_64 Compile , And follow 3-clause BSD Open source Protocol . In use , It can be said that it is similar Boost ASIO, It may be because Boost ASIO The interface design is very cute. , And Boost ASIO Thoughts on asynchronous programming , I personally agree , But I have not read it carefully. Boost ASIO Source code , First Boost I am suffering from the readability of templated programming. , The second reason is that you do not want to develop the product in the case of preemptible design. Chaos, Many things can only be considered by ourselves , In order to gain some benefits .
Enter topic,AboutChaosLibrary source code and all test cases and application services,All inHttps://github.com/lyjdamzwf/chaosDownload
Source code directory structure
Source code of the chaos-chaos Library
Async_method-used for Asynchronous Message Queue implementation, is the smallest task unit, similar to boost: bind & boost: Function Utility-common tools Thread-encapsulation of pthread Task_service-core module, including asynchronous message queue, network I/O management, and timeout events, ask_service can be used as a powerful multi-thread asynchronous programming tool, not just as a reactor module at the network layer (equivalent to io_service of boost ASIO) Log-log component Heart_beat-general-purpose element heartbeat Management Based on task_service. Network-the TCP server is encapsulated based on task_service. The underlying I/O multiplexing uses the epoll lt mode to provide common socket behavior and manage connections in a unified manner, and provides a user space read/write buffer, allowing developers to quickly build a TCP Server Test-some test cases, ranging from a tool-class test to a different type of test Server Program
Simple TCP Server To create a TCP server, use the following three steps: 1. First, we need to define a connection Event Callback. When chaos finds any connection status changes, it will call back this function.
2. Definition of the connection policy class, which tells chaos how to handle TCP Packets
Here, we need to note that for TCP byte stream processing, the underlying layer of chaos has a default mechanism. When a complete data packet is read, handle_packet will be called. You can see that, after receiving the complete data packet, the Service sends the same content to the peer.
The implementation of the Default policy is in default_conn_strategy_t inherited by test_server_echo_conn_t. The process of this class for all TCP byte streams is as follows:
Baotou members of the Default policy:
If you want to use your own TCP byte stream parsing policy, you can inherit the chaos: Network: connection_t to implement it, and inject your own policy when initializing the service, the specific method is to provide a class that you inherit from connection_t, and then use it as the template parameter of tcp_service_t. 3. initialize and start the service
The running mechanism of the chaos: Network: tcp_service_t class is to fix a thread to do the accept job, and the successful connections of the accept will be assigned to each work thread for I/O, the number of work threads can be specified at start. This completes the establishment of a simple TCP echo server. The above is just the key to my interception.CodeThe complete code in the directory of Chaos/test/echo_server is provided for reference.
How to generate and applyChaosTo your project
Currently, the link provided by chaos is a static library (. a) Yes. You can run the build_all.sh script in the root directory to generate the code (you need to install the automake software). You can compile the entire Chos without installing any third-party libraries, after compilation is complete, the Lib temporary directory will be generated in the root directory, which contains the corresponding chaos static library. Then, you can refer to the use case in the test directory to link to your project.
View outside network libraryChaos
Previously, I mentioned that task_service is not only the reactor core of a network library, but also a tool for multithreading and asynchronous programming in daily development, so that you do not have to worry about thread switching, multi-thread message delivery and other details. By simply encapsulating requests into an Asynchronous Method and shipping them to the specified task_service (thread pool), this task can be executed in subsequent series.ArticleI will do a detailed analysis.
ChaosAndLibevent, boost ASIO, Ace, iceAnd other well-known database differences
When I started writing chaos, my original intention was probably not a general library like libevent and boost ASIO, but to help users quickly build a simple and easy-to-use TCP Service, the Network Module written based on the reactor core is also encapsulated for this purpose. if libevent or boost ASIO is used, you still need to care about how to accept a connection, create a startup thread, drive eventloop, and how to allocate threads and manage connections. If Ace is used, ice will appear bloated again. In another perspective, ice is a network service solution, rather than a pure network library, and chaos is between them, that is, to maintain a certain degree of lightweight development, and hope that users can be easily and quickly developed, of course, this will inevitably lose some flexibility, but I personally think This is harmless for most applications.
Performance
For some applications, although the network layer does not become the bottleneck of the entire service, the performance of the network library is still crucial. I personally think it is a good way to test the throughput on the local machine, in addition, you don't need to consider the restrictions of the hardware nic. in the same machine environment, I evaluated the buffer size, connections, and single thread/multithreading at different application layers.
The specific process is that the client starts n threads and starts n TCP connections to send data to the server, after the server receives the complete data packet, it immediately returns the same content to the peer (like the echo server above). After a period of time, it counts the throughput of the entire process. The following are the data I have collected:
Test Environment Information
Server Model: HP dl160
CPU: e5504
Mem:
OS: centos 5.8
Of course, we need to mention that this throughput test report is not comparable with the throughput tests of some other network libraries. After all, different hardware environments, different test codes bring about a bigger gap than we think.
The throughput test client can find the complete code in the test/throughput_client directory.
For the server code, see echo_server.
To be continued
Later, I will continue to share with you some series of articles based on my personal time, continue to discuss some of the problems encountered in the design of chaos, and there are still many problems in the library itself, I will continue to improve.
For more information, see:
Sina Weibo-http://weibo.com/crazyprogramerlyj
Chinaunix blog-http://blog.chinaunix.net/space.php? Uid = 1, 14617649