// File: JianjiaoSQ[P]MyPoorWebServer/webserver.cpp
// Function: WebServer::eventListen()
void WebServer::eventListen() {
    //网络编程基础步骤
    m_listenfd = socket(PF_INET, SOCK_STREAM, 0);
    assert(m_listenfd >= 0);

    //优雅关闭连接
    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);
    ret = listen(m_listenfd, 5); // 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;

    // 创建管道套接字
    ret = socketpair(PF_UNIX, SOCK_STREAM, 0, m_pipefd);
    assert(ret != -1);
    utils.setnonblocking(m_pipefd[1]); // 写端非阻塞    
    //send是将信息发送给套接字缓冲区，如果缓冲区满了，则会阻塞，
    //这时候会进一步增加信号处理函数的执行时间，为此，将其修改为非阻塞。
    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);

    //每隔TIMESLOT时间触发SIGALRM信号
    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 0xb755
jmp 0xb774
leaq 0x2d23(%rip), %rdi      # 0xe47f
leaq 0x2d2c(%rip), %rsi      # 0xe48f
movl $0x65, %edx
leaq 0x2d2e(%rip), %rcx      # 0xe49d
callq 0x3300
movq -0x1d4f8(%rbp), %rcx
xorl %eax, %eax
cmpl 0x1d570(%rcx), %eax
jne 0xb7b8
movq -0x1d4f8(%rbp), %rax
movq 0x2e01(%rip), %rcx      # 0xe594
movq %rcx, -0x10(%rbp)
movl 0x1d56c(%rax), %edi
movl $0x1, %esi
movl $0xd, %edx
leaq -0x10(%rbp), %rcx
movl $0x8, %r8d
callq 0x3750
jmp 0xb7ff
movq -0x1d4f8(%rbp), %rcx
movl $0x1, %eax
cmpl 0x1d570(%rcx), %eax
jne 0xb7fd
movq -0x1d4f8(%rbp), %rax
movq 0x2dc2(%rip), %rcx      # 0xe59c
movq %rcx, -0x18(%rbp)
movl 0x1d56c(%rax), %edi
movl $0x1, %esi
movl $0xd, %edx
leaq -0x18(%rbp), %rcx
movl $0x8, %r8d
callq 0x3750
jmp 0xb7ff
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 0xb893
jmp 0xb8b2
leaq 0x2c21(%rip), %rdi      # 0xe4bb
leaq 0x2bee(%rip), %rsi      # 0xe48f
movl $0x7d, %edx
leaq 0x2bf0(%rip), %rcx      # 0xe49d
callq 0x3300
movq -0x1d4f8(%rbp), %rax
movl 0x1d56c(%rax), %edi
movl $0x5, %esi
callq 0x3510
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jl 0xb8d4
jmp 0xb8f3
leaq 0x2be0(%rip), %rdi      # 0xe4bb
leaq 0x2bad(%rip), %rsi      # 0xe48f
movl $0x7f, %edx
leaq 0x2baf(%rip), %rcx      # 0xe49d
callq 0x3300
movq -0x1d4f8(%rbp), %rdi
addq $0x1d588, %rdi          # imm = 0x1D588
movl $0x4, %esi
callq 0x4740
movl $0x5, %edi
callq 0x3790
movl %eax, %ecx
movq -0x1d4f8(%rbp), %rax
movl %ecx, 0x24(%rax)
cmpl $-0x1, 0x24(%rax)
je 0xb929
jmp 0xb948
leaq 0x2b94(%rip), %rdi      # 0xe4c4
leaq 0x2b58(%rip), %rsi      # 0xe48f
movl $0x87, %edx
leaq 0x2b5a(%rip), %rcx      # 0xe49d
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 0x47b0
movq -0x1d4f8(%rbp), %rcx
movl 0x24(%rcx), %edx
leaq 0x8adf(%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 0xb9a0
jmp 0xb9bf
leaq 0x2b2d(%rip), %rdi      # 0xe4d4
leaq 0x2ae1(%rip), %rsi      # 0xe48f
movl $0x8e, %edx
leaq 0x2ae3(%rip), %rcx      # 0xe49d
callq 0x3300
movq -0x1d4f8(%rbp), %rax
movq %rax, %rdi
addq $0x1d588, %rdi          # imm = 0x1D588
movl 0x20(%rax), %esi
callq 0x4760
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 0x47b0
movq -0x1d4f8(%rbp), %rdi
addq $0x1d588, %rdi          # imm = 0x1D588
movl $0xd, %esi
movl $0x1, %edx
movl $0x1, %ecx
callq 0x4880
movq -0x1d4f8(%rbp), %rdi
addq $0x1d588, %rdi          # imm = 0x1D588
movl $0xe, %esi
leaq -0x7206(%rip), %rdx     # 0x4830
xorl %ecx, %ecx
callq 0x4880
movq -0x1d4f8(%rbp), %rdi
addq $0x1d588, %rdi          # imm = 0x1D588
movl $0xf, %esi
leaq -0x7227(%rip), %rdx     # 0x4830
xorl %ecx, %ecx
callq 0x4880
movl $0x4, %edi
callq 0x37a0
movq -0x1d4f8(%rbp), %rax
movq %rax, %rdx
addq $0x1c, %rdx
leaq 0x89fb(%rip), %rcx      # 0x14478
movq %rdx, (%rcx)
movl 0x24(%rax), %ecx
leaq 0x89f6(%rip), %rax      # 0x14480
movl %ecx, (%rax)
addq $0x1d500, %rsp          # imm = 0x1D500
popq %rbp
retq
nopw %cs:(%rax,%rax)