// File: 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
movq %rsp, %rbp
subq $0x1d500, %rsp          # imm = 0x1D500
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x1d4f8(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x30e0
movl %eax, %ecx
movq -0x1d4f8(%rbp), %rax
movl %ecx, 0x1d56c(%rax)
cmpl $0x0, 0x1d56c(%rax)
jl 0xb6d5
jmp 0xb6f4
leaq 0x2d9b(%rip), %rdi      # 0xe477
leaq 0x2da4(%rip), %rsi      # 0xe487
movl $0x73, %edx
leaq 0x2da6(%rip), %rcx      # 0xe495
callq 0x3300
movq -0x1d4f8(%rbp), %rcx
xorl %eax, %eax
cmpl 0x1d570(%rcx), %eax
jne 0xb738
movq -0x1d4f8(%rbp), %rax
movq 0x2e79(%rip), %rcx      # 0xe58c
movq %rcx, -0x10(%rbp)
movl 0x1d56c(%rax), %edi
movl $0x1, %esi
movl $0xd, %edx
leaq -0x10(%rbp), %rcx
movl $0x8, %r8d
callq 0x3750
jmp 0xb77f
movq -0x1d4f8(%rbp), %rcx
movl $0x1, %eax
cmpl 0x1d570(%rcx), %eax
jne 0xb77d
movq -0x1d4f8(%rbp), %rax
movq 0x2e3a(%rip), %rcx      # 0xe594
movq %rcx, -0x18(%rbp)
movl 0x1d56c(%rax), %edi
movl $0x1, %esi
movl $0xd, %edx
leaq -0x18(%rbp), %rcx
movl $0x8, %r8d
callq 0x3750
jmp 0xb77f
movl $0x0, -0x1c(%rbp)
leaq -0x2c(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x3270
movw $0x2, -0x2c(%rbp)
xorl %edi, %edi
callq 0x3390
movl %eax, %ecx
movq -0x1d4f8(%rbp), %rax
movl %ecx, -0x28(%rbp)
movl (%rax), %eax
movzwl %ax, %edi
callq 0x3080
movw %ax, %cx
movq -0x1d4f8(%rbp), %rax
movw %cx, -0x2a(%rbp)
movl $0x1, -0x30(%rbp)
movl 0x1d56c(%rax), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0x30(%rbp), %rcx
movl $0x4, %r8d
callq 0x3750
movq -0x1d4f8(%rbp), %rax
movl 0x1d56c(%rax), %edi
leaq -0x2c(%rbp), %rsi
movl $0x10, %edx
callq 0x30a0
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jl 0xb813
jmp 0xb832
leaq 0x2c99(%rip), %rdi      # 0xe4b3
leaq 0x2c66(%rip), %rsi      # 0xe487
movl $0x8d, %edx
leaq 0x2c68(%rip), %rcx      # 0xe495
callq 0x3300
movq -0x1d4f8(%rbp), %rax
movl 0x1d56c(%rax), %edi
movl $0x5, %esi
callq 0x3510
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jl 0xb854
jmp 0xb873
leaq 0x2c58(%rip), %rdi      # 0xe4b3
leaq 0x2c25(%rip), %rsi      # 0xe487
movl $0x90, %edx
leaq 0x2c27(%rip), %rcx      # 0xe495
callq 0x3300
movq -0x1d4f8(%rbp), %rdi
addq $0x1d588, %rdi          # imm = 0x1D588
movl $0x5, %esi
callq 0x46c0
movl $0x5, %edi
callq 0x3790
movl %eax, %ecx
movq -0x1d4f8(%rbp), %rax
movl %ecx, 0x24(%rax)
cmpl $-0x1, 0x24(%rax)
je 0xb8a9
jmp 0xb8c8
leaq 0x2c0c(%rip), %rdi      # 0xe4bc
leaq 0x2bd0(%rip), %rsi      # 0xe487
movl $0x98, %edx
leaq 0x2bd2(%rip), %rcx      # 0xe495
callq 0x3300
movq -0x1d4f8(%rbp), %rax
movq %rax, %rdi
addq $0x1d588, %rdi          # imm = 0x1D588
movl 0x24(%rax), %esi
movl 0x1d56c(%rax), %edx
movl 0x1d578(%rax), %r8d
xorl %ecx, %ecx
callq 0x4730
movq -0x1d4f8(%rbp), %rcx
movl 0x24(%rcx), %edx
leaq 0x8b5f(%rip), %rax      # 0x14460
movl %edx, (%rax)
addq $0x1c, %rcx
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x33f0
movl %eax, -0x1c(%rbp)
cmpl $-0x1, -0x1c(%rbp)
je 0xb920
jmp 0xb93f
leaq 0x2ba5(%rip), %rdi      # 0xe4cc
leaq 0x2b59(%rip), %rsi      # 0xe487
movl $0xa0, %edx
leaq 0x2b5b(%rip), %rcx      # 0xe495
callq 0x3300
movq -0x1d4f8(%rbp), %rax
movq %rax, %rdi
addq $0x1d588, %rdi          # imm = 0x1D588
movl 0x20(%rax), %esi
callq 0x46e0
movq -0x1d4f8(%rbp), %rax
movq %rax, %rdi
addq $0x1d588, %rdi          # imm = 0x1D588
movl 0x24(%rax), %esi
movl 0x1c(%rax), %edx
xorl %r8d, %r8d
movl %r8d, %ecx
callq 0x4730
movq -0x1d4f8(%rbp), %rdi
addq $0x1d588, %rdi          # imm = 0x1D588
movl $0xd, %esi
movl $0x1, %edx
movl $0x1, %ecx
callq 0x4800
movq -0x1d4f8(%rbp), %rdi
addq $0x1d588, %rdi          # imm = 0x1D588
movl $0xe, %esi
leaq -0x7206(%rip), %rdx     # 0x47b0
xorl %ecx, %ecx
callq 0x4800
movq -0x1d4f8(%rbp), %rdi
addq $0x1d588, %rdi          # imm = 0x1D588
movl $0xf, %esi
leaq -0x7227(%rip), %rdx     # 0x47b0
xorl %ecx, %ecx
callq 0x4800
movl $0x5, %edi
callq 0x37a0
movq -0x1d4f8(%rbp), %rax
movq %rax, %rdx
addq $0x1c, %rdx
leaq 0x8a7b(%rip), %rcx      # 0x14478
movq %rdx, (%rcx)
movl 0x24(%rax), %ecx
leaq 0x8a76(%rip), %rax      # 0x14480
movl %ecx, (%rax)
addq $0x1d500, %rsp          # imm = 0x1D500
popq %rbp
retq
nopw %cs:(%rax,%rax)