cacert-gosigner/shared/io.go
Jan Dittberner 65855152ce Implement first client command
The client can now talk to the old Perl signer implementation. Running
socat has been documented in README.md. Commonly used I/O code has been
moved to the shared/io.go file.
2020-04-17 19:38:54 +02:00

32 lines
697 B
Go

package shared
import (
"errors"
"github.com/goburrow/serial"
"io"
"time"
)
// receive the requested number of bytes from serial port and stop after the given timeout in seconds
func ReceiveBytes(port *serial.Port, count int, timeout time.Duration) ([]byte, error) {
timeoutCh := time.After(timeout * time.Second)
readCh := make(chan []byte, 1)
errCh := make(chan error, 1)
go func() {
data := make([]byte, count)
if _, err := io.ReadAtLeast(*port, data, count); err != nil {
errCh <- err
} else {
readCh <- data
}
}()
select {
case <-timeoutCh:
return nil, errors.New("timeout")
case err := <-errCh:
return nil, err
case data := <-readCh:
return data, nil
}
}