package door import ( "syscall" ) // OS Specific Write // This assumes that d.writerMutex is locked. type OSWriter struct { Handle int BaseWriter } func (ow *OSWriter) Init(d *Door) { ow.Closed = false ow.Handle = d.Config.Comm_handle ow.ansiCode = make([]byte, 0, 32) } // The low-lever writer function func (ow *OSWriter) OSWrite(buffer []byte) (int, error) { n, err := syscall.Write(ow.Handle, buffer) if (err != nil) || (n != len(buffer)) { 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 n, 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 } // Safe way to check if OSWriter is closed func (ow *OSWriter) IsClosed() bool { return ow.Closed }