problem in memory with ReceiveHttpMemory

Just starting out? Need help? Post your questions and find answers here.
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 388
Joined: Fri Dec 04, 2015 9:26 pm

problem in memory with ReceiveHttpMemory

Post by skinkairewalker »

hi everyone :D

i have a problem with " ReceiveHttpMemory () "

how can i put this function inside of a if condition ?

follow my code >

Code: Select all


*Buffer = AllocateMemory(2048)
Global Message$
Message$ =Space(2048)
InitNetwork()
InitSprite()
InitSound()
InitKeyboard()
InitMouse()

;*Buffer = ReceiveHTTPMemory("http://www.purebasic.com/index.php")
OpenWindow(0,0,0,370,410,"test httpmemory | V0.0.6b",#PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_MinimizeGadget)

  TextGadget(1, 30, 20, 300, 20, "Digite a URL da sua foto :")
  StringGadget(2, 30, 40, 300, 25, "Digite o link da sua foto")
  TextGadget(3, 30, 80, 300, 20, "teste")
  StringGadget(4, 30, 100, 300, 25, "teste")
  TextGadget(5, 30, 140, 300, 20, "teste:")
  StringGadget(6, 30, 160, 300, 25, "teste")
  TextGadget(7, 30, 230, 310, 20, "teste:")
  EditorGadget(8, 20, 250, 330, 150)
  ButtonGadget(9, 240, 200, 90, 30, "Enviar")

Repeat  
  event = WaitWindowEvent()
  
  Select event
      
    Case #PB_Event_Gadget
      
      gad = EventGadget()
      Select gad
          
        Case 9
          
          *Buffer = ReceiveHTTPMemory("http://www.purebasic.com/index.php")
  If *Buffer
    Size = MemorySize(*Buffer)
    Debug "Content: " + PeekS(*Buffer, Size, #PB_UTF8)
    FreeMemory(*Buffer)
    ;FillMemory(*Buffer,Size)
  Else
    Debug "Failed"
        FreeMemory(*Buffer)
    ;FillMemory(*Buffer,Size)
  EndIf
      FreeMemory(*Buffer)
    ;FillMemory(*Buffer,Size)
          
      EndSelect
  
  EndSelect
  
Until  event = #PB_Event_CloseWindow
how can i fix this ?
Deluxe0321
User
User
Posts: 69
Joined: Tue Sep 16, 2008 6:11 am
Location: ger

Re: problem in memory with ReceiveHttpMemory

Post by Deluxe0321 »

To ensure a non-blocking app you may execute the download part in a thread.

Code: Select all


#DownloadDone = #PB_Event_FirstCustomValue

Structure GetWebMem 
  URL.s
  ThreadID.i  
EndStructure

Procedure GetWebMemWorker(*GetWebMem.GetWebMem)
  
  If Not *GetWebMem
    ProcedureReturn #False  
  EndIf
  
  Url.s = *GetWebMem\URL.s
  FreeMemory(*GetWebMem)
  
  *Buffer = ReceiveHTTPMemory(Url.s)
  
  If *Buffer
    Debug "POSTED"
    PostEvent(#DownloadDone,#PB_Ignore,#PB_Ignore,#PB_Ignore,*Buffer)  
  EndIf

EndProcedure


Procedure GetWebMem(URL.s)
  
  *GetWebMem.GetWebMem = AllocateMemory(SizeOf(GetWebMem))
  
  If Not *GetWebMem
    ProcedureReturn #False  
  EndIf
  
  InitializeStructure(*GetWebMem,GetWebMem)
  *GetWebMem\URL.s  = URL.s
  
  *GetWebMem\ThreadID.i = CreateThread(@GetWebMemWorker(),*GetWebMem)
  
  If *GetWebMem\ThreadID.i
    Debug "Thread Created!"
    ProcedureReturn #True  
  EndIf
  
EndProcedure

  

;*Buffer = AllocateMemory(2048) -- no need for that the receive proc does that for you
Global Message$
Message$ =Space(2048)
InitNetwork()
InitSprite()
InitSound()
InitKeyboard()
InitMouse()

;*Buffer = ReceiveHTTPMemory("http://www.purebasic.com/index.php")
OpenWindow(0,0,0,370,410,"test httpmemory | V0.0.6b",#PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_MinimizeGadget)

  TextGadget(1, 30, 20, 300, 20, "Digite a URL da sua foto :")
  StringGadget(2, 30, 40, 300, 25, "Digite o link da sua foto")
  TextGadget(3, 30, 80, 300, 20, "teste")
  StringGadget(4, 30, 100, 300, 25, "teste")
  TextGadget(5, 30, 140, 300, 20, "teste:")
  StringGadget(6, 30, 160, 300, 25, "teste")
  TextGadget(7, 30, 230, 310, 20, "teste:")
  EditorGadget(8, 20, 250, 330, 150)
  ButtonGadget(9, 240, 200, 90, 30, "Enviar")
  

  
Repeat 
  event = WaitWindowEvent()
 
  Select event
      
    Case #DownloadDone
      EventData = EventData()
      Debug "Event Received!  --> Size "+Str(MemorySize(EventData))
      If EventData
        Message$ = PeekS(EventData,MemorySize(EventData),#PB_Ascii)
        SetGadgetText(8,Message$)
        FreeMemory(EventData)
      EndIf
      
    Case #PB_Event_Gadget
     
      gad = EventGadget()
      Select gad
         
        Case 9
         
          GetWebMem("http://www.purebasic.com/index.php")
         
      EndSelect
 
  EndSelect
 
Until  event = #PB_Event_CloseWindow
infratec
Always Here
Always Here
Posts: 5527
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: problem in memory with ReceiveHttpMemory

Post by infratec »

Hi,

you should also fire an event if the download is 'finished' but failed:

Code: Select all

Procedure GetWebMemWorker(*GetWebMem.GetWebMem)
 
  If Not *GetWebMem
    ProcedureReturn #False 
  EndIf
 
  Url.s = *GetWebMem\URL.s
  FreeMemory(*GetWebMem)
 
  *Buffer = ReceiveHTTPMemory(Url.s)
 
  If *Buffer
    Debug "POSTED"
    PostEvent(#DownloadDone,#PB_Ignore,#PB_Ignore,#PB_Ignore,*Buffer) 
  Else
    Debug "FAILED"
    PostEvent(#DownloadFailed)
  EndIf
  
EndProcedure
Bernd
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 388
Joined: Fri Dec 04, 2015 9:26 pm

Re: problem in memory with ReceiveHttpMemory

Post by skinkairewalker »

thanks Deluxe0321 and infratec xD
Post Reply