// File: /workspace/github_cpp_makefile_star2_O1/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 $0x208, %rsp            # imm = 0x208
movw $0xbb8, -0x2e(%rbp)     # imm = 0xBB8
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movl $0x10, -0x38(%rbp)
leaq -0x128(%rbp), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, 0x38(%rdi)
movaps %xmm0, 0x28(%rdi)
movaps %xmm0, 0x18(%rdi)
movaps %xmm0, 0x8(%rdi)
movaps %xmm0, -0x8(%rdi)
movaps %xmm0, -0x18(%rdi)
movq $0x0, 0x48(%rdi)
xorl %esi, %esi
callq 0x4c44
leaq -0xd8(%rbp), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq $0x0, 0x20(%rax)
leaq -0xb0(%rbp), %rbx
movq %rbx, %rdi
callq 0x3430
movb $0x0, 0x30(%rbx)
movl $0x4, %r15d
leaq -0x140(%rbp), %rbx
leaq -0x230(%rbp), %r14
movq %rbx, -0x230(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4bc6
decq %r15
jne 0x477e
leaq 0x2c5e(%rip), %rdi      # 0x73fa
callq 0x3480
leaq -0x2e(%rbp), %rbx
movq %rbx, %rdi
callq 0x4631
movl %eax, %r15d
movl $0x5, %edi
callq 0x3550
leaq -0x44(%rbp), %rcx
movl %r15d, 0x4(%rcx)
movl $0x80000001, (%rcx)     # imm = 0x80000001
movl %eax, -0x34(%rbp)
movl %eax, %edi
movl $0x1, %esi
movl %r15d, %edx
callq 0x3410
movzwl (%rbx), %esi
leaq 0x2b1b(%rip), %rdi      # 0x72ff
xorl %eax, %eax
callq 0x3040
movq 0x67ce(%rip), %r13      # 0xafc0
movl -0x34(%rbp), %edi
leaq -0x230(%rbp), %rsi
movl $0x14, %edx
movl $0x1f4, %ecx            # imm = 0x1F4
callq 0x3180
testl %eax, %eax
jle 0x47f2
movl %eax, %eax
shlq $0x2, %rax
leaq (%rax,%rax,2), %rbx
xorl %r12d, %r12d
cmpl %r15d, -0x22c(%rbp,%r12)
jne 0x487d
movl %r15d, %edi
leaq -0x70(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0x30e0
movl %eax, -0x2c(%rbp)
testl %eax, %eax
js 0x499c
movl -0x6c(%rbp), %edi
callq 0x3160
movq %rax, %r14
movl $0x19, %edx
movq %r13, %rdi
leaq 0x2acc(%rip), %rsi      # 0x7327
callq 0x33b0
testq %r14, %r14
je 0x48ba
movq %r14, %rdi
callq 0x3150
movq %r13, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x33b0
jmp 0x48d3
testb $0x1, -0x230(%rbp,%r12)
je 0x498a
movl $0x17, %edx
movq %r13, %rdi
leaq 0x2aa6(%rip), %rsi      # 0x7341
callq 0x33b0
callq 0x32a0
testq %rax, %rax
je 0x491b
movq %r13, %rdi
movq %rax, %rsi
callq 0x3200
movq %rax, %r14
jmp 0x4932
movq (%r13), %rax
movq -0x18(%rax), %rax
leaq (%rax,%r13), %rdi
movl 0x20(%r13,%rax), %esi
orl $0x1, %esi
callq 0x34f0
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x3320
movsbl %al, %esi
movq %r13, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3210
movl -0x2c(%rbp), %edx
movl %edx, -0x40(%rbp)
movl $0x80000001, -0x44(%rbp) # imm = 0x80000001
movl -0x34(%rbp), %edi
movl $0x1, %esi
leaq -0x44(%rbp), %rcx
callq 0x3410
jmp 0x498a
movl $0x24, %edx
movq %r13, %rdi
leaq 0x2a49(%rip), %rsi      # 0x7373
callq 0x33b0
movq %r13, %r14
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3320
movsbl %al, %esi
movq %r14, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3210
leaq -0x2c(%rbp), %rax
movq %rax, -0x50(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x140(%rbp), %rsi
leaq -0x1127(%rip), %rdx     # 0x384c
leaq -0x50(%rbp), %rcx
callq 0x4a54
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x498a
callq 0x54b0
addq $0xc, %r12
cmpq %r12, %rbx
jne 0x481c
jmp 0x47f2
leaq 0x2976(%rip), %rdi      # 0x7319
callq 0x3270
movl $0x1, %edi
callq 0x33d0
jmp 0x49b8
jmp 0x49b8
jmp 0x49b8
movq %rax, %rbx
leaq -0x230(%rbp), %rdi
leaq -0xd8(%rbp), %rax
movq %rax, (%rdi)
movb $0x0, 0x8(%rdi)
callq 0x51b0
movb $0x1, %al
leaq -0x230(%rbp), %rdi
movb %al, 0x8(%rdi)
movb %al, -0x80(%rbp)
callq 0x5196
leaq -0xb0(%rbp), %rdi
callq 0x3500
movq -0x140(%rbp), %r14
movq -0x138(%rbp), %r15
cmpq %r15, %r14
je 0x4a23
movq %r14, %rdi
callq 0x3510
addq $0x8, %r14
jmp 0x4a03
jmp 0x4a18
movq %rax, %rdi
callq 0x4db1
movq %rax, %rbx
leaq -0xb0(%rbp), %rdi
callq 0x3540
leaq -0x128(%rbp), %rdi
callq 0x533c
jmp 0x4a40
movq %rax, %rbx
leaq -0x140(%rbp), %rdi
callq 0x4c06
movq %rbx, %rdi
callq 0x3530