package door import ( "syscall" ) // OS Specific Writer // This assumes that d.writerMutex is locked. type OSWriter struct { Handle syscall.Handle BaseWriter } func (ow *OSWriter) Init(d *Door) { ow.Closed = false ow.Handle = syscall.Handle(d.WRITEFD) ow.ansiCode = make([]byte, 0, 32) } // The low-level writer function func (ow *OSWriter) OSWrite(buffer []byte) (int, error) { var l uint32 = uint32(len(buffer)) WSA_Buffer := syscall.WSABuf{Len: uint32(l), Buf: &buffer[0]} var UitnZero_1 uint32 = uint32(0) var DataWrite uint32 = uint32(0) var err error err = syscall.WSASend(ow.Handle, &WSA_Buffer, 1, &DataWrite, UitnZero_1, nil, nil) if (err != nil) || (l != DataWrite) { if !ow.Closed { ow.Closed = true // Don't need to close reader, it will close itself. // It knows when the caller is gone before the writer ever will! } } return int(DataWrite), err } func (ow *OSWriter) Write(buffer []byte) (int, error) { if ow.Closed { return 0, ErrDisconnected } return ow.OSWrite(buffer) } func (ow *OSWriter) Stop() { ow.Closed = true } func (ow *OSWriter) IsClosed() bool { return ow.Closed } /* func Writer(d *Door) { var handle syscall.Handle = syscall.Handle(d.WRITEFD) // Config.Comm_handle) // handle syscall.Handle, writerChannel *chan string log.Println("Writer") defer d.wg.Done() var Closed bool = false for output := range d.writerChannel { if output == "" { Closed = true continue } if Closed { log.Println("Ignoring write output.") continue } if strings.HasSuffix(output, RestorePos) { output += Color(d.LastColor) } else { d.UpdateLastColor(output, &d.LastColor) } var l uint32 = uint32(len(output)) var buffer []byte = []byte(output) WSA_Buffer := syscall.WSABuf{Len: uint32(l), Buf: &buffer[0]} var UitnZero_1 uint32 = uint32(0) var DataWrite uint32 = uint32(0) var err error err = syscall.WSASend(handle, &WSA_Buffer, 1, &DataWrite, UitnZero_1, nil, nil) if DEBUG_OUTPUT { if err != nil { fmt.Printf("write: %d bytes, error: %#v\n", DataWrite, err) } else { log.Printf("write %d bytes\n", DataWrite) } } if (err != nil) || (l != DataWrite) { log.Println("CloseChannel") Closed = true d.writerMutex.Lock() if !d.WriterClosed { d.WriterClosed = true } d.writerMutex.Unlock() // } // } } } log.Println("closeChannel") d.writerMutex.Lock() if !d.WriterClosed { d.WriterClosed = true } d.writerMutex.Unlock() log.Println("~Writer") // return } */