//void ConsumeData(void*) //{ // while (!g_ProducerStarted) // { // Sleep(10); // } // // FileHandle hConsumer = CreateFile(LN250FileName.c_str(), // GENERIC_WRITE, // FILE_SHARE_READ | FILE_SHARE_WRITE, // NULL, // OPEN_EXISTING, 0, NULL); // // if (!hConsumer.isValid()) // { // UCSBUtility::LogError(__FUNCTION__, __FILE__, __LINE__, "Failed to open file %ls %d\\n", LN250FileName.c_str(), // GetLastError()); // return; // } // // char buffer[1024]; // DWORD totalRead = 0; // while(g_ProducerStarted) // { // //DWORD size = GetFileSize(hFile, NULL); // DWORD numRead = 0; // ReadFile(hConsumer, &buffer, sizeof(buffer), &numRead, NULL); // totalRead += numRead; // Sleep(sleepConsumer); // } // // UCSBUtility::LogError(__FUNCTION__, __FILE__, __LINE__, "Total Bytes Read %d\\n", totalRead); //}; // // int _tmain(int argc, _TCHAR* argv[]) { HANDLE hFile = CreateFile(LN250FileName.c_str(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL); CloseHandle(hFile); FileHandle hProducer = CreateFile(LN250FileName.c_str(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 0, NULL); if (!hProducer.isValid()) { UCSBUtility::LogError(__FUNCTION__, __FILE__, __LINE__, "Failed to open file %ls %d\\n", LN250FileName.c_str(), GetLastError()); return 1; } HANDLE hThread0 = (HANDLE) _beginthread(ProduceData, 0, (HANDLE)hProducer); HANDLE hThread1 = (HANDLE) _beginthread(ProduceData, 0, (HANDLE)hProducer); HANDLE handles[] = { hThread0, hThread1 }; WaitForMultipleObjects(2, handles, true, INFINITE); return 0; }
// This is the heart of the download code // (that's separate from the priority queue, which is the heart of // the timing mechanism) // This simply does what it claims // Reads a file from the web given an existing internet connection and // source and destination names int ReadWebFile(HINTERNET iNetConnection, wstring const& sourceName, wstring const& destinationName) { InternetHandle hURL = HttpOpenRequest(iNetConnection, L"POST", // GET or POST sourceName.c_str(), // root "/" by default NULL, // Use default HTTP/1.1 as the version NULL, // Do not provide any referrer NULL, // Do not provide Accept types 0, NULL); if (hURL == NULL) { //Error = GetLastError(); //LogInetError(Error, L"HttpOpenRequest"); return 1; } if (!HttpSendRequest(hURL, NULL, NULL, NULL, 0)) { UCSBUtility::LogError(__FUNCTION__, __FILE__, __LINE__, "HttpSendRequest %d\n", GetLastError()); return 1; } wstring lengthData; lengthData.resize(256); DWORD QIRead = lengthData.size(); DWORD index = 0; if (!HttpQueryInfo(hURL, HTTP_QUERY_CONTENT_LENGTH, &lengthData[0], &QIRead, &index)) { DWORD lastError = GetLastError (); UCSBUtility::LogError(__FUNCTION__, __FILE__, __LINE__, "Unable to get the content length (lastError = %d)\n", lastError); vector<char> temp; temp.resize(QIRead); HttpQueryInfo(hURL, HTTP_QUERY_CONTENT_LENGTH, &temp[0], &QIRead, &index); return 1; } DWORD contentLength = _wtoi(lengthData.c_str()); UCSBUtility::LogError(__FUNCTION__, __FILE__, __LINE__, "Opening file: %ls\nLength: %d bytes\n", sourceName.c_str(), contentLength); //printf("Opening file: %ls\nLength: %d bytes\n", sourceName.c_str(), contentLength); vector<char> buffer; buffer.resize(contentLength + 1); DWORD bytesRead = 0; BOOL bRead = InternetReadFile(hURL, &buffer[0], buffer.size() - 1, &bytesRead); if (!bRead || bytesRead == 0) { UCSBUtility::LogError(__FUNCTION__, __FILE__, __LINE__, "Failed to get the data\n"); return 1; } //printf ("%d bytes\n", bytesRead); FileHandle hFile (CreateFile(destinationName.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)); if (!hFile.isValid()) { UCSBUtility::LogError(__FUNCTION__, __FILE__, __LINE__, "Unable to open output file\n"); return 1; } DWORD bytesWritten = 0; if (!WriteFile(hFile, &buffer[0], bytesRead, &bytesWritten, NULL)) { UCSBUtility::LogError(__FUNCTION__, __FILE__, __LINE__, "Failed to write the file\n"); return 1; } return 0; }