// File: huanghuide[P]SimpleNetworkTunnel/InnerRunner.cpp
// Function: InnerRunner::SocketRead(int)
void InnerRunner::SocketRead(int sockFd)
{
    bool successFlag = false;

    char buf[MSG_LEN];
    int recvLen = 0;

    while (true)
    {
        recvLen = recv(mSockFd, buf, MSG_LEN, 0);

        if (recvLen > 0)
        {
            if (mOuterRunner == NULL)
            {
                mOuterRunner = new OuterRunner(this);
            }

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

            //printf("INNER: Receive message from client. SockFd %d, length %d\n", mSockFd, recvLen);
            //PrintBuf(buf, recvLen);
            
            ProcessDataMessage(buf, recvLen);
            successFlag = true;

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

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x840, %rsp            # imm = 0x840
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x830(%rbp)
movb $0x0, -0xd(%rbp)
movl $0x0, -0x814(%rbp)
movq -0x830(%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 0x54c0
movq -0x830(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x543a
movl $0x60, %edi
callq 0x3340
movq -0x830(%rbp), %rsi
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x838(%rbp)
callq 0x70b0
jmp 0x53fe
movq -0x830(%rbp), %rax
movq -0x838(%rbp), %rcx
movq %rcx, 0x28(%rax)
jmp 0x543a
movq -0x838(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x820(%rbp)
movl %eax, -0x824(%rbp)
movl $0x60, %esi
callq 0x3360
jmp 0x5537
leaq 0xae5f(%rip), %rax      # 0x102a0
cmpl $0x1, (%rax)
je 0x5452
leaq 0xae53(%rip), %rax      # 0x102a0
cmpl $0x2, (%rax)
jne 0x54a1
movl $0x0, -0x828(%rbp)
movl -0x828(%rbp), %eax
cmpl -0x814(%rbp), %eax
jge 0x549f
movslq -0x828(%rbp), %rax
movsbl -0x810(%rbp,%rax), %eax
xorl $0xa5, %eax
movb %al, %cl
movslq -0x828(%rbp), %rax
movb %cl, -0x810(%rbp,%rax)
movl -0x828(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x828(%rbp)
jmp 0x545c
jmp 0x54a1
movq -0x830(%rbp), %rdi
leaq -0x810(%rbp), %rsi
movl -0x814(%rbp), %edx
callq 0x5550
movb $0x1, -0xd(%rbp)
jmp 0x551c
cmpl $0x0, -0x814(%rbp)
jne 0x54cf
movb $0x0, -0xd(%rbp)
jmp 0x551c
callq 0x3050
cmpl $0xb, (%rax)
je 0x54ed
callq 0x3050
cmpl $0x4, (%rax)
je 0x54ed
callq 0x3050
cmpl $0x73, (%rax)
jne 0x54fc
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x3470
jmp 0x539b
callq 0x3050
movl (%rax), %edx
leaq 0xc9ee(%rip), %rdi      # 0x11ef8
leaq 0x5cf0(%rip), %rsi      # 0xb201
movb $0x0, %al
callq 0x63c0
movb $0x0, -0xd(%rbp)
testb $0x1, -0xd(%rbp)
jne 0x552e
movq -0x830(%rbp), %rdi
callq 0x55a0
addq $0x840, %rsp            # imm = 0x840
popq %rbp
retq
movq -0x820(%rbp), %rdi
callq 0x34c0
nopw %cs:(%rax,%rax)