[Herm programmer Development Guide] Chapter 2 Herm framework and network communication components

Source: Internet
Author: User
Tags epoll

Http://blog.csdn.net/herm_lib daemon. Huang # Gmail # com

 

2.1 Overview

This chapter briefly introduces a commonly used communication component structure starting from the structure of network communication components. Then, the implementation of the framework is described based on the structure of the communication component, and the object model, network, peer, and Session of the framework are described. Finally, we will analyze what measures are used to ensure the performance and where the performance can be improved. Finally, security measures are provided.

 

2.2 network communication components

A network application is generally divided into two layers: the logical layer and the network layer. The relationship is as follows:

+ --------------- +

| Logic layer |

------------------

| Network Layer |

+ --------------- +

 

The network communication component to be introduced in this section is the implementation of the network layer here.

 

A network communication component generally contains the following three layers:

1) receive and send a message packet)

Use the socket API to receive and send UDP datagram or TCP data streams from network interfaces.

 

2) message packet buffer

Most applications require a message packet buffer, which can balance the mismatch between sending and receiving and the processing speed of the logic layer.

 

3) Dispatch messages

The message of the network component is sent to a pre-processing before the application. When the application logic data goes down, necessary processing may also be performed at this layer.

 

2.3 Herm framework

 

Herm framework focuses on implementing a set of high-performance communication components on FreeBSD & Linux, implementing a common network layer function. The logic layer operates on the internal functions of the components through several C ++ interfaces.

The framework uses a multiplexing mechanism to send and receive data. epoll is used in Linux, kqueue is used in FreeBSD, and select is used in windows. From the implementation, we can be certain that only 63 TCP connections are supported on windows, which is not suitable for developing high-concurrency TCP servers. However, UDP server development is no problem. Framework will create a thread to send and receive data, so that the CPU can be fully utilized in multi-core machines.

 

The message buffer in the framework uses the ring buffer that writes non-lock as a read. For details, see utils/buffer. H & buffer. cpp.

 

Object Model

Herm framework uses an object-oriented method to abstract several parts of network components. Framework abstracts three objects: Network, peer, and session.

A network maintains a thread environment and controls one or more peer objects. Peer can be considered as an abstraction of a tcp client, TCP server, UDP client, or UDP server, manage one or more sessions. A session is a connection of TCP or the two ends of UDP data interaction.

 

2.4 performance and security considerations

Performance and simplicity are sometimes a conflict. To ensure performance, it may make code implementation more complicated. There are three aspects related to framework performance: the multiplexing mechanism for sending and receiving, the multi-thread locking for public buffer read/write, the number of data copies, and the memory management of objects.

 

Epoll in Linux has the lT & et mode. framework uses a compilation macro to determine whether to use it or not, so as to achieve the most efficient data sending and receiving. The read/write locks of the buffer. framework is designed to avoid locking and eliminate the performance loss of locking and unlocking.

The number of data copies is an important performance concern. Ideally, after the communication component delivers the message package of a session to the logic layer, it only performs one memory copy, it is a copy of the protocol stack buffer to the application layer space, that is, the copy of the call Socket API Recv. When writing this article, the Framework copies the memory twice, and the component copies the message package once when it passes it to the logic layer. This is a compromise between performance and implementation complexity and call security. Directly pass the message package address of the component to the logic layer. In actual use, there are some constraints. improper use may cause serious errors.

Finally, there is the problem of Object Memory Management. In a TCP server, there are sometimes several thousand sessions, and object management has a great impact on performance. The principle of Framework implementation is that system initialization is that all objects have been pre-created. During system operation, dynamic creation or destruction of objects is absolutely not allowed. But for a peer object, considering a TCP server peer, it is quite stable. Therefore, in the network object, STD: List is directly used for management, which is simple and direct, and the code is easy to maintain.

 

In terms of security, it is mainly about illegal message packet control. When a variety of network objects are in an invalid status, they strictly control the methods.

 

The next chapter describes how to use the framework to implement TCP Server & client/udp Server & client.

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.