void AAudioVolume::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) { Super::PostEditChangeProperty(PropertyChangedEvent); Settings.Volume = FMath::Clamp<float>( Settings.Volume, 0.0f, 1.0f ); AmbientZoneSettings.InteriorTime = FMath::Max<float>( 0.01f, AmbientZoneSettings.InteriorTime ); AmbientZoneSettings.InteriorLPFTime = FMath::Max<float>( 0.01f, AmbientZoneSettings.InteriorLPFTime ); AmbientZoneSettings.ExteriorTime = FMath::Max<float>( 0.01f, AmbientZoneSettings.ExteriorTime ); AmbientZoneSettings.ExteriorLPFTime = FMath::Max<float>( 0.01f, AmbientZoneSettings.ExteriorLPFTime ); if (PropertyChangedEvent.Property && PropertyChangedEvent.Property->GetFName() == GET_MEMBER_NAME_CHECKED(AAudioVolume, Priority)) { GetWorld()->AudioVolumes.Sort([](const AAudioVolume& A, const AAudioVolume& B) { return (A.GetPriority() > B.GetPriority()); }); } if (PropertyChangedEvent.Property && PropertyChangedEvent.Property->GetFName() == GET_MEMBER_NAME_CHECKED(AAudioVolume, bEnabled)) { if (bEnabled) { AddProxy(); } else { RemoveProxy(); } } else if (bEnabled) { UpdateProxy(); } }
void AAudioVolume::SetInteriorSettings(const FInteriorSettings& NewInteriorSettings) { if (NewInteriorSettings != AmbientZoneSettings) { AmbientZoneSettings = NewInteriorSettings; if (bEnabled) { UpdateProxy(); } } }
void AAudioVolume::SetReverbSettings(const FReverbSettings& NewReverbSettings) { if (NewReverbSettings != Settings) { Settings = NewReverbSettings; if (bEnabled) { UpdateProxy(); } } }
void AAudioVolume::SetPriority(const float NewPriority) { if (NewPriority != Priority) { Priority = NewPriority; GetWorld()->AudioVolumes.Sort([](const AAudioVolume& A, const AAudioVolume& B) { return (A.GetPriority() > B.GetPriority()); }); if (bEnabled) { UpdateProxy(); } } }
int CloHttpCurl::TransferProcPOSTFile() { SetState( E_HTTPSTATE_RUNNING ); THttpMemoryBuffer headerBuffer; THttpMemoryBuffer bodyBuffer; // 启动curl m_pAssoc->m_curlHandle = curl_easy_init(); // 设置访问链接 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_URL, m_pAssoc->m_pTransferParam->cURL); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_VERBOSE, 1L); // 设置附加的Header struct curl_slist *chunk = NULL; if( strlen(m_pAssoc->m_pTransferParam->cHead) > 0 ) { BuildCustomHeader(&chunk, m_pAssoc->m_pTransferParam->cHead); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HTTPHEADER, chunk); } // 添加POST文件信息 struct curl_httppost *post=NULL; struct curl_httppost *last=NULL; #ifdef _UNICODE CString strLocalFile = (CA2T)m_pAssoc->m_pTransferParam->cLocalFile; #else CString strLocalFile = m_pAssoc->m_pTransferParam->cLocalFile; #endif if( locom::CloFile::IsFileExists(strLocalFile) ) { curl_formadd(&post, &last,CURLFORM_COPYNAME, m_pAssoc->m_pTransferParam->pData, CURLFORM_FILE, m_pAssoc->m_pTransferParam->cLocalFile, CURLFORM_END); /* Set the form info */ curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HTTPPOST, post); } // 设置代理信息 UpdateProxy(m_pAssoc->m_pTransferParam->cURL); // 设置传输状态回调 SetTransferCallback((void *)&headerBuffer, (void *)&bodyBuffer); // 设置访问Agent curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_USERAGENT, "httpclient-vika/1.0"); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_FOLLOWLOCATION, 1); // 自动重定向 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_AUTOREFERER, 1); // 启动获取 CURLcode code = curl_easy_perform(m_pAssoc->m_curlHandle); // 结果处理 int ret = 0; if( code == CURLE_OK ) { ClearBuffer(); // 保存任务结果数据 SAFE_NEW_MEMORYBUFFER(&m_pAssoc->m_HeaderRecv,headerBuffer.size,headerBuffer.size); COPY_MEMORYBUFFER(&headerBuffer,&m_pAssoc->m_HeaderRecv); SAFE_NEW_MEMORYBUFFER(&m_pAssoc->m_BodyRecv,bodyBuffer.size,bodyBuffer.size); COPY_MEMORYBUFFER(&bodyBuffer,&m_pAssoc->m_BodyRecv); // 保存任务结果 m_pAssoc->m_nResult = E_HTTPTRANS_RESULT_COMPLETE; ret = 0; } else { m_pAssoc->m_nResult = E_HTTPTRANS_RESULT_FAILED; m_pAssoc->m_ErrorCode = E_HTTPTASK_ERR_CURL_EXCEPTION; ret = -1; } // 清理缓存 // 这里有点特别,因为是通过 BufferRealloc() 分配的 SAFE_FREE_BUFFER( headerBuffer.buffer ); SAFE_FREE_BUFFER( bodyBuffer.buffer ); m_pAssoc->SetCURLCode( code ); TaskCode(); // 清理表单信息 if( post != NULL ) curl_formfree(post); // 清理Header参数信息 if( chunk != NULL ) curl_slist_free_all(chunk); // 释放curl curl_easy_cleanup(m_pAssoc->m_curlHandle); m_pAssoc->m_curlHandle = NULL; // 任务结束 SetState( E_HTTPSTATE_STOP ); return ret; }
int CloHttpCurl::TransferProcPOST() { SetState( E_HTTPSTATE_RUNNING ); THttpMemoryBuffer headerBuffer; THttpMemoryBuffer bodyBuffer; // 启动curl m_pAssoc->m_curlHandle = curl_easy_init(); // 设置访问链接 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_URL, m_pAssoc->m_pTransferParam->cURL); // 设置附加的Header struct curl_slist *chunk = NULL; if( strlen(m_pAssoc->m_pTransferParam->cHead) > 0 ) { BuildCustomHeader(&chunk, m_pAssoc->m_pTransferParam->cHead); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HTTPHEADER, chunk); } // 设置代理信息 UpdateProxy(m_pAssoc->m_pTransferParam->cURL); // 设置Post数据 if( m_pAssoc->m_pTransferParam->bFormFormat ) { curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_POSTFIELDS, m_pAssoc->m_pTransferParam->pData); // 如果是表单,不要定义长度 } else { curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_POSTFIELDS, m_pAssoc->m_pTransferParam->pData); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_POSTFIELDSIZE, m_pAssoc->m_pTransferParam->uDataLength); } // 设置传输状态回调 SetTransferCallback((void *)&headerBuffer, (void *)&bodyBuffer); // 设置访问Agent curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_USERAGENT, "httpclient-vika/1.0"); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_FOLLOWLOCATION, 1); // 自动重定向 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_AUTOREFERER, 1); // 启动获取 CURLcode code = curl_easy_perform(m_pAssoc->m_curlHandle); // 清理附加的Header参数信息 if( chunk != NULL ) curl_slist_free_all(chunk); // 结果处理 int ret = 0; if( code == CURLE_OK ) { ClearBuffer(); // 保存任务结果数据 SAFE_NEW_MEMORYBUFFER(&m_pAssoc->m_HeaderRecv,headerBuffer.size,headerBuffer.size); COPY_MEMORYBUFFER(&headerBuffer,&m_pAssoc->m_HeaderRecv); SAFE_NEW_MEMORYBUFFER(&m_pAssoc->m_BodyRecv,bodyBuffer.size,bodyBuffer.size); COPY_MEMORYBUFFER(&bodyBuffer,&m_pAssoc->m_BodyRecv); // 保存任务结果 m_pAssoc->m_nResult = E_HTTPTRANS_RESULT_COMPLETE; ret = 0; } else { m_pAssoc->m_nResult = E_HTTPTRANS_RESULT_FAILED; m_pAssoc->m_ErrorCode = E_HTTPTASK_ERR_CURL_EXCEPTION; ret = -1; } // 清理缓存 // 这里有点特别,因为是通过 BufferRealloc() 分配的 SAFE_FREE_BUFFER( headerBuffer.buffer ); SAFE_FREE_BUFFER( bodyBuffer.buffer ); m_pAssoc->SetCURLCode( code ); TaskCode(); // 释放curl curl_easy_cleanup(m_pAssoc->m_curlHandle); m_pAssoc->m_curlHandle = NULL; // 任务结束 SetState( E_HTTPSTATE_STOP ); return ret; }
int CloHttpCurl::TransferProcDOWNLOAD2() { THttpMemoryBuffer headerBuffer; SetState( E_HTTPSTATE_RUNNING ); // 启动curl m_pAssoc->m_curlHandle = curl_easy_init(); // 设置访问链接 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_URL, m_pAssoc->m_pTransferParam->cURL); // 设置附加的Header struct curl_slist *chunk = NULL; if( strlen(m_pAssoc->m_pTransferParam->cHead) > 0 ) { BuildCustomHeader(&chunk, m_pAssoc->m_pTransferParam->cHead); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HTTPHEADER, chunk); } // 设置代理信息 UpdateProxy(m_pAssoc->m_pTransferParam->cURL); // 设置访问Agent curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_USERAGENT, "httpclient-vika/1.0"); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_AUTOREFERER, 1); // 设置读取,保存数据的函数 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_WRITEFUNCTION, ToBufferCallback); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_WRITEDATA, this); // 先设置获取Header的函数 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HEADERFUNCTION, ToHeaderCallback); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HEADERDATA, (void *)&headerBuffer); // 设置progress回写 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_NOPROGRESS, 0); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_PROGRESSFUNCTION, ProgressCallback); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_PROGRESSDATA, this); // 开始获取 CURLcode res = curl_easy_perform(m_pAssoc->m_curlHandle); // 结果处理 int ret = 0; if( res == CURLE_OK ) { // 保存任务结果数据 SAFE_NEW_MEMORYBUFFER(&m_pAssoc->m_HeaderRecv,headerBuffer.size,headerBuffer.size); COPY_MEMORYBUFFER(&headerBuffer,&m_pAssoc->m_HeaderRecv); // 保存任务结果 m_pAssoc->m_nResult = E_HTTPTRANS_RESULT_COMPLETE; ret = 0; } else { m_pAssoc->m_nResult = E_HTTPTRANS_RESULT_FAILED; m_pAssoc->m_ErrorCode = E_HTTPTASK_ERR_CURL_EXCEPTION; ret = -1; } // 清理缓存 // 这里有点特别,因为是通过 BufferRealloc() 分配的 SAFE_FREE_BUFFER( headerBuffer.buffer ); m_pAssoc->SetCURLCode( res ); TaskCode(); // 释放curl curl_easy_cleanup(m_pAssoc->m_curlHandle); m_pAssoc->m_curlHandle = NULL; // 任务结束 SetState( E_HTTPSTATE_STOP ); return ret; }
int CloHttpCurl::TransferProcDOWNLOAD() { if( m_pAssoc->m_pTransferParam->bToBuffer ) // 下载到内存 { return TransferProcDOWNLOAD2(); } /////////////////////////////////////////////////////////////////// // 下载到文件 SetState( E_HTTPSTATE_RUNNING ); THttpMemoryBuffer headerBuffer; // 打开本地文件 THttpFileBuffer fileBuffer; memset(&fileBuffer, 0, sizeof(THttpFileBuffer)); // 关闭文件 #define FILE_CLOSE(p) if(p) { fclose(p);(p) = NULL;} // 看看文件夹是否有效 #ifdef _UNICODE if( !locom::CloFile::IsDirectoryExists( (CA2T)m_pAssoc->m_pTransferParam->cSavePath) ) { locom::CloFile::CreateDirectory( (CA2T)m_pAssoc->m_pTransferParam->cSavePath ); } #else if (!locom::CloFile::IsDirectoryExists(m_pAssoc->m_pTransferParam->cSavePath) ) { locom::CloFile::CreateDirectory(m_pAssoc->m_pTransferParam->cSavePath); } #endif // 启动curl //CURL_INIT(); m_pAssoc->m_curlHandle = curl_easy_init(); // 设置访问链接 CURLcode res = curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_URL, m_pAssoc->m_pTransferParam->cURL); _recvHeader: if( GetState() == E_HTTPSTATE_STOP ) //genobili add on 2010 - 01 - 22 { // 有错误发生 m_pAssoc->m_nResult = E_HTTPTRANS_RESULT_FAILED; m_pAssoc->m_ErrorCode = E_HTTPTASK_ERR_CURL_EXCEPTION; FILE_CLOSE( fileBuffer.fp ); // 清理缓存 // 这里有点特别,因为是通过 BufferRealloc() 分配的 SAFE_FREE_BUFFER( headerBuffer.buffer ); m_pAssoc->SetCURLCode(res); TaskCode(); // 释放curl curl_easy_cleanup(m_pAssoc->m_curlHandle); m_pAssoc->m_curlHandle = NULL; // 任务结束 SetState( E_HTTPSTATE_STOP ); return -1; } // 设置附加的Header struct curl_slist *chunk = NULL; if( strlen(m_pAssoc->m_pTransferParam->cHead) > 0 ) { BuildCustomHeader(&chunk, m_pAssoc->m_pTransferParam->cHead); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HTTPHEADER, chunk); } // 设置代理信息 UpdateProxy(m_pAssoc->m_pTransferParam->cURL); // 设置访问Agent curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_USERAGENT, "httpclient-vika/1.0"); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_AUTOREFERER, 1); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_NOBODY, 1); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HEADER, 1); // 先设置获取Header的函数 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HEADERFUNCTION, ToHeaderCallback); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HEADERDATA, (void *)&headerBuffer); curl_easy_perform(m_pAssoc->m_curlHandle); // 获取http code long httpCode = 0; res = curl_easy_getinfo(m_pAssoc->m_curlHandle, CURLINFO_HTTP_CODE, &httpCode); // 成功 if( httpCode == 200 && CURLE_OK == res ) { double contentLength = 0; curl_easy_getinfo(m_pAssoc->m_curlHandle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &contentLength); char * url = NULL; curl_easy_getinfo(m_pAssoc->m_curlHandle, CURLINFO_EFFECTIVE_URL, (void *)&url); // 如果没有指定文件名 if( !m_pAssoc->m_pTransferParam->bRename || strlen(m_pAssoc->m_pTransferParam->cNewName) == 0 ) { // 获取有效的文件名 std::string filename = GetUrlFileName(url); if( filename.length() > 0 ) { strcpy_s(m_pAssoc->m_pTransferParam->cNewName, 1024, filename.c_str()); } } if( strlen(m_pAssoc->m_pTransferParam->cNewName) > 0 ) { // 以新建方式,创建本地文件 std::string filePath = m_pAssoc->m_pTransferParam->cSavePath; filePath += "\\"; filePath += m_pAssoc->m_pTransferParam->cNewName; FILE * fp; errno_t err = fopen_s(&fp, filePath.c_str(), "w+b"); if( err != 0 ) { // 创建本地文件失败 m_pAssoc->m_nResult = E_HTTPTRANS_RESULT_FAILED; m_pAssoc->m_ErrorCode = E_HTTPTASK_ERR_FAIL_TO_OPEN_LOCAL_FILE; // 清理缓存 // 这里有点特别,因为是通过 BufferRealloc() 分配的 SAFE_FREE_BUFFER(headerBuffer.buffer); // 释放curl curl_easy_cleanup(m_pAssoc->m_curlHandle); m_pAssoc->m_curlHandle = NULL; // 任务结束 SetState( E_HTTPSTATE_STOP ); return -1; } // 准备写文件了 fileBuffer.fp = fp; } else { // 错误,没有获取到文件名 m_pAssoc->m_nResult = E_HTTPTRANS_RESULT_FAILED; m_pAssoc->m_ErrorCode = E_HTTPTASK_ERR_CANNOT_GET_REMOTE_FILE_NAME; // FILE_CLOSE( fileBuffer.fp ); // 清理缓存 // 这里有点特别,因为是通过 BufferRealloc() 分配的 SAFE_FREE_BUFFER( headerBuffer.buffer ); // 释放curl curl_easy_cleanup(m_pAssoc->m_curlHandle); m_pAssoc->m_curlHandle = NULL; // 任务结束 SetState( E_HTTPSTATE_STOP ); return -1; } // 先保存一下Header SAFE_NEW_MEMORYBUFFER(&m_pAssoc->m_HeaderRecv,headerBuffer.size,headerBuffer.size); COPY_MEMORYBUFFER(&headerBuffer,&m_pAssoc->m_HeaderRecv); goto _recvBody; } else if( httpCode == 302 && CURLE_OK == res ) { char * pUrl = NULL; curl_easy_getinfo(m_pAssoc->m_curlHandle, CURLINFO_REDIRECT_URL, (void *)&pUrl); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_URL, pUrl); goto _recvHeader; } else { // 有错误发生 m_pAssoc->m_nResult = E_HTTPTRANS_RESULT_FAILED; m_pAssoc->m_ErrorCode = E_HTTPTASK_ERR_CURL_EXCEPTION; FILE_CLOSE( fileBuffer.fp ); // 清理缓存 // 这里有点特别,因为是通过 BufferRealloc() 分配的 SAFE_FREE_BUFFER( headerBuffer.buffer ); m_pAssoc->SetCURLCode(res); TaskCode(); // 释放curl curl_easy_cleanup(m_pAssoc->m_curlHandle); m_pAssoc->m_curlHandle = NULL; // 任务结束 SetState( E_HTTPSTATE_STOP ); return -1; } _recvBody: curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_NOBODY, 0); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HEADER, 0); // 设置读取,保存数据的函数 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_WRITEFUNCTION, ToFileCallback); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_WRITEDATA, (void *)&fileBuffer); // 设置progress回写 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_NOPROGRESS, 0); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_PROGRESSFUNCTION, ProgressCallback); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_PROGRESSDATA, this); // 开始获取 res = curl_easy_perform(m_pAssoc->m_curlHandle); // 结果处理 int ret = 0; if( res == CURLE_OK ) { // 保存任务结果 m_pAssoc->m_nResult = E_HTTPTRANS_RESULT_COMPLETE; ret = 0; } else { m_pAssoc->m_nResult = E_HTTPTRANS_RESULT_FAILED; m_pAssoc->m_ErrorCode = E_HTTPTASK_ERR_CURL_EXCEPTION; ret = -1; } FILE_CLOSE( fileBuffer.fp ); // 清理缓存 // 这里有点特别,因为是通过 BufferRealloc() 分配的 SAFE_FREE_BUFFER( headerBuffer.buffer ); m_pAssoc->SetCURLCode( res ); TaskCode(); // 释放curl curl_easy_cleanup(m_pAssoc->m_curlHandle); m_pAssoc->m_curlHandle = NULL; // 任务结束 SetState( E_HTTPSTATE_STOP ); return ret; }
void AAudioVolume::TransformUpdated(USceneComponent* InRootComponent, EUpdateTransformFlags UpdateTransformFlags, ETeleportType Teleport) { UpdateProxy(); }
int CloHttpCurl::TransferProcPOSTFormData() { THttpMemoryBuffer headerBuffer; THttpMemoryBuffer bodyBuffer; SetState( E_HTTPSTATUS_START ); // 启动curl m_pAssoc->m_curlHandle = curl_easy_init(); // 设置访问链接 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_URL, m_pAssoc->m_pTransferParam->cURL); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_VERBOSE, 1L); // 设置附加的Header struct curl_slist *chunk = NULL; if( strlen(m_pAssoc->m_pTransferParam->cHead) > 0 ) { BuildCustomHeader(&chunk, m_pAssoc->m_pTransferParam->cHead); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HTTPHEADER, chunk); } // 添加POST文件信息 struct curl_httppost *post=NULL; struct curl_httppost *last=NULL; // 设置Post数据 if( m_pAssoc->m_pTransferParam->pData ) { if( m_pAssoc->m_pTransferParam->bFormData ) { // 如果是表单,不要定义长度 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_POSTFIELDS, m_pAssoc->m_pTransferParam->pData); } else { curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_POSTFIELDS, m_pAssoc->m_pTransferParam->pData); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_POSTFIELDSIZE, m_pAssoc->m_pTransferParam->uDataLength); } } // if( m_pAssoc->m_pTransferParam->form.fformadd_cb ) { m_pAssoc->m_pTransferParam->form.fformadd_cb( (void**)&post, (void**)&last , (fun_formadd_write_callback)curl_formadd , m_pAssoc->m_pTransferParam->form.pformdata ); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HTTPPOST, post); } // 设置代理信息 UpdateProxy(m_pAssoc->m_pTransferParam->cURL); // 设置传输状态回调 SetTransferCallback((void *)&headerBuffer, (void *)&bodyBuffer); // 设置访问Agent curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_USERAGENT, GetUserAgent() ); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_FOLLOWLOCATION, 1); // 自动重定向 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_AUTOREFERER, 1); // 启动获取 CURLcode code = curl_easy_perform(m_pAssoc->m_curlHandle); GET_HTTP_CODE(); ClearBuffer(); // 结果处理 if( code == CURLE_OK ) { // 保存任务结果数据 SAFE_NEW_MEMORYBUFFER(&m_pAssoc->m_HeaderRecv,headerBuffer.size,headerBuffer.size); COPY_MEMORYBUFFER(&headerBuffer,&m_pAssoc->m_HeaderRecv); SAFE_NEW_MEMORYBUFFER(&m_pAssoc->m_BodyRecv,bodyBuffer.size,bodyBuffer.size); COPY_MEMORYBUFFER(&bodyBuffer,&m_pAssoc->m_BodyRecv); } // 清理缓存 // 这里有点特别,因为是通过 BufferRealloc() 分配的 SAFE_FREE_BUFFER( headerBuffer.buffer ); SAFE_FREE_BUFFER( bodyBuffer.buffer ); // 清理表单信息 if( post != NULL ) curl_formfree(post); // 清理附加的Header参数信息 if( chunk != NULL ) { curl_slist_free_all(chunk); chunk = 0; } // 释放curl curl_easy_cleanup(m_pAssoc->m_curlHandle); m_pAssoc->m_curlHandle = NULL; m_pAssoc->SetErrCode( code ); TaskCode(); // 任务结束 SetState( E_HTTPSTATUS_STOP ); return (code != CURLE_OK)?-1:0; }
int CloHttpCurl::TransferProcDOWNLOAD_TOBUFFER() { THttpMemoryBuffer headerBuffer; SetState( E_HTTPSTATUS_START ); // 启动curl m_pAssoc->m_curlHandle = curl_easy_init(); // 设置访问链接 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_URL, m_pAssoc->m_pTransferParam->cURL); // 设置附加的Header struct curl_slist *chunk = NULL; if( strlen(m_pAssoc->m_pTransferParam->cHead) > 0 ) { BuildCustomHeader(&chunk, m_pAssoc->m_pTransferParam->cHead); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HTTPHEADER, chunk); } // 设置代理信息 UpdateProxy(m_pAssoc->m_pTransferParam->cURL); // 设置访问Agent curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_USERAGENT, GetUserAgent() ); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_AUTOREFERER, 1); // 设置读取,保存数据的函数 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_WRITEFUNCTION, ToBufferCallback); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_WRITEDATA, this); // 先设置获取Header的函数 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HEADERFUNCTION, ToHeaderCallback); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HEADERDATA, (void *)&headerBuffer); // 设置progress回写 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_NOPROGRESS, 0); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_PROGRESSFUNCTION, ProgressCallback); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_PROGRESSDATA, this); // 开始获取 CURLcode code = curl_easy_perform(m_pAssoc->m_curlHandle); GET_HTTP_CODE(); ClearBuffer(); // 结果处理 if( code == CURLE_OK ) { // 保存任务结果数据 SAFE_NEW_MEMORYBUFFER(&m_pAssoc->m_HeaderRecv,headerBuffer.size,headerBuffer.size); COPY_MEMORYBUFFER(&headerBuffer,&m_pAssoc->m_HeaderRecv); } // 清理缓存 // 这里有点特别,因为是通过 BufferRealloc() 分配的 SAFE_FREE_BUFFER( headerBuffer.buffer ); // 清理附加的Header参数信息 if( chunk != NULL ) { curl_slist_free_all(chunk); chunk = 0; } // 释放curl curl_easy_cleanup(m_pAssoc->m_curlHandle); m_pAssoc->m_curlHandle = NULL; m_pAssoc->SetErrCode( code ); TaskCode(); // 任务结束 SetState( E_HTTPSTATUS_STOP ); return (code != CURLE_OK)?-1:0; }
int CloHttpCurl::TransferProcDOWNLOAD() { if( m_pAssoc->m_pTransferParam->bToBuffer ) // 下载到内存 { return TransferProcDOWNLOAD_TOBUFFER(); } /////////////////////////////////////////////////////////////////// // 下载到文件 SetState( E_HTTPSTATUS_START ); THttpMemoryBuffer headerBuffer; // 打开本地文件 THttpFileBuffer fileBuffer; memset(&fileBuffer, 0, sizeof(THttpFileBuffer)); // 关闭文件 #define FILE_CLOSE(p) if(p) { fclose(p);(p) = NULL;} // 看看文件夹是否有效 #ifdef _UNICODE if( !locom::CloFile::IsDirectoryExists( CA2T(m_pAssoc->m_pTransferParam->cSavePath) ) ) locom::CloFile::CreateDirectory( CA2T(m_pAssoc->m_pTransferParam->cSavePath) ); #else if (!locom::CloFile::IsDirectoryExists(m_pAssoc->m_pTransferParam->cSavePath) ) locom::CloFile::CreateDirectory(m_pAssoc->m_pTransferParam->cSavePath); #endif // 启动curl m_pAssoc->m_curlHandle = curl_easy_init(); // 设置访问链接 CURLcode code = curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_URL, m_pAssoc->m_pTransferParam->cURL); // 设置附加的Header struct curl_slist *chunk = NULL; _recvHeader: int state = GetState(); if( state != E_HTTPSTATUS_START ) // 防止 死loop , { // 有错误发生 FILE_CLOSE( fileBuffer.fp ); // 清理缓存 // 这里有点特别,因为是通过 BufferRealloc() 分配的 SAFE_FREE_BUFFER( headerBuffer.buffer ); // 清理附加的Header参数信息 if( chunk != NULL ) { curl_slist_free_all(chunk); chunk = 0; } // 释放curl curl_easy_cleanup(m_pAssoc->m_curlHandle); m_pAssoc->m_curlHandle = NULL; m_pAssoc->SetErrCode(code); TaskCode(); // 任务结束 SetState( E_HTTPSTATUS_STOP ); return -1; } if( strlen(m_pAssoc->m_pTransferParam->cHead) > 0 ) { BuildCustomHeader(&chunk, m_pAssoc->m_pTransferParam->cHead); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HTTPHEADER, chunk); } // 设置代理信息 UpdateProxy(m_pAssoc->m_pTransferParam->cURL); // 设置访问Agent curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_USERAGENT, GetUserAgent() ); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_AUTOREFERER, 1); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_NOBODY, 1); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HEADER, 1); // 先设置获取Header的函数 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HEADERFUNCTION, ToHeaderCallback); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HEADERDATA, (void *)&headerBuffer); curl_easy_perform(m_pAssoc->m_curlHandle); // 获取http code m_pAssoc->m_lhttpCode = 0; code = curl_easy_getinfo(m_pAssoc->m_curlHandle, CURLINFO_HTTP_CODE, &m_pAssoc->m_lhttpCode); // 成功 if( m_pAssoc->m_lhttpCode == 200 && CURLE_OK == code ) { double contentLength = 0; char * url = NULL; curl_easy_getinfo(m_pAssoc->m_curlHandle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &contentLength); curl_easy_getinfo(m_pAssoc->m_curlHandle, CURLINFO_EFFECTIVE_URL, (void *)&url); // 如果没有指定文件名 if( !m_pAssoc->m_pTransferParam->bRename || strlen(m_pAssoc->m_pTransferParam->cNewFileName) == 0 ) { // 获取有效的文件名 std::string filename = GetUrlFileName(url); if( filename.length() > 0 ) { strcpy_s(m_pAssoc->m_pTransferParam->cNewFileName, 1024, filename.c_str()); } } if( strlen(m_pAssoc->m_pTransferParam->cNewFileName) > 0 ) { // 以新建方式,创建本地文件 char cNewFile[ConstLocalFileLength]; strcpy(cNewFile , m_pAssoc->m_pTransferParam->cSavePath ); strcat( cNewFile , "\\"); strcat( cNewFile , m_pAssoc->m_pTransferParam->cNewFileName ); FILE * fp = fopen(cNewFile , "w+b"); if( fp == 0 ) { // 清理缓存 // 这里有点特别,因为是通过 BufferRealloc() 分配的 SAFE_FREE_BUFFER(headerBuffer.buffer); // 清理附加的Header参数信息 if( chunk != NULL ) { curl_slist_free_all(chunk); chunk = 0; } // 释放curl curl_easy_cleanup(m_pAssoc->m_curlHandle); m_pAssoc->m_curlHandle = NULL; // 创建本地文件失败 m_pAssoc->SetErrCode( HTTPE_CREATE_FILE ); TaskCode(); // 任务结束 SetState( E_HTTPSTATUS_STOP ); return -1; } // 准备写文件了 fileBuffer.fp = fp; } else { // FILE_CLOSE( fileBuffer.fp ); // 清理缓存 // 这里有点特别,因为是通过 BufferRealloc() 分配的 SAFE_FREE_BUFFER( headerBuffer.buffer ); // 清理附加的Header参数信息 if( chunk != NULL ) { curl_slist_free_all(chunk); chunk = 0; } // 释放curl curl_easy_cleanup(m_pAssoc->m_curlHandle); m_pAssoc->m_curlHandle = NULL; m_pAssoc->SetErrCode( HTTPE_FILE_NAME_NULL ); TaskCode(); // 任务结束 SetState( E_HTTPSTATUS_STOP ); return -1; } // 先保存一下Header SAFE_NEW_MEMORYBUFFER(&m_pAssoc->m_HeaderRecv,headerBuffer.size,headerBuffer.size); COPY_MEMORYBUFFER(&headerBuffer,&m_pAssoc->m_HeaderRecv); goto _recvBody; } else if( m_pAssoc->m_lhttpCode == 302 && CURLE_OK == code ) { // 重定向 char * pUrl = NULL; curl_easy_getinfo(m_pAssoc->m_curlHandle, CURLINFO_REDIRECT_URL, (void *)&pUrl); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_URL, pUrl); // 清理附加的Header参数信息 if( chunk != NULL ) { curl_slist_free_all(chunk); chunk = 0; } goto _recvHeader; } else { // 有错误发生 FILE_CLOSE( fileBuffer.fp ); // 清理缓存 // 这里有点特别,因为是通过 BufferRealloc() 分配的 SAFE_FREE_BUFFER( headerBuffer.buffer ); // 清理附加的Header参数信息 if( chunk != NULL ) curl_slist_free_all(chunk); // 释放curl curl_easy_cleanup(m_pAssoc->m_curlHandle); m_pAssoc->m_curlHandle = NULL; m_pAssoc->SetErrCode(code); TaskCode(); // 任务结束 SetState( E_HTTPSTATUS_STOP ); return -1; } _recvBody: curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_NOBODY, 0); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HEADER, 0); // 设置读取,保存数据的函数 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_WRITEFUNCTION, ToFileCallback); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_WRITEDATA, (void *)&fileBuffer); // 设置progress回写 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_NOPROGRESS, 0); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_PROGRESSFUNCTION, ProgressCallback); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_PROGRESSDATA, this); // 开始获取 code = curl_easy_perform(m_pAssoc->m_curlHandle); FILE_CLOSE( fileBuffer.fp ); // 清理缓存 // 这里有点特别,因为是通过 BufferRealloc() 分配的 SAFE_FREE_BUFFER( headerBuffer.buffer ); // 清理附加的Header参数信息 if( chunk != NULL ) curl_slist_free_all(chunk); // 释放curl curl_easy_cleanup(m_pAssoc->m_curlHandle); m_pAssoc->m_curlHandle = NULL; m_pAssoc->SetErrCode( code ); TaskCode(); // 任务结束 SetState( E_HTTPSTATUS_STOP ); return (code != CURLE_OK)?-1:0; }
int CloHttpCurl::TransferProcGET() { THttpMemoryBuffer headerBuffer; THttpMemoryBuffer bodyBuffer; SetState( E_HTTPSTATUS_START ); // 启动curl m_pAssoc->m_curlHandle = curl_easy_init(); // 设置访问链接 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_URL, m_pAssoc->m_pTransferParam->cURL); // 设置代理信息 UpdateProxy(m_pAssoc->m_pTransferParam->cURL); // 设置附加的Header struct curl_slist *chunk = NULL; if( strlen(m_pAssoc->m_pTransferParam->cHead) > 0 ) { BuildCustomHeader(&chunk, m_pAssoc->m_pTransferParam->cHead); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HTTPHEADER, chunk); } // 设置传输状态回调 SetTransferCallback((void *)&headerBuffer, (void *)&bodyBuffer); // 设置访问Agent curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_USERAGENT, GetUserAgent() ); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_FOLLOWLOCATION, 1); // 自动重定向 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_AUTOREFERER, 1); // 启动获取 CURLcode code = curl_easy_perform(m_pAssoc->m_curlHandle); GET_HTTP_CODE(); ClearBuffer(); if( code == CURLE_OK ) { // 保存任务结果数据 // modified by loach 2009-07-27 SAFE_NEW_MEMORYBUFFER(&m_pAssoc->m_HeaderRecv,headerBuffer.size,headerBuffer.size); COPY_MEMORYBUFFER(&headerBuffer,&m_pAssoc->m_HeaderRecv); SAFE_NEW_MEMORYBUFFER(&m_pAssoc->m_BodyRecv,bodyBuffer.size,bodyBuffer.size); COPY_MEMORYBUFFER(&bodyBuffer,&m_pAssoc->m_BodyRecv); } // 这里有点特别,因为是通过 BufferRealloc() 分配的 // 清理缓存 SAFE_FREE_BUFFER(headerBuffer.buffer); SAFE_FREE_BUFFER(bodyBuffer.buffer); // 清理附加的Header参数信息 if( chunk != NULL ) curl_slist_free_all(chunk); // 释放curl curl_easy_cleanup(m_pAssoc->m_curlHandle); m_pAssoc->m_curlHandle = NULL; m_pAssoc->SetErrCode(code); TaskCode(); // 任务结束 SetState( E_HTTPSTATUS_STOP ); return (code != 0 )?-1:0; }
int CloHttpCurl::TransferProcDELETE() { THttpMemoryBuffer headerBuffer; THttpMemoryBuffer bodyBuffer; SetState( E_HTTPSTATUS_START ); // 启动curl m_pAssoc->m_curlHandle = curl_easy_init(); // 设置访问链接 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_URL, m_pAssoc->m_pTransferParam->cURL); // 可能需要POST数据 if( m_pAssoc->m_pTransferParam->pData != NULL && m_pAssoc->m_pTransferParam->uDataLength ) { curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_POSTFIELDS, m_pAssoc->m_pTransferParam->pData); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_POSTFIELDSIZE, m_pAssoc->m_pTransferParam->uDataLength); } // 设置附加动作 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_CUSTOMREQUEST, "DELETE"); // 设置代理信息 UpdateProxy(m_pAssoc->m_pTransferParam->cURL); // 设置附加的Header struct curl_slist *chunk = NULL; if( strlen(m_pAssoc->m_pTransferParam->cHead) > 0 ) { BuildCustomHeader(&chunk, m_pAssoc->m_pTransferParam->cHead); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_HTTPHEADER, chunk); } // 设置传输状态回调 SetTransferCallback(&headerBuffer, &bodyBuffer); // 设置访问Agent curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_USERAGENT, GetUserAgent() ); curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_FOLLOWLOCATION, 1); // 自动重定向 curl_easy_setopt(m_pAssoc->m_curlHandle, CURLOPT_AUTOREFERER, 1); // 启动获取 CURLcode code = curl_easy_perform(m_pAssoc->m_curlHandle); GET_HTTP_CODE(); ClearBuffer(); // 结果处理 if( code == CURLE_OK ) { // 保存任务结果数据 SAFE_NEW_MEMORYBUFFER(&m_pAssoc->m_HeaderRecv,headerBuffer.size,headerBuffer.size); COPY_MEMORYBUFFER(&headerBuffer,&m_pAssoc->m_HeaderRecv); SAFE_NEW_MEMORYBUFFER(&m_pAssoc->m_BodyRecv,bodyBuffer.size,bodyBuffer.size); COPY_MEMORYBUFFER(&bodyBuffer,&m_pAssoc->m_BodyRecv); } // 清理缓存 // 这里有点特别,因为是通过 BufferRealloc() 分配的 SAFE_FREE_BUFFER( headerBuffer.buffer ); SAFE_FREE_BUFFER( bodyBuffer.buffer ); // 清理附加的Header参数信息 if( chunk != NULL ) { curl_slist_free_all(chunk); chunk = 0; } // 释放curl curl_easy_cleanup(m_pAssoc->m_curlHandle); m_pAssoc->m_curlHandle = NULL; m_pAssoc->SetErrCode( code ); TaskCode(); // 任务结束 SetState( E_HTTPSTATUS_STOP ); return (code != CURLE_OK)?-1:0; }