input_linux.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. // go routine Reader for input
  29. // This "times out" every ReaderTimeval
  30. func Reader(d *Door) {
  31. // I need non-blocking reads here.
  32. /*
  33. ReaderTimeval = syscall.Timeval{Sec: int64(ReaderInterval.Seconds()),
  34. Usec: ReaderInterval.Microseconds() % time.Second.Microseconds()}
  35. */
  36. log.Println(ReaderInterval, ReaderTimeval)
  37. readerBuffer = make([]rune, 0, READ_SIZE*2)
  38. defer func() {
  39. log.Printf("~Reader\n")
  40. //if d.ReaderCanClose {
  41. d.wg.Done()
  42. //}
  43. }()
  44. defer func() {
  45. if err := recover(); err != nil {
  46. log.Printf("Reader: %#v\n", err)
  47. }
  48. }()
  49. var fdset syscall.FdSet
  50. var readbuffer [READ_SIZE]byte
  51. var runebuffer bytes.Buffer
  52. var runeread = bufio.NewReaderSize(&runebuffer, 1)
  53. var selectTimeval syscall.Timeval
  54. for {
  55. clearAll(&fdset)
  56. set(&fdset, d.READFD)
  57. selectTimeval = ReaderTimeval
  58. v, err := syscall.Select(d.READFD+1, &fdset, nil, nil, &selectTimeval)
  59. if err == syscall.EINTR {
  60. continue
  61. }
  62. // log.Printf("Select: %#v / %#v\n", v, err)
  63. if v == -1 {
  64. log.Printf("Reader ERR: %#v\n", err)
  65. d.readerMutex.Lock()
  66. if !d.ReaderClosed {
  67. d.ReaderClosed = true
  68. close(d.readerChannel)
  69. }
  70. return
  71. }
  72. if v == 0 {
  73. // timeout
  74. ReadRune(d, runeread, true)
  75. // process(d, false)
  76. d.readerMutex.Lock()
  77. if d.ReaderEnd {
  78. if !d.ReaderClosed {
  79. d.ReaderClosed = true
  80. close(d.readerChannel)
  81. }
  82. d.readerMutex.Unlock()
  83. return
  84. }
  85. d.readerMutex.Unlock()
  86. continue
  87. }
  88. // The buffer used here must have len & cap to size you want to read.
  89. // use [BUFF_SIZE]byte for readone
  90. r, err := syscall.Read(d.READFD, readbuffer[:])
  91. if r == -1 {
  92. log.Println("syscall.Read -1 (closed)")
  93. d.readerMutex.Lock()
  94. defer d.readerMutex.Unlock()
  95. if !d.ReaderClosed {
  96. d.ReaderClosed = true
  97. d.Disconnected = true
  98. close(d.readerChannel)
  99. }
  100. return
  101. }
  102. if r == 0 {
  103. log.Printf("Select said ready, but: %#v %#v\n", r, err)
  104. d.readerMutex.Lock()
  105. defer d.readerMutex.Unlock()
  106. if !d.ReaderClosed {
  107. d.ReaderClosed = true
  108. d.Disconnected = true
  109. close(d.readerChannel)
  110. }
  111. return
  112. }
  113. if DEBUG_INPUT {
  114. log.Printf("Reader << %d, %#v\n", r, readbuffer[:r])
  115. }
  116. // write the received bytes into runebuffer.
  117. runebuffer.Write(readbuffer[:r])
  118. ReadRune(d, runeread, false)
  119. // process(d, true)
  120. // buffer = append(buffer, readone[0])
  121. /*
  122. Take2:
  123. input, size = utf8.DecodeRune(buffer)
  124. if input != utf8.RuneError {
  125. d.readerChannel <- input
  126. for size > 0 {
  127. ArrayDelete(&buffer, 0)
  128. size--
  129. }
  130. if len(buffer) > 0 {
  131. goto Take2
  132. }
  133. timeoutCount = 0
  134. } else {
  135. // Not a valid rune
  136. continue
  137. }
  138. */
  139. // d.readerChannel <- rune(buffer[0])
  140. }
  141. }