// File: /workspace/github_cpp_makefile_star2_O1/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 0x3690
movq %rdi, %rbx
leaq -0x40(%rbp), %r14
movq $0x0, -0x40(%rbp)
movq $0x17d7840, -0x38(%rbp) # imm = 0x17D7840
movq %r14, %rdi
movq %r14, %rsi
callq 0x20f0
cmpl $-0x1, %eax
jne 0x34f9
callq 0x2050
cmpl $0x4, (%rax)
je 0x34df
cmpb $0x1, 0x38(%rbx)
jne 0x3628
movq 0x48(%rbx), %rdi
movl (%rdi), %eax
movl %eax, %ecx
subl 0x40(%rbx), %ecx
cmpl 0x8(%rbx), %ecx
jae 0x35fc
cmpb $0x1, 0x39(%rbx)
jne 0x3527
movl %eax, 0x3c(%rbx)
movl %eax, 0x50(%rdi)
movb $0x0, 0x39(%rbx)
movl 0x3c(%rbx), %r15d
movl 0x40(%rbx), %eax
movl 0x10(%rdi), %ecx
subl %r15d, %ecx
addl (%rdi), %ecx
movl %eax, %r13d
subl %r15d, %r13d
addl 0x8(%rbx), %r13d
cmpl %ecx, %r13d
cmovael %ecx, %r13d
movl $0x5b4, %ecx            # imm = 0x5B4
cmpl %ecx, %r13d
movl $0x5b4, %r12d           # imm = 0x5B4
cmovbl %r13d, %r12d
testl %r13d, %r13d
je 0x3628
leaq 0x5c(%rdi), %rcx
movq %rcx, -0x30(%rbp)
subl %eax, %r15d
addq (%rbx), %r15
movl $0x1, %esi
callq 0x2c68
movl %r12d, %edx
movq -0x30(%rbp), %rdi
movq %r15, %rsi
callq 0x21a0
movq 0x48(%rbx), %rdx
movq 0x628(%rdx), %rdi
addq $0x50, %rdx
leal 0xc(%r12), %ecx
movq %rcx, -0x30(%rbp)
xorl %esi, %esi
callq 0x3ab8
callq 0x2060
movq %rax, %r15
movq 0x48(%rbx), %rdi
addq $0x20, %rdi
movl 0x3c(%rbx), %eax
addl %r12d, %eax
movl %eax, -0x40(%rbp)
movq %r14, %rsi
callq 0x3744
movq %r15, (%rax)
movq 0x48(%rbx), %rsi
movq 0x628(%rsi), %rdi
addq $0x50, %rsi
movq -0x30(%rbp), %rdx
callq 0x3a92
movq 0x48(%rbx), %rdi
movl 0xc(%rdi), %eax
cmpl $0x2, %eax
je 0x3606
testl %eax, %eax
jne 0x360c
incl 0x640(%rdi)
jmp 0x360c
movq %rbx, %rdi
callq 0x324e
jmp 0x367f
incl 0x644(%rdi)
xorl %esi, %esi
callq 0x2c7e
addl 0x3c(%rbx), %r12d
movl %r12d, 0x3c(%rbx)
movq 0x48(%rbx), %rax
movl %r12d, 0x50(%rax)
testl %r13d, %r13d
jne 0x367f
movq 0x48(%rbx), %rdi
xorl %esi, %esi
callq 0x2c68
movq 0x48(%rbx), %rdi
callq 0x2cb6
testb %al, %al
je 0x367f
movq 0x48(%rbx), %rdx
movq 0x628(%rdx), %rdi
addq $0x50, %rdx
xorl %esi, %esi
movl $0xc, %ecx
callq 0x3ab8
movq 0x48(%rbx), %rsi
movq 0x628(%rsi), %rdi
addq $0x50, %rsi
movl $0xc, %edx
callq 0x3a92
movq 0x48(%rbx), %rdi
xorl %esi, %esi
callq 0x2c7e
movq 0x48(%rbx), %rax
cmpb $0x0, 0x620(%rax)
je 0x34cf
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop