// File: /workspace/github_cpp_makefile_star2_O3/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 0x4c48
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 0x4bc4
decq %r15
jne 0x477c
leaq 0x2c60(%rip), %rdi      # 0x73fa
callq 0x3480
leaq -0x2e(%rbp), %rbx
movq %rbx, %rdi
callq 0x462d
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 0x2b1d(%rip), %rdi      # 0x72ff
xorl %eax, %eax
callq 0x3040
movq 0x67d0(%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 0x47f0
movl %eax, %eax
shlq $0x2, %rax
leaq (%rax,%rax,2), %rbx
xorl %r12d, %r12d
cmpl %r15d, -0x22c(%rbp,%r12)
jne 0x487b
movl %r15d, %edi
leaq -0x70(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0x30e0
movl %eax, -0x2c(%rbp)
testl %eax, %eax
js 0x499a
movl -0x6c(%rbp), %edi
callq 0x3160
movq %rax, %r14
movl $0x19, %edx
movq %r13, %rdi
leaq 0x2ace(%rip), %rsi      # 0x7327
callq 0x33b0
testq %r14, %r14
je 0x48b8
movq %r14, %rdi
callq 0x3150
movq %r13, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x33b0
jmp 0x48d1
testb $0x1, -0x230(%rbp,%r12)
je 0x4988
movl $0x17, %edx
movq %r13, %rdi
leaq 0x2aa8(%rip), %rsi      # 0x7341
callq 0x33b0
callq 0x32a0
testq %rax, %rax
je 0x4919
movq %r13, %rdi
movq %rax, %rsi
callq 0x3200
movq %rax, %r14
jmp 0x4930
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 0x4988
movl $0x24, %edx
movq %r13, %rdi
leaq 0x2a4b(%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 -0x1125(%rip), %rdx     # 0x384c
leaq -0x50(%rbp), %rcx
callq 0x4a52
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x4988
callq 0x54a0
addq $0xc, %r12
cmpq %r12, %rbx
jne 0x481a
jmp 0x47f0
leaq 0x2978(%rip), %rdi      # 0x7319
callq 0x3270
movl $0x1, %edi
callq 0x33d0
jmp 0x49b6
jmp 0x49b6
jmp 0x49b6
movq %rax, %rbx
leaq -0x230(%rbp), %rdi
leaq -0xd8(%rbp), %rax
movq %rax, (%rdi)
movb $0x0, 0x8(%rdi)
callq 0x5188
movb $0x1, %al
leaq -0x230(%rbp), %rdi
movb %al, 0x8(%rdi)
movb %al, -0x80(%rbp)
callq 0x516e
leaq -0xb0(%rbp), %rdi
callq 0x3500
movq -0x140(%rbp), %r14
movq -0x138(%rbp), %r15
cmpq %r15, %r14
je 0x4a21
movq %r14, %rdi
callq 0x3510
addq $0x8, %r14
jmp 0x4a01
jmp 0x4a16
movq %rax, %rdi
callq 0x4db5
movq %rax, %rbx
leaq -0xb0(%rbp), %rdi
callq 0x3540
leaq -0x128(%rbp), %rdi
callq 0x531a
jmp 0x4a3e
movq %rax, %rbx
leaq -0x140(%rbp), %rdi
callq 0x4c0a
movq %rbx, %rdi
callq 0x3530