From ddaf360aaad674975bcea864aace61e07473bc5b Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Wed, 30 Nov 2016 22:22:54 +0100 Subject: [PATCH] platforms/firmata/client: use io.ReadFull This CL uses io.ReadFull to make sure exactly n bytes are read, and correctly handle io.EOF and co. Fixes #343. Signed-off-by: Sebastien Binet --- platforms/firmata/client/client.go | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/platforms/firmata/client/client.go b/platforms/firmata/client/client.go index 370a5d06..26fd557c 100644 --- a/platforms/firmata/client/client.go +++ b/platforms/firmata/client/client.go @@ -315,21 +315,9 @@ func (b *Client) write(data []byte) (err error) { return } -func (b *Client) read(length int) (buf []byte, err error) { - i := 0 - for length > 0 { - tmp := make([]byte, length) - if i, err = b.connection.Read(tmp); err != nil { - if err.Error() != "EOF" { - return - } - <-time.After(5 * time.Millisecond) - } - if i > 0 { - buf = append(buf, tmp...) - length = length - i - } - } +func (b *Client) read(n int) (buf []byte, err error) { + buf = make([]byte, n) + _, err = io.ReadFull(b.connection, buf) return }