input_linux.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package door
  2. import (
  3. "bufio"
  4. "bytes"
  5. "log"
  6. "strings"
  7. "syscall"
  8. "time"
  9. )
  10. var ReaderInterval = time.Duration(200) * time.Millisecond
  11. var ReaderTimeval syscall.Timeval = syscall.NsecToTimeval(int64(ReaderInterval))
  12. // Output a nice string representation of the rune buffer.
  13. func extended_output(buffer []rune) string {
  14. var output string = string(buffer)
  15. output = strings.Replace(output, "\x1b", "^[", -1)
  16. return output
  17. }
  18. // syscall.FdSet clear all
  19. func clearAll(fdSetPtr *syscall.FdSet) {
  20. for index := range (*fdSetPtr).Bits {
  21. (*fdSetPtr).Bits[index] = 0
  22. }
  23. }
  24. // syscall.FdSet set fd
  25. func set(fdSetPtr *syscall.FdSet, fd int) {
  26. (*fdSetPtr).Bits[fd/64] |= 1 << uint64(fd%64)
  27. }
  28. func RuneToInt8(r rune) int8 {
  29. return int8(r)
  30. }
  31. // go routine Reader for input
  32. // This "times out" every ReaderTimeval
  33. func Reader(d *Door) {
  34. // I need non-blocking reads here.
  35. /*
  36. ReaderTimeval = syscall.Timeval{Sec: int64(ReaderInterval.Seconds()),
  37. Usec: ReaderInterval.Microseconds() % time.Second.Microseconds()}
  38. */
  39. log.Println(ReaderInterval, ReaderTimeval)
  40. readerBuffer = make([]rune, 0, READ_SIZE*2)
  41. defer func() {
  42. log.Printf("~Reader\n")
  43. if d.ReaderCanClose {
  44. d.wg.Done()
  45. }
  46. }()
  47. defer func() {
  48. if err := recover(); err != nil {
  49. log.Printf("Reader: %#v\n", err)
  50. }
  51. }()
  52. var fdset syscall.FdSet
  53. var readbuffer [READ_SIZE]byte
  54. var runebuffer bytes.Buffer
  55. var runeread = bufio.NewReaderSize(&runebuffer, 1)
  56. var selectTimeval syscall.Timeval
  57. for {
  58. clearAll(&fdset)
  59. set(&fdset, d.READFD)
  60. selectTimeval = ReaderTimeval
  61. v, err := syscall.Select(d.READFD+1, &fdset, nil, nil, &selectTimeval)
  62. if err == syscall.EINTR {
  63. continue
  64. }
  65. // log.Printf("Select: %#v / %#v\n", v, err)
  66. if v == -1 {
  67. log.Printf("Reader ERR: %#v\n", err)
  68. d.readerMutex.Lock()
  69. if !d.ReaderClosed {
  70. d.ReaderClosed = true
  71. close(d.readerChannel)
  72. }
  73. return
  74. }
  75. if v == 0 {
  76. // timeout
  77. ReadRune(d, runeread, true)
  78. // process(d, false)
  79. continue
  80. }
  81. // The buffer used here must have len & cap to size you want to read.
  82. // use [BUFF_SIZE]byte for readone
  83. r, err := syscall.Read(d.READFD, readbuffer[:])
  84. if r == -1 {
  85. log.Println("syscall.Read -1 (closed)")
  86. d.readerMutex.Lock()
  87. defer d.readerMutex.Unlock()
  88. if !d.ReaderClosed {
  89. d.ReaderClosed = true
  90. d.Disconnected = true
  91. close(d.readerChannel)
  92. }
  93. return
  94. }
  95. if r == 0 {
  96. log.Printf("Select said ready, but: %#v %#v\n", r, err)
  97. d.readerMutex.Lock()
  98. defer d.readerMutex.Unlock()
  99. if !d.ReaderClosed {
  100. d.ReaderClosed = true
  101. d.Disconnected = true
  102. close(d.readerChannel)
  103. }
  104. return
  105. }
  106. if DEBUG_INPUT {
  107. log.Printf("Reader << %d, %#v\n", r, readbuffer[:r])
  108. }
  109. // write the received bytes into runebuffer.
  110. runebuffer.Write(readbuffer[:r])
  111. ReadRune(d, runeread, false)
  112. // process(d, true)
  113. // buffer = append(buffer, readone[0])
  114. /*
  115. Take2:
  116. input, size = utf8.DecodeRune(buffer)
  117. if input != utf8.RuneError {
  118. d.readerChannel <- input
  119. for size > 0 {
  120. ArrayDelete(&buffer, 0)
  121. size--
  122. }
  123. if len(buffer) > 0 {
  124. goto Take2
  125. }
  126. timeoutCount = 0
  127. } else {
  128. // Not a valid rune
  129. continue
  130. }
  131. */
  132. // d.readerChannel <- rune(buffer[0])
  133. }
  134. }