In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you the practice of how to achieve a simple network buffer on C++. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
1. Preface
Please think about the following questions:
1)。 Why do you need to design a network buffer? isn't there a read-write buffer in the kernel?
The relationship between the network buffer that needs to be designed and the TCP buffer in the kernel is shown in the following figure, which is bound through socket. Specifically, the network buffer includes a read (receive) buffer and a write (send) buffer. The purpose of the read buffer is: when reading data from the TCP, it is not sure that the read is a complete data packet. If it is an incomplete data packet, it needs to be cached in the buffer first, and the business processing will not be carried out until the data packet is complete. The purpose of designing a write buffer is that writing data to TCP cannot guarantee that all data is written successfully. If the TCP write buffer is full, the packet will be discarded, so a write buffer is needed to temporarily store the data that needs to be written.
2)。 Should the buffer be set to heap memory or stack memory?
Suppose you have a server program that needs to connect multiple clients at the same time, and each socket is a connection object, so different socket needs its own read-write buffer. If the buffer is set to stack memory, it is easy to explode, so it is more reasonable to set it to heap memory. In addition, the upper limit of buffer capacity is generally limited and does not need to be allocated too large at first, but only when the buffer is insufficient.
3)。 What are the basic requirements of a read-write buffer?
From the above analysis, it is not difficult to draw a conclusion that although the read-write buffer is two independent buffers, its core function is the same and its code can be reused.
Read-write buffers provide at least two types of interfaces: storing data and reading data
Read-write buffer requirements: first-in, first-out, to ensure that the stored data is orderly
4)。 How to define the packet?
The first uses special characters to define the packet: for example,\ n,\ r\ n, the second defines the packet by its length, in which the length of the entire packet is first stored and then read according to the length.
5)。 Several common buffer designs
① ringbuffer+ read-write pointer
Ringbuffer is a continuous piece of memory, and when the end has been written, the data will continue to be written from the head, so it feels like a ring, but it is actually a circular array. The disadvantages of ringbuffer are also obvious: it cannot be extended, and an additional IO call is required for the data at the beginning and the end.
② extensible read-write buffer + read-write pointer
The following figure designs a scalable read-write buffer, which allocates a fixed size of memory when created, and the whole structure is divided into reserved data space. Reserved space is used to store the necessary information, and the real space to store data consists of continuous memory. This buffer design can be expanded compared to ringbuffer, but it also has some disadvantages: due to the need to maximize the use of space, the data will be moved to the beginning, and the moving operation will slow down the read and write speed.
This paper implements an extensible read-write buffer + read-write pointer.
two。 Data structure
Design and initialization of ① Buffer Class
The data structure of the Buffer class is shown below, where MCDS is the pointer to the buffer, and m_max_size is the length of the buffer, which is initially set to 10 and multiplied according to the expansion factor m_expand_par. The expansion factor m_expand_par is set to 2, which means that the length of each amplification is doubled, that is, the length of the buffer is 10, 20, 40, 80 with the expansion.
Class Buffer {public: Buffer (); / / construct ~ Buffer (); int init (); / / allocate buffer private: char* masks / / buffer pointer size_t mreadreadindex; / / read pointer position size_t m_write_index / / write pointer position size_t masks maxSize; / / buffer length size_t masks expandable pars; / / expansion factor}
Initialize member variables in the initialization list of the constructor. The actual initialization buffer allocates memory in the init function with a size of m_max_size. The reason for not initializing the buffer in the constructor is that if the allocation fails in the constructor, it cannot be processed, or it can be handled by using RAII means.
Buffer::Buffer (): m_read_index (0), m_write_index (0), m_max_size (10), m_expand_par (2), Buffer::~Buffer (nullptr) {} Buffer::~Buffer () {delete [] m _ chars;} int Buffer::init () {int Buffer::init = new chars [m _ max_size] (); if (masks = = nullptr) {cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.