-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileIO.c
371 lines (341 loc) · 11 KB
/
FileIO.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*------------------------------------------------------------------------------
-- SOURCE FILE: FileIO.c Contains functions relating to File IO for
-- the 3980 wireless protocol.
--
-- PROGRAM: Dean and the Rockets' Wireless Protocol Testing and Evaluation
-- Facilitator
--
-- FUNCTIONS:
-- FRAME CreateFrame(HWND hWnd, PBYTE psBuf, DWORD dwLength);
-- VOID OpenFileTransmit(HWND hWnd);
-- VOID CloseFileTransmit(HWND hWnd);
-- BOOL OpenFileReceive(HWND hWnd);
-- VOID CloseFileReceive(HWND hWnd);
-- VOID WriteToFile(HWND hWnd);
-- VOID ReadFromFile(HWND hWnd);
-- FRAME CreateNullFrame(HWND hWnd);
--
--
-- DATE: Dec 2, 2010
--
-- REVISIONS:
--
-- DESIGNER: Dean Morin/Daniel Wright/Ian Lee
--
-- PROGRAMMER: Daniel Wright/Ian Lee
--
-- NOTES: Functions relating to File IO
------------------------------------------------------------------------------*/
#include "FileIO.h"
/*------------------------------------------------------------------------------
-- FUNCTION: CreateFrame
--
-- DATE: Nov 24, 2010
--
-- REVISIONS:
--
-- DESIGNER: Ian Lee
--
-- PROGRAMMER: Ian Lee
--
-- INTERFACE: CreateFrame(HWND hWnd, PBYTE psBuf, DWORD dwLength)
-- hWnd - a handle to the window
-- PBYTE psBuf - data from file
-- DWORD dwLength - length of data from file
--
-- RETURNS: FRAME - the created frame
--
-- NOTES: Creates a frame to be put into the FTP buffer
--
------------------------------------------------------------------------------*/
FRAME CreateFrame(HWND hWnd, PBYTE psBuf, DWORD dwLength){
DWORD i;
FRAME myFrame;
PWNDDATA pwd = NULL;
pwd = (PWNDDATA) GetWindowLongPtr(hWnd, 0);
myFrame.soh = 0x1;
myFrame.sequence = pwd->TxSequenceNumber;
pwd->TxSequenceNumber= (pwd->TxSequenceNumber==0)?1:0;
myFrame.length = (SHORT)dwLength;
for (i = 0; i<dwLength;i++) {
myFrame.payload[i] = *(psBuf++);
}
while(i<MAX_PAYLOAD_SIZE){
myFrame.payload[i++] =0;
}
myFrame.crc =0;
myFrame.crc = crcFast((PBYTE)&myFrame,FRAME_SIZE - sizeof(crc));
if(crcFast((PBYTE)&myFrame,FRAME_SIZE)!=0){
DISPLAY_ERROR("Failure Creating Frame");
}
return myFrame;
}
/*------------------------------------------------------------------------------
-- FUNCTION: OpenFileTransmit
--
-- DATE: Dec 2, 2010
--
-- REVISIONS:
--
-- DESIGNER: Daniel Wright
--
-- PROGRAMMER: Daniel Wright
--
-- INTERFACE: OpenFileTransmit(HWND hWnd)
-- hWnd - a handle to the window
--
-- RETURNS: VOID.
--
-- NOTES: Uses GetOpenFileName to get the name of the file to be
-- transmitted and opens it.
------------------------------------------------------------------------------*/
VOID OpenFileTransmit(HWND hWnd){
PWNDDATA pwd = {0};
OPENFILENAME ofn;
TCHAR szFile[100] = {0};
pwd = (PWNDDATA)GetWindowLongPtr(hWnd, 0);
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFile = szFile;
ofn.lpstrFile[0] = TEXT('\0');
ofn.lpstrFilter = TEXT("All\0*.*\0");
ofn.nMaxFile = sizeof(szFile);
if(GetOpenFileName(&ofn) == 0){
return;
}
pwd->hFileTransmit =CreateFile(ofn.lpstrFile, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
pwd->NumOfReads = 0;
PostMessage(hWnd, WM_FILLFTPBUF, 0, 0);
pwd->TxSequenceNumber = 0;
}
/*------------------------------------------------------------------------------
-- FUNCTION: CloseFileTransmit
--
-- DATE: Dec 2, 2010
--
-- REVISIONS:
--
-- DESIGNER: Daniel Wright
--
-- PROGRAMMER: Daniel Wright
--
-- INTERFACE: CloseFileTransmit(HWND hWnd)
-- hWnd - a handle to the window
--
-- RETURNS: VOID.
--
-- NOTES: Closes the file handle to the file to be transmitted
-- and sets it to null. Sends message to fill File to Port
-- Buffer.
------------------------------------------------------------------------------*/
VOID CloseFileTransmit(HWND hWnd){
PWNDDATA pwd = {0};
pwd = (PWNDDATA)GetWindowLongPtr(hWnd, 0);
if(pwd->hFileTransmit != NULL){
if(!CloseHandle(pwd->hFileTransmit)){
DISPLAY_ERROR("Failed to close Transmit File");
}
pwd->hFileTransmit = NULL;
pwd->NumOfReads = 0;
}
}
/*------------------------------------------------------------------------------
-- FUNCTION: OpenFileReceive
--
-- DATE: Dec 2, 2010
--
-- REVISIONS:
--
-- DESIGNER: Daniel Wright
--
-- PROGRAMMER: Daniel Wright
--
-- INTERFACE: OpenFileReceive(HWND hWnd)
-- hWnd - a handle to the window
--
-- RETURNS: VOID.
--
-- NOTES: Uses GetSaveFileName to get the name of the file for data
-- to be written to and opens it.
------------------------------------------------------------------------------*/
BOOL OpenFileReceive(HWND hWnd){
PWNDDATA pwd = {0};
OPENFILENAME ofn;
TCHAR szFile[100] = {0};
pwd = (PWNDDATA)GetWindowLongPtr(hWnd, 0);
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFile = szFile;
ofn.lpstrFile[0] = TEXT('\0');
ofn.lpstrFilter = TEXT("All\0*.*\0");
ofn.nMaxFile = sizeof(szFile);
if(GetSaveFileName(&ofn) == 0){
return FALSE;
}
pwd->hFileReceive = CreateFile(ofn.lpstrFile, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
SetWindowLongPtr(hWnd, 0, (LONG_PTR) pwd);
return TRUE;
}
/*------------------------------------------------------------------------------
-- FUNCTION: CloseFileReceive
--
-- DATE: Dec 2, 2010
--
-- REVISIONS:
--
-- DESIGNER: Daniel Wright
--
-- PROGRAMMER: Daniel Wright
--
-- INTERFACE: CloseFileReceive(HWND hWnd)
-- hWnd - a handle to the window
--
-- RETURNS: VOID.
--
-- NOTES: Closes the handle to the receive file and sets it to null.
------------------------------------------------------------------------------*/
VOID CloseFileReceive(HWND hWnd){
PWNDDATA pwd = {0};
pwd = (PWNDDATA)GetWindowLongPtr(hWnd, 0);
if(pwd->hFileReceive){
if(!CloseHandle(pwd->hFileReceive)){
DISPLAY_ERROR("Failed to close Receive File");
}
pwd->hFileReceive = NULL;
}
}
/*------------------------------------------------------------------------------
-- FUNCTION: WriteToFile
--
-- DATE: Dec 2, 2010
--
-- REVISIONS:
--
-- DESIGNER: Daniel Wright
--
-- PROGRAMMER: Daniel Wright
--
-- INTERFACE: WriteToFile(HWND hWnd)
-- hWnd - a handle to the window
--
-- RETURNS: VOID.
--
-- NOTES: Removes frames from the Port to File Queue and writes them
-- to the receive file until the queue is empty.
------------------------------------------------------------------------------*/
VOID WriteToFile(HWND hWnd){
PWNDDATA pwd = {0};
DWORD dwBytesWritten = 0;
PFRAME tempFrame = {0};
HANDLE hMutex = {0};
pwd = (PWNDDATA)GetWindowLongPtr(hWnd, 0);
while(pwd->PTFQueueSize != 0){
hMutex = CreateMutex(NULL, FALSE, TEXT("PTFMutex"));
WaitForSingleObject(hMutex,INFINITE);
tempFrame = RemoveFromFrameQueue(&pwd->PTFBuffHead, 1);
ReleaseMutex(hMutex);
if(tempFrame->length != 0){
DisplayFrameInfo(hWnd, *tempFrame);
pwd->NumOfFrames++;
SetWindowLongPtr(hWnd, 0, (LONG_PTR) pwd);
}
if(!WriteFile(pwd->hFileReceive, tempFrame->payload, tempFrame->length, &dwBytesWritten, NULL)){
DISPLAY_ERROR("Failed to write to file");
} else {
pwd->PTFQueueSize--;
}
if(tempFrame->length != MAX_PAYLOAD_SIZE ){
CloseFileReceive(hWnd);
MessageBox(hWnd, TEXT("Finished receiving file"),
TEXT("Transfer Complete"), MB_OK);
}
}
}
/*------------------------------------------------------------------------------
-- FUNCTION: ReadFromFile
--
-- DATE: Dec 2, 2010
--
-- REVISIONS:
--
-- DESIGNER: Daniel Wright
--
-- PROGRAMMER: Daniel Wright
--
-- INTERFACE: ReadFromFile(HWND hWnd)
-- hWnd - a handle to the window
--
-- RETURNS: VOID.
--
-- NOTES: While the File to Port queue is not full, reads in increments
-- of 1019 bytes from the file to be transmitted, calls CreateFrame
-- to frame them and adds them to the File to Port queue.
------------------------------------------------------------------------------*/
VOID ReadFromFile(HWND hWnd){
PWNDDATA pwd = {0};
DWORD dwBytesRead = 0;
DWORD dwBytesWritten = 0;
DWORD dwSizeOfFile = 0;
BOOL eof = FALSE;
FRAME frame;
HANDLE hMutex = {0};
int i;
PBYTE ReadBuffer = (PBYTE) malloc(sizeof(BYTE) *1019);
pwd = (PWNDDATA)GetWindowLongPtr(hWnd, 0);
if (!(dwSizeOfFile = GetFileSize(pwd->hFileTransmit, NULL))) {
return;
}
while(pwd->FTPQueueSize < FULL_BUFFER && pwd->hFileTransmit != NULL){
if((i =dwSizeOfFile - ((pwd->NumOfReads) * 1019)) > 0){
if(!ReadFile(pwd->hFileTransmit, ReadBuffer, 1019, &dwBytesRead, NULL)){
DISPLAY_ERROR("Failed to read from file");
}
++pwd->NumOfReads;
frame = CreateFrame(hWnd, ReadBuffer, dwBytesRead);
} else if((dwSizeOfFile - ((pwd->NumOfReads) * 1019)) == 0){
CloseFileTransmit(hWnd);
++pwd->NumOfReads;
frame = CreateNullFrame(hWnd);
MessageBox(hWnd, TEXT("Transmit File Buffering Complete"),
TEXT("File Read Complete"), MB_OK);
} else {
CloseFileTransmit(hWnd);
MessageBox(hWnd, TEXT("Transmit File Buffering Complete"),
TEXT("File Read Complete"), MB_OK);
return;
}
hMutex = CreateMutex(NULL, FALSE, TEXT("FTPMutex"));
WaitForSingleObject(hMutex,INFINITE);
AddToFrameQueue(&pwd->FTPBuffHead, &pwd->FTPBuffTail, frame);
pwd->FTPQueueSize+=1;
ReleaseMutex(hMutex);
}
}
/*------------------------------------------------------------------------------
-- FUNCTION: CreateNullFrame
--
-- DATE: Dec 2, 2010
--
-- REVISIONS:
--
-- DESIGNER: Daniel Wright
--
-- PROGRAMMER: Daniel Wright
--
-- INTERFACE: CreateNullFrame(HWND hWnd)
-- hWnd - a handle to the window
--
-- RETURNS: VOID.
--
-- NOTES: Creates a frame with the payload consisting entirely of nulls,
-- to be send at the end of a file if no nulls were sent in
-- previous frames.
------------------------------------------------------------------------------*/
FRAME CreateNullFrame(HWND hWnd){
FRAME nullFrame = CreateFrame(hWnd, 0, 0);
return nullFrame;
}