// File: /workspace/github_cpp_makefile_star2_O2/white0dew[P]WebServer/webserver.cpp
// Function: WebServer::eventLoop()
void WebServer::eventLoop()
{
    bool timeout = false;
    bool stop_server = false;

    while (!stop_server)
    {
        //等待所监控文件描述符上有事件的产生
        int number = epoll_wait(m_epollfd, events, MAX_EVENT_NUMBER, -1);
        //EINTR错误的产生：当阻塞于某个慢系统调用的一个进程捕获某个信号且相应信号处理函数返回时，该系统调用可能返回一个EINTR错误。
        //例如：在socket服务器端，设置了信号捕获机制，有子进程，
        //当在父进程阻塞于慢系统调用时由父进程捕获到了一个有效信号时，内核会致使accept返回一个EINTR错误(被中断的系统调用)。
        //在epoll_wait时，因为设置了alarm定时触发警告，导致每次返回-1，errno为EINTR，对于这种错误返回
        //忽略这种错误，让epoll报错误号为4时，再次做一次epoll_wait
        if (number < 0 && errno != EINTR)
        {
            LOG_ERROR("%s", "epoll failure");
            break;
        }
        //对所有就绪事件进行处理
        for (int i = 0; i < number; i++)
        {
            int sockfd = events[i].data.fd;
            //处理新到的客户连接
            if (sockfd == m_listenfd)
            {
                bool flag = dealclinetdata();
                if (false == flag)
                    continue;
            }
            //处理异常事件
            else if (events[i].events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR))
            {
                //服务器端关闭连接，移除对应的定时器
                util_timer *timer = users_timer[sockfd].timer;
                deal_timer(timer, sockfd);
            }
            //处理定时器信号
            else if ((sockfd == m_pipefd[0]) && (events[i].events & EPOLLIN))
            {
                //接收到SIGALRM信号，timeout设置为True
                bool flag = dealwithsignal(timeout, stop_server);
                if (false == flag)
                    LOG_ERROR("%s", "dealclientdata failure");
            }
            //处理客户连接上接收到的数据
            else if (events[i].events & EPOLLIN)
            {
                dealwithread(sockfd);
            }
            //处理客户连接上send的数据
            else if (events[i].events & EPOLLOUT)
            {
                dealwithwrite(sockfd);
            }
        }

        //处理定时器为非必须事件，收到信号并不是立马处理
        //完成读写事件后，再进行处理
        if (timeout)
        {
            utils.timer_handler();
            LOG_INFO("%s", "timer tick");
            timeout = false;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
xorl %eax, %eax
leaq 0xe(%rsp), %r14
movb %al, (%r14)
leaq 0xf(%rsp), %r15
movb %al, (%r15)
leaq 0xac(%rdi), %r12
leaq 0x1d588(%rdi), %rax
movq %rax, 0x10(%rsp)
xorl %eax, %eax
testb $0x1, %al
jne 0x79c9
movl 0x24(%rbx), %edi
movq %r12, %rsi
movl $0x2710, %edx           # imm = 0x2710
pushq $-0x1
popq %rcx
callq 0x31b0
movl %eax, %r13d
testl %eax, %eax
jns 0x789e
callq 0x3050
cmpl $0x4, (%rax)
jne 0x79c3
testl %r13d, %r13d
movl $0x0, %eax
cmovlel %eax, %r13d
imulq $0xc, %r13, %r13
xorl %ebp, %ebp
cmpq %rbp, %r13
je 0x7971
movslq 0xb0(%rbx,%rbp), %rdx
cmpl 0x1d56c(%rbx), %edx
jne 0x78d3
movq %rbx, %rdi
callq 0x7468
jmp 0x78fb
movl 0xac(%rbx,%rbp), %eax
testw $0x2018, %ax            # imm = 0x2018
je 0x7901
movq 0x1d580(%rbx), %rax
movq %rdx, %rcx
shlq $0x5, %rcx
movq 0x18(%rax,%rcx), %rsi
movq %rbx, %rdi
callq 0x73f6
addq $0xc, %rbp
jmp 0x78b0
testb $0x1, %al
je 0x7951
cmpl 0x1c(%rbx), %edx
jne 0x7951
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x75a6
testb %al, %al
jne 0x78fb
cmpl $0x0, 0x14(%rbx)
jne 0x78fb
callq 0x53c4
movq %rax, %rdi
pushq $0x3
popq %rsi
leaq 0x964(%rip), %rdx       # 0x8298
leaq 0xc4e(%rip), %rcx       # 0x8589
xorl %eax, %eax
callq 0x5e62
callq 0x53c4
movq %rax, %rdi
callq 0x6222
jmp 0x78fb
testb $0x1, %al
jne 0x7965
testb $0x4, %al
je 0x78fb
movq %rbx, %rdi
movl %edx, %esi
callq 0x772c
jmp 0x78fb
movq %rbx, %rdi
movl %edx, %esi
callq 0x7610
jmp 0x78fb
cmpb $0x0, 0xe(%rsp)
je 0x79ba
movq 0x10(%rsp), %rdi
callq 0x3f96
cmpl $0x0, 0x14(%rbx)
jne 0x79b5
callq 0x53c4
movq %rax, %rdi
pushq $0x1
popq %rsi
leaq 0x8fe(%rip), %rdx       # 0x8298
leaq 0xbff(%rip), %rcx       # 0x85a0
xorl %eax, %eax
callq 0x5e62
callq 0x53c4
movq %rax, %rdi
callq 0x6222
movb $0x0, 0xe(%rsp)
movb 0xf(%rsp), %al
jmp 0x786e
cmpl $0x0, 0x14(%rbx)
je 0x79d8
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x53c4
leaq 0x8b4(%rip), %rdx       # 0x8298
leaq 0xb90(%rip), %rcx       # 0x857b
pushq $0x3
popq %rsi
movq %rax, %rdi
xorl %eax, %eax
callq 0x5e62
callq 0x53c4
movq %rax, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6222
nop