// File: 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
subq $0x270, %rsp            # imm = 0x270
movl $0x0, -0x4(%rbp)
movl $0xffffffff, -0x8(%rbp) # imm = 0xFFFFFFFF
movw $0xbb8, -0xa(%rbp)      # imm = 0xBB8
movl $0xffffffff, -0x10(%rbp) # imm = 0xFFFFFFFF
movl $0x10, -0x24(%rbp)
leaq -0xf0(%rbp), %rdi
movl $0x4, %esi
callq 0x5080
leaq 0xa6d6(%rip), %rdi      # 0xf35b
xorl %eax, %eax
callq 0x3030
jmp 0x4c8e
leaq -0xa(%rbp), %rdi
callq 0x4b40
movl %eax, -0x23c(%rbp)
jmp 0x4c9f
movl -0x23c(%rbp), %eax
movl %eax, -0x8(%rbp)
movl $0x5, %edi
callq 0x3590
movl %eax, -0x100(%rbp)
movl -0x8(%rbp), %eax
movl %eax, -0x10c(%rbp)
movl $0x80000001, -0x110(%rbp) # imm = 0x80000001
movl -0x100(%rbp), %edi
movl -0x8(%rbp), %edx
movl $0x1, %esi
leaq -0x110(%rbp), %rcx
callq 0x3450
movzwl -0xa(%rbp), %esi
leaq 0xa685(%rip), %rdi      # 0xf375
xorl %eax, %eax
callq 0x3030
jmp 0x4cf9
jmp 0x4cfb
movl -0x100(%rbp), %edi
leaq -0x200(%rbp), %rsi
movl $0x14, %edx
movl $0x1f4, %ecx            # imm = 0x1F4
callq 0x31a0
movl %eax, -0x240(%rbp)
jmp 0x4d1f
movl -0x240(%rbp), %eax
movl %eax, -0x104(%rbp)
movl $0x0, -0x204(%rbp)
movl -0x204(%rbp), %eax
cmpl -0x104(%rbp), %eax
jge 0x4f32
movslq -0x204(%rbp), %rcx
leaq -0x200(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
cmpl -0x8(%rbp), %eax
jne 0x4e64
movl -0x8(%rbp), %edi
leaq -0x20(%rbp), %rsi
leaq -0x24(%rbp), %rdx
callq 0x3100
movl %eax, -0x244(%rbp)
jmp 0x4d80
movl -0x244(%rbp), %eax
movl %eax, -0x10(%rbp)
cmpl $0x0, -0x10(%rbp)
jge 0x4dca
leaq 0xa5f9(%rip), %rdi      # 0xf38f
callq 0x32b0
jmp 0x4d9d
movl $0x1, %edi
callq 0x3410
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf8(%rbp)
movl %eax, -0xfc(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x5430
jmp 0x4f37
movl -0x1c(%rbp), %eax
movl %eax, -0x214(%rbp)
movl -0x214(%rbp), %edi
callq 0x3180
movq %rax, -0x210(%rbp)
movq 0x131d4(%rip), %rdi     # 0x17fc0
leaq 0xa5aa(%rip), %rsi      # 0xf39d
callq 0x3370
movq %rax, -0x250(%rbp)
jmp 0x4e01
movq -0x250(%rbp), %rdi
movq -0x210(%rbp), %rsi
callq 0x3370
movq %rax, -0x258(%rbp)
jmp 0x4e1d
movq -0x258(%rbp), %rdi
movq 0x13155(%rip), %rsi     # 0x17f80
callq 0x33b0
jmp 0x4e32
movl -0x10(%rbp), %eax
movl %eax, -0x10c(%rbp)
movl $0x80000001, -0x110(%rbp) # imm = 0x80000001
movl -0x100(%rbp), %edi
movl -0x10(%rbp), %edx
movl $0x1, %esi
leaq -0x110(%rbp), %rcx
callq 0x3450
jmp 0x4f1c
movslq -0x204(%rbp), %rcx
leaq -0x200(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x4f1a
movq 0x13132(%rip), %rdi     # 0x17fc0
leaq 0xa522(%rip), %rsi      # 0xf3b7
callq 0x3370
movq %rax, -0x260(%rbp)
jmp 0x4ea3
callq 0x51d0
movq -0x260(%rbp), %rdi
movq %rax, -0x220(%rbp)
movq -0x220(%rbp), %rsi
callq 0x5160
movq %rax, -0x268(%rbp)
jmp 0x4ecb
movq -0x268(%rbp), %rdi
movq 0x130a7(%rip), %rsi     # 0x17f80
callq 0x33b0
jmp 0x4ee0
leaq -0x10(%rbp), %rax
movq %rax, -0x238(%rbp)
leaq -0x17e2(%rip), %rdx     # 0x3710
leaq -0x230(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
leaq -0x238(%rbp), %rcx
callq 0x5200
jmp 0x4f0e
leaq -0x230(%rbp), %rdi
callq 0x5410
jmp 0x4f1c
jmp 0x4f1e
movl -0x204(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x204(%rbp)
jmp 0x4d35
jmp 0x4cfb
movq -0xf8(%rbp), %rdi
callq 0x3550
nopw %cs:(%rax,%rax)