From 0bb19ba8bd00b424de4af419b0c9599ac625c72a Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Mon, 20 Apr 2020 22:01:06 +0200 Subject: [PATCH] Introduce configurable buffer size --- client/config.go | 2 ++ client/main.go | 8 +++++++- client/processing/process.go | 2 +- client/protocol/protocol.go | 16 +++++++++++++--- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/client/config.go b/client/config.go index 719dded..d8b19da 100644 --- a/client/config.go +++ b/client/config.go @@ -11,6 +11,7 @@ import ( type ClientConfig struct { SerialAddress string `yaml:"serial_address"` BaudRate int `yaml:"serial_baudrate"` + BufferSize uint16 `yaml:"serial_buffer_size"` Paranoid bool `yaml:"paranoid"` Debug bool `yaml:"debug"` GNUPGBinary string `yaml:"gnupg_bin"` @@ -21,6 +22,7 @@ type ClientConfig struct { var defaultConfig = ClientConfig{ SerialAddress: "/dev/ttyUSB0", BaudRate: 115200, + BufferSize: 2048, Paranoid: false, Debug: false, OpenSSLBinary: "/usr/bin/openssl", diff --git a/client/main.go b/client/main.go index bb3c61d..6f7b4ff 100644 --- a/client/main.go +++ b/client/main.go @@ -46,7 +46,13 @@ func main() { responseChannel := make(chan *datastructures.SignerResponse, 1) readWriteCloser := (io.ReadWriteCloser)(port) - protocolHandler := protocol.NewProtocolHandler(requestChannel, &responseChannel, &readWriteCloser) + clientProtocolConfig := protocol.NewSignerProtocolConfig() + if clientConfig.BufferSize != 0 { + clientProtocolConfig.BufferSize = int(clientConfig.BufferSize) + } + protocolHandler := protocol.NewProtocolHandler( + requestChannel, &responseChannel, &readWriteCloser, clientProtocolConfig, + ) cancelChannel := make(chan os.Signal, 1) signal.Notify(cancelChannel, syscall.SIGTERM, syscall.SIGINT) diff --git a/client/processing/process.go b/client/processing/process.go index 6263920..ed93b3d 100644 --- a/client/processing/process.go +++ b/client/processing/process.go @@ -8,7 +8,7 @@ import ( func Process(response *datastructures.SignerResponse) (err error) { logrus.Infof("process response of type %s", response.Action) - logrus.Tracef("process response %v", response) + logrus.Tracef("process response %+v", response) switch response.Action { case datastructures.ActionNul: diff --git a/client/protocol/protocol.go b/client/protocol/protocol.go index 8be88d3..24290e4 100644 --- a/client/protocol/protocol.go +++ b/client/protocol/protocol.go @@ -16,6 +16,14 @@ type SignerProtocolHandler interface { HandleSignerProtocol() error } +type signerProtocolConfig struct { + BufferSize int +} + +func NewSignerProtocolConfig() *signerProtocolConfig { + return &signerProtocolConfig{BufferSize: 2048} +} + type SignerProtocolRequestChannel struct { C chan *datastructures.SignerRequest closed bool @@ -45,6 +53,7 @@ type protocolHandler struct { requestChannel *SignerProtocolRequestChannel responseChannel *chan *datastructures.SignerResponse serialConnection *io.ReadWriteCloser + config *signerProtocolConfig } type UnExpectedAcknowledgeByte struct { @@ -67,11 +76,12 @@ func (ph *protocolHandler) Close() error { return nil } -func NewProtocolHandler(requests *SignerProtocolRequestChannel, response *chan *datastructures.SignerResponse, serialConnection *io.ReadWriteCloser) SignerProtocolHandler { +func NewProtocolHandler(requests *SignerProtocolRequestChannel, response *chan *datastructures.SignerResponse, serialConnection *io.ReadWriteCloser, config *signerProtocolConfig) SignerProtocolHandler { return &protocolHandler{ requestChannel: requests, responseChannel: response, serialConnection: serialConnection, + config: config, } } @@ -79,7 +89,7 @@ func (ph *protocolHandler) HandleSignerProtocol() error { for { select { case request := <-ph.requestChannel.C: - log.Debugf("handle request %+v", request) + log.Tracef("handle request %+v", request) var err error var lengthBytes, responseBytes *[]byte var checksum byte @@ -187,7 +197,7 @@ func (ph *protocolHandler) readResponse() (*[]byte, *[]byte, byte, error) { var byteBuffer = bytes.NewBuffer(make([]byte, 0)) for { - readBuffer, err := shared.ReceiveBytes(ph.serialConnection, 100, 5*time.Second) + readBuffer, err := shared.ReceiveBytes(ph.serialConnection, ph.config.BufferSize, 5*time.Second) if err != nil { return nil, nil, 0, err }