2.3 Utilities
send just calls display. If print-txrx? is set, it also logs what was sent.
«send» ::=
(provide print-txrx?) (define print-txrx? : (Parameter Boolean) (make-parameter #f)) (: send ((U Bytes String) -> Void)) (define (send x) (define y (if (bytes? x) x (string->bytes/latin-1 x))) (when (print-txrx?) (eprintf "--> ~a ~a~n" y (bytes->list y))) (display y))
Racket’s read-bytes may return fewer bytes than requested if end-of-file is reached early. This function will quit with an error message if the server ends the response stream at an unexpected moment. If print-txrx? is set, it also logs what was received.
«expect» ::=
(: expect (Nonnegative-Integer -> Bytes)) (define (expect n) (define res (read-bytes n)) (when (print-txrx?) (eprintf "<-- ~a ~a~n" res (if (eof-object? res) "" (bytes->list res)))) (define len (if (eof-object? res) 0 (bytes-length res))) (if (or (eof-object? res) (len . < . n)) (error 'expect "expected ~a bytes, got ~a with early end-of-file" n len) res))