// File: wenzhelu[P]RUDP/RUDP.cpp
// Function: RUDP::init(unsigned int, char const*, char const*, char const*)
void RUDP::init(uint window, const char *port, const char *remoteIp, const char *remotePort) {
    srand(time(nullptr));   // set random seed
    memset(buff, 0, PACKET_SIZE + HEADER_LEN);
    this->sendBase = 0;
    // make the sequence number in the buffer the same with send base
    *(uint *)(this->buff) = this->sendBase;
    this->winSize = window;
    this->status = SLOW_START;
    this->cWnd = PACKET_SIZE;
    this->close = false;
    this->RTT = 500;
    this->TimeOut = 3000;
    this->throughput = 100000000;
    this->congestNum = 0;
    this->slowStartNum = 0;
    
    this->sock = new Usock();
    this->sock->init(port, remoteIp, remotePort);
    this->sender = new Sender(this);
    this->listener = new Listener(this);
    
    th_sender = new thread(&Sender::sending, sender);
    th_listener = new thread(&Listener::recAns, listener);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x68(%rbp)
xorl %eax, %eax
movl %eax, -0x6c(%rbp)
movl %eax, %edi
callq 0x2210
movl %eax, %edi
callq 0x2230
movl -0x6c(%rbp), %esi
movq -0x68(%rbp), %rdi
addq $0x50, %rdi
movl $0x5c0, %edx            # imm = 0x5C0
callq 0x2140
movq -0x68(%rbp), %rax
movl $0x0, (%rax)
movl (%rax), %ecx
movl %ecx, 0x50(%rax)
movl -0xc(%rbp), %ecx
movl %ecx, 0x4(%rax)
movl $0x0, 0xc(%rax)
movl $0x5b4, 0x10(%rax)      # imm = 0x5B4
movb $0x0, 0x620(%rax)
movl $0x1f4, 0x14(%rax)      # imm = 0x1F4
movl $0xbb8, 0x18(%rax)      # imm = 0xBB8
movl $0x5f5e100, 0x8(%rax)   # imm = 0x5F5E100
movl $0x0, 0x644(%rax)
movl $0x0, 0x640(%rax)
movl $0x24, %edi
callq 0x2250
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x60(%rbp)
callq 0x6e30
jmp 0x44ec
movq -0x68(%rbp), %rax
movq -0x60(%rbp), %rcx
movq %rcx, 0x628(%rax)
movq 0x628(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
callq 0x6e40
movl $0x68, %edi
callq 0x2250
movq -0x68(%rbp), %rsi
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x78(%rbp)
callq 0x5520
jmp 0x4532
movq -0x68(%rbp), %rax
movq -0x78(%rbp), %rcx
movq %rcx, 0x610(%rax)
movl $0x30, %edi
callq 0x2250
movq -0x68(%rbp), %rsi
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x80(%rbp)
callq 0x4790
jmp 0x4560
movq -0x68(%rbp), %rax
movq -0x80(%rbp), %rcx
movq %rcx, 0x618(%rax)
movl $0x8, %edi
callq 0x2250
movq -0x68(%rbp), %rdx
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x88(%rbp)
leaq 0x150f(%rip), %rax      # 0x5aa0
movq %rax, -0x48(%rbp)
movq $0x0, -0x40(%rbp)
addq $0x610, %rdx            # imm = 0x610
leaq -0x48(%rbp), %rsi
callq 0x47c0
jmp 0x45af
movq -0x68(%rbp), %rax
movq -0x88(%rbp), %rcx
movq %rcx, 0x638(%rax)
movl $0x8, %edi
callq 0x2250
movq -0x68(%rbp), %rdx
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x90(%rbp)
leaq -0x1af3(%rip), %rax     # 0x2af0
movq %rax, -0x58(%rbp)
movq $0x0, -0x50(%rbp)
addq $0x618, %rdx            # imm = 0x618
leaq -0x58(%rbp), %rsi
callq 0x4880
jmp 0x4601
movq -0x68(%rbp), %rax
movq -0x90(%rbp), %rcx
movq %rcx, 0x630(%rax)
addq $0x90, %rsp
popq %rbp
retq
movq -0x60(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x2220
jmp 0x4693
movq -0x78(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x2220
jmp 0x4693
movq -0x80(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x2220
jmp 0x4693
movq -0x88(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x2220
jmp 0x4693
movq -0x90(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x2220
movq -0x30(%rbp), %rdi
callq 0x23a0
nopl (%rax)