-
Notifications
You must be signed in to change notification settings - Fork 0
/
PortPlaybackThread.cpp
834 lines (604 loc) · 18.3 KB
/
PortPlaybackThread.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
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
// PortPlaybackThread.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include "PortPlaybackThread.h"
#include "DataStream.h"
#define R_BUFF_SIZE 4096
#define MS_PER_ST_UPDATE 10
extern "C"
{
#if !defined(WINCE)
#define DEFAULT_SERIALPORT_SPECIFICATION_TEMPLATE L"\\\\.\\COM%d"
#define DEFAULT_SERIALPORT_SPECIFICATION_TEMPLATE_A "\\\\.\\COM%d"
#else
#define DEFAULT_SERIALPORT_SPECIFICATION_TEMPLATE L"COM%d:"
#define DEFAULT_SERIALPORT_SPECIFICATION_TEMPLATE_A "COM%d:"
#endif
bool m_bInitialized = false;
DataStream* m_apDataStreams[THREAD_MAX] = {0};
int m_iDataStreamCount = 0;
DWORD m_dwPlaybackStartMilliseconds = 0;
const char m_cHexNibbles[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
const char m_cHexNibblesUpperCase[] = {
'A', 'B', 'C', 'D', 'E', 'F'
};
BOOL ReadCaptureLine(DataStream* pDataStream);
void HexStringToBytes(const char* pHex, int iHexLength, BYTE* pBinary);
int HexNibbleToNbr(char nibble);
BOOL WritePacket(DataStream* pDataStream, DataPacket* pDataPacket);
DWORD WINAPI ThreadProc( LPVOID lpParam );
DWORD m_dwMinimumScheculedMilliSecondToSend = 0xFFFFFFFFlu;
LIBRARY_BYTESTREAMPLAYBACK_API
void PortPlayback_Initialize(void)
{
for (int idx = 0; idx < m_iDataStreamCount; ++idx)
{
if (m_apDataStreams[idx] != NULL)
delete m_apDataStreams[idx];
}
memset(m_apDataStreams, 0, sizeof(m_apDataStreams));
m_iDataStreamCount = 0;
m_bInitialized = true;
}
LIBRARY_BYTESTREAMPLAYBACK_API
void PortPlayback_DeInitialize(void)
{
PortPlayback_Stop();
for (int idx = 0; idx < m_iDataStreamCount; ++idx)
{
if (m_apDataStreams[idx] != NULL)
delete m_apDataStreams[idx];
}
memset(m_apDataStreams, 0, sizeof(m_apDataStreams));
m_iDataStreamCount = 0;
m_bInitialized = false;
}
LIBRARY_BYTESTREAMPLAYBACK_API
int PortPlayback_AddDataStream(const wchar_t wszFilename[],
const int iComPortToPlayOn)
{
if (m_iDataStreamCount >= THREAD_MAX)
return -1;
DataStream* pDataStream = new DataStream(wszFilename, iComPortToPlayOn);
m_apDataStreams[m_iDataStreamCount] = pDataStream;
// read the streams, compute the playback times and create the byte array to send
// first, open the capture file
HANDLE hFile
= CreateFile(pDataStream->m_wszFilename,
GENERIC_READ,
0,
0,
OPEN_ALWAYS,
0,
0);
int iResult = 0;
bool bStartThreads = true;
if (hFile == INVALID_HANDLE_VALUE)
{
// error opening file; abort, returning the port #
DWORD dw = GetLastError();
iResult = pDataStream->m_iComPortToPlayOn;
bStartThreads = false;
delete pDataStream;
m_apDataStreams[m_iDataStreamCount] = NULL;
return -2;
}
pDataStream->m_hFile = hFile;
// read the header
int iFileIndex = -1;
DWORD dwTotalBytesRead = 0;
DWORD dwBytesRead;
char strRecordInit[R_BUFF_SIZE];
memset(strRecordInit, 0, sizeof(strRecordInit));
char* acSubStrings[8];
memset(acSubStrings, 0, sizeof(acSubStrings));
int token = -1;
acSubStrings[++token] = strRecordInit;
char c;
bool bReadingHeader = true;
while(bReadingHeader)
{
if (!ReadFile(hFile, &c, 1, &dwBytesRead, 0)) // !!!
{
// error reading file; abort, returning the port # + 1100
DWORD dwLastErr = GetLastError();
iResult = (pDataStream->m_iComPortToPlayOn + 1100);
bStartThreads = false;
delete pDataStream;
m_apDataStreams[m_iDataStreamCount] = NULL;
::CloseHandle(hFile);
return -3;
}
if (dwBytesRead == 0)
break;
switch (c)
{
default:
strRecordInit[dwTotalBytesRead++] = c;
break;
case '\t':
strRecordInit[dwTotalBytesRead++] = '\0';
acSubStrings[++token] = &(strRecordInit[dwTotalBytesRead]);
break;
case '\r':
strRecordInit[dwTotalBytesRead++] = '\0';
acSubStrings[++token] = &(strRecordInit[dwTotalBytesRead]);
if (!ReadFile(hFile, &c, 1, &dwBytesRead, 0))
{
// error reading from file; abort, returning the port # + 1300
DWORD dwLastErr = GetLastError();
iResult = (pDataStream->m_iComPortToPlayOn + 1300);
bStartThreads = false;
delete pDataStream;
m_apDataStreams[m_iDataStreamCount] = NULL;
::CloseHandle(hFile);
return -4;
}
if (c == '\n')
{
bReadingHeader = false;
}
else
{
// error reading from file; abort, returning the port # + 1300
iResult = (pDataStream->m_iComPortToPlayOn + 1300);
bStartThreads = false;
delete pDataStream;
m_apDataStreams[m_iDataStreamCount] = NULL;
::CloseHandle(hFile);
return -5;
}
break;
}
} // while(true)
// acSubStrings[0] contains "CAN PORT" || "SERIAL PORT"
// acSubStrings[1] should contain pointer to COM port recording was made on during file creation
// acSubStrings[2] : baud
// acSubStrings[3] : data bits
// acSubStrings[4] : parity
// acSubStrings[5] : stop bits
// acSubStrings[6] : flow control
const bool bIsCanPort
= !strcmp(acSubStrings[0], "CAN PORT");
int iIndexToStartAt = 2;
if (!bIsCanPort)
{
if (strcmp(acSubStrings[0], "SERIAL PORT"))
{
// So neither CAN PORT nor SERIAL PORT text find, an older
// playback file format.
// acSubStrings[0] should contain pointer to COM port recording was made on during file creation
// acSubStrings[1] : baud
// acSubStrings[2] : data bits
// acSubStrings[3] : parity
// acSubStrings[4] : stop bits
// acSubStrings[5] : flow control
iIndexToStartAt = 1;
}
}
if (acSubStrings[iIndexToStartAt] == NULL) {
iResult = (pDataStream->m_iComPortToPlayOn + 1300);
bStartThreads = false;
delete pDataStream;
m_apDataStreams[m_iDataStreamCount] = NULL;
::CloseHandle(hFile);
return -11;
}
const int iBaudRate = atoi(acSubStrings[iIndexToStartAt++]);
if (acSubStrings[iIndexToStartAt] == NULL) {
iResult = (pDataStream->m_iComPortToPlayOn + 1300);
bStartThreads = false;
delete pDataStream;
m_apDataStreams[m_iDataStreamCount] = NULL;
::CloseHandle(hFile);
return -11;
}
const int iDataBits = atoi(acSubStrings[iIndexToStartAt++]);
if (acSubStrings[iIndexToStartAt] == NULL) {
iResult = (pDataStream->m_iComPortToPlayOn + 1300);
bStartThreads = false;
delete pDataStream;
m_apDataStreams[m_iDataStreamCount] = NULL;
::CloseHandle(hFile);
return -11;
}
const int iParity = atoi(acSubStrings[iIndexToStartAt++]);
if (acSubStrings[iIndexToStartAt] == NULL) {
iResult = (pDataStream->m_iComPortToPlayOn + 1300);
bStartThreads = false;
delete pDataStream;
m_apDataStreams[m_iDataStreamCount] = NULL;
::CloseHandle(hFile);
return -11;
}
const int iStopBits = atoi(acSubStrings[iIndexToStartAt++]);
if (acSubStrings[iIndexToStartAt] == NULL) {
iResult = (pDataStream->m_iComPortToPlayOn + 1300);
bStartThreads = false;
delete pDataStream;
m_apDataStreams[m_iDataStreamCount] = NULL;
::CloseHandle(hFile);
return -11;
}
const int iFlowControl = atoi(acSubStrings[iIndexToStartAt++]);
/*
char* pEndPoint;
m_uiHPC = strtoul((strRecordInit + (tokens[5])), &pEndPoint, 10);
m_uiMultiMsBlock = ((m_uiHPC/1000) * MS_PER_ST_UPDATE);
*/
// open the com port
wchar_t wszComPort[MAX_PATH+1] = {0};
swprintf(wszComPort,
DEFAULT_SERIALPORT_SPECIFICATION_TEMPLATE,
pDataStream->m_iComPortToPlayOn);
HANDLE hCommPort
= CreateFile(wszComPort, // filename
GENERIC_WRITE | GENERIC_READ, // desired access
0, // share mode
NULL, // Security Attributes
OPEN_EXISTING, // Creation Disposition
FILE_ATTRIBUTE_NORMAL, // Flags And Attributes
NULL); // Template File
if (hCommPort == INVALID_HANDLE_VALUE)
{
// error opening port; abort, returning the port #
iResult = pDataStream->m_iComPortToPlayOn;
bStartThreads = false;
delete pDataStream;
m_apDataStreams[m_iDataStreamCount] = NULL;
return -6;
}
// set up the com port parameters
DCB commState;
if (GetCommState(hCommPort, &commState))
{
commState.BaudRate = iBaudRate;
commState.ByteSize = iDataBits;
commState.Parity = iParity;
commState.StopBits = iStopBits;
if (iFlowControl == 0)
{
// XON/XOFF
commState.fOutX = TRUE;
commState.fInX = TRUE;
// hardware
commState.fOutxCtsFlow = FALSE;
commState.fOutxDsrFlow = FALSE;
commState.fDtrControl = DTR_CONTROL_DISABLE;
commState.fRtsControl = RTS_CONTROL_DISABLE;
}
else if (iFlowControl == 1)
{
// XON/XOFF
commState.fOutX = FALSE;
commState.fInX = FALSE;
// hardware
commState.fOutxCtsFlow = TRUE;
commState.fOutxDsrFlow = TRUE;
commState.fDtrControl = DTR_CONTROL_ENABLE;
commState.fRtsControl = RTS_CONTROL_ENABLE;
}
else
{
// flow control == none
// XON/XOFF
commState.fOutX = FALSE;
commState.fInX = FALSE;
// hardware
commState.fOutxCtsFlow = FALSE;
commState.fOutxDsrFlow = FALSE;
commState.fDtrControl = DTR_CONTROL_DISABLE;
commState.fRtsControl = RTS_CONTROL_DISABLE;
}
if (!SetCommState(hCommPort, &commState))
{
// error opening port; abort, returning the port # + 100
DWORD dw = GetLastError();
iResult = pDataStream->m_iComPortToPlayOn + 100;
bStartThreads = false;
delete pDataStream;
m_apDataStreams[m_iDataStreamCount] = NULL;
::CloseHandle(hFile);
::CloseHandle(hCommPort);
return -7;
}
}
else
{
// error opening port; abort, returning the port # + 200
DWORD dw = GetLastError();
iResult = pDataStream->m_iComPortToPlayOn + 200;
bStartThreads = false;
delete pDataStream;
m_apDataStreams[m_iDataStreamCount] = NULL;
::CloseHandle(hFile);
::CloseHandle(hCommPort);
return -8;
}
// set up the com port timeouts
COMMTIMEOUTS commTimeouts;
if (GetCommTimeouts(hCommPort, &commTimeouts))
{
commTimeouts.WriteTotalTimeoutConstant = 0;
commTimeouts.WriteTotalTimeoutMultiplier = 0;
if (!SetCommTimeouts(hCommPort, &commTimeouts))
{
// error opening port; abort, returning the port # + 300
DWORD dw = GetLastError();
iResult = pDataStream->m_iComPortToPlayOn + 300;
bStartThreads = false;
delete pDataStream;
m_apDataStreams[m_iDataStreamCount] = NULL;
::CloseHandle(hFile);
::CloseHandle(hCommPort);
return -9;
}
}
else
{
// error opening port; abort, returning the port # + 400
DWORD dw = GetLastError();
iResult = pDataStream->m_iComPortToPlayOn + 400;
bStartThreads = false;
delete pDataStream;
m_apDataStreams[m_iDataStreamCount] = NULL;
::CloseHandle(hFile);
::CloseHandle(hCommPort);
return -10;
}
pDataStream->m_hComPort = hCommPort;
// Read in ALL data to play back
while (ReadCaptureLine(pDataStream));
return m_iDataStreamCount++;
}
LIBRARY_BYTESTREAMPLAYBACK_API
DWORD PortRecorder_GetTotalBytesPlayed(int iPortPlaybackObjectID)
{
if (iPortPlaybackObjectID < 0 || iPortPlaybackObjectID >= m_iDataStreamCount)
return 0;
return m_apDataStreams[iPortPlaybackObjectID]->m_dwTotalBytesPlayed;
}
volatile long* m_pThreadPlayingBackCount;
LIBRARY_BYTESTREAMPLAYBACK_API
int PortPlayback_Play(volatile long* pThreadPlayingBackCount)
{
*pThreadPlayingBackCount = 0;
m_pThreadPlayingBackCount = pThreadPlayingBackCount;
if (!m_bInitialized)
return -1;
if (m_iDataStreamCount < 1)
return -2;
HANDLE ahThreadArray[THREAD_MAX];
memset(ahThreadArray, NULL, sizeof(ahThreadArray));
DWORD adwThreadIdArray[THREAD_MAX];
memset(adwThreadIdArray, 0, sizeof(adwThreadIdArray));
for (int threadIdx = 0; threadIdx < m_iDataStreamCount; ++threadIdx)
{
DataPacket* pDataPacket = m_apDataStreams[threadIdx]->m_pDataPacketRootNode;
while (pDataPacket != NULL)
{
pDataPacket->m_dwScheculedMilliSecondToSend -= m_dwMinimumScheculedMilliSecondToSend;
pDataPacket = pDataPacket->m_pNext;
}
ahThreadArray[threadIdx] = CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadProc, // thread function name
m_apDataStreams[threadIdx], // argument to thread function
CREATE_SUSPENDED, // we'll check for creation
&(adwThreadIdArray[threadIdx])); // returns the thread identifier
if (ahThreadArray[threadIdx] != NULL)
{
// It's very important that our threads are NOT delayed by
// the client's GUI thread for whatever reason.
::SetThreadPriority(ahThreadArray[threadIdx], THREAD_PRIORITY_HIGHEST);
}
}
m_dwPlaybackStartMilliseconds = ::GetTickCount();
for (int threadIdx = 0; threadIdx < m_iDataStreamCount; ++threadIdx)
{
DWORD dwRC = ResumeThread(ahThreadArray[threadIdx]);
if (dwRC == (DWORD) -1)
{
DWORD dwErr = GetLastError();
}
}
}
LIBRARY_BYTESTREAMPLAYBACK_API
void PortPlayback_Pause(void)
{
if (!m_bInitialized)
return;
for (int idx = 0; idx < m_iDataStreamCount; ++idx)
m_apDataStreams[idx]->m_bStopThread = TRUE;
}
LIBRARY_BYTESTREAMPLAYBACK_API
void PortPlayback_Stop(void)
{
if (!m_bInitialized)
return;
for (int idx = 0; idx < m_iDataStreamCount; ++idx) {
m_apDataStreams[idx]->m_bStopThread = TRUE;
::Sleep(500);
}
}
BOOL ReadCaptureLine(DataStream* pDataStream)
{
char* pszSubStrings[7];
memset(pszSubStrings, 0, sizeof(pszSubStrings));
char strRecord[R_BUFF_SIZE];
memset(strRecord, 0, sizeof(strRecord));
int iTokenIdx = -1;
pszSubStrings[++iTokenIdx] = strRecord;
BOOL bFailed = false;
DWORD dwBytesRead;
DWORD dwTotalBytesRead = 0;
char c;
int iResult = 0;
bool bReadingRecord = true;
while (bReadingRecord)
{
if (!ReadFile(pDataStream->m_hFile, &c, 1, &dwBytesRead, 0))
{
// error reading from file; abort, returning the port # + 1100
DWORD dwLastErr = GetLastError();
iResult = (pDataStream->m_iComPortToPlayOn + 1100);
bFailed = true;
bReadingRecord = false;
continue;
}
if (dwBytesRead == 0)
{
bReadingRecord = false;
continue;
}
switch (c)
{
default:
strRecord[dwTotalBytesRead++] = c;
break;
case '\t':
strRecord[dwTotalBytesRead++] = '\0';
pszSubStrings[++iTokenIdx] = &(strRecord[dwTotalBytesRead]);
break;
case '\r':
strRecord[dwTotalBytesRead++] = '\0';
pszSubStrings[++iTokenIdx] = &(strRecord[dwTotalBytesRead]);
// Read trailing \n
if ((!ReadFile(pDataStream->m_hFile, &c, 1, &dwBytesRead, 0)) || (dwBytesRead == 0))
{
// error writing to file; abort, returning the port # + 1300
DWORD dwLastErr = GetLastError();
iResult = (pDataStream->m_iComPortToPlayOn + 1300);
bFailed = TRUE;
}
if (c != '\n')
{
// error writing to file; abort, returning the port # + 1300
iResult = (pDataStream->m_iComPortToPlayOn + 1300);
bFailed = TRUE;
}
bReadingRecord = false;
break;
}
} // while (true)
// if not a full line
// "[milliseconds]\t[bytes read]\r\n"
// just return
if (iTokenIdx != 2)
return false;
if (bFailed)
return false;
// "01234\ta8fe84b5234e\r\n" << Data will ALWAYS have 2-digit hex values (byte), never odd # of hex values (eg, 0xa should be impossible)
// pszSubStrings[0] = "012345"
// pszSubStrings[1] = "a8fe84b5234e"
DataPacket dataPacket;
dataPacket.m_dwScheculedMilliSecondToSend = (DWORD)_atoi64(strRecord);
if (m_dwMinimumScheculedMilliSecondToSend > dataPacket.m_dwScheculedMilliSecondToSend)
{
m_dwMinimumScheculedMilliSecondToSend = dataPacket.m_dwScheculedMilliSecondToSend;
}
const DWORD dwTotalDataHexDigits = strlen(pszSubStrings[1]);
const DWORD dwByteCount = dwTotalDataHexDigits/2;
BYTE* pbyteData = new BYTE[dwByteCount];
HexStringToBytes(pszSubStrings[1], dwTotalDataHexDigits, pbyteData);
dataPacket.AddData(pbyteData, dwByteCount);
delete [] pbyteData;
pDataStream->AddDataPacket(&dataPacket);
return true;
}
int HexNibbleToNbr(char nibble)
{
int i = 0;
for (i = 15; i >= 0; --i)
{
if (m_cHexNibbles[i] == nibble)
return i;
}
// the hex values in the capture file started out as lower case but just in case...
for (i = 5; i >= 0; --i)
{
if (m_cHexNibblesUpperCase[i] == nibble)
return i + 10;
}
return i;
}
void HexStringToBytes(const char* pHex, int iHexLength, BYTE* pBinary)
{
// Need the 'iHexLength - 1' because each iteration
// uses pHex[i] AND pHex[i+1]
for (int i = 0, j = 0; i < iHexLength - 1; ++j)
{
pBinary[j] = HexNibbleToNbr(pHex[i++]) << 4;
pBinary[j] += HexNibbleToNbr(pHex[i++]);
}
}
BOOL WritePacket(DataStream* pDataStream,
DataPacket* pDataPacket)
{
DWORD dwTotalBytesWritten = 0;
DWORD dwBytesWritten = 0;
BOOL bSuccess;
BOOL bResult = TRUE;
DWORD dwStartTicks = GetTickCount();
while ( dwTotalBytesWritten < pDataPacket->m_dwDataLength
&& (GetTickCount() - dwStartTicks) < pDataPacket->m_dwDataLength*50)
{
bResult = WriteFile(pDataStream->m_hComPort,
pDataPacket->m_pbyteData + dwTotalBytesWritten,
pDataPacket->m_dwDataLength - dwTotalBytesWritten,
&dwBytesWritten,
NULL);
// Check for a problem or pending operation.
if (bResult)
{
dwTotalBytesWritten += dwBytesWritten;
pDataStream->m_dwTotalBytesPlayed += dwBytesWritten;
}
}
return true;
}
DWORD WINAPI ThreadProc( LPVOID lpParam )
{
DataStream* pDataStream = (DataStream*)lpParam;
if (pDataStream == NULL)
return -1;
if (pDataStream->m_pDataPacketRootNode == NULL)
return -2;
::InterlockedIncrement(m_pThreadPlayingBackCount);
DataPacket* pDataPacket = pDataStream->m_pDataPacketRootNode;
// play the stream
DWORD dwNow;
while ( (!pDataStream->m_bStopThread)
&& pDataPacket != NULL)
{
dwNow = ::GetTickCount();
if (dwNow < m_dwPlaybackStartMilliseconds)
{
// GetTickCount() rollover occured.
// 0 dwNow m_dwPlaybackStartMilliseconds UINT_MAX
// | | | |
// +-----+-------+-------------------------------+
dwNow = (UINT_MAX - m_dwPlaybackStartMilliseconds) + dwNow;
}
else
{
// 0 m_dwPlaybackStartMilliseconds dwNow UINT_MAX
// | | | |
// +---+--------------------------------+--------+
dwNow = dwNow - m_dwPlaybackStartMilliseconds;
}
int iDiff = int((__int64)pDataPacket->m_dwScheculedMilliSecondToSend - (__int64)dwNow);
if (iDiff > 0)
Sleep(iDiff);
WritePacket(pDataStream, pDataPacket);
pDataPacket = pDataPacket->m_pNext;
}
::InterlockedDecrement(m_pThreadPlayingBackCount);
return 0;
}
};