write_windows.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package door
  2. import (
  3. "fmt"
  4. "log"
  5. "strings"
  6. "syscall"
  7. )
  8. func Writer(d *Door) {
  9. var handle syscall.Handle = syscall.Handle(d.WRITEFD) // Config.Comm_handle)
  10. // handle syscall.Handle, writerChannel *chan string
  11. log.Println("Writer")
  12. defer d.wg.Done()
  13. var Closed bool = false
  14. for output := range d.writerChannel {
  15. // for {
  16. /*
  17. select {
  18. case <-d.closeChannel:
  19. close(d.writerChannel)
  20. log.Println("~Writer")
  21. return
  22. default:
  23. }
  24. */
  25. // select {
  26. /*
  27. case <-d.closeChannel:
  28. close(d.writerChannel)
  29. log.Println("~Writer")
  30. return
  31. */
  32. /*
  33. case output, ok := <-d.writerChannel:
  34. if !ok {
  35. log.Println("closeChannel")
  36. d.writerMutex.Lock()
  37. if !d.WriterClosed {
  38. d.WriterClosed = true
  39. }
  40. d.writerMutex.Unlock()
  41. log.Println("~Writer")
  42. return
  43. } else {
  44. */
  45. if output == "" {
  46. Closed = true
  47. continue
  48. }
  49. if Closed {
  50. log.Println("Ignoring write output.")
  51. continue
  52. }
  53. if strings.HasSuffix(output, RestorePos) {
  54. output += Color(d.LastColor)
  55. } else {
  56. d.UpdateLastColor(output, &d.LastColor)
  57. }
  58. var l uint32 = uint32(len(output))
  59. var buffer []byte = []byte(output)
  60. WSA_Buffer := syscall.WSABuf{Len: uint32(l), Buf: &buffer[0]}
  61. var UitnZero_1 uint32 = uint32(0)
  62. var DataWrite uint32 = uint32(0)
  63. var err error
  64. err = syscall.WSASend(handle, &WSA_Buffer, 1, &DataWrite, UitnZero_1, nil, nil)
  65. if DEBUG_OUTPUT {
  66. if err != nil {
  67. fmt.Printf("write: %d bytes, error: %#v\n", DataWrite, err)
  68. } else {
  69. log.Printf("write %d bytes\n", DataWrite)
  70. }
  71. }
  72. if (err != nil) || (l != DataWrite) {
  73. log.Println("CloseChannel")
  74. Closed = true
  75. d.writerMutex.Lock()
  76. if !d.WriterClosed {
  77. d.WriterClosed = true
  78. }
  79. d.writerMutex.Unlock()
  80. /*
  81. if !d.Disconnect() {
  82. atomic.StoreInt32(&d.Disconnected, 1)
  83. close(d.writerChannel)
  84. }
  85. log.Println("~Writer")
  86. return
  87. */
  88. // }
  89. // }
  90. }
  91. }
  92. log.Println("closeChannel")
  93. d.writerMutex.Lock()
  94. if !d.WriterClosed {
  95. d.WriterClosed = true
  96. }
  97. d.writerMutex.Unlock()
  98. log.Println("~Writer")
  99. // return
  100. /*
  101. for output := range *writerChannel {
  102. l := uint32(len(output))
  103. buffer := []byte(output)
  104. WSA_Buffer := syscall.WSABuf{Len: uint32(l), Buf: &buffer[0]}
  105. UitnZero_1 := uint32(0)
  106. DataWrite := uint32(0)
  107. err := syscall.WSASend(handle, &WSA_Buffer, 1, &DataWrite, UitnZero_1, nil, nil)
  108. if err != nil {
  109. fmt.Printf("write: %d bytes, error: %#v\n", DataWrite, err)
  110. }
  111. if (err != nil) || (l != DataWrite) {
  112. close(*writerChannel)
  113. break
  114. }
  115. }
  116. */
  117. }