helpers.scm 1.1 KB

1234567891011121314151617181920212223242526272829
  1. #; (use-modules (rnrs bytevectors))
  2. ;; see also: https://www.gnu.org/software/guile/manual/html_node/Modules-and-the-File-System.html#Modules-and-the-File-System
  3. (define-module (networking-lib helpers)
  4. #:export (display-byte-vector
  5. byte-vector->utf8-message
  6. send-message-on-socket
  7. make-protocol))
  8. (use-modules (rnrs bytevectors)
  9. (ice-9 textual-ports))
  10. (define (byte-vector->utf8-message bv len)
  11. (when (= len 0) (display "length was zero"))
  12. (let ([minimal-bv (make-bytevector len)])
  13. ;; bytevector-copy! source source-start target target-start len
  14. (bytevector-copy! bv 0 minimal-bv 0 len)
  15. (utf8->string minimal-bv)))
  16. (define (display-byte-vector vec len)
  17. (display (simple-format #f
  18. "I got the following message: ~s\n"
  19. (byte-vector->utf8-message vec len))))
  20. (define (send-message-on-socket sock message)
  21. (display (simple-format #f "sending message: ~s\n" message))
  22. (display (simple-format #f "representation: ~s\n" (string->utf8 message)))
  23. (send sock (string->utf8 message)))