Problem wit Threads and HTTPSendRequest_
Posted: Sat May 17, 2008 8:23 am
Ok, i am working on a multi-threaded Downloader. There is a Gadget with a List of the Files, upon pressing "Download" a LinkedList gets filled with the necessary Information as is passed to the threaded Procedure as pointer. So far so good. The Thread itself checks his own number and waits until he is the first thread or if the thread before him is finished. that works like a charm. But now i have a problem: The first download works wonderful. The whole routine is running through. From the second on, it hangs at the HTTPSENDREQUEST_ and does nothing, no matter what kind of files i try to download. I am sitting over this problem since 2 days and don't find a solution ... Here's the (threaded) procedure-code:
Code: Select all
Procedure.s download_file(*arg.dl)
Protected host.s,get_url.s,result.s,open_handle,connect_handle,request_handle,headers.s
Protected post_data.s,post_data_len,send_handle,Buffer.s,buffer2.s,bytes_read.l,total_read.l
Protected linkstart, linkend, form1url$,rs_url$,durchlauf, save_file$,tmp$,arg$
Protected firstslash,secondslash, hOutput,shouldhave, xPos,i,timer1,timer2,totaltimer,timerstart,timerend
Protected Dim myarray.l(2),zaehler,cookie$,threadNumber,tmp.b
Shared mutex
LockMutex(Mutex)
threadNumber=*arg\number ;Thread Number?
arg$=*arg\url + "|" + *arg\referer + "|" + *arg\name ;Backwards-Compatibility to the old download-code
Repeat
SelectElement(PassedArguments(),threadnumber-1); Thread before is running?
tmp=passedarguments()\finished
Delay(1000)
;Debug "I am THREAD " + Str(threadNumber) + " and Status of THREAD " + Str(threadNumber-1) + " is " + Str(tmp)
Until tmp ; waits for the thread to finish
Debug "Thread Number " + Str(threadNumber) + " startet ...." ;start the sequence
arg$=*arg\url + "|" + *arg\referer + "|" + *arg\name ;Old Download-Code
AddGadgetItem(#list_status,0,"Get File ...")
tmp$=arg$
arg$=ReplaceString(arg$,"view/","",1)
;Debug arg$
;--------------------------------------------------------------------------------------------------------------
; Get Referer, Cookies, etc.
;--------------------------------------------------------------------------------------------------------------
Repeat
xPos=FindString(arg$,"|",xPos+1)
If xPos>0
i=i+1
myarray(i)=xPos
;Debug Str(i) + ". | : " + Str(myarray(i))
EndIf
Until xPos=0
rs_url$=Left(arg$,myarray(1)-1)
referer$=Mid(arg$,myarray(1)+1,myarray(2)-myarray(1)-1)
save_file$=Right(arg$,Len(arg$)-myarray(2))
;Debug "URL: " + rs_url$
;Debug "REFERER: " + referer$
;Debug "SAVE: " + save_file$
result=""
firstslash=FindString(rs_url$,"/",8)
host.s = Mid(rs_url$,8,firstslash-8)
get_url.s = Right(rs_url$,Len(rs_url$)-firstslash+1)
;Debug "HOST: " + host.s
;Debug "URL : " + get_url.s
result.s = ""
;hOutput=CreateFile(#PB_Any,save_file$)
open_handle = InternetOpen_("Microsoft Internet Explorer 7.0",#INTERNET_OPEN_TYPE_DIRECT,"","",0)
connect_handle = InternetConnect_(open_handle,host,#INTERNET_DEFAULT_HTTP_PORT,"","",#INTERNET_SERVICE_HTTP,0,0)
headers.s=headers.s + "Cookie$
HttpAddRequestHeaders_(request_handle,headers,Len(headers), #HTTP_ADDREQ_FLAG_REPLACE | #HTTP_ADDREQ_FLAG_ADD)
request_handle = HttpOpenRequest_(connect_handle,"GET",get_url,"","",0,0,0)
send_handle = HttpSendRequest_(request_handle,"",0,"",0)
cookie$=getqueryInfo(request_handle,43)
Debug "COOKIE : " + cookie$
;SetGadgetAttribute(#progressbar,#PB_ProgressBar_Maximum,shouldhave)
;--------------------------------------------------------------------------------------------------------------
; get the actual file ...
;--------------------------------------------------------------------------------------------------------------
arg$=tmp$
;Debug "ARG: " + arg$
i=0
Repeat
xPos=FindString(arg$,"|",xPos+1)
If xPos>0
i=i+1
myarray(i)=xPos
;Debug Str(i) + ". | : " + Str(myarray(i))
EndIf
Until xPos=0
rs_url$=Left(arg$,myarray(1)-1)
referer$=Mid(arg$,myarray(1)+1,myarray(2)-myarray(1)-1)
save_file$=Right(arg$,Len(arg$)-myarray(2))
Debug "URL: " + rs_url$
Debug "REFERER: " + referer$
Debug "SAVE: " + save_file$
result=""
firstslash=FindString(rs_url$,"/",8)
host.s = Mid(rs_url$,8,firstslash-8)
get_url.s = Right(rs_url$,Len(rs_url$)-firstslash+1)
Debug "HOST: " + host.s
Debug "URL : " + get_url.s
result.s = ""
hOutput=CreateFile(threadnumber,save_file$)
Debug "Datei-Handle: " + Str(hOutput)
open_handle = InternetOpen_("Microsoft Internet Explorer 7.0",#INTERNET_OPEN_TYPE_DIRECT,"","",0)
Debug "Open-Handle : " + Str(open_handle)
connect_handle = InternetConnect_(open_handle,host,#INTERNET_DEFAULT_HTTP_PORT,"","",#INTERNET_SERVICE_HTTP,0,0)
Debug "Conn-Handle : " + Str (connect_handle)
headers.s=headers.s + "Cookie: " + cookie$
headers.s=headers.s + Chr(13)+Chr(10)
headers.s = headers.s + "Referer: " + referer$
headers.s = headers.s + Chr(13)+Chr(10)
Debug "Headers : " + headers
HttpAddRequestHeaders_(request_handle,headers,Len(headers), #HTTP_ADDREQ_FLAG_REPLACE | #HTTP_ADDREQ_FLAG_ADD)
request_handle = HttpOpenRequest_(connect_handle,"GET",get_url,"","",0,0,0)
Debug "REQ-Handle : " + Str(request_handle)
send_handle = HttpSendRequest_(request_handle,"",0,"",0)
Debug "SND-Handle : " + Str(send_handle)
shouldhave=Val(GetQueryInfo(request_handle, 5))
Debug "SIZE: " + Str(shouldhave)
SetGadgetAttribute(#progressbar,#PB_ProgressBar_Maximum,shouldhave)
Buffer.s = Space(4096)
total_read = 0
totaltimer=0
timerstart=ElapsedMilliseconds()
Debug "TIMER START: " + Str(timerstart)
bytes_read=0
Repeat
InternetReadFile_(request_handle,@Buffer,4096,@bytes_read)
WriteData(threadnumber,@Buffer,bytes_read)
Buffer = Space(4096)
timer1=ElapsedMilliseconds()-timerstart
total_read=total_read+bytes_read
;If total_read>0:Debug "SPEED : " + Str(total_read/timer1):EndIf
If total_read>0 And timer1>0:SetGadgetText(#text_speed,Str(total_read/timer1) + " KB/sec"):EndIf
Debug "BYZES: " + Str(total_read)
SetGadgetState(#progressbar,total_read)
Until bytes_read=0
totaltimer=totaltimer+timer1
If shouldhave=total_read Or total_read>shouldhave
SelectElement(PassedArguments(),threadnumber)
PassedArguments()\finished=#True
Debug "ITEM " + Str(threadNumber) + " ist fertig!"
;MessageRequester("Download ok","Download succesfull finished!")
msg_groesse=msg_groesse+total_read
msg_dateien=msg_dateien+1
msg_zeit=(ElapsedMilliseconds()-timerstart)/1000
;msg_speed=msg_groesse/msg_zeit
AddGadgetItem(#list_status,0,"------------------------------")
AddGadgetItem(#list_status,0,"Ges. Grösse: " + Str(msg_groesse/1024) + "KB")
AddGadgetItem(#list_status,0,"Ges. Dateien: " + Str(msg_dateien))
AddGadgetItem(#list_status,0,"Ges. Zeit: " + Str(msg_zeit)+ " Sekunden")
AddGadgetItem(#list_status,0,"------------------------------")
InternetCloseHandle_(open_handle)
EndIf
SetGadgetState(#progressbar,0)
anzahl_threads=anzahl_threads-1
CloseFile(threadNumber)
UnlockMutex(Mutex)
cookie$=""
EndProcedure