-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathqueue.c
512 lines (441 loc) · 15 KB
/
queue.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
//? Marius Negrutiu (marius.negrutiu@protonmail.com) :: 2014/02/02
#include "main.h"
#include "queue.h"
#include "utils.h"
VOID QueueInitialize(
_Inout_ PQUEUE pQueue,
_In_ LPCTSTR szName,
_In_ int iThreadCount
)
{
assert( pQueue );
assert( szName && *szName );
assert( iThreadCount < MAX_WORKER_THREADS );
// Name
if ( szName && *szName ) {
lstrcpyn( pQueue->szName, szName, ARRAYSIZE( pQueue->szName ) );
} else {
wnsprintf( pQueue->szName, ARRAYSIZE( pQueue->szName ), _T( "%p" ), pQueue );
}
// Queue
InitializeCriticalSectionAndSpinCount( &pQueue->csLock, 3000 );
pQueue->pHead = NULL;
pQueue->iLastId = 0;
// Worker threads
pQueue->iThreadMaxCount = __min( iThreadCount, MAX_WORKER_THREADS - 1 );
pQueue->iThreadCount = 0; // Don't create worker threads yet
pQueue->iThreadBusyCount = 0;
pQueue->hThreadTermEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); /// Manual
pQueue->hThreadWakeEvent = CreateEvent( NULL, FALSE, FALSE, NULL ); /// Automatic
assert( pQueue->hThreadTermEvent );
assert( pQueue->hThreadWakeEvent );
TRACE( _T( " QueueInitialize(%s, ThCnt:%d)\n" ), szName, pQueue->iThreadMaxCount );
}
VOID QueueDestroy( _Inout_ PQUEUE pQueue )
{
assert( pQueue );
// Worker threads
if ( pQueue->iThreadCount > 0 ) {
HANDLE pObj[MAX_WORKER_THREADS];
ULONG i, iObjCnt = 0;
const ULONG QUEUE_WAIT_FOR_THREADS = 12000;
/// Signal thread termination
SetEvent( pQueue->hThreadTermEvent );
/// Build a list of thread handles
for (i = 0; i < pQueue->iThreadCount; i++)
if (pQueue->pThreads[i].hThread)
pObj[iObjCnt++] = pQueue->pThreads[i].hThread;
TRACE( _T( " Waiting for %u threads (max. %ums)...\n" ), iObjCnt, QUEUE_WAIT_FOR_THREADS );
/// Wait for all threads to terminate
if (iObjCnt > 0) {
DWORD dwTime = dwTime = GetTickCount();
DWORD iWait = WaitForMultipleObjects( iObjCnt, pObj, TRUE, QUEUE_WAIT_FOR_THREADS );
dwTime = GetTickCount() - dwTime;
if (iWait == WAIT_OBJECT_0 || iWait == WAIT_ABANDONED_0) {
TRACE( _T( " Threads closed in %ums\n" ), dwTime );
MyZeroMemory( pQueue->pThreads, ARRAYSIZE( pQueue->pThreads ) * sizeof( THREAD ) );
} else if (iWait == WAIT_TIMEOUT) {
TRACE( _T( " Threads failed to stop after %ums. Will terminate them forcedly\n" ), dwTime );
for (i = 0; i < iObjCnt; i++)
TerminateThread( pObj[i], 666 );
} else {
DWORD err = GetLastError();
TRACE( _T( " [!] WaitForMultipleObjects( ObjCnt:%u ) == 0x%x, GLE == 0x%x\n" ), iObjCnt, iWait, err );
for (i = 0; i < iObjCnt; i++)
TerminateThread( pObj[i], 666 );
}
}
}
CloseHandle( pQueue->hThreadTermEvent );
CloseHandle( pQueue->hThreadWakeEvent );
pQueue->hThreadTermEvent = NULL;
pQueue->hThreadWakeEvent = NULL;
pQueue->iThreadCount = 0;
// Queue
///QueueLock( pQueue );
QueueReset( pQueue );
DeleteCriticalSection( &pQueue->csLock );
// Name
TRACE( _T( " QueueDestroy(%s)\n" ), pQueue->szName );
*pQueue->szName = _T( '\0' );
}
VOID QueueNewThread(_Inout_ PQUEUE pQueue)
{
ULONG availableThreads = pQueue->iThreadCount - InterlockedCompareExchange(&pQueue->iThreadBusyCount, -1, -1);
if (availableThreads == 0 && pQueue->iThreadCount < pQueue->iThreadMaxCount)
{
PTHREAD pThread = &pQueue->pThreads[pQueue->iThreadCount];
pThread->pQueue = pQueue;
pThread->hTermEvent = pQueue->hThreadTermEvent;
pThread->hWakeEvent = pQueue->hThreadWakeEvent;
wnsprintf(pThread->szName, ARRAYSIZE(pThread->szName), _T("%s%02d"), pQueue->szName, pQueue->iThreadCount);
pThread->hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc, pThread, 0, &pThread->iTID);
assert(pThread->hThread);
if (pThread->hThread) {
pQueue->iThreadCount++;
TRACE(_T(" Th:%s create. %u running threads\n"), pThread->szName, pQueue->iThreadCount);
} else {
DWORD err = GetLastError();
TRACE(_T(" [!] CreateThread(%s) == 0x%x\n"), pThread->szName, err);
MyZeroMemory(pThread, sizeof(*pThread));
}
}
}
VOID QueueLock( _Inout_ PQUEUE pQueue )
{
assert( pQueue );
EnterCriticalSection( &pQueue->csLock );
TRACE2( _T( " QueueLock(%s)\n" ), pQueue->szName );
}
VOID QueueUnlock( _Inout_ PQUEUE pQueue )
{
assert( pQueue );
LeaveCriticalSection( &pQueue->csLock );
TRACE2( _T( " QueueUnlock(%s)\n" ), pQueue->szName );
}
BOOL QueueReset( _Inout_ PQUEUE pQueue )
{
BOOL bRet = TRUE;
assert( pQueue );
TRACE( _T( " QueueReset(%s)\n" ), pQueue->szName );
while ( pQueue->pHead )
bRet = bRet && QueueRemove( pQueue, pQueue->pHead );
pQueue->iLastId = 0;
return bRet;
}
PQUEUE_REQUEST QueueFind( _Inout_ PQUEUE pQueue, _In_ ULONG iReqID )
{
PQUEUE_REQUEST pReq;
assert( pQueue );
for (pReq = pQueue->pHead; pReq && pReq->iId != iReqID; pReq = pReq->pNext);
TRACE2( _T( " QueueFind(%s, ID:%u) == 0x%p\n" ), pQueue->szName, iReqID, pReq );
return pReq;
}
PQUEUE_REQUEST QueueFindNextWaiting( _Inout_ PQUEUE pQueue )
{
// NOTES:
// New requests are always added to the front of the queue. The first (chronologically) waiting request is the last one
// We'll select the request with the highest priority (Note: lower value means higher priority)
PQUEUE_REQUEST pReq, pSelectedReq = NULL;
ULONG iSelectedPrio = ULONG_MAX - 1;
assert( pQueue );
for (pReq = pQueue->pHead; pReq; pReq = pReq->pNext) {
if (pReq->iStatus == REQUEST_STATUS_WAITING && !pReq->bAbort && pReq->iPriority <= iSelectedPrio) {
if (pReq->iDependId > 0) {
PQUEUE_REQUEST pReqDepend = QueueFind( pQueue, pReq->iDependId );
if (!pReqDepend || pReqDepend->iStatus != REQUEST_STATUS_DONE)
continue; /// This request depends on another that's not finished
}
pSelectedReq = pReq;
iSelectedPrio = pReq->iPriority;
}
}
TRACE2( _T( " QueueFindNextWaiting(%s) == ID:%u, Prio:%u, Ptr:0x%p\n" ), pQueue->szName, pSelectedReq ? pSelectedReq->iId : 0, pSelectedReq ? pSelectedReq->iPriority : 0, pSelectedReq );
return pSelectedReq;
}
BOOL QueueAdd(
_Inout_ PQUEUE pQueue,
_In_ PQUEUE_REQUEST_PARAM pParam,
_Out_opt_ PQUEUE_REQUEST *ppReq
)
{
BOOL bRet = TRUE;
assert( pQueue );
assert( pParam );
if (pParam->pszURL && *pParam->pszURL && ((pParam->iLocalType != REQUEST_LOCAL_FILE) || (pParam->pszLocalFile && *pParam->pszLocalFile))) {
PQUEUE_REQUEST pReq = (PQUEUE_REQUEST)MyAlloc( sizeof( QUEUE_REQUEST ) );
if (pReq) {
MyZeroMemory( pReq, sizeof( *pReq ) );
pReq->iId = ++pQueue->iLastId;
pReq->iPriority = (pParam->iPriority == DEFAULT_VALUE) ? DEFAULT_PRIORITY : pParam->iPriority;
pReq->iDependId = pParam->iDependId;
pReq->iStatus = REQUEST_STATUS_WAITING;
pReq->pQueue = pQueue;
MyStrDup( pReq->pszURL, pParam->pszURL );
pReq->iLocalType = pParam->iLocalType;
if (pReq->iLocalType == REQUEST_LOCAL_FILE)
MyStrDup( pReq->Local.pszFile, pParam->pszLocalFile );
if (pParam->pszProxy && *pParam->pszProxy) {
MyStrDup( pReq->pszProxy, pParam->pszProxy );
}
if (pParam->pszProxyUser && *pParam->pszProxyUser) {
MyStrDup( pReq->pszProxyUser, pParam->pszProxyUser );
}
if (pParam->pszProxyPass && *pParam->pszProxyPass) {
MyStrDup( pReq->pszProxyPass, pParam->pszProxyPass );
}
if (pParam->pszMethod && *pParam->pszMethod) {
lstrcpyn( pReq->szMethod, pParam->pszMethod, ARRAYSIZE( pReq->szMethod ) );
} else {
lstrcpy( pReq->szMethod, _T( "GET" ) ); /// Default
}
if (pParam->pszHeaders && *pParam->pszHeaders) {
MyStrDup( pReq->pszHeaders, pParam->pszHeaders );
}
if (pParam->pData && (pParam->iDataSize > 0)) {
MyDataDup( pReq->pData, pParam->pData, pParam->iDataSize );
pReq->iDataSize = pParam->iDataSize;
}
pReq->iTimeoutConnect = pParam->iTimeoutConnect;
pReq->iTimeoutReconnect = pParam->iTimeoutReconnect;
pReq->iOptConnectRetries = pParam->iOptConnectRetries;
pReq->iOptConnectTimeout = pParam->iOptConnectTimeout;
pReq->iOptReceiveTimeout = pParam->iOptReceiveTimeout;
pReq->iOptSendTimeout = pParam->iOptSendTimeout;
if (pParam->pszReferrer && *pParam->pszReferrer) {
MyStrDup( pReq->pszReferer, pParam->pszReferrer );
}
pReq->iHttpInternetFlags = pParam->iHttpInternetFlags;
pReq->iHttpSecurityFlags = pParam->iHttpSecurityFlags;
GetLocalFileTime( &pReq->tmEnqueue );
pReq->iWin32Error = ERROR_SUCCESS;
pReq->pszWin32Error = NULL;
AllocErrorStr( pReq->iWin32Error, &pReq->pszWin32Error );
pReq->iHttpStatus = 0;
pReq->pszHttpStatus = NULL;
MyStrDup( pReq->pszHttpStatus, _T( "N/A" ) );
// Add to the front
pReq->pNext = pQueue->pHead;
pQueue->pHead = pReq;
// Return
if (ppReq)
*ppReq = pReq;
// Wake up one worker thread
SetEvent( pQueue->hThreadWakeEvent );
// Create a worker thread if necessary
QueueNewThread(pQueue);
TRACE(
_T( " QueueAdd(%s, ID:%u, %s %s -> %s, Prio:%u, DependsOn:%d)\n" ),
pQueue->szName,
pReq->iId,
pReq->szMethod,
pReq->pszURL,
pReq->iLocalType == REQUEST_LOCAL_NONE ? TEXT_LOCAL_NONE : (pReq->iLocalType == REQUEST_LOCAL_FILE ? pReq->Local.pszFile : TEXT_LOCAL_MEMORY),
pReq->iPriority, pReq->iDependId
);
} else {
bRet = FALSE;
}
} else {
bRet = FALSE;
}
return bRet;
}
BOOL QueueRemove( _Inout_ PQUEUE pQueue, _In_ PQUEUE_REQUEST pReq )
{
BOOL bRet = TRUE;
assert( pQueue );
if (pReq) {
TRACE(
_T( " QueueRemove(%s, ID:%u, Err:%u \"%s\", St:%s, Speed:%s, Time:%ums, Drops:%u, %s %s -> %s)\n" ),
pQueue->szName,
pReq->iId,
pReq->iWin32Error != ERROR_SUCCESS ? pReq->iWin32Error : pReq->iHttpStatus,
pReq->iWin32Error != ERROR_SUCCESS ? pReq->pszWin32Error : pReq->pszHttpStatus,
pReq->iStatus == REQUEST_STATUS_WAITING ? TEXT_STATUS_WAITING : (pReq->iStatus == REQUEST_STATUS_DOWNLOADING ? TEXT_STATUS_DOWNLOADING : TEXT_STATUS_COMPLETED),
pReq->Speed.szSpeed,
MyTimeDiff( &pReq->tmDisconnect, &pReq->tmConnect ),
pReq->iConnectionDrops,
pReq->szMethod,
pReq->pszURL,
pReq->iLocalType == REQUEST_LOCAL_NONE ? TEXT_LOCAL_NONE : (pReq->iLocalType == REQUEST_LOCAL_FILE ? pReq->Local.pszFile : TEXT_LOCAL_MEMORY)
);
// Free request's content
MyFree( pReq->pszURL );
MyFree( pReq->pszProxy );
MyFree( pReq->pszProxyUser );
MyFree( pReq->pszProxyPass );
MyFree( pReq->pszHeaders );
MyFree( pReq->pData );
MyFree( pReq->pszSrvHeaders );
MyFree( pReq->pszSrvIP );
MyFree( pReq->pszWin32Error );
MyFree( pReq->pszHttpStatus );
MyFree( pReq->pszReferer );
switch (pReq->iLocalType)
{
case REQUEST_LOCAL_FILE:
MyFree( pReq->Local.pszFile );
if (pReq->Local.hFile != NULL && pReq->Local.hFile != INVALID_HANDLE_VALUE)
CloseHandle( pReq->Local.hFile );
break;
case REQUEST_LOCAL_MEMORY:
VirtualFree(pReq->Local.pMemory, 0, MEM_RELEASE);
break;
case REQUEST_LOCAL_NONE:
break;
}
// Remove from list
{
PQUEUE_REQUEST pPrevReq;
for (pPrevReq = pQueue->pHead; (pPrevReq != NULL) && (pPrevReq->pNext != pReq); pPrevReq = pPrevReq->pNext);
if (pPrevReq) {
pPrevReq->pNext = pReq->pNext;
} else {
pQueue->pHead = pReq->pNext;
}
}
// Destroy the request
MyFree( pReq );
}
else {
bRet = FALSE;
}
return bRet;
}
BOOL QueueAbort( _In_ PQUEUE pQueue, _In_ PQUEUE_REQUEST pReq, _In_opt_ DWORD dwWaitMS )
{
BOOL bRet = TRUE;
assert( pQueue );
if (pReq) {
TRACE(
_T( " QueueAbort(%s, ID:%u, Prio:%u, St:%s, %s %s -> %s)\n" ),
pQueue->szName,
pReq->iId, pReq->iPriority,
pReq->iStatus == REQUEST_STATUS_WAITING ? TEXT_STATUS_WAITING : (pReq->iStatus == REQUEST_STATUS_DOWNLOADING ? TEXT_STATUS_DOWNLOADING : TEXT_STATUS_COMPLETED),
pReq->szMethod,
pReq->pszURL,
pReq->iLocalType == REQUEST_LOCAL_NONE ? TEXT_LOCAL_NONE : (pReq->iLocalType == REQUEST_LOCAL_FILE ? pReq->Local.pszFile : TEXT_LOCAL_MEMORY)
);
switch (pReq->iStatus) {
case REQUEST_STATUS_WAITING:
pReq->bAbort = TRUE;
pReq->iStatus = REQUEST_STATUS_DONE;
/// Error code
pReq->iWin32Error = ERROR_INTERNET_OPERATION_CANCELLED;
MyFree( pReq->pszWin32Error );
AllocErrorStr( pReq->iWin32Error, &pReq->pszWin32Error );
break;
case REQUEST_STATUS_DOWNLOADING:
pReq->bAbort = TRUE;
/// The worker thread will do the rest...
if (dwWaitMS) {
/// Wait until it'll finish the job
const DWORD dwAbortWait = 100;
DWORD dwAbortElapsed = 0;
while ((pReq->iStatus == REQUEST_STATUS_DOWNLOADING) && (dwAbortElapsed < dwWaitMS)) {
Sleep( dwAbortWait );
dwAbortElapsed += dwAbortWait;
}
}
break;
case REQUEST_STATUS_DONE:
/// Nothing to do
break;
default: assert( !"Unknown request status" );
}
} else {
bRet = FALSE;
}
return bRet;
}
ULONG QueueSize( _Inout_ PQUEUE pQueue )
{
PQUEUE_REQUEST pReq;
ULONG iSize;
assert( pQueue );
for (pReq = pQueue->pHead, iSize = 0; pReq; pReq = pReq->pNext, iSize++);
TRACE2( _T( " QueueSize(%s) == %u\n" ), pQueue->szName, iSize );
return iSize;
}
ULONG RequestOptimalBufferSize( _In_ PQUEUE_REQUEST pReq )
{
ULONG iSize;
assert( pReq );
if (pReq->Speed.iSpeed > 0) {
/// Use already computed speed (bps)
iSize = pReq->Speed.iSpeed;
} else {
/// Estimate speed based on what's been received so far
ULONG iMS = GetTickCount() - pReq->Speed.iChunkTime;
if (iMS > 10) {
iSize = (pReq->Speed.iChunkSize / iMS) * 1000;
} else {
iSize = MIN_BUFFER_SIZE;
}
}
iSize -= iSize % 1024; /// Align to kilobyte
iSize *= 4; /// Allow the speed to grow. Accommodate more seconds' data...
iSize = __max( iSize, MIN_BUFFER_SIZE );
iSize = __min( iSize, MAX_BUFFER_SIZE );
if (pReq->iLocalType == REQUEST_LOCAL_MEMORY) {
iSize = __min( iSize, (ULONG)((ULONG64)MAX_MEMORY_CONTENT_LENGTH - pReq->iRecvSize) );
}
/* TRACE(
_T( "[BufSize = %.4u KB] Speed = %u bps, ChunkTime = %u (%u ms), ChunkSize = %u\n" ),
iSize / 1024,
pReq->Speed.iSpeed,
pReq->Speed.iChunkTime, GetTickCount() - pReq->Speed.iChunkTime,
pReq->Speed.iChunkSize
);*/
return iSize;
}
BOOL RequestMemoryToString( _In_ PQUEUE_REQUEST pReq, _Out_ LPTSTR pszString, _In_ ULONG iStringLen )
{
BOOL bRet = FALSE;
if (pReq && pszString && iStringLen) {
pszString[0] = _T( '\0' );
if (pReq->iLocalType == REQUEST_LOCAL_MEMORY) {
if (pReq->iStatus == REQUEST_STATUS_DONE) {
if (pReq->iWin32Error == ERROR_SUCCESS && (pReq->iHttpStatus >= 200 && pReq->iHttpStatus < 300)) {
if (pReq->Local.pMemory && pReq->iRecvSize > 0) {
BinaryToString( pReq->Local.pMemory, (ULONG)pReq->iRecvSize, pszString, iStringLen );
bRet = TRUE;
}
}
}
}
}
return bRet;
}
BOOL RequestDataToString( _In_ PQUEUE_REQUEST pReq, _Out_ LPTSTR pszString, _In_ ULONG iStringLen )
{
BOOL bRet = FALSE;
if (pReq && pszString && iStringLen) {
BinaryToString( pReq->pData, pReq->iDataSize, pszString, iStringLen );
bRet = TRUE;
}
return bRet;
}
int QueueWakeThreads( _In_ PQUEUE pQueue, _In_ ULONG iThreadsToWake )
{
int iCount = 0;
assert( pQueue );
if (iThreadsToWake > 0) {
ULONG i, n;
/// Number of threads
n = __min( iThreadsToWake, pQueue->iThreadCount );
/// Determine whether we're already running in a worker thread
for (i = 0; i < pQueue->iThreadCount; i++) {
if (pQueue->pThreads[i].iTID == GetCurrentThreadId()) {
n--; /// One less thread to wake up
break;
}
}
/// Wake
for (i = 0; i < n; i++)
SetEvent( pQueue->hThreadWakeEvent );
iCount += n;
}
return iCount;
}