// File: /workspace/github_cpp_makefile_star2_O3/JianjiaoSQ[P]MyPoorWebServer/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);
        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))
            {
                //服务器端关闭连接，移除对应的定时器
                tw_timer *timer = users_timer[sockfd].timer;
                deal_timer(timer, sockfd);
            }
            //处理信号
            else if ((sockfd == m_pipefd[0]) && (events[i].events & EPOLLIN))
            {
                bool flag = dealwithsignal(timeout, stop_server);
                if (false == flag)
                    LOG_ERROR("%s", "dealclientdata failure");
            }
            //处理客户连接上接收到的数据
            else if (events[i].events & EPOLLIN)
            {
                dealwithread(sockfd);
            }
            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 %r13d, %r13d
leaq 0xe(%rsp), %r14
movb %r13b, (%r14)
leaq 0xf(%rsp), %r15
movb %r13b, (%r15)
leaq 0xac(%rdi), %r12
leaq 0x1d588(%rdi), %rax
movq %rax, 0x10(%rsp)
movl 0x24(%rbx), %edi
movq %r12, %rsi
movl $0x2710, %edx           # imm = 0x2710
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x3190
testl %eax, %eax
js 0x814f
je 0x8159
movl %eax, %eax
shlq $0x2, %rax
leaq (%rax,%rax,2), %rbp
xorl %r13d, %r13d
movslq 0xb0(%rbx,%r13), %rdx
cmpl 0x1d56c(%rbx), %edx
jne 0x80a1
movq %rbx, %rdi
callq 0x7c38
jmp 0x80cb
movl 0xac(%rbx,%r13), %eax
testl $0x2018, %eax           # imm = 0x2018
je 0x80d6
movq 0x1d580(%rbx), %rax
movq %rdx, %rcx
shlq $0x5, %rcx
movq 0x18(%rax,%rcx), %rsi
movq %rbx, %rdi
callq 0x7bc4
addq $0xc, %r13
cmpq %r13, %rbp
jne 0x8087
jmp 0x8148
testb $0x1, %al
je 0x8128
cmpl 0x1c(%rbx), %edx
jne 0x8128
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x7d80
testb %al, %al
jne 0x80cb
cmpl $0x0, 0x14(%rbx)
jne 0x80cb
callq 0x58ee
movq %rax, %rdi
movl $0x3, %esi
leaq 0x1195(%rip), %rdx      # 0x92a0
leaq 0x1477(%rip), %rcx      # 0x9589
xorl %eax, %eax
callq 0x6444
callq 0x58ee
movq %rax, %rdi
callq 0x6886
jmp 0x80cb
testb $0x1, %al
jne 0x813c
testb $0x4, %al
je 0x80cb
movq %rbx, %rdi
movl %edx, %esi
callq 0x7f0c
jmp 0x80cb
movq %rbx, %rdi
movl %edx, %esi
callq 0x7de8
jmp 0x80cb
movb 0xe(%rsp), %r13b
jmp 0x8159
callq 0x3050
cmpl $0x4, (%rax)
jne 0x81c0
testb $0x1, %r13b
je 0x81a6
movq 0x10(%rsp), %rdi
callq 0x41e2
cmpl $0x0, 0x14(%rbx)
jne 0x819e
callq 0x58ee
movq %rax, %rdi
movl $0x1, %esi
leaq 0x111d(%rip), %rdx      # 0x92a0
leaq 0x1416(%rip), %rcx      # 0x95a0
xorl %eax, %eax
callq 0x6444
callq 0x58ee
movq %rax, %rdi
callq 0x6886
movb $0x0, 0xe(%rsp)
xorl %r13d, %r13d
cmpb $0x1, 0xf(%rsp)
jne 0x8057
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x14(%rbx)
jne 0x81b1
callq 0x58ee
leaq 0x10ce(%rip), %rdx      # 0x92a0
leaq 0x13a2(%rip), %rcx      # 0x957b
movq %rax, %rdi
movl $0x3, %esi
xorl %eax, %eax
callq 0x6444
callq 0x58ee
movq %rax, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6886
nop