This repository has been archived by the owner on Dec 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
slide_window.c
323 lines (288 loc) · 9.47 KB
/
slide_window.c
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#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
#define max_wait_num 1000
/*
* The following definitions are just as NetRiver instructions.
*/
typedef enum { data, ack, nak } frame_kind;
typedef struct frame_head {
frame_kind kind;
unsigned int seq;
unsigned int ack;
unsigned char data[100];
};
typedef struct frame {
frame_head head;
unsigned int size;
};
/*
* stop and wait
*/
int stud_slide_window_stop_and_wait(char *pBuffer, int bufferSize, UINT8
messageType) {
// two queues for window and waiting
static frame *window[WINDOW_SIZE_STOP_WAIT] = { 0 };
static frame *waiting[max_wait_num] = { 0 };
static int win_begin = 0, win_end = 0, wait_begin = 0, wait_end = 0;
static int win_num = 0, wait_num = 0;
// when send
if (messageType == MSG_TYPE_SEND) {
// push pBuffer into waiting queue
// allocate space for frame
waiting[wait_end] = (frame *)malloc(sizeof (frame));
// copy pBuffer to waiting queue
memcpy(waiting[wait_end], (frame *)pBuffer, sizeof (frame));
waiting[wait_end]->size = (unsigned int)bufferSize;
wait_end = (wait_end + 1) % max_wait_num;
wait_num++;
// when window is not full
if (win_num < WINDOW_SIZE_STOP_WAIT) {
// pop one frame from waiting queue
frame *temp = waiting[wait_begin];
wait_begin = (wait_begin + 1) % max_wait_num;
wait_num--;
// push the frame into window
window[win_end] = temp;
win_end = (win_end + 1) % WINDOW_SIZE_STOP_WAIT;
win_num++;
// try to send
SendFRAMEPacket((unsigned char *)temp, temp->size);
}
// when window is full, no more needs to be done
}
// when receive
else if (messageType == MSG_TYPE_RECEIVE) {
frame *temp = (frame *)pBuffer;
frame_kind kind = (frame_kind)ntohl((unsigned long)temp->head.kind);
// check kind
if (kind == nak) {
// when nak, first find which frame needs to be resent
int cursor = win_begin;
unsigned int seq = ntohl(temp->head.ack);
while (seq != ntohl(window[cursor]->head.seq))
cursor = (cursor + 1) % WINDOW_SIZE_STOP_WAIT;
// then resend it
SendFRAMEPacket((unsigned char *)window[cursor],
window[cursor]->size);
} else if (kind == ack) {
// when ack, close windows before or equal it
unsigned int ack = ntohl(temp->head.ack);
while (ntohl(window[win_begin]->head.seq) <= ack) {
// close a window
free(window[win_begin]);
win_begin = (win_begin + 1) % WINDOW_SIZE_STOP_WAIT;
win_num--;
// when waiting queue is not empty, then pop one and push into window
if (wait_num > 0) {
window[win_end] = waiting[wait_begin];
// try to send it
SendFRAMEPacket((unsigned char *)window[win_end],
window[win_end]->size);
wait_begin = (wait_begin + 1) % max_wait_num;
wait_num--;
win_end = (win_end + 1) % WINDOW_SIZE_STOP_WAIT;
win_num++;
}
}
}
}
// when time out
else if (messageType == MSG_TYPE_TIMEOUT) {
UINT32 temp = ntohl(*((UINT32 *)pBuffer));
int cursor = win_begin;
// resend all frames after MSG_TYPE_TIMEOUT
if (temp <= window[cursor]->head.seq)
SendFRAMEPacket((unsigned char *)window[cursor],
window[cursor]->size);
cursor = (cursor + 1) % WINDOW_SIZE_STOP_WAIT;
while (cursor != win_end) {
if (temp <= window[cursor]->head.seq)
SendFRAMEPacket((unsigned char *)window[cursor],
window[cursor]->size);
cursor = (cursor + 1) % WINDOW_SIZE_STOP_WAIT;
}
}
return 0;
}
/*
* Almost same as stop-wait except WINDOW_SIZE_BACK_N_FRAME.
*/
/*
* back n frame
*/
int stud_slide_window_back_n_frame(char *pBuffer, int bufferSize, UINT8
messageType) {
// two queues for window and waiting
static frame *window[WINDOW_SIZE_BACK_N_FRAME] = { 0 };
static frame *waiting[max_wait_num] = { 0 };
static int win_begin = 0, win_end = 0, wait_begin = 0, wait_end = 0;
static int win_num = 0, wait_num = 0;
// when send
if (messageType == MSG_TYPE_SEND) {
// push pBuffer into waiting queue
// allocate space for frame
waiting[wait_end] = (frame *)malloc(sizeof (frame));
// copy pBuffer to waiting queue
memcpy(waiting[wait_end], (frame *)pBuffer, sizeof (frame));
waiting[wait_end]->size = (unsigned int)bufferSize;
wait_end = (wait_end + 1) % max_wait_num;
wait_num++;
// when window is not full
if (win_num < WINDOW_SIZE_BACK_N_FRAME) {
// pop one frame from waiting queue
frame *temp = waiting[wait_begin];
wait_begin = (wait_begin + 1) % max_wait_num;
wait_num--;
// push the frame into window
window[win_end] = temp;
win_end = (win_end + 1) % WINDOW_SIZE_BACK_N_FRAME;
win_num++;
// try to send
SendFRAMEPacket((unsigned char *)temp, temp->size);
}
// when window is full, no more needs to be done
}
// when receive
else if (messageType == MSG_TYPE_RECEIVE) {
frame *temp = (frame *)pBuffer;
frame_kind kind = (frame_kind)ntohl((unsigned long)temp->head.kind);
// check kind
if (kind == nak) {
// when nak, first find which frame needs to be resent
int cursor = win_begin;
unsigned int seq = ntohl(temp->head.ack);
while (seq != ntohl(window[cursor]->head.seq))
cursor = (cursor + 1) % WINDOW_SIZE_BACK_N_FRAME;
// then resend it
SendFRAMEPacket((unsigned char *)window[cursor],
window[cursor]->size);
} else if (kind == ack) {
// when ack, close windows before or equal it
unsigned int ack = ntohl(temp->head.ack);
while (ntohl(window[win_begin]->head.seq) <= ack) {
// close a window
free(window[win_begin]);
win_begin = (win_begin + 1) % WINDOW_SIZE_BACK_N_FRAME;
win_num--;
// when waiting queue is not empty, then pop one and push into window
if (wait_num > 0) {
window[win_end] = waiting[wait_begin];
// try to send it
SendFRAMEPacket((unsigned char *)window[win_end],
window[win_end]->size);
wait_begin = (wait_begin + 1) % max_wait_num;
wait_num--;
win_end = (win_end + 1) % WINDOW_SIZE_BACK_N_FRAME;
win_num++;
}
}
}
}
// when time out
else if (messageType == MSG_TYPE_TIMEOUT) {
UINT32 temp = ntohl(*((UINT32 *)pBuffer));
int cursor = win_begin;
// resend all frames after MSG_TYPE_TIMEOUT
// here is where I found the bug of lab system
SendFRAMEPacket((unsigned char *)window[cursor], window[cursor]->size);
cursor = (cursor + 1) % WINDOW_SIZE_BACK_N_FRAME;
while (cursor != win_end) {
SendFRAMEPacket((unsigned char *)window[cursor],
window[cursor]->size);
cursor = (cursor + 1) % WINDOW_SIZE_BACK_N_FRAME;
}
}
return 0;
}
/*
* Almost same as back-n-frame.
* The only difference is that back-n-frame has TIMEOUT, but choice-frame-resend has nak.
*/
/*
* choice frame resend
*/
int stud_slide_window_choice_frame_resend(char *pBuffer, int bufferSize, UINT8
messageType) {
// two queues for window and waiting
static frame *window[WINDOW_SIZE_BACK_N_FRAME] = { 0 };
static frame *waiting[max_wait_num] = { 0 };
static int win_begin = 0, win_end = 0, wait_begin = 0, wait_end = 0;
static int win_num = 0, wait_num = 0;
// when send
if (messageType == MSG_TYPE_SEND) {
// push pBuffer into waiting queue
// allocate space for frame
waiting[wait_end] = (frame *)malloc(sizeof (frame));
// copy pBuffer to waiting queue
memcpy(waiting[wait_end], (frame *)pBuffer, sizeof (frame));
waiting[wait_end]->size = (unsigned int)bufferSize;
wait_end = (wait_end + 1) % max_wait_num;
wait_num++;
// when window is not full
if (win_num < WINDOW_SIZE_BACK_N_FRAME) {
// pop one frame from waiting queue
frame *temp = waiting[wait_begin];
wait_begin = (wait_begin + 1) % max_wait_num;
wait_num--;
// push the frame into window
window[win_end] = temp;
win_end = (win_end + 1) % WINDOW_SIZE_BACK_N_FRAME;
win_num++;
// try to send
SendFRAMEPacket((unsigned char *)temp, temp->size);
}
// when window is full, no more needs to be done
}
// when receive
else if (messageType == MSG_TYPE_RECEIVE) {
frame *temp = (frame *)pBuffer;
frame_kind kind = (frame_kind)ntohl((unsigned long)temp->head.kind);
// check kind
if (kind == nak) {
// when nak, first find which frame needs to be resent
int cursor = win_begin;
unsigned int seq = ntohl(temp->head.ack);
while (seq != ntohl(window[cursor]->head.seq))
cursor = (cursor + 1) % WINDOW_SIZE_BACK_N_FRAME;
// then resend it
SendFRAMEPacket((unsigned char *)window[cursor],
window[cursor]->size);
} else if (kind == ack) {
// when ack, close windows before or equal it
unsigned int ack = ntohl(temp->head.ack);
while (ntohl(window[win_begin]->head.seq) <= ack) {
// close a window
free(window[win_begin]);
win_begin = (win_begin + 1) % WINDOW_SIZE_BACK_N_FRAME;
win_num--;
// when waiting queue is not empty, then pop one and push into window
if (wait_num > 0) {
window[win_end] = waiting[wait_begin];
// try to send it
SendFRAMEPacket((unsigned char *)window[win_end],
window[win_end]->size);
wait_begin = (wait_begin + 1) % max_wait_num;
wait_num--;
win_end = (win_end + 1) % WINDOW_SIZE_BACK_N_FRAME;
win_num++;
}
}
}
}
// when time out
else if (messageType == MSG_TYPE_TIMEOUT) {
UINT32 temp = ntohl(*((UINT32 *)pBuffer));
int cursor = win_begin;
// resend all frames after MSG_TYPE_TIMEOUT
SendFRAMEPacket((unsigned char *)window[cursor], window[cursor]->size);
cursor = (cursor + 1) % WINDOW_SIZE_BACK_N_FRAME;
while (cursor != win_end) {
SendFRAMEPacket((unsigned char *)window[cursor],
window[cursor]->size);
cursor = (cursor + 1) % WINDOW_SIZE_BACK_N_FRAME;
}
}
return 0;
}