// File: /workspace/github_cpp_makefile_star2_O2/guoxuanhan[P]NetServer/httpserver.cpp
// Function: HttpServer::HandleMessage(std::shared_ptr<TcpConnection> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
void HttpServer::HandleMessage(const spTcpConnection& sptcpconn, std::string &msg)
{
    std::shared_ptr<HttpSession> sphttpsession;
    spTimer sptimer;
    {
        std::lock_guard <std::mutex> lock(mutex_);
        sphttpsession = httpsessionnlist_[sptcpconn];
        sptimer = timerlist_[sptcpconn];
    }
    sptimer->Adjust(5000, Timer::TimerType::TIMER_ONCE, std::bind(&TcpConnection::Shutdown, sptcpconn));

    if(threadpool_.GetThreadNum() > 0)
    {
        //Bug20190320：多线程下，对象的声明周期管理问题，就像在这里，向线程池传入了phttpsession和ptcpconn，怎么知道其指向对象是否已经析构了呢？
        //所以，需要采用智能指针来管理对象,替换原始指针
        //线程池处理业务，处理完后投递回本IO线程执行send
        //std::cout << "threadpool_.AddTask" << std::endl;
        HttpRequestContext httprequestcontext;
        std::string responsecontext;
        bool result = sphttpsession->PraseHttpRequest(msg, httprequestcontext);
        if(result == false)
        {
            sphttpsession->HttpError(400, "Bad request", httprequestcontext, responsecontext); //请求报文解析错误，报400
            sptcpconn->Send(responsecontext);
            return;
        }

        sptcpconn->SetAsyncProcessing(true);
        threadpool_.AddTask([=]() {
            //HttpRequestContext req = httprequestcontext;
            std::string responsemsg;
            sphttpsession->HttpProcess(httprequestcontext, responsemsg);

            sptcpconn->Send(responsemsg); //任务已经处理完成，执行跨线程调度，即回调

            if(!sphttpsession->KeepAlive())
            {
                //短连接，可以告诉框架层数据发完就可以关掉TCP连接，不过这里注释掉，还是交给客户端主动关闭吧
                //sptcpconn->HandleClose();
            }
        });
    }
    else
    {
        //没有开启业务线程池，业务计算直接在IO线程执行
        HttpRequestContext httprequestcontext;
        std::string responsecontext;
        bool result = sphttpsession->PraseHttpRequest(msg, httprequestcontext);
        if(result == false)
        {
            sphttpsession->HttpError(400, "Bad request", httprequestcontext, responsecontext); //请求报文解析错误，报400
            sptcpconn->Send(responsecontext);
            return;
        }

        sphttpsession->HttpProcess(httprequestcontext, responsecontext);

        sptcpconn->Send(responsecontext);

        if(!sphttpsession->KeepAlive())
        {
            //短连接，可以告诉框架层数据发完就可以关掉TCP连接，不过这里注释掉，还是交给客户端主动关闭吧
            //sptcpconn->HandleClose();
        }
    }
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x220, %rsp            # imm = 0x220
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r14
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movaps %xmm0, 0x10(%rsp)
leaq 0x60(%rdi), %r12
movq %r12, %rdi
callq 0x56bc
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6a44
movq %rsp, %rdi
movq %rax, %rsi
callq 0x6d70
leaq 0x30(%r14), %rdi
movq %rbx, %rsi
callq 0x6a94
leaq 0x10(%rsp), %r13
movq %r13, %rdi
movq %rax, %rsi
callq 0x6dd6
movq %r12, %rdi
callq 0x4280
movq (%r13), %r12
leaq 0x5010(%rip), %rax      # 0xb2f0
leaq 0x20(%rsp), %rsi
movq %rax, (%rsi)
andq $0x0, 0x8(%rsi)
leaq 0x170(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rdx
callq 0x74ee
leaq 0xa0(%rsp), %rdi
movq %r13, %rsi
callq 0x6ae4
leaq 0xa0(%rsp), %rcx
movq %r12, %rdi
movl $0x1388, %esi           # imm = 0x1388
xorl %edx, %edx
callq 0xd856
leaq 0xa0(%rsp), %rdi
callq 0x4a60
leaq 0x188(%rsp), %rdi
callq 0x6c7a
addq $0x250, %r14            # imm = 0x250
movq %r14, %rdi
callq 0xcf56
testl %eax, %eax
jle 0x64c8
leaq 0x180(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
leaq 0x1a0(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
leaq 0x1c0(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
leaq 0x1d8(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x210(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
leaq 0x30(%rsp), %rdx
movq %rdx, -0x10(%rdx)
andq $0x0, -0x8(%rdx)
movb %cl, (%rax)
movb %cl, (%rdx)
movq (%rsp), %rdi
leaq 0x170(%rsp), %rdx
movq %r15, %rsi
callq 0x830c
testb %al, %al
je 0x6583
movq (%rbx), %rax
movb $0x1, 0x36(%rax)
leaq 0xa0(%rsp), %rdi
movq %rsp, %rsi
callq 0x6df2
leaq 0xb0(%rsp), %r15
leaq 0x170(%rsp), %rsi
movq %r15, %rdi
callq 0x6b2c
leaq 0x160(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x7124
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movl $0xd0, %edi
callq 0x4320
movq %rax, %rbx
movaps 0xa0(%rsp), %xmm0
andq $0x0, 0xa8(%rsp)
movups %xmm0, (%rax)
andq $0x0, 0xa0(%rsp)
leaq 0x10(%rax), %rdi
movq %r15, %rsi
callq 0x7e3a
movq %rbx, %rdi
addq $0xc0, %rdi
movq %r12, %rsi
callq 0x7124
leaq 0x40(%rsp), %rsi
movq %rbx, (%rsi)
leaq 0x3d3(%rip), %rax       # 0x6868
movq %rax, 0x18(%rsi)
leaq 0x420(%rip), %rax       # 0x68c0
movq %rax, 0x10(%rsi)
movq %r14, %rdi
callq 0xcf04
leaq 0x40(%rsp), %rdi
callq 0x4a60
leaq 0xa0(%rsp), %rdi
callq 0x6836
jmp 0x65df
leaq 0xb0(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
leaq 0xd0(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
leaq 0xf0(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
leaq 0x108(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x140(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
leaq 0x180(%rsp), %rdx
movq %rdx, -0x10(%rdx)
andq $0x0, -0x8(%rdx)
movb %cl, (%rax)
movb %cl, (%rdx)
movq (%rsp), %rdi
leaq 0xa0(%rsp), %rdx
movq %r15, %rsi
callq 0x830c
movq (%rsp), %r14
testb %al, %al
je 0x65f3
leaq 0xa0(%rsp), %rsi
leaq 0x170(%rsp), %rdx
movq %r14, %rdi
callq 0x8690
jmp 0x6635
movq (%rsp), %r14
leaq 0x804d(%rip), %rsi      # 0xe5db
leaq 0x80(%rsp), %rdi
leaq 0xa0(%rsp), %rdx
callq 0x4130
leaq 0x80(%rsp), %rdx
leaq 0x170(%rsp), %rcx
leaq 0x20(%rsp), %r8
movq %r14, %rdi
movl $0x190, %esi            # imm = 0x190
callq 0x8e30
leaq 0x80(%rsp), %rdi
callq 0x4170
movq (%rbx), %rdi
leaq 0x20(%rsp), %rsi
callq 0xb03c
leaq 0x20(%rsp), %rdi
callq 0x4170
leaq 0x170(%rsp), %rdi
jmp 0x665a
leaq 0x7fe1(%rip), %rsi      # 0xe5db
leaq 0x60(%rsp), %rdi
leaq 0x20(%rsp), %rdx
callq 0x4130
leaq 0x60(%rsp), %rdx
leaq 0xa0(%rsp), %rcx
leaq 0x170(%rsp), %r8
movq %r14, %rdi
movl $0x190, %esi            # imm = 0x190
callq 0x8e30
leaq 0x60(%rsp), %rdi
callq 0x4170
movq (%rbx), %rdi
leaq 0x170(%rsp), %rsi
callq 0xb03c
leaq 0x170(%rsp), %rdi
callq 0x4170
leaq 0xa0(%rsp), %rdi
callq 0x6bcc
leaq 0x18(%rsp), %rdi
callq 0x6c7a
leaq 0x8(%rsp), %rdi
callq 0x6c7a
addq $0x220, %rsp            # imm = 0x220
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x4170
jmp 0x66dc
jmp 0x66d9
jmp 0x66a9
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x4170
jmp 0x66f6
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x4a60
leaq 0xa0(%rsp), %rdi
callq 0x6836
jmp 0x66f6
jmp 0x66f3
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x6c7a
jmp 0x66f6
movq %rax, %rbx
leaq 0x170(%rsp), %rdi
callq 0x4170
leaq 0xa0(%rsp), %rdi
jmp 0x6708
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x4170
leaq 0x170(%rsp), %rdi
callq 0x6bcc
jmp 0x6745
jmp 0x6735
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x4a60
jmp 0x6726
movq %rax, %rbx
leaq 0x188(%rsp), %rdi
callq 0x6c7a
jmp 0x6745
movq %rax, %rbx
jmp 0x6745
movq %rax, %rbx
movq %r12, %rdi
callq 0x4280
leaq 0x18(%rsp), %rdi
callq 0x6c7a
leaq 0x8(%rsp), %rdi
callq 0x6c7a
movq %rbx, %rdi
callq 0x4570
nop