void BufferThread(void *vp) { BufferThreadData * pThreadData = (BufferThreadData *) vp; int bytesRead; int bytesToRead; DWORD lastUpdate = 0; showPercentage(0); ShowWindowAsync(hwndText, SW_HIDE); ShowWindowAsync(hwndProgress, SW_SHOW); while (!pThreadData->killBufferThread) { // read into the buffer if it isn't full if (((pThreadData->circularBufferEnd + 1) % CIRCULAR_BUFFER_SIZE) != pThreadData->circularBufferStart) { if (pThreadData->circularBufferStart > pThreadData->circularBufferEnd) bytesToRead = pThreadData->circularBufferStart - pThreadData->circularBufferEnd - 1; else bytesToRead = CIRCULAR_BUFFER_SIZE - pThreadData->circularBufferEnd; bytesRead = recv(pThreadData->tcpSocket, pThreadData->circularBuffer + pThreadData->circularBufferEnd, 1, 0); if ((bytesRead == SOCKET_ERROR) || (bytesRead == 0)) break; pThreadData->bytesRead += bytesRead; pThreadData->circularBufferEnd = (pThreadData->circularBufferEnd + bytesRead) % CIRCULAR_BUFFER_SIZE; } else Sleep(10); if (GetTickCount() - lastUpdate > 100) { int start = pThreadData->circularBufferStart; int end = pThreadData->circularBufferEnd; if (end < start) end += CIRCULAR_BUFFER_SIZE; showPercentage((end - start) * 100 / CIRCULAR_BUFFER_SIZE); lastUpdate = GetTickCount(); } } pThreadData->killBufferThread = 1; _endthread(); }
//--------------------------------------------------------------------------- __fastcall TfrmExecutie::TfrmExecutie(TComponent* Owner, String uitv, int percentage) : TfrmTestwin(Owner, percentage == -1) { if (percentage != -1) showPercentage (percentage); uitvaller = uitv; done = false; }
void BufferThread( void *vp ) { BufferThreadData* pThreadData = (BufferThreadData *) vp; int bytesRead = 0; int bytesToRead = 0; #ifdef INCLUDE_STATUS_WINDOW showPercentage(0); ShowWindowAsync(hwndText, SW_HIDE); ShowWindowAsync(hwndProgress, SW_SHOW); #endif while ( !pThreadData->killBufferThread ) { // read into the buffer if it isn't full if ( pThreadData->circularBufferFilled < CIRCULAR_BUFFER_SIZE ) { if ( pThreadData->circularBufferStart > pThreadData->circularBufferEnd ) { bytesToRead = pThreadData->circularBufferStart - pThreadData->circularBufferEnd; } else { bytesToRead = CIRCULAR_BUFFER_SIZE - pThreadData->circularBufferEnd; } if ( bytesToRead > 4096 ) { bytesToRead = 4096; } bytesRead = recv( pThreadData->tcpSocket, pThreadData->circularBuffer + pThreadData->circularBufferEnd, bytesToRead, 0 ); if ( ( SOCKET_ERROR == bytesRead ) || ( bytesRead == 0 ) ) { break; } pThreadData->bytesRead += bytesRead; pThreadData->circularBufferFilled += bytesRead; pThreadData->circularBufferEnd = (pThreadData->circularBufferEnd + bytesRead) % CIRCULAR_BUFFER_SIZE; } else { // wait for more data to come Sleep(50); } } pThreadData->killBufferThread = 1; _endthread(); }
size_t httpRead( void* ptr, size_t size, size_t nmemb, void* datasource ) { int bytesRequested = size * nmemb; int bytesRead = 0; int bytesToCopy; BufferThreadData * pThreadData = (BufferThreadData *) datasource; while ((bytesRead < bytesRequested) && (!pThreadData->killBufferThread || (pThreadData->circularBufferFilled > 0))) { if (pThreadData->circularBufferFilled > 0) { if (pThreadData->circularBufferStart < pThreadData->circularBufferEnd) bytesToCopy = pThreadData->circularBufferEnd - pThreadData->circularBufferStart; else bytesToCopy = CIRCULAR_BUFFER_SIZE - pThreadData->circularBufferStart; if (bytesRead + bytesToCopy > bytesRequested) bytesToCopy = bytesRequested - bytesRead; memcpy((char *) ptr + bytesRead, pThreadData->circularBuffer + pThreadData->circularBufferStart, bytesToCopy); pThreadData->circularBufferStart = (pThreadData->circularBufferStart + bytesToCopy) % CIRCULAR_BUFFER_SIZE; pThreadData->circularBufferFilled -= bytesToCopy; #ifdef INCLUDE_STATUS_WINDOW showPercentage(pThreadData->circularBufferFilled * 100 / CIRCULAR_BUFFER_SIZE); #endif bytesRead += bytesToCopy; } } /* Who knows... maybe there's still some data in the buffer? *CM* // Don't give any data back if we're supposed to be dead if (pThreadData->killBufferThread) return 0; */ return bytesRead / size; }