// File: zeeev[P]DASVC/bamtools/src/api/internal/io/TcpSocket_p.cpp
// Function: BamTools::Internal::TcpSocket::ReadFromSocket()
int64_t TcpSocket::ReadFromSocket(void) {

    // check for any socket engine errors
    if ( !m_engine->IsValid() ) {
        m_errorString = "TcpSocket::ReadFromSocket - socket disconnected";
        ResetSocketEngine();
        return -1;
    }

    // wait for ready read
    bool timedOut;
    const bool isReadyRead = m_engine->WaitForRead(5000, &timedOut);

    // if not ready
    if ( !isReadyRead ) {

        // if we simply timed out
        if ( timedOut ) {
            // TODO: get add'l error info from engine ?
            m_errorString = "TcpSocket::ReadFromSocket - timed out waiting for ready read";
        }

        // otherwise, there was some other error
        else {
            // TODO: get add'l error info from engine ?
            m_errorString = "TcpSocket::ReadFromSocket - encountered error while waiting for ready read";
        }

        // return failure
        return -1;
    }

    // get number of bytes available from socket
    const int64_t bytesToRead = m_engine->NumBytesAvailable();
    if ( bytesToRead < 0 ) {
        // TODO: get add'l error info from engine ?
        m_errorString = "TcpSocket::ReadFromSocket - encountered error while determining numBytesAvailable";
        return -1;
    }

    // make space in buffer & read from socket
    char* buffer = m_readBuffer.Reserve(bytesToRead);
    const int64_t numBytesRead = m_engine->Read(buffer, bytesToRead);
    if ( numBytesRead == -1 ) {
        // TODO: get add'l error info from engine ?
        m_errorString = "TcpSocket::ReadFromSocket - encountered error while reading bytes";
    }

    // return number of bytes actually read
    return numBytesRead;
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x28(%rsp)
movq 0x28(%rsp), %rax
movq %rax, (%rsp)
movq 0x70(%rax), %rdi
callq 0x72350
testb $0x1, %al
jne 0xe07ed
movq (%rsp), %rdi
addq $0x100, %rdi            # imm = 0x100
leaq 0xbb95(%rip), %rsi      # 0xec366
callq 0x78d10
movq (%rsp), %rdi
callq 0x787e0
movq $-0x1, 0x30(%rsp)
jmp 0xe08f4
movq (%rsp), %rax
movq 0x70(%rax), %rdi
movl $0x1388, %esi           # imm = 0x1388
leaq 0x27(%rsp), %rdx
callq 0x7bdb0
andb $0x1, %al
movb %al, 0x26(%rsp)
testb $0x1, 0x26(%rsp)
jne 0xe0856
testb $0x1, 0x27(%rsp)
je 0xe0831
movq (%rsp), %rdi
addq $0x100, %rdi            # imm = 0x100
leaq 0xbb6c(%rip), %rsi      # 0xec396
callq 0x78d10
jmp 0xe0848
movq (%rsp), %rdi
addq $0x100, %rdi            # imm = 0x100
leaq 0xbb90(%rip), %rsi      # 0xec3d3
callq 0x78d10
movq $-0x1, 0x30(%rsp)
jmp 0xe08f4
movq (%rsp), %rax
movq 0x70(%rax), %rdi
callq 0x7b190
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
jge 0xe0892
movq (%rsp), %rdi
addq $0x100, %rdi            # imm = 0x100
leaq 0xbb9c(%rip), %rsi      # 0xec41e
callq 0x78d10
movq $-0x1, 0x30(%rsp)
jmp 0xe08f4
movq (%rsp), %rdi
addq $0x80, %rdi
movq 0x18(%rsp), %rsi
callq 0x73760
movq %rax, %rcx
movq (%rsp), %rax
movq %rcx, 0x10(%rsp)
movq 0x70(%rax), %rdi
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x745a0
movq %rax, 0x8(%rsp)
cmpq $-0x1, 0x8(%rsp)
jne 0xe08ea
movq (%rsp), %rdi
addq $0x100, %rdi            # imm = 0x100
leaq 0xbb8b(%rip), %rsi      # 0xec470
callq 0x78d10
movq 0x8(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
addq $0x38, %rsp
retq
nop