Introduce configurable buffer size

This commit is contained in:
Jan Dittberner 2020-04-20 22:01:06 +02:00
parent 9924771531
commit 0bb19ba8bd
4 changed files with 23 additions and 5 deletions

View File

@ -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",

View File

@ -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)

View File

@ -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:

View File

@ -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
}