// File: /workspace/github_cpp_makefile_star2_O3/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 0x48(%rdi), %rax
cmpb $0x0, 0x620(%rax)
jne 0x3633
movq %rdi, %rbx
leaq -0x38(%rbp), %r14
movq $0x0, -0x38(%rbp)
movq $0x17d7840, -0x30(%rbp) # imm = 0x17D7840
movq %r14, %rdi
movq %r14, %rsi
callq 0x20f0
cmpl $-0x1, %eax
jne 0x34ab
callq 0x2050
cmpl $0x4, (%rax)
je 0x3491
movq 0x48(%rbx), %rdi
cmpb $0x1, 0x38(%rbx)
jne 0x35a6
movl (%rdi), %ecx
movl 0x8(%rbx), %edx
movl 0x40(%rbx), %eax
movl %ecx, %esi
subl %eax, %esi
cmpl %edx, %esi
jae 0x35fb
cmpb $0x1, 0x39(%rbx)
jne 0x34e3
movl %ecx, 0x3c(%rbx)
movl %ecx, 0x50(%rdi)
movb $0x0, 0x39(%rbx)
movl 0x8(%rbx), %edx
movl 0x40(%rbx), %eax
movl 0x3c(%rbx), %r15d
movl 0x10(%rdi), %esi
subl %r15d, %esi
addl %ecx, %esi
movl %eax, %ecx
subl %r15d, %ecx
addl %edx, %ecx
cmpl %esi, %ecx
cmovael %esi, %ecx
movl $0x5b4, %edx            # imm = 0x5B4
cmpl %edx, %ecx
movl $0x5b4, %r13d           # imm = 0x5B4
cmovbl %ecx, %r13d
testl %ecx, %ecx
je 0x35a6
leaq 0x5c(%rdi), %r12
subl %eax, %r15d
addq (%rbx), %r15
movl $0x1, %esi
callq 0x2c48
movl %r13d, %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x21a0
movq 0x48(%rbx), %rdx
movq 0x628(%rdx), %rdi
addq $0x50, %rdx
leal 0xc(%r13), %r15d
xorl %esi, %esi
movl %r15d, %ecx
callq 0x3a40
callq 0x2060
movq %rax, %r12
movq 0x48(%rbx), %rdi
addq $0x20, %rdi
movl 0x3c(%rbx), %eax
addl %r13d, %eax
movl %eax, -0x38(%rbp)
movq %r14, %rsi
callq 0x36d6
movq %r12, (%rax)
movq 0x48(%rbx), %rsi
movq 0x628(%rsi), %rdi
addq $0x50, %rsi
movq %r15, %rdx
callq 0x3a1a
movq 0x48(%rbx), %rdi
movl 0xc(%rdi), %eax
cmpl $0x2, %eax
je 0x3605
testl %eax, %eax
jne 0x360b
incl 0x640(%rdi)
jmp 0x360b
xorl %esi, %esi
callq 0x2c48
movq 0x48(%rbx), %rdi
callq 0x2c96
testb %al, %al
je 0x3622
movq 0x48(%rbx), %rdx
movq 0x628(%rdx), %rdi
addq $0x50, %rdx
xorl %esi, %esi
movl $0xc, %ecx
callq 0x3a40
movq 0x48(%rbx), %rsi
movq 0x628(%rsi), %rdi
addq $0x50, %rsi
movl $0xc, %edx
callq 0x3a1a
movq 0x48(%rbx), %rdi
xorl %esi, %esi
callq 0x2c5e
jmp 0x3622
movq %rbx, %rdi
callq 0x3200
jmp 0x3622
incl 0x644(%rdi)
xorl %esi, %esi
callq 0x2c5e
addl 0x3c(%rbx), %r13d
movl %r13d, 0x3c(%rbx)
movq 0x48(%rbx), %rax
movl %r13d, 0x50(%rax)
movq 0x48(%rbx), %rax
cmpb $0x0, 0x620(%rax)
je 0x3481
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq