Page 1 of 1

[id] = OpenConnection(nil...) not returning valid identifer

Posted: Sat Apr 21, 2012 12:16 am
by djrikki
In the below function called base:DownloadFile() I make the following declation:

Code: Select all

 connection = OpenConnection(nil, server$, 80) 
immediately afterwards I debugprint the ID using

Code: Select all

 debugprint(connection).
What returns is not a Integer as expected, but something like this: UserData: 0x4675f2a0

And the rest of the code fails because it is expecting receiving an integer.

Code: Select all

Global base, data$, count, done
base = {}

Function base:DownloadFile(server$, url$, downloadpath$, filename$)
    
    Local connection
    
	connection = OpenConnection(nil, server$, 80)

	debugprint(connection)

	SendData(connection, "GET " .. url$ .. " HTTP/1.0\r\n\r\n")
	data$, count, done = ReceiveData(connection, #RECEIVEALL)

	; Redirect
	If FindStr(data$,"302 Found") > -1
	    CloseConnection(connection)
	    redirection$ = MidStr(data$,FindStr(data$,"Location: "),StrLen(data$))
	    redirection$ = MidStr(redirection$,10,FindStr(redirection$,"\n")-11)
	    
	    server$ = ReplaceStr(redirection$, "http://", "")
	    server$ = MidStr(server$,0,FindStr(server$,"/"))
	    
	    connection = OpenConnection(nil, server$, 80)
	    
	    SendData(connection, "GET " .. redirection$ .. " HTTP/1.0\r\n\r\n")
	    
		data$ = nil
	 
		; Skip HTTP Headers
	    While data$ = nil Or FindStr(data$,"Content-Type:") = -1
	    	data$, count, done = ReceiveData(connection, #RECEIVELINE)
	   	Wend
   	
	   	data$, count, done = ReceiveData(connection, #RECEIVELINE) ; Skip blank line
	   	
	    data$, count, done = ReceiveData(connection, #RECEIVEBYTES, 65536) ; Read file data
	    	    
	ElseIf FindStr(data,"200 OK") > -1   
	    	    
	EndIf

	; Not Found
	If FindStr(data$,"404 Found") > -1
	    Return(False)
	    
	; Found - Download it
	Else
		OpenFile(connection,downloadpath$ .. filename$, #MODE_WRITE)
	    Return(True)
	EndIf
EndFunction
    
success = base:DownloadFile("www.os4depot.net","http://www.os4depot.net/share/game/board/africa.lha","RAM:","africa.lha")

Function SaveToDisk(id)
   	WriteString(id,data$)
   	CloseConnection(id)
	CloseFile(id)
	End() 
Endfunction

Function base:BuildFile(msg)
    
	Local segment$
	segment$, count, done = ReceiveData(msg.id, #RECEIVEBYTES, 65536) ; Read file data
			
	data$ = data$ .. segment$
						
	If StrLen(data$) = 297517 Then SaveToDisk(msg.id)
EndFunction
    
Function base_BuildFile(msg) base:BuildFile(msg) EndFunction

InstallEventHandler({OnReceiveData = base_BuildFile})

Repeat
    WaitEvent()
Forever

Re: [id] = OpenConnection(nil...) not returning valid identifer

Posted: Sat Apr 21, 2012 12:40 am
by PEB
The "automatic ID selection" always returns a variable of type #LIGHTUSERDATA. (Check documentation.)

Re: [id] = OpenConnection(nil...) not returning valid identifer

Posted: Sat Apr 21, 2012 1:24 pm
by djrikki
Hmm, well okay I am still not any closer to working out how I am supposed to use this within my sample code. Have a whole weekend ahead of me to work it out.

Re: [id] = OpenConnection(nil...) not returning valid identifer

Posted: Sat Apr 21, 2012 1:41 pm
by djrikki
This auto id idenfier passes correctly between OpenConnection/CloseConnection and ReceiveData, but when I use it with OpenFile/WriteLine/CloseFile it does not. :S

Re: [id] = OpenConnection(nil...) not returning valid identifer

Posted: Sat Apr 21, 2012 5:34 pm
by djrikki
Thanks PEB, I got it working, going to post the code for free use in the showcase.

Re: [id] = OpenConnection(nil...) not returning valid identifer

Posted: Sat Apr 21, 2012 5:46 pm
by djrikki
First time I've had to auto valid identifiers so didn't really understand what was meant to happen, expected it to just return an integer!

Re: [id] = OpenConnection(nil...) not returning valid identifer

Posted: Sun Apr 22, 2012 10:44 am
by Allanon
As I understand it it's implemented this way to do not interefer with your code, so you are still free to use any fixed id without taking care of what id the Hollywood auto-id feature have used.

Re: [id] = OpenConnection(nil...) not returning valid identifer

Posted: Sun Apr 22, 2012 11:00 pm
by airsoftsoftwair
djrikki wrote:This auto id idenfier passes correctly between OpenConnection/CloseConnection and ReceiveData, but when I use it with OpenFile/WriteLine/CloseFile it does not. :S
You cannot pass identifiers obtained from OpenConnection() to OpenFile() or similar functions. OpenConnection() will return a connection handle that can only be used with the network API functions.
Allanon wrote:As I understand it it's implemented this way to do not interefer with your code, so you are still free to use any fixed id without taking care of what id the Hollywood auto-id feature have used.
Yep, that's exactly the idea behind it :)

Re: [id] = OpenConnection(nil...) not returning valid identifer

Posted: Mon Apr 23, 2012 12:12 am
by djrikki
No worries, just that I've never had to use them before so didn't really comprehend how to use correctly.  Sorted now, I've posted my solution to asynchronous downloads in the Showcase forum for anyone to pick up and use.