-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHTTPGetFilesThread.cpp
422 lines (347 loc) · 10.7 KB
/
HTTPGetFilesThread.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
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "HTTPGetFilesThread.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
__fastcall Task::Task(PFileList pFileList, HINTERNET hInternet)
{
m_dwId = pFileList->dwTaskId;
m_asHost = pFileList->asHost;
m_pFiles = pFileList->pFiles;
m_pBuffer = new byte[BUFFER_SIZE];
m_hConnect = m_hRequest = NULL;
m_pReadyFiles = new TList;
m_pCurrentFile = NULL;
m_dwTimeout = 3000;
if(hInternet)
m_hConnect = InternetConnect(hInternet, String(m_asHost).c_str(),
INTERNET_DEFAULT_HTTP_PORT,
NULL,NULL,
INTERNET_SERVICE_HTTP,
0, m_dwId);
m_eStatus = (m_hConnect) ? tsReady : tsError;
}
__fastcall Task::~Task()
{
if(m_hConnect) InternetCloseHandle(m_hConnect);
if(m_hRequest) InternetCloseHandle(m_hRequest);
while(m_pFiles->Count)
{
PFileDescr pItem = (PFileDescr)m_pFiles->Items[0];
delete pItem;
m_pFiles->Delete(0);
}
delete m_pFiles;
if(m_pCurrentFile)
{
if(m_pCurrentFile->pData) delete m_pCurrentFile->pData;
delete m_pCurrentFile;
}
while(m_pReadyFiles->Count)
{
PReadyFile pItem = (PReadyFile)m_pReadyFiles->Items[0];
if(pItem->pData) delete pItem->pData;
delete pItem;
m_pReadyFiles->Delete(0);
}
delete m_pReadyFiles;
delete [] m_pBuffer;
}
void __fastcall Task::Touch()
{
if(m_hConnect)
{
if(!m_pCurrentFile)
{
m_pCurrentFile = new tagReadyFile;
m_pCurrentFile->dwTaskId = m_dwId;
for(int i = 0; i < FILE_DESCR_ID_SIZE; i++)
m_pCurrentFile->sFileDescr.dwFileId[i] = ((PFileDescr)m_pFiles->Items[0])->dwFileId[i];
m_pCurrentFile->sFileDescr.asFileName = ((PFileDescr)m_pFiles->Items[0])->asFileName;
m_pCurrentFile->dwErrorCode = 0;
m_pCurrentFile->pData = NULL;
m_dwCurrentFileLength = 0;
}
if(m_eStatus == tsReady || m_eStatus == tsFileReady)
{
LPCTSTR rgpszAcceptTypes[] = {TEXT("*/*"), NULL};
m_hRequest = HttpOpenRequest(m_hConnect,
TEXT("GET"),
String(((PFileDescr)m_pFiles->Items[0])->asFileName).c_str(),
TEXT("HTTP/1.1"), NULL,
rgpszAcceptTypes,
INTERNET_FLAG_KEEP_CONNECTION |
INTERNET_FLAG_NO_CACHE_WRITE |
INTERNET_FLAG_PRAGMA_NOCACHE |
INTERNET_FLAG_RELOAD,
m_dwId);
if(m_hRequest && HttpSendRequest(m_hRequest, NULL, 0, NULL, 0))
{
m_eStatus = tsReqSent;
m_dwWaitFor = GetTickCount() + m_dwTimeout;
}
else m_eStatus = tsError;
}
else if(m_eStatus == tsReqSent)
{
DWORD dwIndex = 0;
DWORD dwBufferSize = BUFFER_SIZE - 1;
//if(InternetQueryDataAvailable(m_hRequest, &m_dwCurrentFileLength, 0, 0))
if(HttpQueryInfo(m_hRequest, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_CONTENT_LENGTH, m_pBuffer, &dwBufferSize, &dwIndex))
{
m_dwCurrentFileLength = *((DWORD*)m_pBuffer);
m_eStatus = tsFileReading;
m_dwWaitFor = GetTickCount() + m_dwTimeout;
}
else if(GetTickCount() > m_dwWaitFor) m_eStatus = tsError;
}
else if(m_eStatus == tsFileReading)
{
DWORD dwBytesRead = 0;
if(InternetReadFile(m_hRequest, m_pBuffer, BUFFER_SIZE - 1, &dwBytesRead))
{
if(dwBytesRead)
{
m_dwWaitFor = GetTickCount() + m_dwTimeout;
if(!m_pCurrentFile->pData) m_pCurrentFile->pData = new TMemoryStream;
m_pCurrentFile->pData->Write(m_pBuffer, dwBytesRead);
if(m_pCurrentFile->pData->Size == m_dwCurrentFileLength)
{
m_pCurrentFile->pData->Seek(0, soFromBeginning);
m_pReadyFiles->Add(m_pCurrentFile);
m_pCurrentFile = NULL;
m_dwCurrentFileLength = 0;
PFileDescr pFileDescr = (PFileDescr)m_pFiles->Items[0];
delete pFileDescr;
m_pFiles->Delete(0);
if(!m_pFiles->Count) m_eStatus = tsComplete;
else m_eStatus = tsFileReady;
}
}
else if(GetTickCount() > m_dwWaitFor) m_eStatus = tsError;
}
else m_eStatus = tsError;
}
if(m_eStatus == tsError)
{
if(m_pCurrentFile->pData)
{
delete m_pCurrentFile->pData;
m_pCurrentFile->pData = NULL;
}
m_pCurrentFile->dwErrorCode = GetLastError();
m_pReadyFiles->Add(m_pCurrentFile);
m_pCurrentFile = NULL;
m_dwCurrentFileLength = 0;
PFileDescr pFileDescr = (PFileDescr)m_pFiles->Items[0];
delete pFileDescr;
m_pFiles->Delete(0);
if(!m_pFiles->Count) m_eStatus = tsComplete;
else m_eStatus = tsFileReady;
}
}
}
bool __fastcall Task::GetConnected()
{
return(m_hConnect != NULL);
}
DWORD __fastcall Task::GetId()
{
return m_dwId;
}
TList* __fastcall Task::GetReadyFiles()
{
return m_pReadyFiles;
}
PReadyFile __fastcall Task::GetReadyFile()
{
PReadyFile pReadyFile = NULL;
if(m_pReadyFiles->Count)
{
pReadyFile = (PReadyFile)m_pReadyFiles->Items[0];
m_pReadyFiles->Delete(0);
}
return pReadyFile;
}
bool __fastcall Task::GetComplete()
{
return (m_eStatus == tsComplete);
}
bool __fastcall Task::GetFileReady()
{
return (m_pReadyFiles->Count > 0);
}
//---------------------------------------------------------------------------
// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
//
// Synchronize(UpdateCaption);
//
// where UpdateCaption could look like:
//
// void __fastcall THTTPGetFilesThread::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }
//---------------------------------------------------------------------------
__fastcall THTTPGetFilesThread::THTTPGetFilesThread(bool CreateSuspended)
: TThread(CreateSuspended)
{
m_pTaskList = new TList;
m_pReadyTasks = new TThreadList;
m_pNewTaskEvent = new TSimpleEvent(false);
m_pFileReadyEvent = new TSimpleEvent(false);
m_pDeleteTaskEvent = new TSimpleEvent(false);
m_dwNewTaskId = 1;
m_hInternet = NULL;
m_pNewTask = NULL;
m_dwCurrentTaskId = 0xFFFFFFFF;
m_dwTaskToDelete = 0xFFFFFFFF;
}
//---------------------------------------------------------------------------
void __fastcall THTTPGetFilesThread::Execute()
{
m_hInternet = InternetOpen(TEXT("Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.0; .NET CLR 1.0.2914)"),
INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if(m_hInternet)
{
while(!Terminated)
{
if(m_pNewTaskEvent->WaitFor(1) == wrSignaled)
{
PTask pNewTask = new Task(m_pNewTask, m_hInternet);
m_pTaskList->Add(pNewTask);
delete m_pNewTask;
m_pNewTask = NULL;
m_pNewTaskEvent->ResetEvent();
}
if(m_pDeleteTaskEvent->WaitFor(1) == wrSignaled)
{
for(int i = 0; i < m_pTaskList->Count; i++)
{
PTask pTask = (PTask)m_pTaskList->Items[i];
if(m_dwTaskToDelete == pTask->Id)
{
m_pTaskList->Delete(i);
delete pTask;
break;
}
}
m_pDeleteTaskEvent->ResetEvent();
}
int i = 0;
while(i < m_pTaskList->Count)
{
PTask pTask = (PTask)m_pTaskList->Items[i];
if(pTask && pTask->Connected)
{
pTask->Touch();
if(pTask->FileReady || pTask->Complete)
{
PReadyFile pReadyFile = pTask->ReadyFile;
while(pReadyFile)
{
m_pReadyTasks->LockList()->Add(pReadyFile);
m_pReadyTasks->UnlockList();
m_pFileReadyEvent->SetEvent();
pReadyFile = pTask->ReadyFile;
}
}
if(pTask->Complete && !pTask->FileReady)
{
delete pTask;
m_pTaskList->Delete(i);
}
else i++;
}
}
Sleep(5);
}
InternetCloseHandle(m_hInternet);
}
}
//---------------------------------------------------------------------------
__fastcall THTTPGetFilesThread::~THTTPGetFilesThread()
{
delete m_pFileReadyEvent;
delete m_pNewTaskEvent;
delete m_pDeleteTaskEvent;
while(m_pTaskList->Count)
{
PTask pItem = (PTask)m_pTaskList->Items[0];
delete pItem;
m_pTaskList->Delete(0);
}
delete m_pTaskList;
TList *pList = m_pReadyTasks->LockList();
while(pList->Count)
{
PReadyFile pTask = (PReadyFile)pList->Items[0];
delete pTask->pData;
delete pTask;
pList->Delete(0);
}
m_pReadyTasks->UnlockList();
delete m_pReadyTasks;
}
DWORD __fastcall THTTPGetFilesThread::AddTask(PFileList pFiles)
{
DWORD dwTaskId = 0xFFFFFFFF;
DWORD dwWaitFor = GetTickCount() + 1000;
while(m_pNewTaskEvent->WaitFor(1) == wrSignaled && dwWaitFor < GetTickCount());
if(m_pNewTaskEvent->WaitFor(1) == wrTimeout)
{
dwTaskId = m_dwNewTaskId++;
if(m_dwNewTaskId == 0xFFFFFFFF) m_dwNewTaskId = 1;
pFiles->dwTaskId = dwTaskId;
m_pNewTask = new tagFileList;
m_pNewTask->pFiles = new TList;
m_pNewTask->asHost = pFiles->asHost;
m_pNewTask->dwTaskId = pFiles->dwTaskId;
for(int i = 0; i < pFiles->pFiles->Count; i++)
{
PFileDescr pNewFileDescr = new tagFileDescr;
PFileDescr pFiledescr = (PFileDescr)pFiles->pFiles->Items[i];
for(int j = 0; j < FILE_DESCR_ID_SIZE; j++)
pNewFileDescr->dwFileId[j] = pFiledescr->dwFileId[j];
pNewFileDescr->asFileName = pFiledescr->asFileName;
m_pNewTask->pFiles->Add(pNewFileDescr);
}
m_pNewTaskEvent->SetEvent();
}
return dwTaskId;
}
PReadyFile __fastcall THTTPGetFilesThread::GetReadyFile()
{
PReadyFile pReadyFile = NULL;
TList *pList = m_pReadyTasks->LockList();
if(pList->Count)
{
pReadyFile = (PReadyFile)pList->Items[0];
pList->Delete(0);
if(!pList->Count) m_pFileReadyEvent->ResetEvent();
}
m_pReadyTasks->UnlockList();
return pReadyFile;
}
bool __fastcall THTTPGetFilesThread::GetFileReady()
{
return (m_pFileReadyEvent->WaitFor(1) == wrSignaled);
}
bool __fastcall THTTPGetFilesThread::DeleteTask(DWORD dwTaskId)
{
bool brc = false;
DWORD dwWaitFor = GetTickCount() + 1000;
while(m_pDeleteTaskEvent->WaitFor(1) == wrSignaled && dwWaitFor < GetTickCount());
if(m_pDeleteTaskEvent->WaitFor(1) == wrTimeout)
{
m_dwTaskToDelete = dwTaskId;
m_pDeleteTaskEvent->SetEvent();
}
return brc;
}
bool __fastcall THTTPGetFilesThread::GetComplete()
{
return (m_pTaskList->Count == 0);
}