-
Notifications
You must be signed in to change notification settings - Fork 0
/
slidingWindows.cpp
215 lines (188 loc) · 5.86 KB
/
slidingWindows.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Sliding Windows
// 12f23eddde <1800012926@pku.edu.cn> - Mar 31 2021
// - START TEMPLATE -
#include "sysinclude.h"
extern void SendFRAMEPacket(unsigned char* pData, unsigned int len);
#define WINDOW_SIZE_STOP_WAIT 1
#define WINDOW_SIZE_BACK_N_FRAME 4
typedef enum {DATA, ACK, NAK} FrameType;
struct FrameHeader{
FrameType type;
unsigned int seq;
unsigned int ack;
unsigned char data[100];//数据
};
struct Frame {
FrameHeader head; //帧头
unsigned int size; //数据的大小
};
// - END TEMPLATE -
#include <queue>
#include <deque>
#include <cstdlib>
using namespace std;
/*
* 停等协议测试函数
*/
int stud_slide_window_stop_and_wait(char *pBuffer, int bufferSize, UINT8 messageType){
static queue <Frame> sendList; // 发送队列
static bool sendNext = true; // 能否发送
Frame f; // Netriver的远古编译器对语法有些神秘限制
printf("[STOPWAIT] bufferSize=%d, messageType=%d\n");
switch (messageType){
case MSG_TYPE_SEND:
// 先把帧存进队列
memcpy(&f, pBuffer, bufferSize);
f.size = bufferSize;
sendList.push(f);
printf("[STOPWAIT] SEND push packet\n");
if(sendNext){ // 可以发送包
// 发送队首包
f = sendList.front();
SendFRAMEPacket((unsigned char *)(&f), (unsigned int)f.size);
sendNext = false; // 在ACK前不发送
printf("[STOPWAIT] SEND send packet");
}
break;
case MSG_TYPE_RECEIVE:
sendList.pop(); // 移除已经发送的帧
sendNext = true; // 可以继续发送帧
if(!sendList.empty()){ // 如果队列里有帧,在这里发送
f = sendList.front();
SendFRAMEPacket((unsigned char *)&f, (unsigned int)f.size);
sendNext = false; // 在ACK前不发送
printf("[STOPWAIT] RECEIVE send packet");
}
break;
case MSG_TYPE_TIMEOUT:
// 发送队首包
f = sendList.front();
SendFRAMEPacket((unsigned char *)&f, (unsigned int)f.size);
sendNext = false; // 在ACK前不发送
printf("[STOPWAIT] TIMEOUT send packet\n");
break;
default:
printf("[STOPWAIT] message type not specfied: %d\n", messageType);
break;
}
return 0;
}
/*
* 回退n帧测试函数
*/
int stud_slide_window_back_n_frame(char *pBuffer, int bufferSize, UINT8 messageType)
{
static deque <Frame> sendList; // 发送队列
Frame f; // Netriver的远古编译器对语法有些神秘限制
int ackedFrameSeq;
int pop_count;
printf("[STOPWAIT] bufferSize=%d, messageType=%d\n");
switch (messageType){
case MSG_TYPE_SEND:
// 先把帧存进队列
memcpy(&f, pBuffer, bufferSize);
f.size = bufferSize;
sendList.push_back(f);
printf("[STOPWAIT] SEND push packet seq=%d\n", ntohl(f.head.seq));
if(sendList.size() <= WINDOW_SIZE_BACK_N_FRAME){ // 窗口没有满
// 可以发送包
SendFRAMEPacket((unsigned char *)(&f), (unsigned int)f.size);
printf("[STOPWAIT] SEND send packet seq=%d\n", ntohl(f.head.seq));
}
break;
case MSG_TYPE_RECEIVE:
memcpy(&f, pBuffer, bufferSize);
ackedFrameSeq = ntohl(f.head.ack);
pop_count = 0;
while(!sendList.empty() && ntohl(sendList.front().head.seq) <= ackedFrameSeq){
sendList.pop_front(); // 移除已经发送的帧
pop_count += 1;
}
while(!sendList.empty() && pop_count){
// sendList: * | * | 4-2 | 4-1 |
f = sendList[WINDOW_SIZE_BACK_N_FRAME - pop_count];
pop_count -= 1;
SendFRAMEPacket((unsigned char *)&f, (unsigned int)f.size);
printf("[STOPWAIT] RECEIVE send packet seq=%d\n", ntohl(f.head.seq));
}
break;
case MSG_TYPE_TIMEOUT:
// 发送窗口内所有的帧
for(int i = 0; i < WINDOW_SIZE_BACK_N_FRAME; i++){
f = sendList[i];
SendFRAMEPacket((unsigned char *)&f, (unsigned int)f.size);
printf("[STOPWAIT] TIMEOUT send packet seq=%d\n", ntohl(f.head.seq));
}
break;
default:
printf("[STOPWAIT] message type not specfied: %d\n", messageType);
break;
}
return 0;
}
/*
* 选择性重传测试函数
*/
int stud_slide_window_choice_frame_resend(char *pBuffer, int bufferSize, UINT8 messageType)
{
static deque <Frame> sendList; // 发送队列
Frame f; // Netriver的远古编译器对语法有些神秘限制
int ackedFrameSeq;
int pop_count;
FrameType type;
printf("[STOPWAIT] bufferSize=%d, messageType=%d\n");
switch (messageType){
case MSG_TYPE_SEND:
// 先把帧存进队列
memcpy(&f, pBuffer, bufferSize);
f.size = bufferSize;
sendList.push_back(f);
printf("[STOPWAIT] SEND push packet seq=%d\n", ntohl(f.head.seq));
if(sendList.size() <= WINDOW_SIZE_BACK_N_FRAME){ // 窗口没有满
// 可以发送包
SendFRAMEPacket((unsigned char *)(&f), (unsigned int)f.size);
printf("[STOPWAIT] SEND send packet seq=%d\n", ntohl(f.head.seq));
}
break;
case MSG_TYPE_RECEIVE:
memcpy(&f, pBuffer, bufferSize);
ackedFrameSeq = ntohl(f.head.ack);
type = (FrameType) ntohl(f.head.type);
if(type == ACK){
pop_count = 0;
while(!sendList.empty() && ntohl(sendList.front().head.seq) <= ackedFrameSeq){
sendList.pop_front(); // 移除已经发送的帧
pop_count += 1;
}
while(!sendList.empty() && pop_count){
// sendList: * | * | 4-2 | 4-1 |
f = sendList[WINDOW_SIZE_BACK_N_FRAME - pop_count];
pop_count -= 1;
SendFRAMEPacket((unsigned char *)&f, (unsigned int)f.size);
printf("[STOPWAIT] RECEIVE send packet seq=%d\n", ntohl(f.head.seq));
}
} else {
// 选择重传
for(int i = 0; i < sendList.size(); i++){
if(ntohl(sendList[i].head.seq) == ackedFrameSeq){
f = sendList[i];
SendFRAMEPacket((unsigned char *)&f, (unsigned int)f.size);
printf("[STOPWAIT] RECEIVE send packet seq=%d\n", ntohl(f.head.seq));
}
}
}
break;
case MSG_TYPE_TIMEOUT:
// 发送窗口内所有的帧
for(int i = 0; i < WINDOW_SIZE_BACK_N_FRAME; i++){
f = sendList[i];
SendFRAMEPacket((unsigned char *)&f, (unsigned int)f.size);
printf("[STOPWAIT] TIMEOUT send packet seq=%d\n", ntohl(f.head.seq));
}
break;
default:
printf("[STOPWAIT] message type not specfied: %d\n", messageType);
break;
}
return 0;
}