input_windows.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 RuneToInt8(r rune) int8 {
  25. return int8(r)
  26. }
  27. func Reader(d *Door) {
  28. // handle syscall.Handle, readerChannel *chan byte
  29. C.init_wsa()
  30. var runebuffer bytes.Buffer
  31. var runeread = bufio.NewReaderSize(&runebuffer, 1)
  32. // readerBuffer = make([]rune, 0, READ_SIZE*2)
  33. readerbuffer := C.malloc(C.sizeof_char * READ_SIZE)
  34. defer C.free(unsafe.Pointer(readerbuffer))
  35. defer func() {
  36. log.Printf("~Reader")
  37. if d.ReaderCanClose {
  38. d.wg.Done()
  39. }
  40. }()
  41. defer func() {
  42. if err := recover(); err != nil {
  43. log.Printf("Reader: %#v\n", err)
  44. }
  45. }()
  46. var r int
  47. for {
  48. read := C.windows_read(C.int(d.Config.Comm_handle), (*C.char)(readerbuffer), C.int(READ_SIZE))
  49. r = int(read)
  50. // log.Printf("windows_read: %d\n", r)
  51. if r == -1 {
  52. log.Println("window_read -1 (closed)")
  53. d.readerMutex.Lock()
  54. defer d.readerMutex.Unlock()
  55. if !d.ReaderClosed {
  56. d.ReaderClosed = true
  57. d.Disconnected = true
  58. close(d.readerChannel)
  59. }
  60. return
  61. }
  62. if r == 0 {
  63. // timeout
  64. ReadRune(d, runeread, true)
  65. // process(d, false)
  66. continue
  67. }
  68. gobytes := C.GoBytes(unsafe.Pointer(readerbuffer), read)
  69. if DEBUG_INPUT {
  70. log.Printf("Reader << %d, %#v\n", r, gobytes)
  71. }
  72. runebuffer.Write(gobytes)
  73. ReadRune(d, runeread, false)
  74. /*
  75. var input rune
  76. // Is this unicode?
  77. var err error = nil
  78. for err == nil {
  79. //RuneRead:
  80. input, _, err = runeread.ReadRune()
  81. if err == io.EOF {
  82. break // continue
  83. }
  84. if err != nil {
  85. if DEBUG_INPUT {
  86. log.Printf("ReadRune: %#v\n", err)
  87. }
  88. // errors EOF
  89. break // continue // break for loop
  90. }
  91. if input == unicode.ReplacementChar {
  92. runeread.UnreadRune()
  93. b, _ := runeread.ReadByte()
  94. if DEBUG_INPUT {
  95. log.Printf("Reader (byte) >> %x\n", b)
  96. }
  97. readerBuffer = append(readerBuffer, rune(b))
  98. // d.readerChannel <- rune(b)
  99. } else {
  100. if DEBUG_INPUT {
  101. log.Printf("Reader >> %x\n", input)
  102. }
  103. readerBuffer = append(readerBuffer, input)
  104. // d.readerChannel <- input
  105. }
  106. // process(d, true)
  107. } // goto RuneRead
  108. process(d, true)
  109. */
  110. }
  111. }