// File: /workspace/github_cpp_makefile_star2_O2/fltflt[P]Tinyhttpd_with_threadpool_epoll/httpd.cpp
// Function: main
int main(void)
{
	int server_sock = -1;
	u_short port = 3000;
	int connfd_fd = -1;
	struct sockaddr_in client_name;
	socklen_t client_name_len = sizeof(client_name);


 //创建线程池
	ThreadPool pool(4);
	printf("线程池创建成功！\n");
 //pthread_t newthread;
 //启动server socket
	server_sock = startup(&port);


 //创建epoll事件
	int epfd, nfds;
 //生成用于处理accept的epoll专用的文件描述符
	epfd=epoll_create(5);
	struct epoll_event ev,events[20];
	ev.data.fd = server_sock;
//设置要处理的事件类型
	ev.events=EPOLLIN|EPOLLET;
//注册epoll事件
	epoll_ctl(epfd,EPOLL_CTL_ADD,server_sock,&ev);

	printf("httpd running on port:%d\n", port);




	for ( ; ; ) 
	{
    //等待epoll事件的发生
		nfds = epoll_wait(epfd,events,20,500);
    //处理所发生的所有事件
		for (int i = 0; i < nfds; ++i)
		{
        if (events[i].data.fd == server_sock)//如果新监测到一个SOCKET用户连接到了绑定的SOCKET端口，建立新的连接。
        {
        	connfd_fd = accept(server_sock,(sockaddr *)&client_name, &client_name_len);
        	if (connfd_fd < 0)
        	{
        		perror("connfd_fd < 0");
        		exit(1);
        	}
        	char *str = inet_ntoa(client_name.sin_addr);
        	cout << "accapt a connection from " << str << endl;


        	ev.data.fd = connfd_fd;
            //设置用于注测的读操作事件
        	ev.events = EPOLLIN|EPOLLET;
            //注册ev
        	epoll_ctl(epfd,EPOLL_CTL_ADD,connfd_fd,&ev);
        }
        else if (events[i].events&EPOLLIN)//如果是已经连接的用户，并且收到数据，那么进行读入。
        {    
        	std::cout<<"start worker thread ID:"<<std::this_thread::get_id()<<std::endl;
        	pool.enqueue(accept_request,(void*)&connfd_fd);
        }
        }
    }
    close(server_sock);
    return(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
leaq -0x2c(%rbp), %rax
orl $-0x1, (%rax)
leaq -0x32(%rbp), %rbx
movw $0xbb8, (%rbx)          # imm = 0xBB8
leaq -0x30(%rbp), %rax
movl $0x10, (%rax)
leaq -0x130(%rbp), %rdi
pushq $0x4
popq %rsi
callq 0x4706
leaq 0x2ec9(%rip), %rdi      # 0x73fa
callq 0x3480
movq %rbx, %rdi
callq 0x4429
movl %eax, %r15d
pushq $0x5
popq %rdi
callq 0x3540
movl %eax, %r12d
leaq -0x40(%rbp), %rcx
movl %r15d, 0x4(%rcx)
movl $0x80000001, (%rcx)     # imm = 0x80000001
pushq $0x1
popq %rsi
movl %eax, %edi
movl %r15d, %edx
callq 0x3410
movzwl (%rbx), %esi
leaq 0x2d8e(%rip), %rdi      # 0x72ff
xorl %eax, %eax
callq 0x3030
movl %r12d, %edi
leaq -0x220(%rbp), %rsi
pushq $0x14
popq %rdx
movl $0x1f4, %ecx            # imm = 0x1F4
callq 0x3190
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
imulq $0xc, %rax, %r14
xorl %r13d, %r13d
cmpq %r13, %r14
je 0x4578
cmpl %r15d, -0x21c(%rbp,%r13)
jne 0x4619
movl %r15d, %edi
leaq -0x68(%rbp), %rsi
leaq -0x30(%rbp), %rdx
callq 0x30e0
movl %eax, -0x2c(%rbp)
testl %eax, %eax
js 0x4687
movl -0x64(%rbp), %edi
callq 0x3160
movq %rax, %rbx
movq 0x69e4(%rip), %rdi      # 0xafc0
leaq 0x2d44(%rip), %rsi      # 0x7327
callq 0x3330
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3330
movq %rax, %rdi
callq 0x31a0
movl -0x2c(%rbp), %edx
movl %edx, -0x3c(%rbp)
movl $0x80000001, -0x40(%rbp) # imm = 0x80000001
movl %r12d, %edi
pushq $0x1
popq %rsi
leaq -0x40(%rbp), %rcx
callq 0x3410
jmp 0x467e
testb $0x1, -0x220(%rbp,%r13)
je 0x467e
movq 0x6995(%rip), %rdi      # 0xafc0
leaq 0x2d0f(%rip), %rsi      # 0x7341
callq 0x3330
movq %rax, %rbx
callq 0x32b0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x47ac
movq %rax, %rdi
callq 0x31a0
leaq -0x2c(%rbp), %rax
movq %rax, -0x48(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x130(%rbp), %rsi
leaq -0xfb0(%rip), %rdx      # 0x36bc
leaq -0x48(%rbp), %rcx
callq 0x47c8
leaq -0x50(%rbp), %rdi
callq 0x53a8
addq $0xc, %r13
jmp 0x45a0
leaq 0x2c8b(%rip), %rdi      # 0x7319
callq 0x3280
pushq $0x1
popq %rdi
callq 0x33c0
jmp 0x46a1
jmp 0x46a1
jmp 0x46a1
movq %rax, %rbx
leaq -0x130(%rbp), %rdi
callq 0x48f0
movq %rbx, %rdi
callq 0x3520