remy / next-http

Send and recieve http requests into banks - for the Spectrum Next
45 stars 6 forks source link

Timeout is too small and can bail before data is returned #7

Closed em00k closed 1 year ago

em00k commented 1 year ago

https://github.com/remy/next-http/blob/0acf3204d71a9824bda973e50ceb2b240d79226e/src/esp-timeout.asm#L1

The timeout gives up too quickly if a request takes some seconds to process eg:

https://zxnext.uk/scr?s=!hollywood&p=1

I have done a bodge fix to wait for raster


InitESPTimeout:
    MODULE InitESPTimeout
        push hl
        ld hl, ESPTimeout mod 65536     ; Timeout is a 32-bit value, so save the two LSBs first,
        ld (CheckESPTimeout.Value), hl
        ld hl, ESPTimeout / 65536   ; then the two MSBs.
        ld (CheckESPTimeout.Value2), hl
        pop hl
        ret
    ENDMODULE

; Modifies: nothing
CheckESPTimeout:
    MODULE CheckESPTimeout
        push hl
        push af
Value   EQU $+1
        ld hl, SMC
        dec hl
        ld (Value), hl
        call    WaitRaster
        ld a, h
        or l
        jr z, Rollover
Success:    pop af
        pop hl
        ret
Failure:    ld hl, (Wifi.timeout)
HandleError:
        call Error          ; Ignore current stack depth, and just jump

Rollover:
Value2  EQU $+1
        ld hl, SMC          ; Check the two upper values
        ld a, h
        or l
        jr z, Failure           ; If we hit here, 32 bit value is $00000000
        dec hl
        call    WaitRaster 
        ld (Value2), hl
        ld hl, ESPTimeout mod 65536
        ld (Value), hl
        jr Success
WaitRaster:

        push    bc 
        push    af 
    waitloop:
        ld      bc, $243b 
        ld      a, $1f     ; only really care about lsb 
        out     (c) , a 
        inc     b 
        in      a, (c)
        cp      192 
        jr      nz, waitloop 
        pop     af 
        pop     bc 
        ret 

    ENDMODULE