From c98fffe722d8a91a883aec3b480d69a720aa70e5 Mon Sep 17 00:00:00 2001 From: Saxon Milton Date: Sat, 14 Oct 2023 00:34:08 +0000 Subject: [PATCH] revid: fix manual input reader and writer The reader and writer pipes should be created on call to Start and not in NewManualInput. This is so that if Close is called, which will close the reader and the writer, a call to start will create them again. Approved-by: Alan Noble Approved-by: David Sutton --- device/device.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/device/device.go b/device/device.go index f1a359d6..b918aba4 100644 --- a/device/device.go +++ b/device/device.go @@ -31,6 +31,7 @@ package device import ( "fmt" "io" + "errors" "bitbucket.org/ausocean/av/revid/config" ) @@ -89,12 +90,14 @@ type ManualInput struct { // NewManualInput provides a new ManualInput. func NewManualInput() *ManualInput { - r, w := io.Pipe() - return &ManualInput{reader: r, writer: w} + return &ManualInput{} } // Read reads from the manual input and puts the bytes into p. func (m *ManualInput) Read(p []byte) (int, error) { + if !m.isRunning { + return 0, errors.New("manual input has not been started, can't read") + } return m.reader.Read(p) } @@ -109,6 +112,7 @@ func (m *ManualInput) Set(c config.Config) error { return nil } // to satisfy the Device interface. func (m *ManualInput) Start() error { m.isRunning = true + m.reader, m.writer = io.Pipe() return nil } @@ -131,5 +135,8 @@ func (m *ManualInput) IsRunning() bool { return m.isRunning } // Write writes p to the ManualInput's writer side of its pipe. func (m *ManualInput) Write(p []byte) (int, error) { + if !m.isRunning { + return 0, errors.New("manual input has not been started, can't write") + } return m.writer.Write(p) }