// File: /workspace/github_cpp_makefile_star2_O2/XuhuiSun95[P]Ticket-Booking-Service/server/Server.cpp
// Function: Server::ProcessTicket(int const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
void Server::ProcessTicket(const int &sock, const std::string &type) {

    std::string msg = mServer->GetMessage(sock);
    std::cout << "Message Received: " << msg << std::endl;

    if(msg.find(type)!=std::string::npos) {

        std::size_t ws = msg.find(" ");
        int ticket = std::stoi(msg.substr(ws+1));
        
        mtx.lock();
        std::string reply = HandleRequest(ticket);
        mtx.unlock();
        std::cout << "Ticket left: " << mTickets << std::endl;

        mServer->SendMessage(sock, reply);
    } else {

        //  Create a new forward socket to handle the forward operation
        ServerSocket* pair = new ServerSocket();
        pair->Init(mPairInfo.first, mPairInfo.second, false);

        std::cout << "Forward Message!" << std::endl;
        std::string reply = pair->ForwardTicket(msg);
        std::cout << "Receive pair reply: " << reply << std::endl;

        delete pair;

        std::cout << "Forward reply!" << std::endl;
        mServer->SendMessage(sock, reply);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x48, %rsp
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r14
movq 0x8(%rdi), %rsi
leaq -0x60(%rbp), %rdi
movq %rbx, %rdx
callq 0x38ec
movq 0x4921(%rip), %rdi      # 0x6fc8
leaq 0x1956(%rip), %rsi      # 0x4004
callq 0x21c0
leaq -0x60(%rbp), %rsi
movq %rax, %rdi
callq 0x2190
movq %rax, %rdi
callq 0x20e0
leaq -0x60(%rbp), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x2300
cmpq $-0x1, %rax
je 0x2784
leaq 0x1bf5(%rip), %rsi      # 0x42db
leaq -0x60(%rbp), %r15
movq %r15, %rdi
xorl %edx, %edx
callq 0x22f0
leaq 0x1(%rax), %rdx
leaq -0x40(%rbp), %rdi
pushq $-0x1
popq %rcx
movq %r15, %rsi
callq 0x21e0
leaq -0x40(%rbp), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x299c
movl %eax, %r15d
leaq -0x40(%rbp), %rdi
callq 0x20c0
movl %r15d, -0x1c(%rbp)
leaq 0x60(%r14), %r15
movq %r15, %rdi
callq 0x29be
leaq -0x40(%rbp), %rdi
leaq -0x1c(%rbp), %rdx
movq %r14, %rsi
callq 0x2892
movq %r15, %rdi
callq 0x2160
movq 0x4878(%rip), %rdi      # 0x6fc8
leaq 0x18c0(%rip), %rsi      # 0x4017
callq 0x21c0
movl (%r14), %esi
movq %rax, %rdi
callq 0x2290
movq %rax, %rdi
callq 0x20e0
movq 0x8(%r14), %rdi
leaq -0x40(%rbp), %rdx
movq %rbx, %rsi
callq 0x3940
jmp 0x2838
movl $0x41c, %edi            # imm = 0x41C
callq 0x21d0
movq %rax, %r15
movq %rax, %rdi
callq 0x3748
leaq 0x38(%r14), %rsi
leaq 0x58(%r14), %rdx
movq %r15, %rdi
xorl %ecx, %ecx
callq 0x3782
movq 0x4816(%rip), %rdi      # 0x6fc8
leaq 0x186c(%rip), %rsi      # 0x4025
callq 0x21c0
movq %rax, %rdi
callq 0x20e0
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rdx
movq %r15, %rsi
callq 0x3984
movq 0x47eb(%rip), %rdi      # 0x6fc8
leaq 0x1852(%rip), %rsi      # 0x4036
callq 0x21c0
leaq -0x40(%rbp), %rsi
movq %rax, %rdi
callq 0x2190
movq %rax, %rdi
callq 0x20e0
movq %r15, %rdi
callq 0x376c
movq %r15, %rdi
callq 0x21b0
movq 0x47b4(%rip), %rdi      # 0x6fc8
leaq 0x1830(%rip), %rsi      # 0x404b
callq 0x21c0
movq %rax, %rdi
callq 0x20e0
movq 0x8(%r14), %rdi
leaq -0x40(%rbp), %rdx
movq %rbx, %rsi
callq 0x3940
leaq -0x40(%rbp), %rdi
callq 0x20c0
leaq -0x60(%rbp), %rdi
callq 0x20c0
addq $0x48, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
jmp 0x287e
movq %rax, %rbx
movq %r15, %rdi
callq 0x21b0
jmp 0x2881
jmp 0x287e
jmp 0x287e
jmp 0x2870
jmp 0x287e
jmp 0x287e
jmp 0x2870
movq %rax, %rbx
leaq -0x40(%rbp), %rdi
callq 0x20c0
jmp 0x2881
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x20c0
movq %rbx, %rdi
callq 0x22b0