Posted to tcl by Bradipo at Mon Jul 29 00:39:19 GMT 2024view raw

  1. proc readbytes {chan n b} {
  2. puts stdout "[clock format [clock seconds]]\
  3. readbytes $chan $n [binary encode hex $b]"
  4. if {[string length $b] == 2} {
  5. fileevent $chan readable [list readmorebytes $chan $n $b]
  6. } else {
  7. if {[eof $chan]} {
  8. fileevent $chan readable {}
  9. catch {close $chan}
  10. puts stderr "short read readbytes"
  11. } else {
  12. append b [read $chan 1]
  13. fileevent $chan readable [list readbytes $chan $n $b]
  14. }
  15. }
  16. }
  17.  
  18. proc readmorebytes {chan n b} {
  19. puts stdout "[clock format [clock seconds]]\
  20. readmorebytes $chan $n [binary encode hex $b]"
  21. if {[string length $b] == $n} {
  22. fileevent $chan readable {}
  23. catch {close $chan}
  24. puts stdout "[clock format [clock seconds]]\
  25. got $n bytes [binary encode hex $b]"
  26. } else {
  27. if {[eof $chan]} {
  28. fileevent $chan readable {}
  29. catch {close $chan}
  30. puts stderr "short read readmorebytes"
  31. } else {
  32. append b [read $chan 1]
  33. fileevent $chan readable [list readmorebytes $chan $n $b]
  34. }
  35. }
  36. }
  37.  
  38. proc doreceive {} {
  39. set fd [open /dev/urandom rb]
  40. fconfigure $fd -translation binary -blocking 0
  41. fileevent $fd readable [list readbytes $fd 8 ""]
  42. }
  43.  
  44. proc receive {} {
  45. puts stdout "[clock format [clock seconds]] starting receive"
  46. doreceive
  47. puts stdout "[clock format [clock seconds]] rescheduling receive"
  48. after 10000 receive
  49. }
  50.  
  51. receive
  52.  
  53. ------------------------------------------------------------------------
  54.  
  55. Sun Jul 28 18:38:55 MDT 2024 starting receive
  56. Sun Jul 28 18:38:55 MDT 2024 rescheduling receive
  57. Sun Jul 28 18:38:55 MDT 2024 readbytes file4 8
  58. Sun Jul 28 18:38:55 MDT 2024 readbytes file4 8 7c
  59. Sun Jul 28 18:38:55 MDT 2024 readbytes file4 8 7cef
  60. Sun Jul 28 18:38:55 MDT 2024 readmorebytes file4 8 7cef
  61. Sun Jul 28 18:38:55 MDT 2024 readmorebytes file4 8 7cef86
  62. Sun Jul 28 18:38:55 MDT 2024 readmorebytes file4 8 7cef8663
  63. Sun Jul 28 18:38:55 MDT 2024 readmorebytes file4 8 7cef8663a9
  64. Sun Jul 28 18:38:55 MDT 2024 readmorebytes file4 8 7cef8663a9d8
  65. Sun Jul 28 18:38:55 MDT 2024 readmorebytes file4 8 7cef8663a9d8bf
  66. Sun Jul 28 18:38:55 MDT 2024 readmorebytes file4 8 7cef8663a9d8bff5
  67. Sun Jul 28 18:38:55 MDT 2024 got 8 bytes 7cef8663a9d8bff5
  68.  
  69.