Introduce configurable buffer size
This commit is contained in:
parent
9924771531
commit
0bb19ba8bd
4 changed files with 23 additions and 5 deletions
|
@ -11,6 +11,7 @@ import (
|
||||||
type ClientConfig struct {
|
type ClientConfig struct {
|
||||||
SerialAddress string `yaml:"serial_address"`
|
SerialAddress string `yaml:"serial_address"`
|
||||||
BaudRate int `yaml:"serial_baudrate"`
|
BaudRate int `yaml:"serial_baudrate"`
|
||||||
|
BufferSize uint16 `yaml:"serial_buffer_size"`
|
||||||
Paranoid bool `yaml:"paranoid"`
|
Paranoid bool `yaml:"paranoid"`
|
||||||
Debug bool `yaml:"debug"`
|
Debug bool `yaml:"debug"`
|
||||||
GNUPGBinary string `yaml:"gnupg_bin"`
|
GNUPGBinary string `yaml:"gnupg_bin"`
|
||||||
|
@ -21,6 +22,7 @@ type ClientConfig struct {
|
||||||
var defaultConfig = ClientConfig{
|
var defaultConfig = ClientConfig{
|
||||||
SerialAddress: "/dev/ttyUSB0",
|
SerialAddress: "/dev/ttyUSB0",
|
||||||
BaudRate: 115200,
|
BaudRate: 115200,
|
||||||
|
BufferSize: 2048,
|
||||||
Paranoid: false,
|
Paranoid: false,
|
||||||
Debug: false,
|
Debug: false,
|
||||||
OpenSSLBinary: "/usr/bin/openssl",
|
OpenSSLBinary: "/usr/bin/openssl",
|
||||||
|
|
|
@ -46,7 +46,13 @@ func main() {
|
||||||
responseChannel := make(chan *datastructures.SignerResponse, 1)
|
responseChannel := make(chan *datastructures.SignerResponse, 1)
|
||||||
|
|
||||||
readWriteCloser := (io.ReadWriteCloser)(port)
|
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)
|
cancelChannel := make(chan os.Signal, 1)
|
||||||
signal.Notify(cancelChannel, syscall.SIGTERM, syscall.SIGINT)
|
signal.Notify(cancelChannel, syscall.SIGTERM, syscall.SIGINT)
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
|
|
||||||
func Process(response *datastructures.SignerResponse) (err error) {
|
func Process(response *datastructures.SignerResponse) (err error) {
|
||||||
logrus.Infof("process response of type %s", response.Action)
|
logrus.Infof("process response of type %s", response.Action)
|
||||||
logrus.Tracef("process response %v", response)
|
logrus.Tracef("process response %+v", response)
|
||||||
|
|
||||||
switch response.Action {
|
switch response.Action {
|
||||||
case datastructures.ActionNul:
|
case datastructures.ActionNul:
|
||||||
|
|
|
@ -16,6 +16,14 @@ type SignerProtocolHandler interface {
|
||||||
HandleSignerProtocol() error
|
HandleSignerProtocol() error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type signerProtocolConfig struct {
|
||||||
|
BufferSize int
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSignerProtocolConfig() *signerProtocolConfig {
|
||||||
|
return &signerProtocolConfig{BufferSize: 2048}
|
||||||
|
}
|
||||||
|
|
||||||
type SignerProtocolRequestChannel struct {
|
type SignerProtocolRequestChannel struct {
|
||||||
C chan *datastructures.SignerRequest
|
C chan *datastructures.SignerRequest
|
||||||
closed bool
|
closed bool
|
||||||
|
@ -45,6 +53,7 @@ type protocolHandler struct {
|
||||||
requestChannel *SignerProtocolRequestChannel
|
requestChannel *SignerProtocolRequestChannel
|
||||||
responseChannel *chan *datastructures.SignerResponse
|
responseChannel *chan *datastructures.SignerResponse
|
||||||
serialConnection *io.ReadWriteCloser
|
serialConnection *io.ReadWriteCloser
|
||||||
|
config *signerProtocolConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
type UnExpectedAcknowledgeByte struct {
|
type UnExpectedAcknowledgeByte struct {
|
||||||
|
@ -67,11 +76,12 @@ func (ph *protocolHandler) Close() error {
|
||||||
return nil
|
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{
|
return &protocolHandler{
|
||||||
requestChannel: requests,
|
requestChannel: requests,
|
||||||
responseChannel: response,
|
responseChannel: response,
|
||||||
serialConnection: serialConnection,
|
serialConnection: serialConnection,
|
||||||
|
config: config,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,7 +89,7 @@ func (ph *protocolHandler) HandleSignerProtocol() error {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case request := <-ph.requestChannel.C:
|
case request := <-ph.requestChannel.C:
|
||||||
log.Debugf("handle request %+v", request)
|
log.Tracef("handle request %+v", request)
|
||||||
var err error
|
var err error
|
||||||
var lengthBytes, responseBytes *[]byte
|
var lengthBytes, responseBytes *[]byte
|
||||||
var checksum byte
|
var checksum byte
|
||||||
|
@ -187,7 +197,7 @@ func (ph *protocolHandler) readResponse() (*[]byte, *[]byte, byte, error) {
|
||||||
var byteBuffer = bytes.NewBuffer(make([]byte, 0))
|
var byteBuffer = bytes.NewBuffer(make([]byte, 0))
|
||||||
|
|
||||||
for {
|
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 {
|
if err != nil {
|
||||||
return nil, nil, 0, err
|
return nil, nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue