// File: huanghuide[P]SimpleNetworkTunnel/OuterRunner.cpp
// Function: OuterRunner::SocketRead(int)
void OuterRunner::SocketRead(int sockFd)
{
    bool successFlag = false;
    char buf[MSG_LEN];
    int recvLen = 0;
    
    if (mSendStatus == ESTABLISHED)
    {
        while (true)
        {
            recvLen = recv(mSockFd, buf, MSG_LEN, 0);

            if (recvLen > 0)
            {
            	if ((gRoleType == MIDDLE) || (gRoleType == CLIENT))
            	{
            		for (int index = 0; index < recvLen; index++)
            		{
            			buf[index] = buf[index] ^ SIMPLE_XOR_VALUE;
            		}
            	}

                //printf("Receive from the remote server. SockFd %d, Length %d\n", mSockFd, recvLen);
                //PrintBuf(buf, recvLen);
                
                if (mInnerRunner != NULL)
                {
                    mInnerRunner->AddPendingMsg(buf, recvLen);
                }
                
                successFlag = true;

                break;
            }
            else if (recvLen == 0)
            {
                // socket is normally closed
                successFlag = false;
                
                break;
            }
            else
            {
                if ((errno == EWOULDBLOCK) || (errno == EINTR) || (errno == EINPROGRESS))
                {
                    usleep(1000);
                    continue;
                }
                else 
                {
                    gLog.Write("OuterRunner::SocketRead receive socket error! errno %d\n", errno);
                    successFlag = false;
                    
                    break;
                }
            }
        }

        if (!successFlag)
        {
            HandleError();
        }
    }  
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x820, %rsp            # imm = 0x820
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x820(%rbp)
movb $0x0, -0xd(%rbp)
movl $0x0, -0x814(%rbp)
cmpl $0x2, 0x30(%rax)
jne 0x739f
jmp 0x7267
movq -0x820(%rbp), %rax
movl 0xc(%rax), %edi
leaq -0x810(%rbp), %rsi
movl $0x800, %edx            # imm = 0x800
xorl %ecx, %ecx
callq 0x3120
movl %eax, -0x814(%rbp)
cmpl $0x0, -0x814(%rbp)
jle 0x732f
leaq 0x9002(%rip), %rax      # 0x102a0
cmpl $0x1, (%rax)
je 0x72af
leaq 0x8ff6(%rip), %rax      # 0x102a0
cmpl $0x0, (%rax)
jne 0x72fe
movl $0x0, -0x818(%rbp)
movl -0x818(%rbp), %eax
cmpl -0x814(%rbp), %eax
jge 0x72fc
movslq -0x818(%rbp), %rax
movsbl -0x810(%rbp,%rax), %eax
xorl $0xa5, %eax
movb %al, %cl
movslq -0x818(%rbp), %rax
movb %cl, -0x810(%rbp,%rax)
movl -0x818(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x818(%rbp)
jmp 0x72b9
jmp 0x72fe
movq -0x820(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x7329
movq -0x820(%rbp), %rax
movq 0x28(%rax), %rdi
leaq -0x810(%rbp), %rsi
movl -0x814(%rbp), %edx
callq 0x5600
movb $0x1, -0xd(%rbp)
jmp 0x738b
cmpl $0x0, -0x814(%rbp)
jne 0x733e
movb $0x0, -0xd(%rbp)
jmp 0x738b
callq 0x3050
cmpl $0xb, (%rax)
je 0x735c
callq 0x3050
cmpl $0x4, (%rax)
je 0x735c
callq 0x3050
cmpl $0x73, (%rax)
jne 0x736b
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x3470
jmp 0x7267
callq 0x3050
movl (%rax), %edx
leaq 0xab7f(%rip), %rdi      # 0x11ef8
leaq 0x4006(%rip), %rsi      # 0xb386
movb $0x0, %al
callq 0x63c0
movb $0x0, -0xd(%rbp)
testb $0x1, -0xd(%rbp)
jne 0x739d
movq -0x820(%rbp), %rdi
callq 0x73b0
jmp 0x739f
addq $0x820, %rsp            # imm = 0x820
popq %rbp
retq
nopl (%rax,%rax)