// File: /workspace/github_cpp_makefile_star2_O2/wenzhelu[P]RUDP/sender.cpp
// Function: Sender::sending()
void Sender::sending() {
    while (!master->close) {
        this_thread::sleep_for(chrono::milliseconds(25)); // sleep time need to test
        // we are making somehow dangerous assumption here.
        // the listener may need to change the curPtr when
        // packet loss happened
        if (pending) {
//            debug_print("timerBase: %u, sendBase: %u, RTT: %u, timeout: %u\n", timerBase, master->sendBase, master->RTT, master->TimeOut);
            if (master->sendBase - diff >= userDataLen) {
                endTask();
                continue;
            }
            
            // data pending to send, copy data to the buff
            if (resend) {
                // packet loss, request resend by listener
                debug_print("data loss! sendbase: %u\n", master->sendBase);
                curPtr = master->sendBase;
                *((uint*) master->buff) = master->sendBase;
                resend = false;
            }
            
            char *srcBegin = userBuff + (curPtr - diff), *destBegin = master->buff + master->HEADER_LEN;
            uint len = std::min((uint)1460, std::min(byteIncWnd(), userDataLen - (curPtr - diff)));
            
            // len == 0 could be: we have sent all the data but
            // waiting for all the ack
            // or we have reached send out cWnd of bytes and waiting for ACK to
            // "slide" the cWnd
            if (len > 0) {
                master->setDataBit(1);
                memcpy(destBegin, srcBegin, len);
                
                // finally we are sending
                master->sock->printPacket(false, master->buff, master->HEADER_LEN + len);
//                master->startTimes.insert(pair<uint, tp>(curPtr + len, system_clock::now()));
                master->startTimes[curPtr + len] = system_clock::now();
                master->sock->write(master->buff, master->HEADER_LEN + len);
                if (master->status == SLOW_START) master->slowStartNum++;
                else if (master->status == CONG_AVOID) master->congestNum++;

                master->setAckBit(0);   // set ack 0
                // if we have any sending error, the usock class
                // will print debug infomation, and we just ignore any
                // sending error here, we let ACK and resend to fix it.
                
                // when timer times up, we need to know whether we have
                // also, we need this to calculate the RTT
                
                // change the sequence number after sending packet out
                curPtr += len;
                uint *tm = (uint*)master->buff;
                *tm = curPtr;
                continue;
            }
        }
        master->setDataBit(0);
            
        // if no pending data, check if there is pending ACK
        // if no then do nothing
        // if yes just send it
        if (master->testAckBit()) {
            master->sock->printPacket(false, master->buff, master->HEADER_LEN);
            master->sock->write(master->buff, master->HEADER_LEN);
            // not timing only ack packets
            master->setAckBit(0);
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
leaq -0x38(%rbp), %r14
pushq $0xc
popq %r13
movq 0x48(%rbx), %rax
cmpb $0x0, 0x620(%rax)
jne 0x34a7
movq $0x19, -0x38(%rbp)
movq %r14, %rdi
callq 0x29a5
movq 0x48(%rbx), %rdi
cmpb $0x1, 0x38(%rbx)
jne 0x3419
movl (%rdi), %ecx
movl 0x8(%rbx), %edx
movl 0x40(%rbx), %eax
movl %ecx, %esi
subl %eax, %esi
cmpl %edx, %esi
jae 0x3471
cmpb $0x1, 0x39(%rbx)
jne 0x3351
movl %ecx, 0x3c(%rbx)
movl %ecx, 0x50(%rdi)
movb $0x0, 0x39(%rbx)
movl 0x8(%rbx), %edx
movl 0x40(%rbx), %eax
movl 0x3c(%rbx), %r12d
movl 0x10(%rdi), %esi
subl %r12d, %esi
addl %ecx, %esi
movl %eax, %ecx
subl %r12d, %ecx
addl %edx, %ecx
cmpl %esi, %ecx
cmovael %esi, %ecx
movl $0x5b4, %edx            # imm = 0x5B4
cmpl %edx, %ecx
movl $0x5b4, %r15d           # imm = 0x5B4
cmovbl %ecx, %r15d
testl %ecx, %ecx
je 0x3419
movl %r13d, %r14d
leaq 0x5c(%rdi), %r13
subl %eax, %r12d
addq (%rbx), %r12
pushq $0x1
popq %rsi
callq 0x2b6c
movl %r15d, %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0x21d0
movq 0x48(%rbx), %rdx
movq 0x628(%rdx), %rdi
addq $0x50, %rdx
leal 0xc(%r15), %r12d
xorl %esi, %esi
movl %r12d, %ecx
callq 0x390e
callq 0x2050
movq %rax, %r13
movq 0x48(%rbx), %rdi
addq $0x20, %rdi
movl 0x3c(%rbx), %eax
addl %r15d, %eax
movl %eax, -0x2c(%rbp)
leaq -0x2c(%rbp), %rsi
callq 0x355c
movq %r13, (%rax)
movq 0x48(%rbx), %rsi
movq 0x628(%rsi), %rdi
addq $0x50, %rsi
movq %r12, %rdx
callq 0x38e8
movq 0x48(%rbx), %rdi
movl 0xc(%rdi), %eax
cmpl $0x2, %eax
je 0x347e
testl %eax, %eax
movl %r14d, %r13d
jne 0x3487
incl 0x640(%rdi)
jmp 0x3487
xorl %esi, %esi
callq 0x2b6c
movq 0x48(%rbx), %rdi
callq 0x2bba
testb %al, %al
je 0x32f8
movq 0x48(%rbx), %rdx
movq 0x628(%rdx), %rdi
addq $0x50, %rdx
xorl %esi, %esi
movl %r13d, %ecx
callq 0x390e
movq 0x48(%rbx), %rsi
movq 0x628(%rsi), %rdi
addq $0x50, %rsi
pushq $0xc
popq %rdx
callq 0x38e8
movq 0x48(%rbx), %rdi
xorl %esi, %esi
callq 0x2b82
jmp 0x32f8
movq %rbx, %rdi
callq 0x30e8
jmp 0x32f8
incl 0x644(%rdi)
movl %r14d, %r13d
xorl %esi, %esi
callq 0x2b82
addl 0x3c(%rbx), %r15d
movl %r15d, 0x3c(%rbx)
movq 0x48(%rbx), %rax
movl %r15d, 0x50(%rax)
leaq -0x38(%rbp), %r14
jmp 0x32f8
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq