Redis源码系列27:网络框架 serverSocket

网络框架部分的代码:

signal(SIGHUP, SIG_IGN); 
signal(SIGPIPE, SIG_IGN); 

typedef struct aeEventLoop 
{
    long long timeEventNextId;
    aeFileEvent *fileEventHead; 
    aeTimeEvent *timeEventHead;
    int stop;
} aeEventLoop;  

socket(AF_INET, SOCK_STREAM, 0)) == -1
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1 ( on=1) 

struct sockaddr_in sa;

sa.sin_family = AF_INET;
sa.sin_port = htons(port);
sa.sin_addr.s_addr = htonl(INADDR_ANY);

bind(s, (struct sockaddr*)&sa, sizeof(sa)) == -1

listen(s, 5) == -1 

fd_set rfds;
fd_set wfds;
fd_set efds;

FD_ZERO(&rfds);
FD_ZERO(&wfds);
FD_ZERO(&efds);

//然后根据具体的句柄的检测要求来执行不同的操作
if (fe->mask & AE_READABLE) FD_SET(fe->fd, &rfds);
if (fe->mask & AE_WRITABLE) FD_SET(fe->fd, &wfds);
if (fe->mask & AE_EXCEPTION) FD_SET(fe->fd, &efds);

if (maxfd < fe->fd) maxfd = fe->fd; 更新具体的最大fd

retval = select(maxfd+1, &rfds, &wfds, &efds, tvp);
if (retval > 0)
//如果发生了至少一种事件

if ((fe->mask & AE_READABLE && FD_ISSET(fd, &rfds)) ||
(fe->mask & AE_WRITABLE && FD_ISSET(fd, &wfds)) ||
(fe->mask & AE_EXCEPTION && FD_ISSET(fd, &efds)))

if ((fe->mask & AE_READABLE && FD_ISSET(fd, &rfds)) ||
                    (fe->mask & AE_WRITABLE && FD_ISSET(fd, &wfds)) ||
                    (fe->mask & AE_EXCEPTION && FD_ISSET(fd, &efds)))
                {
                    int mask = 0;

                    if (fe->mask & AE_READABLE && FD_ISSET(fd, &rfds))
                        mask |= AE_READABLE;
                    if (fe->mask & AE_WRITABLE && FD_ISSET(fd, &wfds))
                        mask |= AE_WRITABLE;
                    if (fe->mask & AE_EXCEPTION && FD_ISSET(fd, &efds))
                        mask |= AE_EXCEPTION;

 

你可能感兴趣的:(redis)