// File: 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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
movb $0x0, -0x9(%rbp)
movb $0x0, -0xa(%rbp)
movb -0xa(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0xc50a
jmp 0xc785
movq -0x38(%rbp), %rsi
movl 0x24(%rsi), %edi
addq $0xac, %rsi
movl $0x2710, %edx           # imm = 0x2710
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x31f0
movl %eax, -0x10(%rbp)
cmpl $0x0, -0x10(%rbp)
jge 0xc579
callq 0x3060
cmpl $0x4, (%rax)
je 0xc579
movq -0x38(%rbp), %rcx
xorl %eax, %eax
cmpl 0x14(%rcx), %eax
jne 0xc574
callq 0x6df0
movq %rax, %rdi
movl $0x3, %esi
leaq 0x1d4f(%rip), %rdx      # 0xe2a8
leaq 0x2002(%rip), %rcx      # 0xe562
movb $0x0, %al
callq 0x9300
callq 0x6df0
movq %rax, %rdi
callq 0x9870
jmp 0xc785
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0xc72c
movq -0x38(%rbp), %rcx
movq %rcx, %rax
addq $0xac, %rax
movslq -0x14(%rbp), %rdx
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl 0x1d56c(%rcx), %eax
jne 0xc5db
movq -0x38(%rbp), %rdi
callq 0xbe10
andb $0x1, %al
movb %al, -0x19(%rbp)
movb -0x19(%rbp), %al
andb $0x1, %al
movzbl %al, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0xc5d6
jmp 0xc71e
jmp 0xc71c
movq -0x38(%rbp), %rax
addq $0xac, %rax
movslq -0x14(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x2018, %eax           # imm = 0x2018
cmpl $0x0, %eax
je 0xc62b
movq -0x38(%rbp), %rdi
movq 0x1d580(%rdi), %rax
movslq -0x18(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rsi
movl -0x18(%rbp), %edx
callq 0xbd60
jmp 0xc71a
movq -0x38(%rbp), %rcx
movl -0x18(%rbp), %eax
cmpl 0x1c(%rcx), %eax
jne 0xc6bc
movq -0x38(%rbp), %rax
addq $0xac, %rax
movslq -0x14(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xc6bc
movq -0x38(%rbp), %rdi
leaq -0x9(%rbp), %rsi
leaq -0xa(%rbp), %rdx
callq 0xc050
andb $0x1, %al
movb %al, -0x29(%rbp)
movb -0x29(%rbp), %al
andb $0x1, %al
movzbl %al, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0xc6ba
movq -0x38(%rbp), %rcx
xorl %eax, %eax
cmpl 0x14(%rcx), %eax
jne 0xc6b8
callq 0x6df0
movq %rax, %rdi
movl $0x3, %esi
leaq 0x1c0b(%rip), %rdx      # 0xe2a8
leaq 0x1ecc(%rip), %rcx      # 0xe570
movb $0x0, %al
callq 0x9300
callq 0x6df0
movq %rax, %rdi
callq 0x9870
jmp 0xc6ba
jmp 0xc718
movq -0x38(%rbp), %rax
addq $0xac, %rax
movslq -0x14(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xc6e9
movq -0x38(%rbp), %rdi
movl -0x18(%rbp), %esi
callq 0xc120
jmp 0xc716
movq -0x38(%rbp), %rax
addq $0xac, %rax
movslq -0x14(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0xc714
movq -0x38(%rbp), %rdi
movl -0x18(%rbp), %esi
callq 0xc310
jmp 0xc716
jmp 0xc718
jmp 0xc71a
jmp 0xc71c
jmp 0xc71e
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0xc580
testb $0x1, -0x9(%rbp)
je 0xc780
movq -0x38(%rbp), %rdi
addq $0x1d588, %rdi          # imm = 0x1D588
callq 0x4920
movq -0x38(%rbp), %rcx
xorl %eax, %eax
cmpl 0x14(%rcx), %eax
jne 0xc77c
callq 0x6df0
movq %rax, %rdi
movl $0x1, %esi
leaq 0x1b47(%rip), %rdx      # 0xe2a8
leaq 0x1e1f(%rip), %rcx      # 0xe587
movb $0x0, %al
callq 0x9300
callq 0x6df0
movq %rax, %rdi
callq 0x9870
movb $0x0, -0x9(%rbp)
jmp 0xc4fc
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)