-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathTRANSFER.C
857 lines (664 loc) · 26.3 KB
/
TRANSFER.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
/*-----------------------------------------------------------------------------
This is a part of the Microsoft Source Code Samples.
Copyright (C) 1995 Microsoft Corporation.
All rights reserved.
This source code is only intended as a supplement to
Microsoft Development Tools and/or WinHelp documentation.
See these sources for detailed information regarding the
Microsoft samples programs.
MODULE: Transfer.c
PURPOSE: Transfer a file (receive or send).
FUNCTIONS:
TransferRepeatCreate - Preps program for a repeated send
TransferRepeatDestroy - Completes a repeated send
TransferRepeatDo - Sends the data to the writer thread
TransferFileTextStart - Preps program for a text file send
TransferFileTextEnd - Completes a file transfer
TransferThreadProc - Thread procedure to do actual transfer
TransferFileText - Preps program for a text file send
ReceiveFileText - Preps program for a text file capture
OpenTheFile - Opens a file
CreateTheFile - Creates a file
GetTransferSizes - Determines transfer metrics from file and buffer sizes
ShowTransferStatistics - Displays transfer stats
CheckForMessges - Peek message check to keep things flowing
during a transfer
SendFile - Send a file
CaptureFile - Sets the receive state for file capture
-----------------------------------------------------------------------------*/
#include <windows.h>
#include <commctrl.h>
#include <malloc.h>
#include "mttty.h"
//
// Globals used in this file only
//
HANDLE hFile;
HANDLE hTransferAbortEvent;
HANDLE hTransferThread;
UINT uTimerId;
MMRESULT mmTimer = (MMRESULT)NULL;
TCHAR * lpBuf;
//
// Prototypes for functions called only within this file
//
DWORD WINAPI TransferThreadProc(LPVOID);
HANDLE OpenTheFile( LPCTSTR );
HANDLE CreateTheFile( LPCTSTR );
void CaptureFile( HANDLE, HWND );
UINT CheckForMessages( void );
BOOL GetTransferSizes( HANDLE, DWORD *, DWORD *, DWORD *);
/*-----------------------------------------------------------------------------
FUNCTION: TransferRepeatCreate(LPCTSTR, DWORD)
PURPOSE: Prepares program for a repeated text file transfer (send)
PARAMETERS:
lpstrFileName - name of file selected to send
dwFrequency - frequency of timer
COMMENTS: This function sets up a window timer to fire off
every so often. When it fires, TransferRepeatDo is
called with the same name as above. This causes the file transfer
to actuall take place.
TransferRepeatDestroy is called to kill the timer.
This function disables certain menu items that should not be
available for the duration of a repeated send even if the actual
Tx is not taking place.
HISTORY: Date: Author: Comment:
1/29/96 AllenD Wrote it
-----------------------------------------------------------------------------*/
void TransferRepeatCreate(LPCTSTR lpszFileName, DWORD dwFrequency)
{
HMENU hMenu;
UINT MenuFlags ;
DWORD dwFileSize;
DWORD dwMaxPackets;
DWORD dwPacketSize;
DWORD dwRead;
//
// open the file
//
hFile = OpenTheFile(lpszFileName);
if (hFile == INVALID_HANDLE_VALUE)
return;
//
// modify transfer menu
//
hMenu = GetMenu(ghwndMain);
MenuFlags = MF_DISABLED | MF_GRAYED;
EnableMenuItem(hMenu, ID_TRANSFER_SENDFILETEXT, MenuFlags);
EnableMenuItem(hMenu, ID_TRANSFER_SENDREPEATEDLY, MenuFlags);
EnableMenuItem(hMenu, ID_TRANSFER_ABORTREPEATEDSENDING, MF_ENABLED);
EnableMenuItem(hMenu, ID_TRANSFER_RECEIVEFILETEXT, MenuFlags);
//
// enable abort button and progress bar
//
SetWindowText(GetDlgItem(ghWndStatusDlg, IDC_ABORTBTN), _T("Abort Tx"));
ShowWindow(GetDlgItem(ghWndStatusDlg, IDC_ABORTBTN), SW_SHOW);
if (!GetTransferSizes(hFile, &dwPacketSize, &dwMaxPackets, &dwFileSize)) {
TransferRepeatDestroy();
return;
}
// Allocate a buffer
lpBuf = HeapAlloc(ghWriterHeap, 0, dwFileSize);
if (lpBuf == NULL) {
ErrorReporter(_T("HeapAlloc (data block from writer heap).\r\nFile is too large"));
TransferRepeatDestroy();
return;
}
// fill the buffer
if (!ReadFile(hFile, lpBuf, dwFileSize, &dwRead, NULL)) {
ErrorReporter(_T("Can't read from file\n"));
TransferRepeatDestroy();
}
if (dwRead != dwFileSize)
ErrorReporter(_T("Didn't read entire file\n"));
mmTimer = timeSetEvent((UINT) dwFrequency, 10, TransferRepeatDo, dwRead, TIME_PERIODIC);
if (mmTimer == (MMRESULT) NULL) {
ErrorReporter(_T("Could not create mm timer"));
TransferRepeatDestroy();
}
else {
REPEATING(TTYInfo) = TRUE;
OutputDebugString(_T("Timer setup.\n"));
}
return;
}
/*-----------------------------------------------------------------------------
FUNCTION: TransferRepeatDestroy( void )
PURPOSE: Stops a repeated text file transfer (send)
COMMENTS: Kills the repeated-send timer.
HISTORY: Date: Author: Comment:
1/29/96 AllenD Wrote it
-----------------------------------------------------------------------------*/
void TransferRepeatDestroy()
{
HMENU hMenu;
DWORD MenuFlags;
MMRESULT mmRes;
if (mmTimer != (MMRESULT) NULL) {
mmRes = timeKillEvent(mmTimer);
if (mmRes != TIMERR_NOERROR)
ErrorReporter(_T("Can't kill mm timer"));
mmTimer = (MMRESULT) NULL;
}
// close the file
CloseHandle(hFile);
// inform writer to abort all pending write requests
if (!WriterAddFirstNodeTimeout(WRITE_ABORT, 0, 0, NULL, NULL, NULL, 500))
ErrorReporter(_T("Couldn't inform writer to abort sending."));
// free the buffer
if (!HeapFree(ghWriterHeap, 0, lpBuf))
ErrorReporter(_T("HeapFree (data block from writer heap)"));
REPEATING(TTYInfo) = FALSE;
OutputDebugString(_T("Repeated transfer destroyed.\r\n"));
//
// enable transfer menu
//
hMenu = GetMenu(ghwndMain);
MenuFlags = MF_ENABLED;
EnableMenuItem(hMenu, ID_TRANSFER_ABORTREPEATEDSENDING, MF_DISABLED | MF_GRAYED);
EnableMenuItem(hMenu, ID_TRANSFER_SENDFILETEXT, MenuFlags);
EnableMenuItem(hMenu, ID_TRANSFER_SENDREPEATEDLY, MenuFlags);
EnableMenuItem(hMenu, ID_TRANSFER_RECEIVEFILETEXT, MenuFlags);
ShowWindow(GetDlgItem(ghWndStatusDlg, IDC_ABORTBTN), SW_HIDE);
return;
}
/*-----------------------------------------------------------------------------
FUNCTION: TransferRepeatDo( void )
PURPOSE: Performs a single text file transfer (send)
COMMENTS: Allocates a block to hold the file.
Prepares the writer packet.
HISTORY: Date: Author: Comment:
1/29/96 AllenD Wrote it
-----------------------------------------------------------------------------*/
void CALLBACK TransferRepeatDo( UINT uTimerId,
UINT uRes,
DWORD dwFileSize,
DWORD dwRes1,
DWORD dwRes2)
{
if (!WriterAddNewNodeTimeout(WRITE_BLOCK, dwFileSize, 0, lpBuf, 0, 0, 10))
PostMessage(ghwndMain, WM_COMMAND, ID_TRANSFER_ABORTSENDING, MAKELPARAM(IDC_ABORTBTN, 0) );
return;
}
/*-----------------------------------------------------------------------------
FUNCTION: TransferFileTextStart(LPCTSTR)
PURPOSE: Prepares program for a text file transfer (send)
PARAMETERS:
lpstrFileName - name of file selected to send
COMMENTS: Modifies menus and dialog control, then restores them
HISTORY: Date: Author: Comment:
1/26/96 AllenD Wrote it
-----------------------------------------------------------------------------*/
void TransferFileTextStart(LPCTSTR lpstrFileName)
{
DWORD dwThreadId;
HMENU hMenu;
UINT MenuFlags ;
//
// open the file
//
hFile = OpenTheFile(lpstrFileName);
if (hFile == INVALID_HANDLE_VALUE)
return;
//
// modify transfer menu
//
hMenu = GetMenu(ghwndMain);
MenuFlags = MF_DISABLED | MF_GRAYED;
EnableMenuItem(hMenu, ID_TRANSFER_SENDFILETEXT, MenuFlags);
EnableMenuItem(hMenu, ID_TRANSFER_SENDREPEATEDLY, MenuFlags);
EnableMenuItem(hMenu, ID_TRANSFER_ABORTSENDING, MF_ENABLED);
EnableMenuItem(hMenu, ID_TRANSFER_RECEIVEFILETEXT, MenuFlags);
//
// enable abort button and progress bar
//
gfAbortTransfer = FALSE;
SetWindowText(GetDlgItem(ghWndStatusDlg, IDC_ABORTBTN), _T("Abort Tx"));
ShowWindow(GetDlgItem(ghWndStatusDlg, IDC_ABORTBTN), SW_SHOW);
ShowWindow(GetDlgItem(ghWndStatusDlg, IDC_TRANSFERPROGRESS), SW_SHOW);
// start the transfer thread
hTransferAbortEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (hTransferAbortEvent == NULL)
ErrorReporter(_T("CreateEvent(Transfer Abort Event)"));
hTransferThread = CreateThread(NULL, 0,
TransferThreadProc,
(LPVOID) hFile, 0, &dwThreadId);
if (hTransferThread == NULL) {
ErrorReporter(_T("CreateThread (Transfer Thread)"));
TransferFileTextEnd();
}
else
TRANSFERRING(TTYInfo) = TRUE;
return;
}
/*-----------------------------------------------------------------------------
FUNCTION: TransferFileTextEnd()
PURPOSE: Stops a text file transfer (send)
COMMENTS: Modifies menus and dialog control, then restores them
HISTORY: Date: Author: Comment:
1/26/96 AllenD Wrote it
-----------------------------------------------------------------------------*/
void TransferFileTextEnd()
{
HMENU hMenu;
UINT MenuFlags ;
// stop the transfer thread
SetEvent(hTransferAbortEvent);
OutputDebugString(_T("Waiting for transfer thread...\n"));
if (WaitForSingleObject(hTransferThread, 3000) != WAIT_OBJECT_0) {
ErrorReporter(_T("TransferThread didn't stop."));
TerminateThread(hTransferThread, 0);
}
else
OutputDebugString(_T("Transfer thread exited\n"));
CloseHandle(hTransferAbortEvent);
CloseHandle(hTransferThread);
TRANSFERRING(TTYInfo) = FALSE;
//
// enable transfer menu
//
hMenu = GetMenu(ghwndMain);
MenuFlags = MF_ENABLED;
EnableMenuItem(hMenu, ID_TRANSFER_SENDFILETEXT, MenuFlags);
EnableMenuItem(hMenu, ID_TRANSFER_SENDREPEATEDLY, MenuFlags);
EnableMenuItem(hMenu, ID_TRANSFER_RECEIVEFILETEXT, MenuFlags);
EnableMenuItem(hMenu, ID_TRANSFER_ABORTSENDING, MF_DISABLED | MF_GRAYED);
//
// disable abort button and progress bar
//
ShowWindow(GetDlgItem(ghWndStatusDlg, IDC_ABORTBTN), SW_HIDE);
ShowWindow(GetDlgItem(ghWndStatusDlg, IDC_TRANSFERPROGRESS), SW_HIDE);
//
// close the file
//
CloseHandle(hFile);
}
/*-----------------------------------------------------------------------------
FUNCTION: ReceiveFileText(LPCTSTR)
PURPOSE: Prepares program for a text file transfer (receive)
PARAMETERS:
lpstrFileName - name of file selected for receiving
COMMENTS: Modifies menus and control, then restores them
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
void ReceiveFileText(LPCTSTR lpstrFileName)
{
HMENU hMenu;
UINT MenuFlags ;
//
// create the file
//
ghFileCapture = CreateTheFile(lpstrFileName);
if (ghFileCapture == INVALID_HANDLE_VALUE)
return;
/*
setup transfer
disable file menu
*/
hMenu = GetMenu(ghwndMain);
MenuFlags = MF_DISABLED | MF_GRAYED;
EnableMenuItem(hMenu, ID_FILE_CONNECT, MenuFlags);
EnableMenuItem(hMenu, ID_FILE_DISCONNECT, MenuFlags);
//
// disable transfer menu
//
EnableMenuItem(hMenu, ID_TRANSFER_SENDFILETEXT, MenuFlags);
EnableMenuItem(hMenu, ID_TRANSFER_RECEIVEFILETEXT, MenuFlags);
EnableMenuItem(hMenu, ID_TRANSFER_SENDREPEATEDLY, MenuFlags);
//
// enable abort button and progress bar
//
gfAbortTransfer = FALSE;
SetWindowText(GetDlgItem(ghWndStatusDlg, IDC_ABORTBTN), _T("Close Capture"));
ShowWindow(GetDlgItem(ghWndStatusDlg, IDC_ABORTBTN), SW_SHOW);
ShowWindow(GetDlgItem(ghWndStatusDlg, IDC_TRANSFERPROGRESS), SW_SHOW);
//
// send file until done or abort
//
CaptureFile(ghFileCapture, GetDlgItem(ghWndStatusDlg, IDC_TRANSFERPROGRESS));
//
// enable menu
//
hMenu = GetMenu(ghwndMain);
MenuFlags = MF_ENABLED;
ChangeConnection(ghwndMain, CONNECTED(TTYInfo));
//
// enable transfer menu
//
EnableMenuItem(hMenu, ID_TRANSFER_SENDFILETEXT, MenuFlags);
EnableMenuItem(hMenu, ID_TRANSFER_RECEIVEFILETEXT, MenuFlags);
EnableMenuItem(hMenu, ID_TRANSFER_SENDREPEATEDLY, MenuFlags);
//
// hide abort button and progress bar
//
ShowWindow(GetDlgItem(ghWndStatusDlg, IDC_ABORTBTN), SW_HIDE);
ShowWindow(GetDlgItem(ghWndStatusDlg, IDC_TRANSFERPROGRESS), SW_HIDE);
gfAbortTransfer = FALSE;
CloseHandle(ghFileCapture);
return; // returns when file transfer is complete or aborted
}
/*-----------------------------------------------------------------------------
FUNCTION: OpenTheFile(LPCTSTR)
PURPOSE: Open a file and return the file handle
PARAMETERS:
lpFName - name of file to open
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
HANDLE OpenTheFile(LPCTSTR lpFName)
{
HANDLE hTemp;
hTemp = CreateFile(lpFName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0,NULL);
if (hTemp == INVALID_HANDLE_VALUE)
ErrorReporter(_T("CreateFile"));
return hTemp;
}
/*-----------------------------------------------------------------------------
FUNCTION: CreateTheFile(LPCTSTR)
PURPOSE: Creates a file and returns the file handle
PARAMETERS:
lpFName - name of file to create
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
HANDLE CreateTheFile(LPCTSTR lpFName)
{
HANDLE hTemp;
hTemp = CreateFile(lpFName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0,NULL);
if (hTemp == INVALID_HANDLE_VALUE)
ErrorReporter(_T("CreateFile"));
return hTemp;
}
/*-----------------------------------------------------------------------------
FUNCTION: GetTransferSizes(HANDLE, DWORD *, DWORD *, DWORD *)
PURPOSE: Examines file and determines packet size, number of packets,
and file size.
PARAMETERS:
hFile - handle of file to get size information from
pdwDataPacketSize - size of an individual data packet
pdwNumPackets - total number of packets
pdwFileSize - size of file
RETURN:
TRUE - all metrics could be determined
FALSE - something wrong with the file metrics, can't transfer
COMMENTS:
This module can't handle files that are extremely large, so
it may return FALSE if a large file is specified.
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
BOOL GetTransferSizes(HANDLE hFile, DWORD * pdwDataPacketSize, DWORD * pdwNumPackets, DWORD * pdwFileSize)
{
BY_HANDLE_FILE_INFORMATION fi;
if (!GetFileInformationByHandle(hFile, &fi)) {
ErrorReporter(_T("GetFileInformationByHandle"));
return FALSE;
}
else {
if (fi.nFileSizeHigh) {
MessageBox(ghwndMain, _T("File is too large to transfer."), _T("File Transfer Error"), MB_OK);
return FALSE;
}
//
// setup packet size, file size and compute the number of packets
//
*pdwDataPacketSize = MAX_WRITE_BUFFER;
*pdwFileSize = fi.nFileSizeLow;
*pdwNumPackets = *pdwFileSize / *pdwDataPacketSize;
if (*pdwNumPackets > 65534) {
MessageBox(ghwndMain, _T("File is too large for buffer size."), _T("File Transfer Error"), MB_OK);
return FALSE;
}
}
return TRUE;
}
/*-----------------------------------------------------------------------------
FUNCTION: ShowTransferStatistics(DWORD, DWORD, DWORD)
PURPOSE: Displays bytes transferred and bytes per second
PARAMETERS:
dwEnd - ending time in milliseconds
dwStart - starting time
dwBytesTransferred - bytes sent
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
void ShowTransferStatistics(DWORD dwEnd, DWORD dwStart, DWORD dwBytesTransferred)
{
TCHAR szTemp[100];
DWORD dwSecs;
dwSecs = (dwEnd - dwStart) / 1000;
//
// display only if dwSecs != 0; if dwSecs == 0, then divide by zero occurs.
//
if (dwSecs != 0) {
wsprintf(szTemp, _T("Bytes transferred: %d\r\nBytes/Second: %d\r\n"), dwBytesTransferred, dwBytesTransferred / dwSecs);
UpdateStatus(szTemp);
}
return;
}
/*-----------------------------------------------------------------------------
FUNCTION: CheckForMessages
PURPOSE: Check for a message and dispatch it.
RETURN:
If the WM_CLOSE message or the WM_SYSCOMMAND (SC_CLOSE) is
retrieved, WM_CLOSE is posted, and WM_CLOSEQUIT is
returned by the function. This allows the caller
to detect this as an abort condition and exit properly. When
the caller exits, the main message loop in the WinMain function
should be entered again and the WM_CLOSE message will be
handled properly.
If there is a message other than those above, it is dispatched
and TRUE is returned indicating that a message was dispatched.
If no message is found, FALSE is returned.
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
UINT CheckForMessages()
{
MSG msg;
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_CLOSE) {
PostMessage(ghwndMain, WM_CLOSE, 0, 0);
return WM_CLOSE;
}
if (msg.message == WM_SYSCOMMAND && msg.wParam == SC_CLOSE) {
PostMessage(ghwndMain, WM_CLOSE, 0, 0);
return WM_CLOSE;
}
if (!TranslateAccelerator( ghwndMain, ghAccel, &msg )) {
TranslateMessage( &msg ) ;
DispatchMessage( &msg ) ;
}
return TRUE ;
}
return FALSE ;
}
/*-----------------------------------------------------------------------------
FUNCTION: CaptureFile(HANDLE, HWND)
PURPOSE: Receives a file
PARAMETERS:
hFile - handle of file to receive the data being captured (not used)
hWndProgress - window handle of progress bar (not used)
COMMENTS: Sets the receive state and waits for capture to end
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
void CaptureFile(HANDLE hFile, HWND hWndProgress)
{
UINT uMsgResult;
gdwReceiveState = RECEIVE_CAPTURED;
while ( !gfAbortTransfer ) {
uMsgResult = CheckForMessages();
//
// If WM_CLOSE is retrieved, then exit.
// If no message is retrieved, then sleep a little.
// If any other message is retrieved, check for another one.
//
switch(uMsgResult)
{
case WM_CLOSE: gfAbortTransfer = TRUE; break;
case FALSE: Sleep(200); break;
case TRUE: break;
}
}
gdwReceiveState = RECEIVE_TTY;
return;
}
/*-----------------------------------------------------------------------------
FUNCTION: TransferThreadProc(LPVOID)
PURPOSE: Worker thread does all the file transfer work
PARAMETERS:
lpV - actually a HANDLE for the file
COMMENTS: Function allows the hTransferAbortEvent to
signal an abort condition.
If the thread finishes OK, then the thread
calls the TransferFileTextEnd function itself.
HISTORY: Date: Author: Comment:
1/26/96 AllenD Wrote it
-----------------------------------------------------------------------------*/
DWORD WINAPI TransferThreadProc(LPVOID lpV)
{
DWORD dwPacketSize, dwMaxPackets, dwFileSize;
DWORD dwStartTime;
HWND hWndProgress;
HANDLE hFileHandle;
HANDLE hDataHeap = NULL;
BOOL fStarted = TRUE;
BOOL fAborting = FALSE;
hFileHandle = (HANDLE) lpV;
hWndProgress = GetDlgItem(ghWndStatusDlg, IDC_TRANSFERPROGRESS);
// set up transfer metrics
if (!GetTransferSizes(hFileHandle, &dwPacketSize, &dwMaxPackets, &dwFileSize))
fAborting = TRUE;
else {
SendMessage(hWndProgress, PBM_SETRANGE, 0, MAKELPARAM(0, dwMaxPackets+1));
SendMessage(hWndProgress, PBM_SETSTEP, (WPARAM) 1, 0);
SendMessage(hWndProgress, PBM_SETPOS, 0, 0);
}
// set up transfer heaps
if (!fAborting) {
SYSTEM_INFO sysInfo;
GetSystemInfo(&sysInfo);
hDataHeap = HeapCreate(0, sysInfo.dwPageSize * 2, sysInfo.dwPageSize * 10);
if (hDataHeap == NULL) {
ErrorReporter(_T("HeapCreate (Data Heap)"));
fAborting = TRUE;
}
}
// inform writer thread that a file is about to be transferred
if (!fAborting) {
if (!WriterAddNewNode(WRITE_FILESTART, dwFileSize, 0, NULL, NULL, NULL))
fAborting = TRUE;
}
OutputDebugString(_T("Xfer: About to start sending data\n"));
// Get Transfer Start Time
dwStartTime = GetTickCount();
if (WaitForSingleObject(hTransferAbortEvent, 0) == WAIT_OBJECT_0)
fAborting = TRUE;
while (!fAborting) {
TCHAR * lpDataBuf;
PWRITEREQUEST pWrite;
// transfer file, loop until all blocks of file have been read
lpDataBuf = HeapAlloc(hDataHeap, 0, dwPacketSize);
pWrite = HeapAlloc(ghWriterHeap, 0, sizeof(WRITEREQUEST));
if ((lpDataBuf != NULL) && (pWrite != NULL)) {
DWORD dwRead;
// read from file into new buffer
if (ReadFile(hFileHandle, lpDataBuf, dwPacketSize, &dwRead, NULL)) {
WriterAddExistingNode(pWrite, WRITE_FILE, dwRead, 0, lpDataBuf, hDataHeap, hWndProgress);
if (dwRead != dwPacketSize) // eof
break;
}
}
else {
BOOL fRes;
/*
Either the data heap is full, or the writer heap is full.
Free any allocated block, wait a little and try again.
Waiting lets the writer thread send some blocks and free
the data blocks from the data heap and the control
blocks from the writer heap.
*/
if (lpDataBuf) {
EnterCriticalSection(&gcsDataHeap);
fRes = HeapFree(hDataHeap, 0, lpDataBuf);
LeaveCriticalSection(&gcsDataHeap);
if (!fRes)
ErrorReporter(_T("HeapFree (Data block)"));
}
if (pWrite) {
EnterCriticalSection(&gcsWriterHeap);
fRes = HeapFree(ghWriterHeap, 0, pWrite);
LeaveCriticalSection(&gcsWriterHeap);
if (!fRes)
ErrorReporter(_T("HeapFree (Writer block)"));
}
OutputDebugString(_T("Xfer: A heap is full. Waiting...\n"));
// wait a little
// check for abort during the wait
if (WaitForSingleObject(hTransferAbortEvent, 200) == WAIT_OBJECT_0)
fAborting = TRUE;
}
// has the user aborted?
if (WaitForSingleObject(hTransferAbortEvent, 0) == WAIT_OBJECT_0)
fAborting = TRUE;
}
OutputDebugString(_T("Xfer: Done sending packets.\n"));
if (fAborting) {
// inform writer that transfer is aborting
OutputDebugString(_T("Xfer: Sending Abort Packet to writer\n"));
WriterAddFirstNodeTimeout(WRITE_ABORT, dwFileSize, 0, NULL, NULL, NULL, 500);
}
else
WriterAddNewNodeTimeout(WRITE_FILEEND, dwFileSize, 0, NULL, NULL, NULL, 500);
{
// wait til writer thread finishes with all blocks
HANDLE hEvents[2];
DWORD dwRes;
BOOL fTransferComplete;
hEvents[0] = ghTransferCompleteEvent;
hEvents[1] = hTransferAbortEvent;
OutputDebugString(_T("Xfer: Waiting for transfer complete signal from writer\n"));
do {
ResetEvent(hTransferAbortEvent);
dwRes = WaitForMultipleObjects(2, hEvents, FALSE, INFINITE);
switch(dwRes) {
case WAIT_OBJECT_0:
fTransferComplete = TRUE;
OutputDebugString(_T("Transfer complete signal rec'd\n"));
break;
case WAIT_OBJECT_0 + 1:
fAborting = TRUE;
OutputDebugString(_T("Transfer abort signal rec'd\n"));
OutputDebugString(_T("Xfer: Sending Abort Packet to writer\n"));
if (!WriterAddFirstNodeTimeout(WRITE_ABORT, dwFileSize, 0, NULL, NULL, NULL, 500))
ErrorReporter(_T("Can't add abort packet\n"));
break;
case WAIT_TIMEOUT: break;
default:
ErrorReporter(_T("WaitForMultipleObjects(Transfer Complete Event and Transfer Abort Event)"));
fTransferComplete = TRUE;
break;
}
} while (!fTransferComplete);
}
OutputDebugString(_T("Xfer: transfer complete\n"));
// report statistics
if (!fAborting)
ShowTransferStatistics(GetTickCount(), dwStartTime, dwFileSize);
// break down metrics
PostMessage(hWndProgress, PBM_SETPOS, 0, 0);
// break down heaps
if (hDataHeap != NULL) {
if (!HeapDestroy(hDataHeap))
ErrorReporter(_T("HeapDestroy (data heap)"));
}
// If I am done without user intervention, then post the
// "abort" message myself. This will cause the main thread to
// clean up after the file transfer.
if (!fAborting)
PostMessage(ghwndMain, WM_COMMAND, ID_TRANSFER_ABORTSENDING, 0);
// exit thread
return 0;
}