                 tftp-server get dup addr>> port>> tftp-servers get-global set-at
                 [
                     [ read-tftp-command t ]
-                    [ dup io-timeout? [ drop ] [ rethrow ] if f ] recover
+                    [ [ . flush ] with-global f ] recover
                 ] loop
             ] with-variable
         ] with-variable

! Copyright (C) 2019 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays assocs combinators continuations
destructors fry io io.backend.unix io.binary io.directories
io.encodings.binary io.encodings.latin1 io.encodings.string
io.encodings.utf8 io.files io.sockets kernel math math.parser
namespaces pack random sequences sequences.extras splitting
strings ;
IN: protocols.tftp

CONSTANT: TFTP-RRQ 1 ! Read request (RRQ)

        [ [ 1 + ] change-block block>> ] bi
        send-file-block
    ] [
        drop
    ] if ;

: handle-send-file ( bytes -- )
    "\0" split harvest first2 [ utf8 decode ] bi@
    over { [ exists? ] [ file-info directory? not ] } 1&& [
        "netascii" sequence= utf8 binary ? 0 read-file boa
        tftp-client get clients get set-at
        0 handle-send-file-next
    ] [
        2drop "File not found" send-error
    ] if ;

: read-tftp-command ( -- )
    tftp-server get receive tftp-client [
