Introduction
Redis encapsulation of Linux socket, although it also contains encapsulation of UNIX socket, but generally uses TCP socket. Similar to epoll encapsulation in the previous article, epoll uses the underlying socket function to complete convenient function call encapsulation for some columns.
Source code
Anet. h Anet. c
Analysis
It mainly includes the following encapsulation functions (here only describes the encapsulation functions of TCP socket)
Anettcpconnect: Creates a socket and calls the underlying connect for connection.
Anettcpnonblockconnect: similar to anettcpconnect, but the socket set for connection is not blocked.
Anetread: Call the underlying read to read the content in the socket.
Anetwrite: calls the underlying write to write content to the socket.
Anettcpserver: Creates a listening socket and calls bind and listen to start the server and start listening on the port.
Anettcpaccept: Call accept to receive client connections.
Of course, there are other auxiliary functions, such as anetresolve resolution addresses and some functions that set socket attributes, such as anetnonblock anettcpnodelay anettcpkeepalive anetpeertostring.
Description
The main purpose of the socket encapsulation here is to facilitate redis network call. The functions here are called by the redis network communication implementation to be introduced in the next article.