write_windows.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 err != nil {
  66. fmt.Printf("write: %d bytes, error: %#v\n", DataWrite, err)
  67. } else {
  68. log.Printf("write %d bytes\n", DataWrite)
  69. }
  70. if (err != nil) || (l != DataWrite) {
  71. log.Println("CloseChannel")
  72. Closed = true
  73. d.writerMutex.Lock()
  74. if !d.WriterClosed {
  75. d.WriterClosed = true
  76. }
  77. d.writerMutex.Unlock()
  78. /*
  79. if !d.Disconnect() {
  80. atomic.StoreInt32(&d.Disconnected, 1)
  81. close(d.writerChannel)
  82. }
  83. log.Println("~Writer")
  84. return
  85. */
  86. // }
  87. // }
  88. }
  89. }
  90. log.Println("closeChannel")
  91. d.writerMutex.Lock()
  92. if !d.WriterClosed {
  93. d.WriterClosed = true
  94. }
  95. d.writerMutex.Unlock()
  96. log.Println("~Writer")
  97. // return
  98. /*
  99. for output := range *writerChannel {
  100. l := uint32(len(output))
  101. buffer := []byte(output)
  102. WSA_Buffer := syscall.WSABuf{Len: uint32(l), Buf: &buffer[0]}
  103. UitnZero_1 := uint32(0)
  104. DataWrite := uint32(0)
  105. err := syscall.WSASend(handle, &WSA_Buffer, 1, &DataWrite, UitnZero_1, nil, nil)
  106. if err != nil {
  107. fmt.Printf("write: %d bytes, error: %#v\n", DataWrite, err)
  108. }
  109. if (err != nil) || (l != DataWrite) {
  110. close(*writerChannel)
  111. break
  112. }
  113. }
  114. */
  115. }