06 Introduction to the characteristics of bufferevent buffer in communication under libevent

06 Introduction to the characteristics of bufferevent buffer in communication under libevent

以下是关于libevent学习的相关文章:
01 Download and installation of the libevent library and test whether the installation is successful
02 The overall framework idea of ​​the libevent library
03 The main function of
communication
under the
libevent Area features introduction
07libevent library related functions of the bufferevent event
08libevent library communication server and client main functions
09libevent library server and client TCP communication process and code examples

1 Introduction to the characteristics of the bufferevent buffer
We know that the socket network communication socket has two buffers, which are divided into read and write buffers. For example, there are four buffers in the server and client.
The bufferevent event object is also used for socket network communication, so each bufferevent object also has two buffers, that is, read and write buffers.

2 Picture explanation Explain the above picture
Insert picture description here
: when we listen to the read and write of the event, when the read event is triggered, the callback function of the read buffer is triggered, we only need to use bufferevent_read() to read in the callback function. However, you can no longer use read() because it does not support it when encapsulating;
facing the above, you may think that since the read event is triggered after the callback function is called to read, then the write event should be called after the write event is triggered. Just write; but is it really?
Think about it, who triggered the writing event? It's yourself, so the general write event is to use bufferevent_read() in the callback function of the read event to write the data into the buffer after reading. Others do not need to be processed by you. The system will send it automatically for you. You only need to write into the buffer. Yes, so many times some people use bufferevent_setcb() to set the write callback event of bufferevent, that is, parameter 3, and leave it blank, because it doesn't need you to handle it at all, you just need to write into the buffer somewhere.

So to summarize the above words:
Reading is triggered by a read event before calling the callback function to read in the callback;
while writing is to write data directly somewhere (usually to write after reading in the callback function); instead of writing the callback Write in the function. Of course, you can also write inside, or print some information after writing.

Guess you like

Origin blog.csdn.net/weixin_44517656/article/details/108762584