input_windows.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package door
  2. // Don't use : #cgo CFLAGS: -Wall
  3. // https://github.com/golang/go/issues/6883#issuecomment-383800123
  4. // #cgo LDFLAGS: -lws2_32
  5. // #include "input_windows.h"
  6. import "C"
  7. import (
  8. "bufio"
  9. "bytes"
  10. "log"
  11. "strings"
  12. "syscall"
  13. "time"
  14. "unsafe"
  15. )
  16. var ReaderInterval = time.Duration(200) * time.Millisecond
  17. var ReaderTimeval syscall.Timeval = syscall.Timeval{Sec: 0, Usec: 200}
  18. // Output a nice string representation of the rune buffer.
  19. func extended_output(buffer []rune) string {
  20. var output string = string(buffer)
  21. output = strings.Replace(output, "\x1b", "^[", -1)
  22. return output
  23. }
  24. func Reader(d *Door) {
  25. // handle syscall.Handle, readerChannel *chan byte
  26. var WSA syscall.WSAData
  27. syscall.WSAStartup(0x202, &WSA)
  28. // C.init_wsa()
  29. var runebuffer bytes.Buffer
  30. var runeread = bufio.NewReaderSize(&runebuffer, 1)
  31. // readerBuffer = make([]rune, 0, READ_SIZE*2)
  32. readerbuffer := C.malloc(C.sizeof_char * READ_SIZE)
  33. defer C.free(unsafe.Pointer(readerbuffer))
  34. defer func() {
  35. log.Printf("~Reader")
  36. // if d.ReaderCanClose {
  37. d.wg.Done()
  38. // }
  39. }()
  40. defer func() {
  41. if err := recover(); err != nil {
  42. log.Printf("Reader: %#v\n", err)
  43. }
  44. }()
  45. var r int
  46. for {
  47. read := C.windows_read(C.int(d.Config.Comm_handle), (*C.char)(readerbuffer), C.int(READ_SIZE))
  48. r = int(read)
  49. // log.Printf("windows_read: %d\n", r)
  50. if r == -1 {
  51. log.Println("window_read -1 (closed)")
  52. d.readerMutex.Lock()
  53. defer d.readerMutex.Unlock()
  54. if !d.ReaderClosed {
  55. d.ReaderClosed = true
  56. d.Disconnected = true
  57. close(d.readerChannel)
  58. }
  59. return
  60. }
  61. if r == 0 {
  62. // timeout
  63. ReadRune(d, runeread, true)
  64. // process(d, false)
  65. d.readerMutex.Lock()
  66. if d.ReaderEnd {
  67. log.Println("Door closing...")
  68. if !d.ReaderClosed {
  69. d.ReaderClosed = true
  70. close(d.readerChannel)
  71. }
  72. d.readerMutex.Unlock()
  73. return
  74. }
  75. d.readerMutex.Unlock()
  76. continue
  77. }
  78. gobytes := C.GoBytes(unsafe.Pointer(readerbuffer), read)
  79. if DEBUG_INPUT {
  80. log.Printf("Reader << %d, %#v\n", r, gobytes)
  81. }
  82. runebuffer.Write(gobytes)
  83. ReadRune(d, runeread, false)
  84. /*
  85. var input rune
  86. // Is this unicode?
  87. var err error = nil
  88. for err == nil {
  89. //RuneRead:
  90. input, _, err = runeread.ReadRune()
  91. if err == io.EOF {
  92. break // continue
  93. }
  94. if err != nil {
  95. if DEBUG_INPUT {
  96. log.Printf("ReadRune: %#v\n", err)
  97. }
  98. // errors EOF
  99. break // continue // break for loop
  100. }
  101. if input == unicode.ReplacementChar {
  102. runeread.UnreadRune()
  103. b, _ := runeread.ReadByte()
  104. if DEBUG_INPUT {
  105. log.Printf("Reader (byte) >> %x\n", b)
  106. }
  107. readerBuffer = append(readerBuffer, rune(b))
  108. // d.readerChannel <- rune(b)
  109. } else {
  110. if DEBUG_INPUT {
  111. log.Printf("Reader >> %x\n", input)
  112. }
  113. readerBuffer = append(readerBuffer, input)
  114. // d.readerChannel <- input
  115. }
  116. // process(d, true)
  117. } // goto RuneRead
  118. process(d, true)
  119. */
  120. }
  121. }