// File: asdfz1346[P]MyServer/MyServer.cpp
// Function: MyServer::accept_thread_proc(void*)
void *MyServer::accept_thread_proc(void *args){
	MyServer* pthis = ((ARG*)args)->p;
	while(pthis->nstop){
		pthread_mutex_lock(&pthis->accept_mutex);
		pthread_cond_wait(&pthis->accept_cond,&pthis->accept_mutex);//block

		/*accept already,create newclientfd*/
		struct sockaddr_in clientaddr;
		socklen_t addrlen;
		int newfd = accept(pthis->listenfd,(struct sockaddr*)&clientaddr,&addrlen);
		pthread_mutex_unlock(&pthis->accept_mutex);
		if(newfd==-1) continue;
		/*set newfd sndbuf*/
		int nSndBufferLen = 4*1024*1024;
		int nLen          = sizeof(int);
		setsockopt(newfd,SOL_SOCKET,SO_SNDBUF,(char*)&nSndBufferLen,nLen);

		/*set newfd non-blocking;*/
		int oldflag = fcntl(newfd, F_GETFL, 0);
		int newflag = oldflag | O_NONBLOCK;
		if (fcntl(newfd, F_SETFL, newflag) == -1){
			std::cout << "fcntl error, oldflag = " << oldflag << ", newflag = " << newflag << std::endl;
			continue;
		}
		/*insert newfd to the set*/
	pthread_mutex_lock(&pthis->clientset_mutex);
		pthis->clientset.insert(newfd);
		pthread_mutex_unlock(&pthis->clientset_mutex);
		std::cout << "new client connect fd:"<<newfd<<std::endl;
		
		/*add epoll*/
		struct epoll_event e;
		memset (&e,0,sizeof e);
		e.events = EPOLLIN|EPOLLET;
		e.data.fd = newfd;
		if(epoll_ctl(pthis->epollfd,EPOLL_CTL_ADD,newfd,&e)==-1){
			std::cout << "epoll_ctl error,\n"<<std::endl;
		}

	}
	return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
testb $0x1, 0x118(%rax)
je 0x3c99
movq -0x10(%rbp), %rdi
addq $0x70, %rdi
callq 0x3430
movq -0x10(%rbp), %rdi
addq $0xe8, %rdi
movq -0x10(%rbp), %rsi
addq $0x70, %rsi
callq 0x3440
movq -0x10(%rbp), %rax
movl 0x18(%rax), %edi
leaq -0x20(%rbp), %rsi
leaq -0x24(%rbp), %rdx
callq 0x30d0
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rdi
addq $0x70, %rdi
callq 0x3240
cmpl $-0x1, -0x28(%rbp)
jne 0x3b29
jmp 0x3ac7
movl $0x400000, -0x2c(%rbp)  # imm = 0x400000
movl $0x4, -0x30(%rbp)
movl -0x28(%rbp), %edi
movl -0x30(%rbp), %r8d
movl $0x1, %esi
movl $0x7, %edx
leaq -0x2c(%rbp), %rcx
callq 0x34b0
movl -0x28(%rbp), %edi
movl $0x3, %esi
xorl %edx, %edx
movb $0x0, %al
callq 0x31b0
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
orl $0x800, %eax            # imm = 0x800
movl %eax, -0x38(%rbp)
movl -0x28(%rbp), %edi
movl -0x38(%rbp), %edx
movl $0x4, %esi
movb $0x0, %al
callq 0x31b0
cmpl $-0x1, %eax
jne 0x3bd3
movq 0x944a(%rip), %rdi      # 0xcfd8
leaq 0x54d9(%rip), %rsi      # 0x906e
callq 0x32c0
movq %rax, %rdi
movl -0x34(%rbp), %esi
callq 0x3480
movq %rax, %rdi
leaq 0x54d7(%rip), %rsi      # 0x9086
callq 0x32c0
movq %rax, %rdi
movl -0x38(%rbp), %esi
callq 0x3480
movq %rax, %rdi
movq 0x93ff(%rip), %rsi      # 0xcfc8
callq 0x3330
jmp 0x3ac7
movq -0x10(%rbp), %rdi
addq $0x98, %rdi
callq 0x3430
movq -0x10(%rbp), %rdi
addq $0x40, %rdi
leaq -0x28(%rbp), %rsi
callq 0x5fe0
movq %rax, -0x48(%rbp)
movb %dl, -0x40(%rbp)
movq -0x10(%rbp), %rdi
addq $0x98, %rdi
callq 0x3240
movq 0x93c6(%rip), %rdi      # 0xcfd8
leaq 0x547a(%rip), %rsi      # 0x9093
callq 0x32c0
movq %rax, %rdi
movl -0x28(%rbp), %esi
callq 0x3480
movq %rax, %rdi
movq 0x9395(%rip), %rsi      # 0xcfc8
callq 0x3330
leaq -0x54(%rbp), %rdi
xorl %esi, %esi
movl $0xc, %edx
callq 0x31a0
movl $0x80000001, -0x54(%rbp) # imm = 0x80000001
movl -0x28(%rbp), %eax
movl %eax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %edi
movl -0x28(%rbp), %edx
movl $0x1, %esi
leaq -0x54(%rbp), %rcx
callq 0x3410
cmpl $-0x1, %eax
jne 0x3c94
movq 0x935f(%rip), %rdi      # 0xcfd8
leaq 0x542a(%rip), %rsi      # 0x90aa
callq 0x32c0
movq %rax, %rdi
movq 0x9339(%rip), %rsi      # 0xcfc8
callq 0x3330
jmp 0x3ac7
xorl %eax, %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)