// File: huanghuide[P]SimpleNetworkTunnel/OuterRunner.cpp
// Function: OuterRunner::ProcessInitStatus()
void OuterRunner::ProcessInitStatus()
{
    if (mSockFd <= 0)
    {
        mSockFd = socket(AF_INET, SOCK_STREAM, 0);
        if (mSockFd < 0)
        {
            gLog.Write("socket");
            HandleError();
        }

        gRunnerManager.AddRunner(mSockFd, this);

        // Set as non-block socket
        int flags = fcntl(mSockFd, F_GETFL, 0);
        fcntl(mSockFd, F_SETFL, flags | O_NONBLOCK);

        // Set the timeout for sending and receiving
        struct timeval timeout = {3, 0};  
        
        flags = setsockopt(mSockFd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(struct timeval));
        if (flags != 0)
        {
            HandleError();  
        }

        flags = setsockopt(mSockFd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval));
        if (flags != 0)
        {
            HandleError();  
        }

        // 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!");        
            HandleError();  
        }

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

        // 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!");        
            HandleError();  
        }
    }

    struct sockaddr_in addr;
    
    bzero(&addr, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(mDstPort);
    addr.sin_addr.s_addr = inet_addr(mDstIpAddr.c_str());

    int retConn = connect(mSockFd, (sockaddr*)&addr, sizeof(addr));
    
    if (retConn != 0)
    {
        if ((errno == EINPROGRESS) || (errno == EALREADY))
        {
            mSendStatus = CONNECTING;
        }
        else if (errno == EISCONN)
        {
            ProcessEstablishedStatus();
            mSendStatus = ESTABLISHED;

            ProcessEstablishedStatus();
        }
        else
        {
            gLog.Write("OuterRunner::ProcessConnectingStatus: errno %d\n", errno);
            HandleError();                
        }
    }
    else
    {
        gRunnerManager.AddRunner(mSockFd, this);

        mSendStatus = ESTABLISHED;

        ProcessEstablishedStatus();
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xe8(%rbp)
cmpl $0x0, 0xc(%rax)
jg 0x7c06
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x30f0
movl %eax, %ecx
movq -0xe8(%rbp), %rax
movl %ecx, 0xc(%rax)
cmpl $0x0, 0xc(%rax)
jge 0x78b7
leaq -0x29(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0x34d0
movq -0xf0(%rbp), %rdx
leaq 0x3942(%rip), %rsi      # 0xb18b
leaq -0x28(%rbp), %rdi
callq 0x3ec0
jmp 0x7854
leaq 0xa69d(%rip), %rdi      # 0x11ef8
leaq -0x28(%rbp), %rsi
callq 0x6280
jmp 0x7866
leaq -0x28(%rbp), %rdi
callq 0x3210
leaq -0x29(%rbp), %rdi
callq 0x3390
movq -0xe8(%rbp), %rdi
callq 0x73b0
jmp 0x78b7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x78a9
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 0x7d32
movq -0xe8(%rbp), %rdx
movl 0xc(%rdx), %esi
leaq 0xa638(%rip), %rdi      # 0x11f00
callq 0x8120
movq -0xe8(%rbp), %rax
movl 0xc(%rax), %edi
movl $0x3, %esi
xorl %edx, %edx
movb $0x0, %al
callq 0x31d0
movl %eax, %ecx
movq -0xe8(%rbp), %rax
movl %ecx, -0x40(%rbp)
movl 0xc(%rax), %edi
movl -0x40(%rbp), %edx
orl $0x800, %edx            # imm = 0x800
movl $0x4, %esi
movb $0x0, %al
callq 0x31d0
movq -0xe8(%rbp), %rax
movq 0x38c9(%rip), %rcx      # 0xb1e0
movq %rcx, -0x50(%rbp)
movq 0x38c6(%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, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
je 0x7957
movq -0xe8(%rbp), %rdi
callq 0x73b0
movq -0xe8(%rbp), %rax
movl 0xc(%rax), %edi
movl $0x1, %esi
movl $0x14, %edx
leaq -0x50(%rbp), %rcx
movl $0x10, %r8d
callq 0x34e0
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
je 0x798f
movq -0xe8(%rbp), %rdi
callq 0x73b0
movq -0xe8(%rbp), %rax
movl $0x40000, -0x54(%rbp)   # imm = 0x40000
movl $0x4, -0x58(%rbp)
movl 0xc(%rax), %edi
movl -0x58(%rbp), %r8d
movl $0x1, %esi
movl $0x7, %edx
leaq -0x54(%rbp), %rcx
callq 0x34e0
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
je 0x7a57
leaq -0x79(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0x34d0
movq -0xf8(%rbp), %rdx
leaq 0x372e(%rip), %rsi      # 0xb117
leaq -0x78(%rbp), %rdi
callq 0x3ec0
jmp 0x79f4
leaq 0xa4fd(%rip), %rdi      # 0x11ef8
leaq -0x78(%rbp), %rsi
callq 0x6280
jmp 0x7a06
leaq -0x78(%rbp), %rdi
callq 0x3210
leaq -0x79(%rbp), %rdi
callq 0x3390
movq -0xe8(%rbp), %rdi
callq 0x73b0
jmp 0x7a57
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x7a49
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 0x7d32
movq -0xe8(%rbp), %rax
movl 0xc(%rax), %edi
movl -0x58(%rbp), %r8d
movl $0x1, %esi
movl $0x8, %edx
leaq -0x54(%rbp), %rcx
callq 0x34e0
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
je 0x7b26
leaq -0xa1(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x34d0
movq -0x100(%rbp), %rdx
leaq 0x3692(%rip), %rsi      # 0xb138
leaq -0xa0(%rbp), %rdi
callq 0x3ec0
jmp 0x7ab4
leaq 0xa43d(%rip), %rdi      # 0x11ef8
leaq -0xa0(%rbp), %rsi
callq 0x6280
jmp 0x7ac9
leaq -0xa0(%rbp), %rdi
callq 0x3210
leaq -0xa1(%rbp), %rdi
callq 0x3390
movq -0xe8(%rbp), %rdi
callq 0x73b0
jmp 0x7b26
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x7b15
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 0x7d32
movq -0xe8(%rbp), %rax
movl $0x1, -0xa8(%rbp)
movl 0xc(%rax), %edi
movl $0x6, %esi
movl $0x1, %edx
leaq -0xa8(%rbp), %rcx
movl $0x4, %r8d
callq 0x34e0
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
je 0x7c04
leaq -0xc9(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x34d0
movq -0x108(%rbp), %rdx
leaq 0x35d7(%rip), %rsi      # 0xb15b
leaq -0xc8(%rbp), %rdi
callq 0x3ec0
jmp 0x7b92
leaq 0xa35f(%rip), %rdi      # 0x11ef8
leaq -0xc8(%rbp), %rsi
callq 0x6280
jmp 0x7ba7
leaq -0xc8(%rbp), %rdi
callq 0x3210
leaq -0xc9(%rbp), %rdi
callq 0x3390
movq -0xe8(%rbp), %rdi
callq 0x73b0
jmp 0x7c04
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x7bf3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x3210
leaq -0xc9(%rbp), %rdi
callq 0x3390
jmp 0x7d32
jmp 0x7c06
leaq -0xdc(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x31c0
movq -0xe8(%rbp), %rax
movw $0x2, -0xdc(%rbp)
movl 0x58(%rax), %eax
movzwl %ax, %edi
callq 0x3070
movq -0xe8(%rbp), %rdi
movw %ax, -0xda(%rbp)
addq $0x38, %rdi
callq 0x3160
movq %rax, %rdi
callq 0x3350
movl %eax, %ecx
movq -0xe8(%rbp), %rax
movl %ecx, -0xd8(%rbp)
movl 0xc(%rax), %edi
leaq -0xdc(%rbp), %rsi
movl $0x10, %edx
callq 0x32b0
movl %eax, -0xe0(%rbp)
cmpl $0x0, -0xe0(%rbp)
je 0x7d00
callq 0x3050
cmpl $0x73, (%rax)
je 0x7c99
callq 0x3050
cmpl $0x72, (%rax)
jne 0x7ca9
movq -0xe8(%rbp), %rax
movl $0x1, 0x30(%rax)
jmp 0x7cfe
callq 0x3050
cmpl $0x6a, (%rax)
jne 0x7cd4
movq -0xe8(%rbp), %rdi
callq 0x7d40
movq -0xe8(%rbp), %rdi
movl $0x2, 0x30(%rdi)
callq 0x7d40
jmp 0x7cfc
callq 0x3050
movl (%rax), %edx
leaq 0xa216(%rip), %rdi      # 0x11ef8
leaq 0x36d5(%rip), %rsi      # 0xb3be
movb $0x0, %al
callq 0x63c0
movq -0xe8(%rbp), %rdi
callq 0x73b0
jmp 0x7cfe
jmp 0x7d29
movq -0xe8(%rbp), %rdx
movl 0xc(%rdx), %esi
leaq 0xa1ef(%rip), %rdi      # 0x11f00
callq 0x8120
movq -0xe8(%rbp), %rdi
movl $0x2, 0x30(%rdi)
callq 0x7d40
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x34c0
nopl (%rax,%rax)