// File: /workspace/github_cpp_makefile_star2_O2/white0dew[P]WebServer/webserver.cpp
// Function: WebServer::eventListen()
void WebServer::eventListen()
{
    //SOCK_STREAM 表示使用面向字节流的TCP协议
    m_listenfd = socket(PF_INET, SOCK_STREAM, 0);
    assert(m_listenfd >= 0);

    //优雅关闭连接
    //有关setsocket函数可以参考：
    //https://baike.baidu.com/item/setsockopt/10069288?fr=aladdin
    if (0 == m_OPT_LINGER)
    {
        struct linger tmp = {0, 1};
        setsockopt(m_listenfd, SOL_SOCKET, SO_LINGER, &tmp, sizeof(tmp));
    }
    else if (1 == m_OPT_LINGER)
    {
        struct linger tmp = {1, 1};
        setsockopt(m_listenfd, SOL_SOCKET, SO_LINGER, &tmp, sizeof(tmp));
    }

    int ret = 0;
    struct sockaddr_in address;
    bzero(&address, sizeof(address));
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = htonl(INADDR_ANY);
    address.sin_port = htons(m_port);

    int flag = 1;
    setsockopt(m_listenfd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
    ret = bind(m_listenfd, (struct sockaddr *)&address, sizeof(address));
    assert(ret >= 0);
    //表示已连接和未连接的最大队列数总和为5
    ret = listen(m_listenfd, 5);
    assert(ret >= 0);

    //设置服务器的最小时间间隙
    utils.init(TIMESLOT);

    //epoll创建内核事件表
    epoll_event events[MAX_EVENT_NUMBER];
    m_epollfd = epoll_create(5);
    assert(m_epollfd != -1);

    utils.addfd(m_epollfd, m_listenfd, false, m_LISTENTrigmode);
    http_conn::m_epollfd = m_epollfd;

    //socketpair()函数用于创建一对无名的、相互连接的套接子。
    //详情：https://blog.csdn.net/weixin_40039738/article/details/81095013
    ret = socketpair(PF_UNIX, SOCK_STREAM, 0, m_pipefd);
    assert(ret != -1);
    utils.setnonblocking(m_pipefd[1]);
    utils.addfd(m_epollfd, m_pipefd[0], false, 0);

    utils.addsig(SIGPIPE, SIG_IGN);
    utils.addsig(SIGALRM, utils.sig_handler, false);
    utils.addsig(SIGTERM, utils.sig_handler, false);

    alarm(TIMESLOT);

    //工具类,信号和描述符基础操作
    Utils::u_pipefd = m_pipefd;
    Utils::u_epollfd = m_epollfd;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x20, %rsp
movq %rdi, %rbx
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x30b0
movl %eax, 0x1d56c(%rbx)
testl %eax, %eax
js 0x717a
movl 0x1d570(%rbx), %ecx
cmpl $0x1, %ecx
je 0x6ffb
testl %ecx, %ecx
jne 0x7021
movabsq $0x100000000, %rdx      # imm = 0x100000000
jmp 0x7008
movabsq $0x100000000, %rdx      # imm = 0x100000000
incq %rdx
leaq 0x10(%rsp), %rcx
movq %rdx, (%rcx)
pushq $0x1
popq %rsi
pushq $0xd
popq %rdx
pushq $0x8
popq %r8
movl %eax, %edi
callq 0x3660
leaq 0x10(%rsp), %r14
andq $0x0, 0x8(%r14)
movw $0x2, (%r14)
andl $0x0, 0x4(%r14)
movzwl (%rbx), %eax
rolw $0x8, %ax
movw %ax, 0x2(%r14)
leaq 0xc(%rsp), %rcx
movl $0x1, (%rcx)
movl 0x1d56c(%rbx), %edi
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
pushq $0x4
popq %r8
callq 0x3660
movl 0x1d56c(%rbx), %edi
pushq $0x10
popq %rdx
movq %r14, %rsi
callq 0x3080
testl %eax, %eax
js 0x7197
movl 0x1d56c(%rbx), %edi
pushq $0x5
popq %rsi
callq 0x3440
testl %eax, %eax
js 0x71b6
leaq 0x1d588(%rbx), %r14
pushq $0x5
popq %rbp
movq %r14, %rdi
movl %ebp, %esi
callq 0x3e5e
movl %ebp, %edi
callq 0x36a0
movl %eax, 0x24(%rbx)
cmpl $-0x1, %eax
je 0x71d5
movl 0x1d56c(%rbx), %edx
movl 0x1d578(%rbx), %r8d
movq %r14, %rdi
movl %eax, %esi
xorl %ecx, %ecx
callq 0x3e92
movl 0x24(%rbx), %eax
leaq 0x630d(%rip), %rcx      # 0xd3e8
movl %eax, (%rcx)
leaq 0x1c(%rbx), %r15
pushq $0x1
popq %rdi
movl %edi, %esi
xorl %edx, %edx
movq %r15, %rcx
callq 0x3360
cmpl $-0x1, %eax
je 0x71f4
movl 0x20(%rbx), %esi
movq %r14, %rdi
callq 0x3e62
movl 0x1c(%rbx), %edx
movl 0x24(%rbx), %esi
movq %r14, %rdi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x3e92
pushq $0xd
popq %rsi
pushq $0x1
popq %rdx
pushq $0x1
popq %rcx
movq %r14, %rdi
callq 0x3f12
leaq -0x3259(%rip), %r12     # 0x3ed6
pushq $0xe
popq %rsi
movq %r14, %rdi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x3f12
pushq $0xf
popq %rsi
movq %r14, %rdi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x3f12
pushq $0x5
popq %rdi
callq 0x36b0
leaq 0x62a2(%rip), %rax      # 0xd400
movq %r15, (%rax)
movl 0x24(%rbx), %eax
leaq 0x629d(%rip), %rcx      # 0xd408
movl %eax, (%rcx)
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1317(%rip), %rdi      # 0x8498
leaq 0x1320(%rip), %rsi      # 0x84a8
leaq 0x1327(%rip), %rcx      # 0x84b6
pushq $0x73
popq %rdx
callq 0x32b0
leaq 0x1336(%rip), %rdi      # 0x84d4
leaq 0x1303(%rip), %rsi      # 0x84a8
leaq 0x130a(%rip), %rcx      # 0x84b6
movl $0x8d, %edx
callq 0x32b0
leaq 0x1317(%rip), %rdi      # 0x84d4
leaq 0x12e4(%rip), %rsi      # 0x84a8
leaq 0x12eb(%rip), %rcx      # 0x84b6
movl $0x90, %edx
callq 0x32b0
leaq 0x1301(%rip), %rdi      # 0x84dd
leaq 0x12c5(%rip), %rsi      # 0x84a8
leaq 0x12cc(%rip), %rcx      # 0x84b6
movl $0x98, %edx
callq 0x32b0
leaq 0x12f2(%rip), %rdi      # 0x84ed
leaq 0x12a6(%rip), %rsi      # 0x84a8
leaq 0x12ad(%rip), %rcx      # 0x84b6
movl $0xa0, %edx
callq 0x32b0
nop