write_windows.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 {
  15. /*
  16. select {
  17. case <-d.closeChannel:
  18. close(d.writerChannel)
  19. log.Println("~Writer")
  20. return
  21. default:
  22. }
  23. */
  24. select {
  25. /*
  26. case <-d.closeChannel:
  27. close(d.writerChannel)
  28. log.Println("~Writer")
  29. return
  30. */
  31. case output, ok := <-d.writerChannel:
  32. if !ok {
  33. log.Println("closeChannel")
  34. d.writerMutex.Lock()
  35. if !d.WriterClosed {
  36. d.WriterClosed = true
  37. }
  38. d.writerMutex.Unlock()
  39. log.Println("~Writer")
  40. return
  41. } else {
  42. if output == "" {
  43. Closed = true
  44. continue
  45. }
  46. if Closed {
  47. log.Println("Ignoring write output.")
  48. continue
  49. }
  50. if strings.HasSuffix(output, RestorePos) {
  51. output += Color(d.LastColor...)
  52. } else {
  53. d.UpdateLastColor(output, &d.LastColor)
  54. }
  55. var l uint32 = uint32(len(output))
  56. var buffer []byte = []byte(output)
  57. WSA_Buffer := syscall.WSABuf{Len: uint32(l), Buf: &buffer[0]}
  58. var UitnZero_1 uint32 = uint32(0)
  59. var DataWrite uint32 = uint32(0)
  60. var err error
  61. err = syscall.WSASend(handle, &WSA_Buffer, 1, &DataWrite, UitnZero_1, nil, nil)
  62. if err != nil {
  63. fmt.Printf("write: %d bytes, error: %#v\n", DataWrite, err)
  64. }
  65. if (err != nil) || (l != DataWrite) {
  66. log.Println("CloseChannel")
  67. Closed = true
  68. d.writerMutex.Lock()
  69. if !d.WriterClosed {
  70. d.WriterClosed = true
  71. }
  72. d.writerMutex.Unlock()
  73. /*
  74. if !d.Disconnect() {
  75. atomic.StoreInt32(&d.Disconnected, 1)
  76. close(d.writerChannel)
  77. }
  78. log.Println("~Writer")
  79. return
  80. */
  81. }
  82. }
  83. }
  84. }
  85. /*
  86. for output := range *writerChannel {
  87. l := uint32(len(output))
  88. buffer := []byte(output)
  89. WSA_Buffer := syscall.WSABuf{Len: uint32(l), Buf: &buffer[0]}
  90. UitnZero_1 := uint32(0)
  91. DataWrite := uint32(0)
  92. err := syscall.WSASend(handle, &WSA_Buffer, 1, &DataWrite, UitnZero_1, nil, nil)
  93. if err != nil {
  94. fmt.Printf("write: %d bytes, error: %#v\n", DataWrite, err)
  95. }
  96. if (err != nil) || (l != DataWrite) {
  97. close(*writerChannel)
  98. break
  99. }
  100. }
  101. */
  102. }