DWORD WINAPI recvhandler(LPVOID lparam) { printwindow(); int i = (int)(LPVOID)lparam; //cout << "接收" << i << endl; clock_t finalovertime; int t = 0; while (base < buffersize) { if (sendnextseq > base + N || sendnextseq == base)//窗口已满(对方还没有发送ack)或者窗口为空 Sleep(2); message a; simplerecv(a); if (a.get_ack())//收到确认消息且序号正确 { clockstart = clock();//重置计时器 if (a.ackseq >= base)//收到正确的消息序号 { if (constatus == 0) { N += a.ackseq - base + 1;//累积确认 if (N >= ssthresh) constatus = 1;//拥塞避免 } elseif (constatus == 1) { N += (a.ackseq - base + 1) / N;//线性增长 } printwindow(); base = a.ackseq + 1; overtime = 0; t = 0; } elseif (a.ackseq <= base-1) { t++; cout << t << endl; printwindow(); if (constatus == 2) {//快速恢复 N += 1; if (N >= ssthresh) constatus = 1;//进入拥塞避免阶段 } if (t == 3)//三个冗余ack { constatus = 2; //快速重传 overtime = 1; } } } clockend = clock(); } cout << "exitpoint2" << endl; return1; }
DWORD WINAPI sendhandler(LPVOID lparam)//发线程 { int flag = 0; clock_t s = clock(); while (base < buffersize) { int i = (int)(LPVOID)lparam; if (!overtime) { int temp = base; if (base + N == sendnextseq) Sleep(40); for (; sendnextseq < base + N && sendnextseq < buffersize; sendnextseq++) { flag = 0;//正常发送
if (!overtime) { simplesend(msgsend[sendnextseq]); s = clock(); //Sleep(20); }
else { break; } simplesend(msgsend[sendnextseq]); if (temp == base) { clock_t e = clock(); if ((e - s) / CLOCKS_PER_SEC >= WAIT_TIME)//超时重发 { overtime = 1; sendnextseq++; break; } } } } if (overtime == 1)//重新发送 { if (constatus == 1 || constatus == 0) { ssthresh = N / 2; N = 1; constatus = 0;//重新进入慢启动阶段 printwindow(); } elseif (constatus == 2) {//快速重传 ssthresh = N / 2; N = ssthresh + 3; } if (flag) Sleep(10);//减少重传次数 for (int i = base; i < sendnextseq; i++) { //重新发送 if (overtime) simplesend(msgsend[i]); elsebreak; flag++; } overtime = 0;//重传标识归0 s = clock();//重置计时器 } clock_t e = clock(); if ((e - s) / CLOCKS_PER_SEC >= WAIT_TIME)//超时重发 overtime = 1; } cout << "exitpoint1" << endl; return0; }