Refactor client I/O into protocol package
This commit is contained in:
parent
337e974a26
commit
9924771531
7 changed files with 339 additions and 200 deletions
|
@ -4,6 +4,7 @@ import (
|
|||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
@ -53,7 +54,8 @@ readLoop:
|
|||
|
||||
commandChan := make(chan datastructures.SignerRequest, 1)
|
||||
errChan := make(chan error, 1)
|
||||
go Receive(&port, &commandChan, &errChan)
|
||||
readWriteCloser := (io.ReadWriteCloser)(port)
|
||||
go Receive(&readWriteCloser, &commandChan, &errChan)
|
||||
|
||||
select {
|
||||
case command := <-commandChan:
|
||||
|
@ -61,7 +63,7 @@ readLoop:
|
|||
if err != nil {
|
||||
log.Printf("ERROR %v\n", err)
|
||||
} else {
|
||||
_ = SendResponse(&port, response)
|
||||
_ = SendResponse(&readWriteCloser, response)
|
||||
}
|
||||
case <-timeout:
|
||||
log.Println("timeout in main loop")
|
||||
|
@ -76,7 +78,7 @@ readLoop:
|
|||
}
|
||||
|
||||
// Send a response to the client
|
||||
func SendResponse(port *serial.Port, response *datastructures.SignerResponse) error {
|
||||
func SendResponse(port *io.ReadWriteCloser, response *datastructures.SignerResponse) error {
|
||||
if _, err := (*port).Write([]byte{0x02}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -136,7 +138,7 @@ func handleNulAction(command datastructures.SignerRequest) (*datastructures.Sign
|
|||
}
|
||||
|
||||
// Receive a request and generate a request data structure
|
||||
func Receive(port *serial.Port, commandChan *chan datastructures.SignerRequest, errorChan *chan error) {
|
||||
func Receive(port *io.ReadWriteCloser, commandChan *chan datastructures.SignerRequest, errorChan *chan error) {
|
||||
header, err := shared.ReceiveBytes(port, 1, 20)
|
||||
if err != nil {
|
||||
*errorChan <- err
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue