send-non-string-data.scm 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. (import (fibers)
  2. (fibers channels)
  3. (ice-9 match)
  4. ;; structs
  5. (srfi srfi-9)
  6. ;; for functional structs (not part of srfi-9 directly)
  7. (srfi srfi-9 gnu))
  8. (define-immutable-record-type <point>
  9. (make-point x y)
  10. point?
  11. (x point-x set-point-x)
  12. (y point-y set-point-y))
  13. (set-record-type-printer!
  14. <point>
  15. (λ (record port)
  16. (simple-format port
  17. "<point: x: ~a, y: ~a>\n"
  18. (point-x record)
  19. (point-y record))))
  20. (define server
  21. (λ (in out)
  22. ;; infinite blocking loop
  23. (let lp ()
  24. (match (pk 'server-received #|block on get-message|# (get-message in))
  25. ('ping! (put-message out 'pong!))
  26. ('sup (put-message out 'not-much-u))
  27. (msg (put-message out (cons 'wat msg))))
  28. (lp))))
  29. (define client
  30. (λ (in out)
  31. (for-each (λ (msg)
  32. (put-message out msg)
  33. (pk 'client-received (get-message in)))
  34. (list '(1 2 3)
  35. #(1 2 3)
  36. ;; We can pass non-string data between fibers!
  37. (make-point 1 2)))))
  38. (run-fibers (λ ()
  39. (let ((c2s (make-channel))
  40. (s2c (make-channel)))
  41. (spawn-fiber (λ () (server c2s s2c)))
  42. (client s2c c2s))))