// File: huanghuide[P]SimpleNetworkTunnel/InnerInitiator.cpp
// Function: InnerInitiator::Init()
void InnerInitiator::Init()
{
    mSockFd = socket(AF_INET, SOCK_STREAM, 0);
    if (mSockFd < 0)
    {
        gLog.Write("Inner Initiator socket error!");
        exit(1);
    }

    // Set the timeout for sending and receiving
    struct timeval timeout = {3, 0};  
    
    int flags = setsockopt(mSockFd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(struct timeval));
    if (flags != 0)
    {
        gLog.Write("Set socket send timeout error!");        
        exit(1); 
    }

    flags = setsockopt(mSockFd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval));
    if (flags != 0)
    {
        gLog.Write("Set socket receiving timeout error!");        
        exit(1);
    }

    // Set the send and recv buffer
    int bufSize = TCP_SOCK_BUF_SIZE;
    int bufLen = sizeof(int);
    
    flags = setsockopt(mSockFd, SOL_SOCKET, SO_SNDBUF, (char *)&bufSize, bufLen);
    if (flags != 0)
    {
        gLog.Write("Set socket sending buffer error!");        
        exit(1);
    }

    flags = setsockopt(mSockFd, SOL_SOCKET, SO_RCVBUF, (char *)&bufSize, bufLen);
    if (flags != 0)
    {
        gLog.Write("Set socket receiving buffer error!");        
        exit(1);
    }

    // Disable Nagle algorithm
    int enable = 1;
    flags = setsockopt(mSockFd, IPPROTO_TCP, TCP_NODELAY, (void*)&enable, sizeof(enable));
    if (flags != 0)
    {
        gLog.Write("Disable NAGLE algorithm error!");        
        exit(1);
    }


    bzero(&mAddr, sizeof(mAddr));
    
    mAddr.sin_family = AF_INET;
    mAddr.sin_port = htons(mListenPort);
    mAddr.sin_addr.s_addr = htonl(INADDR_ANY);

    // Reuse the socket
    int reuseFlag = 1;
    if (setsockopt(mSockFd, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuseFlag, sizeof(int)) != 0)
    {
        gLog.Write("Cannot reuse the socket");
        
        exit(1);
    }

    // Bind the socket
    if (bind(mSockFd, (sockaddr*)&mAddr, mAddrLen) != 0)
    {
        gLog.Write("Inner Initiator bind socket error!");

        exit(1);
    }

    // Listen the socket
    if (listen(mSockFd, 256) != 0)
    {
        gLog.Write("Inner Initiator listen socket error!");

        exit(1);
    }

    // Add into runner manager
    AddSockFdIntoSet(mSockFd);   
    gRunnerManager.AddRunner(mSockFd, this);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1f0, %rsp            # imm = 0x1F0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x1a8(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x30f0
