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

Report any Hollywood bugs here
Post Reply
djrikki
Posts: 682
Joined: Wed Apr 06, 2011 12:26 am

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

Post by djrikki » Sat Apr 21, 2012 12:16 am

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
Evolve - Rapid GUI Development tool for MUI Royale and RapaGUI
http://myevolve.wordpress.com

PEB
Posts: 283
Joined: Sun Feb 21, 2010 1:28 am

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

Post by PEB » Sat Apr 21, 2012 12:40 am

The "automatic ID selection" always returns a variable of type #LIGHTUSERDATA. (Check documentation.)

djrikki
Posts: 682
Joined: Wed Apr 06, 2011 12:26 am

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

Post by djrikki » Sat Apr 21, 2012 1:24 pm

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.
Evolve - Rapid GUI Development tool for MUI Royale and RapaGUI
http://myevolve.wordpress.com

djrikki
Posts: 682
Joined: Wed Apr 06, 2011 12:26 am

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

Post by djrikki » Sat Apr 21, 2012 1:41 pm

This auto id idenfier passes correctly between OpenConnection/CloseConnection and ReceiveData, but when I use it with OpenFile/WriteLine/CloseFile it does not. :S
Evolve - Rapid GUI Development tool for MUI Royale and RapaGUI
http://myevolve.wordpress.com

djrikki
Posts: 682
Joined: Wed Apr 06, 2011 12:26 am

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

Post by djrikki » Sat Apr 21, 2012 5:34 pm

Thanks PEB, I got it working, going to post the code for free use in the showcase.
Evolve - Rapid GUI Development tool for MUI Royale and RapaGUI
http://myevolve.wordpress.com

djrikki
Posts: 682
Joined: Wed Apr 06, 2011 12:26 am

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

Post by djrikki » Sat Apr 21, 2012 5:46 pm

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!
Evolve - Rapid GUI Development tool for MUI Royale and RapaGUI
http://myevolve.wordpress.com

User avatar
Allanon
Posts: 464
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

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

Post by Allanon » Sun Apr 22, 2012 10:44 am

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.
----------------------------
[Allanon] Fabio Falcucci
AMC - Creative Development // Docs Site // Support Forum
Support me on Patreon for Hollywood libraries!

User avatar
airsoftsoftwair
Posts: 3441
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

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

Post by airsoftsoftwair » Sun Apr 22, 2012 11:00 pm

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 :)

djrikki
Posts: 682
Joined: Wed Apr 06, 2011 12:26 am

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

Post by djrikki » Mon Apr 23, 2012 12:12 am

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.
Evolve - Rapid GUI Development tool for MUI Royale and RapaGUI
http://myevolve.wordpress.com

Post Reply