// File: guoxuanhan[P]NetServer/tcpserver.cpp
// Function: TcpServer::OnNewConnection()
void TcpServer::OnNewConnection()
{
    //循环调用accept，获取所有的建立好连接的客户端fd
    struct sockaddr_in clientaddr;
    int clientfd;
    while( (clientfd = serversocket_.Accept(clientaddr)) > 0)
    {
        std::cout << "New client from IP:" << inet_ntoa(clientaddr.sin_addr)
            << ":" << ntohs(clientaddr.sin_port) << std::endl;

        if(++conncount_ >= MAXCONNECTION)
        {
            close(clientfd);
            continue;
        }
        Setnonblocking(clientfd);

        //选择IO线程loop
        EventLoop *loop = eventloopthreadpool.GetNextLoop();

        //创建连接，注册业务函数
        std::shared_ptr<TcpConnection> sptcpconnection = std::make_shared<TcpConnection>(loop, clientfd, clientaddr);
        sptcpconnection->SetMessaeCallback(messagecallback_);
        sptcpconnection->SetSendCompleteCallback(sendcompletecallback_);
        sptcpconnection->SetCloseCallback(closecallback_);
        sptcpconnection->SetErrorCallback(errorcallback_);
        sptcpconnection->SetConnectionCleanUp(std::bind(&TcpServer::RemoveConnection, this, std::placeholders::_1));
        {
            std::lock_guard<std::mutex> lock(mutex_);
            tcpconnlist_[clientfd] = sptcpconnection;
        }


        newconnectioncallback_(sptcpconnection);
        //Bug，应该把事件添加的操作放到最后,否则bug segement fault,导致HandleMessage中的phttpsession==NULL
        //总之就是做好一切准备工作再添加事件到epoll！！！
        sptcpconnection->AddChannelToLoop();
    }
}

// Assembly:
subq $0xd8, %rsp
movq %rdi, 0xd0(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x1bca0
movl %eax, 0xbc(%rsp)
cmpl $0x0, %eax
jle 0x1f856
movq 0x1ba4b(%rip), %rdi     # 0x3afb0
leaq 0x9e5c(%rip), %rsi      # 0x293c8
callq 0x53a0
movq %rax, 0x20(%rsp)
movl 0xc4(%rsp), %eax
movl %eax, 0xb8(%rsp)
movl 0xb8(%rsp), %edi
callq 0x51c0
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x53a0
movq %rax, %rdi
leaq 0x9e33(%rip), %rsi      # 0x293da
callq 0x53a0
movq %rax, 0x28(%rsp)
movzwl 0xc2(%rsp), %edi
callq 0x56a0
movq 0x28(%rsp), %rdi
movzwl %ax, %esi
callq 0x5600
movq %rax, %rdi
movq 0x1b9ab(%rip), %rsi     # 0x3af80
callq 0x53f0
movq 0x30(%rsp), %rcx
movl 0x98(%rcx), %eax
addl $0x1, %eax
movl %eax, 0x98(%rcx)
cmpl $0x4e20, %eax           # imm = 0x4E20
jl 0x1f606
movl 0xbc(%rsp), %edi
callq 0x56e0
jmp 0x1f53c
movl 0xbc(%rsp), %edi
callq 0x1f9c0
movq 0x30(%rsp), %rdi
addq $0xf8, %rdi
callq 0xb440
movq %rax, 0xb0(%rsp)
leaq 0xa0(%rsp), %rdi
movq %rdi, 0x18(%rsp)
leaq 0xb0(%rsp), %rsi
leaq 0xbc(%rsp), %rdx
leaq 0xc0(%rsp), %rcx
callq 0x1fc90
movq 0x18(%rsp), %rdi
callq 0x6aa0
movq 0x30(%rsp), %rsi
movq %rax, %rdi
addq $0x148, %rsi            # imm = 0x148
callq 0x1fd20
jmp 0x1f675
leaq 0xa0(%rsp), %rdi
callq 0x6aa0
movq 0x30(%rsp), %rsi
movq %rax, %rdi
addq $0x168, %rsi            # imm = 0x168
callq 0x1fd50
jmp 0x1f698
leaq 0xa0(%rsp), %rdi
callq 0x6aa0
movq 0x30(%rsp), %rsi
movq %rax, %rdi
addq $0x188, %rsi            # imm = 0x188
callq 0x1fd80
jmp 0x1f6bb
leaq 0xa0(%rsp), %rdi
callq 0x6aa0
movq 0x30(%rsp), %rsi
movq %rax, %rdi
addq $0x1a8, %rsi            # imm = 0x1A8
callq 0x1fdb0
jmp 0x1f6de
leaq 0xa0(%rsp), %rdi
callq 0x6aa0
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x10(%rsp)
leaq 0x331(%rip), %rcx       # 0x1fa30
movq %rcx, 0x48(%rsp)
movq $0x0, 0x50(%rsp)
movq %rax, 0x40(%rsp)
movq 0x1b87f(%rip), %rcx     # 0x3af98
leaq 0x58(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x1fe10
jmp 0x1f72f
leaq 0x70(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x1fe50
jmp 0x1f740
movq 0x10(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x1fde0
jmp 0x1f751
leaq 0x70(%rsp), %rdi
callq 0x68c0
movq 0x30(%rsp), %rsi
addq $0xd0, %rsi
leaq 0x38(%rsp), %rdi
callq 0x8c20
jmp 0x1f773
movq 0x30(%rsp), %rdi
addq $0xa0, %rdi
leaq 0xbc(%rsp), %rsi
callq 0x1ff00
movq %rax, 0x8(%rsp)
jmp 0x1f793
movq 0x8(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
movq %rsi, (%rsp)
callq 0x1fff0
leaq 0x38(%rsp), %rdi
callq 0x8cc0
movq 0x30(%rsp), %rdi
movq (%rsp), %rsi
addq $0x128, %rdi            # imm = 0x128
callq 0x1d6d0
jmp 0x1f7ca
leaq 0xa0(%rsp), %rdi
callq 0x6aa0
movq %rax, %rdi
callq 0x1c940
jmp 0x1f7e1
leaq 0xa0(%rsp), %rdi
callq 0xe320
jmp 0x1f53c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
jmp 0x1f847
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x68c0
jmp 0x1f847
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
leaq 0x38(%rsp), %rdi
callq 0x8cc0
leaq 0xa0(%rsp), %rdi
callq 0xe320
jmp 0x1f85e
addq $0xd8, %rsp
retq
movq 0x98(%rsp), %rdi
callq 0x5660
nopl (%rax,%rax)