write_linux.go 930 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package door
  2. import (
  3. "syscall"
  4. )
  5. // OS Specific Write
  6. // This assumes that d.writerMutex is locked.
  7. type OSWriter struct {
  8. Handle int
  9. BaseWriter
  10. }
  11. func (ow *OSWriter) Init(d *Door) {
  12. ow.Closed = false
  13. ow.Handle = d.Config.Comm_handle
  14. ow.ansiCode = make([]byte, 0, 32)
  15. }
  16. // The low-lever writer function
  17. func (ow *OSWriter) OSWrite(buffer []byte) (int, error) {
  18. n, err := syscall.Write(ow.Handle, buffer)
  19. if (err != nil) || (n != len(buffer)) {
  20. if !ow.Closed {
  21. ow.Closed = true
  22. // Don't need to close reader, it will close itself.
  23. // It knows when the caller is gone before the writer ever will!
  24. }
  25. }
  26. return n, err
  27. }
  28. func (ow *OSWriter) Write(buffer []byte) (int, error) {
  29. if ow.Closed {
  30. return 0, ErrDisconnected
  31. }
  32. return ow.OSWrite(buffer)
  33. }
  34. func (ow *OSWriter) Stop() {
  35. ow.Closed = true
  36. }
  37. // Safe way to check if OSWriter is closed
  38. func (ow *OSWriter) IsClosed() bool {
  39. return ow.Closed
  40. }