write_windows.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package door
  2. import (
  3. "syscall"
  4. )
  5. // OS Specific Writer
  6. // This assumes that d.writerMutex is locked.
  7. type OSWriter struct {
  8. Handle syscall.Handle
  9. BaseWriter
  10. }
  11. func (ow *OSWriter) Init(d *Door) {
  12. ow.Closed = false
  13. ow.Handle = syscall.Handle(d.WRITEFD)
  14. ow.ansiCode = make([]byte, 0, 32)
  15. }
  16. // The low-level writer function
  17. func (ow *OSWriter) OSWrite(buffer []byte) (int, error) {
  18. var l uint32 = uint32(len(buffer))
  19. WSA_Buffer := syscall.WSABuf{Len: uint32(l), Buf: &buffer[0]}
  20. var UitnZero_1 uint32 = uint32(0)
  21. var DataWrite uint32 = uint32(0)
  22. var err error
  23. err = syscall.WSASend(ow.Handle, &WSA_Buffer, 1, &DataWrite, UitnZero_1, nil, nil)
  24. if (err != nil) || (l != DataWrite) {
  25. if !ow.Closed {
  26. ow.Closed = true
  27. // Don't need to close reader, it will close itself.
  28. // It knows when the caller is gone before the writer ever will!
  29. }
  30. }
  31. return int(DataWrite), err
  32. }
  33. func (ow *OSWriter) Write(buffer []byte) (int, error) {
  34. if ow.Closed {
  35. return 0, ErrDisconnected
  36. }
  37. return ow.OSWrite(buffer)
  38. }
  39. func (ow *OSWriter) Stop() {
  40. ow.Closed = true
  41. }
  42. func (ow *OSWriter) IsClosed() bool {
  43. return ow.Closed
  44. }
  45. /*
  46. func Writer(d *Door) {
  47. var handle syscall.Handle = syscall.Handle(d.WRITEFD) // Config.Comm_handle)
  48. // handle syscall.Handle, writerChannel *chan string
  49. log.Println("Writer")
  50. defer d.wg.Done()
  51. var Closed bool = false
  52. for output := range d.writerChannel {
  53. if output == "" {
  54. Closed = true
  55. continue
  56. }
  57. if Closed {
  58. log.Println("Ignoring write output.")
  59. continue
  60. }
  61. if strings.HasSuffix(output, RestorePos) {
  62. output += Color(d.LastColor)
  63. } else {
  64. d.UpdateLastColor(output, &d.LastColor)
  65. }
  66. var l uint32 = uint32(len(output))
  67. var buffer []byte = []byte(output)
  68. WSA_Buffer := syscall.WSABuf{Len: uint32(l), Buf: &buffer[0]}
  69. var UitnZero_1 uint32 = uint32(0)
  70. var DataWrite uint32 = uint32(0)
  71. var err error
  72. err = syscall.WSASend(handle, &WSA_Buffer, 1, &DataWrite, UitnZero_1, nil, nil)
  73. if DEBUG_OUTPUT {
  74. if err != nil {
  75. fmt.Printf("write: %d bytes, error: %#v\n", DataWrite, err)
  76. } else {
  77. log.Printf("write %d bytes\n", DataWrite)
  78. }
  79. }
  80. if (err != nil) || (l != DataWrite) {
  81. log.Println("CloseChannel")
  82. Closed = true
  83. d.writerMutex.Lock()
  84. if !d.WriterClosed {
  85. d.WriterClosed = true
  86. }
  87. d.writerMutex.Unlock()
  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. */