Changeset 862 for branches/prototype-v0/zoo-project/zoo-kernel/ulinet.c
- Timestamp:
- Feb 1, 2018, 11:27:28 AM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/prototype-v0/zoo-project/zoo-kernel/ulinet.c
r860 r862 29 29 #define MAX_WAIT_MSECS 180*1000 /* Wait max. 180 seconds */ 30 30 #include "ulinet.h" 31 #include "server_internal.h" 31 32 #include <assert.h> 32 33 #include <ctype.h> … … 67 68 return realsize; 68 69 } 70 71 /** 72 * Write the downloaded content to a _HINTERNET structure 73 * 74 * @param buffer the buffer to read 75 * @param size size of each member 76 * @param nmemb number of element to read 77 * @param data the _HINTERNET structure to write in 78 * @return the size red, -1 if buffer is NULL 79 */ 80 size_t write_data_into_file(void *buffer, size_t size, size_t nmemb, void *data) 81 { 82 size_t realsize = size * nmemb; 83 int writen=0; 84 _HINTERNET *psInternet; 85 if(buffer==NULL){ 86 buffer=NULL; 87 return -1; 88 } 89 psInternet=(_HINTERNET *)data; 90 writen+=fwrite(buffer, size, nmemb, psInternet->file); 91 if(psInternet->nDataLen>0){ 92 psInternet->nDataAlloc+=psInternet->nDataLen+writen+1; 93 psInternet->nDataLen += realsize; 94 }else 95 psInternet->nDataLen=realsize+1; 96 buffer=NULL; 97 return realsize; 98 } 99 69 100 70 101 /** … … 363 394 fclose(handle.file); 364 395 unlink(handle.filename); 396 free(handle.filename); 365 397 } 366 398 else{ … … 411 443 * @param dwFlags desired download mode (INTERNET_FLAG_NO_CACHE_WRITE for not using cache file) 412 444 * @param dwContext not used 445 * @return the updated HINTERNET 413 446 */ 414 447 HINTERNET InternetOpenUrl(HINTERNET* hInternet,LPCTSTR lpszUrl,LPCTSTR lpszHeaders,size_t dwHeadersLength,size_t dwFlags,size_t dwContext){ … … 447 480 curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_VERBOSE, 1); 448 481 #endif 449 450 482 451 483 switch(dwFlags) 452 484 { 453 485 case INTERNET_FLAG_NO_CACHE_WRITE: 454 hInternet->ihandle[hInternet->nb].hasCacheFile=-1;455 486 curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, write_data_into); 456 487 curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, (void*)&hInternet->ihandle[hInternet->nb]); 488 hInternet->ihandle[hInternet->nb].hasCacheFile=-1; 457 489 break; 458 490 default: 459 sprintf(hInternet->ihandle[hInternet->nb].filename,"/tmp/ZOO_Cache%d",(int)time(NULL)); 460 hInternet->ihandle[hInternet->nb].filename[24]=0; 461 #ifdef MSG_LAF_VERBOSE 462 fprintf(stderr,"file=%s",hInternet->ihandle[hInternet->nb].filename); 463 #endif 464 hInternet->ihandle[hInternet->nb].filename=filename; 491 memset(filename,0,255); 492 char* tmpUuid=get_uuid(); 493 sprintf(filename,"/tmp/ZOO_Cache%s", tmpUuid); 494 free(tmpUuid); 495 hInternet->ihandle[hInternet->nb].filename=strdup(filename); 465 496 hInternet->ihandle[hInternet->nb].file=fopen(hInternet->ihandle[hInternet->nb].filename,"w+"); 466 467 497 hInternet->ihandle[hInternet->nb].hasCacheFile=1; 468 curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, NULL); 469 curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, hInternet->ihandle[hInternet->nb].file); 470 hInternet->ihandle[hInternet->nb].nDataLen=0; 498 curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEFUNCTION, write_data_into_file); 499 curl_easy_setopt(hInternet->ihandle[hInternet->nb].handle, CURLOPT_WRITEDATA, (void*)&hInternet->ihandle[hInternet->nb]); 471 500 break; 472 501 } … … 514 543 */ 515 544 int processDownloads(HINTERNET* hInternet){ 516 int still_running=0 ;545 int still_running=0,numfds; 517 546 int msgs_left=0; 518 547 int i=0; 519 548 do{ 520 if(curl_multi_perform(hInternet->handle, &still_running)==CURLM_OK) 521 if(still_running){ 522 zSleep(10); 523 } 549 CURLMcode mc; 550 mc = curl_multi_perform(hInternet->handle, &still_running); 551 if(mc==CURLM_OK){ 552 mc = curl_multi_wait(hInternet->handle, NULL, 0, 1000, &numfds); 553 } 554 if(mc != CURLM_OK) { 555 fprintf(stderr, "curl_multi failed, code %d.n", mc); 556 break; 557 } 524 558 }while(still_running); 525 559 for(i=0;i<hInternet->nb;i++){ … … 565 599 fseek (hInternet.file , 0 , SEEK_END); 566 600 dwDataSize=ftell(hInternet.file); //taille du ficher 567 rewind (hInternet.file); 601 //dwDataSize=hInternet.nDataLen; 602 //rewind (hInternet.file); 603 fseek(hInternet.file, 0, SEEK_SET); 568 604 } 569 605 else{ … … 575 611 } 576 612 577 if( dwNumberOfBytesToRead /* buffer size */ < dwDataSize ) 613 if( dwNumberOfBytesToRead /* buffer size */ < dwDataSize ){ 578 614 return 0; 615 } 579 616 580 617 #ifdef MSG_LAF_VERBOSE 581 printf("\nfile size : %dko\n",dwDataSize/1024);618 fprintf(stderr,"\nfile size : %dko\n",dwDataSize/1024); 582 619 #endif 583 620 584 621 if(hInternet.hasCacheFile>0){ 585 *lpdwNumberOfBytesRead = fread(lpBuffer,1,dwDataSize,hInternet.file); 622 int freadRes = fread(lpBuffer,dwDataSize+1,1,hInternet.file); 623 *lpdwNumberOfBytesRead = hInternet.nDataLen; 586 624 } 587 625 else{
Note: See TracChangeset
for help on using the changeset viewer.