movl %eax, %ecx
movq -0x1a8(%rbp), %rax
movl %ecx, 0xc(%rax)
cmpl $0x0, 0xc(%rax)
jge 0x4849
leaq -0x29(%rbp), %rdi
movq %rdi, -0x1b0(%rbp)
callq 0x34d0
movq -0x1b0(%rbp), %rdx
leaq 0x68d7(%rip), %rsi      # 0xb0b6
leaq -0x28(%rbp), %rdi
callq 0x3ec0
jmp 0x47ea
leaq 0xd707(%rip), %rdi      # 0x11ef8
leaq -0x28(%rbp), %rsi
callq 0x6280
jmp 0x47fc
leaq -0x28(%rbp), %rdi
callq 0x3210
leaq -0x29(%rbp), %rdi
callq 0x3390
movl $0x1, %edi
callq 0x3420
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x483b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x3210
leaq -0x29(%rbp), %rdi
callq 0x3390
jmp 0x4f37
movq -0x1a8(%rbp), %rax
movq 0x6989(%rip), %rcx      # 0xb1e0
movq %rcx, -0x50(%rbp)
movq 0x6986(%rip), %rcx      # 0xb1e8
movq %rcx, -0x48(%rbp)
movl 0xc(%rax), %edi
movl $0x1, %esi
movl $0x15, %edx
leaq -0x50(%rbp), %rcx
movl $0x10, %r8d
callq 0x34e0
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0x4917
leaq -0x79(%rbp), %rdi
movq %rdi, -0x1b8(%rbp)
callq 0x34d0
movq -0x1b8(%rbp), %rdx
leaq 0x6827(%rip), %rsi      # 0xb0d4
leaq -0x78(%rbp), %rdi
callq 0x3ec0
jmp 0x48b8
leaq 0xd639(%rip), %rdi      # 0x11ef8
leaq -0x78(%rbp), %rsi
callq 0x6280
jmp 0x48ca
leaq -0x78(%rbp), %rdi
callq 0x3210
leaq -0x79(%rbp), %rdi
callq 0x3390
movl $0x1, %edi
callq 0x3420
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x4909
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x3210
leaq -0x79(%rbp), %rdi
callq 0x3390
jmp 0x4f37
movq -0x1a8(%rbp), %rax
movl 0xc(%rax), %edi
movl $0x1, %esi
movl $0x14, %edx
leaq -0x50(%rbp), %rcx
movl $0x10, %r8d
callq 0x34e0
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0x49e4
leaq -0xa1(%rbp), %rdi
movq %rdi, -0x1c0(%rbp)
callq 0x34d0
movq -0x1c0(%rbp), %rdx
leaq 0x678b(%rip), %rsi      # 0xb0f3
leaq -0xa0(%rbp), %rdi
callq 0x3ec0
jmp 0x4976
leaq 0xd57b(%rip), %rdi      # 0x11ef8
leaq -0xa0(%rbp), %rsi
callq 0x6280
jmp 0x498b
leaq -0xa0(%rbp), %rdi
callq 0x3210
leaq -0xa1(%rbp), %rdi
callq 0x3390
movl $0x1, %edi
callq 0x3420
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x49d3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x3210
leaq -0xa1(%rbp), %rdi
callq 0x3390
jmp 0x4f37
movq -0x1a8(%rbp), %rax
movl $0x40000, -0xa8(%rbp)   # imm = 0x40000
movl $0x4, -0xac(%rbp)
movl 0xc(%rax), %edi
movl -0xac(%rbp), %r8d
movl $0x1, %esi
movl $0x7, %edx
leaq -0xa8(%rbp), %rcx
callq 0x34e0
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0x4ac9
leaq -0xd1(%rbp), %rdi
movq %rdi, -0x1c8(%rbp)
callq 0x34d0
movq -0x1c8(%rbp), %rdx
leaq 0x66ca(%rip), %rsi      # 0xb117
leaq -0xd0(%rbp), %rdi
callq 0x3ec0
jmp 0x4a5b
leaq 0xd496(%rip), %rdi      # 0x11ef8
leaq -0xd0(%rbp), %rsi
callq 0x6280
jmp 0x4a70
leaq -0xd0(%rbp), %rdi
callq 0x3210
leaq -0xd1(%rbp), %rdi
callq 0x3390
movl $0x1, %edi
callq 0x3420
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x4ab8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x3210
leaq -0xd1(%rbp), %rdi
callq 0x3390
jmp 0x4f37
movq -0x1a8(%rbp), %rax
movl 0xc(%rax), %edi
movl -0xac(%rbp), %r8d
movl $0x1, %esi
movl $0x8, %edx
leaq -0xa8(%rbp), %rcx
callq 0x34e0
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0x4b9a
leaq -0xf9(%rbp), %rdi
movq %rdi, -0x1d0(%rbp)
callq 0x34d0
movq -0x1d0(%rbp), %rdx
leaq 0x661a(%rip), %rsi      # 0xb138
leaq -0xf8(%rbp), %rdi
callq 0x3ec0
jmp 0x4b2c
leaq 0xd3c5(%rip), %rdi      # 0x11ef8
leaq -0xf8(%rbp), %rsi
callq 0x6280
jmp 0x4b41
leaq -0xf8(%rbp), %rdi
callq 0x3210
leaq -0xf9(%rbp), %rdi
callq 0x3390
movl $0x1, %edi
callq 0x3420
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x4b89
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x3210
leaq -0xf9(%rbp), %rdi
callq 0x3390
jmp 0x4f37
movq -0x1a8(%rbp), %rax
movl $0x1, -0x100(%rbp)
movl 0xc(%rax), %edi
movl $0x6, %esi
movl $0x1, %edx
leaq -0x100(%rbp), %rcx
movl $0x4, %r8d
callq 0x34e0
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0x4c74
leaq -0x121(%rbp), %rdi
movq %rdi, -0x1d8(%rbp)
callq 0x34d0
movq -0x1d8(%rbp), %rdx
leaq 0x6563(%rip), %rsi      # 0xb15b
leaq -0x120(%rbp), %rdi
callq 0x3ec0
jmp 0x4c06
leaq 0xd2eb(%rip), %rdi      # 0x11ef8
leaq -0x120(%rbp), %rsi
callq 0x6280
jmp 0x4c1b
leaq -0x120(%rbp), %rdi
callq 0x3210
leaq -0x121(%rbp), %rdi
callq 0x3390
movl $0x1, %edi
callq 0x3420
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x4c63
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x3210
leaq -0x121(%rbp), %rdi
callq 0x3390
jmp 0x4f37
movq -0x1a8(%rbp), %rdi
addq $0x28, %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x31c0
movq -0x1a8(%rbp), %rax
movw $0x2, 0x28(%rax)
movl 0x3c(%rax), %eax
movzwl %ax, %edi
callq 0x3070
movw %ax, %cx
movq -0x1a8(%rbp), %rax
movw %cx, 0x2a(%rax)
xorl %edi, %edi
callq 0x3270
movl %eax, %ecx
movq -0x1a8(%rbp), %rax
movl %ecx, 0x2c(%rax)
movl $0x1, -0x128(%rbp)
movl 0xc(%rax), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0x128(%rbp), %rcx
movl $0x4, %r8d
callq 0x34e0
cmpl $0x0, %eax
je 0x4d93
leaq -0x149(%rbp), %rdi
movq %rdi, -0x1e0(%rbp)
callq 0x34d0
movq -0x1e0(%rbp), %rdx
leaq 0x6463(%rip), %rsi      # 0xb17a
leaq -0x148(%rbp), %rdi
callq 0x3ec0
jmp 0x4d25
leaq 0xd1cc(%rip), %rdi      # 0x11ef8
leaq -0x148(%rbp), %rsi
callq 0x6280
jmp 0x4d3a
leaq -0x148(%rbp), %rdi
callq 0x3210
leaq -0x149(%rbp), %rdi
callq 0x3390
movl $0x1, %edi
callq 0x3420
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x4d82
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x3210
leaq -0x149(%rbp), %rdi
callq 0x3390
jmp 0x4f37
movq -0x1a8(%rbp), %rax
movl 0xc(%rax), %edi
movq %rax, %rsi
addq $0x28, %rsi
movl 0x38(%rax), %edx
callq 0x3080
cmpl $0x0, %eax
je 0x4e52
leaq -0x171(%rbp), %rdi
movq %rdi, -0x1e8(%rbp)
callq 0x34d0
movq -0x1e8(%rbp), %rdx
leaq 0x63bc(%rip), %rsi      # 0xb192
leaq -0x170(%rbp), %rdi
callq 0x3ec0
jmp 0x4de4
leaq 0xd10d(%rip), %rdi      # 0x11ef8
leaq -0x170(%rbp), %rsi
callq 0x6280
jmp 0x4df9
leaq -0x170(%rbp), %rdi
callq 0x3210
leaq -0x171(%rbp), %rdi
callq 0x3390
movl $0x1, %edi
callq 0x3420
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x4e41
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x170(%rbp), %rdi
callq 0x3210
leaq -0x171(%rbp), %rdi
callq 0x3390
jmp 0x4f37
movq -0x1a8(%rbp), %rax
movl 0xc(%rax), %edi
movl $0x100, %esi            # imm = 0x100
callq 0x33e0
cmpl $0x0, %eax
je 0x4f09
leaq -0x199(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
callq 0x34d0
movq -0x1f0(%rbp), %rdx
leaq 0x6325(%rip), %rsi      # 0xb1b5
leaq -0x198(%rbp), %rdi
callq 0x3ec0
jmp 0x4e9e
leaq 0xd053(%rip), %rdi      # 0x11ef8
leaq -0x198(%rbp), %rsi
callq 0x6280
jmp 0x4eb3
leaq -0x198(%rbp), %rdi
callq 0x3210
leaq -0x199(%rbp), %rdi
callq 0x3390
movl $0x1, %edi
callq 0x3420
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x4efb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x198(%rbp), %rdi
callq 0x3210
leaq -0x199(%rbp), %rdi
callq 0x3390
jmp 0x4f37
movq -0x1a8(%rbp), %rax
movl 0xc(%rax), %edi
callq 0x3950
movq -0x1a8(%rbp), %rdx
movl 0xc(%rdx), %esi
leaq 0xcfd7(%rip), %rdi      # 0x11f00
callq 0x8120
addq $0x1f0, %rsp            # imm = 0x1F0
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x34c0