input_windows.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package door
  2. // #cgo LDFLAGS: -lws2_32
  3. // #cgo CFLAGS: -Wall
  4. // #include "input_windows.h"
  5. import "C"
  6. import (
  7. "bufio"
  8. "bytes"
  9. "log"
  10. "strings"
  11. "syscall"
  12. "time"
  13. "unsafe"
  14. )
  15. var ReaderInterval = time.Duration(200) * time.Millisecond
  16. var ReaderTimeval syscall.Timeval = syscall.Timeval{Sec: 0, Usec: 200}
  17. // Output a nice string representation of the rune buffer.
  18. func extended_output(buffer []rune) string {
  19. var output string = string(buffer)
  20. output = strings.Replace(output, "\x1b", "^[", -1)
  21. return output
  22. }
  23. func RuneToInt8(r rune) int8 {
  24. return int8(r)
  25. }
  26. func Reader(d *Door) {
  27. // handle syscall.Handle, readerChannel *chan byte
  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. continue
  66. }
  67. gobytes := C.GoBytes(unsafe.Pointer(readerbuffer), read)
  68. if DEBUG_INPUT {
  69. log.Printf("Reader << %d, %#v\n", r, gobytes)
  70. }
  71. runebuffer.Write(gobytes)
  72. ReadRune(d, runeread, false)
  73. /*
  74. var input rune
  75. // Is this unicode?
  76. var err error = nil
  77. for err == nil {
  78. //RuneRead:
  79. input, _, err = runeread.ReadRune()
  80. if err == io.EOF {
  81. break // continue
  82. }
  83. if err != nil {
  84. if DEBUG_INPUT {
  85. log.Printf("ReadRune: %#v\n", err)
  86. }
  87. // errors EOF
  88. break // continue // break for loop
  89. }
  90. if input == unicode.ReplacementChar {
  91. runeread.UnreadRune()
  92. b, _ := runeread.ReadByte()
  93. if DEBUG_INPUT {
  94. log.Printf("Reader (byte) >> %x\n", b)
  95. }
  96. readerBuffer = append(readerBuffer, rune(b))
  97. // d.readerChannel <- rune(b)
  98. } else {
  99. if DEBUG_INPUT {
  100. log.Printf("Reader >> %x\n", input)
  101. }
  102. readerBuffer = append(readerBuffer, input)
  103. // d.readerChannel <- input
  104. }
  105. // process(d, true)
  106. } // goto RuneRead
  107. process(d, true)
  108. */
  109. }
  110. }