2022-08-03 12:38:36 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 CAcert Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Package protocol handles the protocol message marshaling and unmarshalling.
|
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2022-11-29 08:57:23 +00:00
|
|
|
"bytes"
|
2022-12-01 20:36:10 +00:00
|
|
|
"context"
|
2022-11-29 08:57:23 +00:00
|
|
|
"errors"
|
2022-08-03 12:38:36 +00:00
|
|
|
"fmt"
|
2022-11-29 08:57:23 +00:00
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/justincpresley/go-cobs"
|
|
|
|
"github.com/sirupsen/logrus"
|
2022-08-03 12:38:36 +00:00
|
|
|
|
|
|
|
"git.cacert.org/cacert-gosigner/pkg/messages"
|
|
|
|
)
|
|
|
|
|
2022-12-04 18:17:36 +00:00
|
|
|
const COBSDelimiter = 0x00
|
2022-11-28 16:10:46 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
type Command struct {
|
|
|
|
Announce *messages.CommandAnnounce
|
|
|
|
Command interface{}
|
|
|
|
}
|
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
func (c *Command) String() string {
|
|
|
|
return fmt.Sprintf("Cmd[announce={%s}, data={%s}]", c.Announce, c.Command)
|
|
|
|
}
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
type Response struct {
|
|
|
|
Announce *messages.ResponseAnnounce
|
|
|
|
Response interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Response) String() string {
|
2022-11-28 16:10:46 +00:00
|
|
|
return fmt.Sprintf("Rsp[announce={%s}, data={%s}]", r.Announce, r.Response)
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 10:45:59 +00:00
|
|
|
// ServerHandler is responsible for parsing incoming frames and calling commands
|
|
|
|
type ServerHandler interface {
|
|
|
|
// CommandAnnounce handles the initial announcement of a command.
|
2022-12-04 12:47:51 +00:00
|
|
|
CommandAnnounce(context.Context, <-chan []byte) (*Command, error)
|
2022-11-29 10:45:59 +00:00
|
|
|
// CommandData handles the command data.
|
2022-12-04 12:47:51 +00:00
|
|
|
CommandData(context.Context, <-chan []byte, *Command) error
|
2022-11-29 10:45:59 +00:00
|
|
|
// HandleCommand executes the command, generating a response.
|
2022-12-01 20:36:10 +00:00
|
|
|
HandleCommand(context.Context, *Command) (*Response, error)
|
2022-11-29 10:45:59 +00:00
|
|
|
// Respond generates the response for a command.
|
2022-12-04 12:47:51 +00:00
|
|
|
Respond(context.Context, *Response, chan<- []byte) error
|
2022-08-03 12:38:36 +00:00
|
|
|
}
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
type ClientHandler interface {
|
2022-12-04 12:47:51 +00:00
|
|
|
Send(context.Context, *Command, chan<- []byte) error
|
|
|
|
ResponseAnnounce(context.Context, <-chan []byte) (*Response, error)
|
|
|
|
ResponseData(context.Context, <-chan []byte, *Response) error
|
2022-12-01 20:36:10 +00:00
|
|
|
HandleResponse(context.Context, *Response) error
|
2022-11-29 13:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2022-12-01 20:36:10 +00:00
|
|
|
errCommandExpected = errors.New("command must not be nil")
|
2022-12-04 18:17:36 +00:00
|
|
|
ErrCommandAnnounceExpected = errors.New("command must have an announcement")
|
|
|
|
ErrCommandDataExpected = errors.New("command must have data")
|
|
|
|
ErrResponseExpected = errors.New("response must not be nil")
|
2022-11-29 13:05:10 +00:00
|
|
|
|
|
|
|
ErrResponseAnnounceTimeoutExpired = errors.New("response announce timeout expired")
|
|
|
|
ErrResponseDataTimeoutExpired = errors.New("response data timeout expired")
|
|
|
|
)
|
|
|
|
|
|
|
|
type protocolState int8
|
|
|
|
|
|
|
|
const (
|
|
|
|
cmdAnnounce protocolState = iota
|
|
|
|
cmdData
|
|
|
|
handleCommand
|
|
|
|
respond
|
|
|
|
respAnnounce
|
|
|
|
respData
|
|
|
|
handleResponse
|
|
|
|
)
|
|
|
|
|
|
|
|
var protocolStateNames = map[protocolState]string{
|
|
|
|
cmdAnnounce: "CMD ANNOUNCE",
|
|
|
|
cmdData: "CMD DATA",
|
|
|
|
handleCommand: "HANDLE CMD",
|
|
|
|
respond: "RESPOND",
|
|
|
|
respAnnounce: "RESP ANNOUNCE",
|
|
|
|
respData: "RESP DATA",
|
|
|
|
handleResponse: "HANDLE RESP",
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p protocolState) String() string {
|
|
|
|
if name, ok := protocolStateNames[p]; ok {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("unknown %d", p)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServerProtocol struct {
|
|
|
|
handler ServerHandler
|
2022-12-04 12:47:51 +00:00
|
|
|
in <-chan []byte
|
|
|
|
out chan<- []byte
|
2022-11-29 13:05:10 +00:00
|
|
|
logger *logrus.Logger
|
|
|
|
state protocolState
|
|
|
|
}
|
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
func (p *ServerProtocol) Handle(ctx context.Context) error {
|
2022-11-29 13:05:10 +00:00
|
|
|
var (
|
|
|
|
command *Command
|
|
|
|
response *Response
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
2022-12-01 20:36:10 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
close(p.out)
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
return nil
|
|
|
|
default:
|
2022-12-04 18:17:36 +00:00
|
|
|
p.logger.WithField("state", p.state).Debug("handling protocol state")
|
2022-12-01 20:36:10 +00:00
|
|
|
|
|
|
|
switch p.state {
|
|
|
|
case cmdAnnounce:
|
|
|
|
command = p.commandAnnounce(ctx)
|
|
|
|
case cmdData:
|
|
|
|
err = p.commandData(ctx, command)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case handleCommand:
|
|
|
|
response, err = p.handleCommand(ctx, command)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case respond:
|
|
|
|
err = p.respond(ctx, response)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown protocol state %s", p.state)
|
2022-11-29 13:05:10 +00:00
|
|
|
}
|
2022-12-01 20:36:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
func (p *ServerProtocol) commandAnnounce(ctx context.Context) *Command {
|
|
|
|
command, err := p.handler.CommandAnnounce(ctx, p.in)
|
|
|
|
if err != nil {
|
|
|
|
p.logger.WithError(err).Error("could not handle command announce")
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
p.state = cmdData
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
return command
|
|
|
|
}
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
func (p *ServerProtocol) commandData(ctx context.Context, command *Command) error {
|
|
|
|
if command == nil || command.Announce == nil {
|
2022-12-04 18:17:36 +00:00
|
|
|
return ErrCommandAnnounceExpected
|
2022-12-01 20:36:10 +00:00
|
|
|
}
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
err := p.handler.CommandData(ctx, p.in, command)
|
|
|
|
if err != nil {
|
|
|
|
p.logger.WithError(err).Error("could not handle command data")
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
p.state = cmdAnnounce
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
p.state = handleCommand
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
func (p *ServerProtocol) handleCommand(ctx context.Context, command *Command) (*Response, error) {
|
|
|
|
if command == nil || command.Announce == nil || command.Command == nil {
|
2022-12-04 18:17:36 +00:00
|
|
|
return nil, ErrCommandDataExpected
|
2022-12-01 20:36:10 +00:00
|
|
|
}
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
response, err := p.handler.HandleCommand(ctx, command)
|
|
|
|
if err != nil {
|
|
|
|
p.logger.WithError(err).Error("could not handle command")
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
p.state = cmdAnnounce
|
|
|
|
|
|
|
|
return nil, nil
|
2022-11-29 13:05:10 +00:00
|
|
|
}
|
2022-12-01 20:36:10 +00:00
|
|
|
|
|
|
|
p.state = respond
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ServerProtocol) respond(ctx context.Context, response *Response) error {
|
|
|
|
if response == nil {
|
2022-12-04 18:17:36 +00:00
|
|
|
return ErrResponseExpected
|
2022-12-01 20:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := p.handler.Respond(ctx, response, p.out)
|
|
|
|
if err != nil {
|
|
|
|
p.logger.WithError(err).Error("could not respond")
|
|
|
|
|
|
|
|
p.state = cmdAnnounce
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p.state = cmdAnnounce
|
|
|
|
|
|
|
|
return nil
|
2022-11-29 13:05:10 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 12:47:51 +00:00
|
|
|
func NewServer(handler ServerHandler, in <-chan []byte, out chan<- []byte, logger *logrus.Logger) *ServerProtocol {
|
2022-11-29 13:05:10 +00:00
|
|
|
return &ServerProtocol{
|
|
|
|
handler: handler,
|
|
|
|
in: in,
|
|
|
|
out: out,
|
|
|
|
logger: logger,
|
|
|
|
state: cmdAnnounce,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ClientProtocol struct {
|
|
|
|
handler ClientHandler
|
2022-12-04 12:47:51 +00:00
|
|
|
commands <-chan *Command
|
|
|
|
in <-chan []byte
|
|
|
|
out chan<- []byte
|
2022-11-29 13:05:10 +00:00
|
|
|
logger *logrus.Logger
|
|
|
|
state protocolState
|
|
|
|
}
|
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
func (p *ClientProtocol) Handle(ctx context.Context) error {
|
2022-11-29 13:05:10 +00:00
|
|
|
var (
|
|
|
|
response *Response
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
2022-12-01 20:36:10 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
default:
|
2022-12-04 18:17:36 +00:00
|
|
|
p.logger.WithField("state", p.state).Debug("handling protocol state")
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
switch p.state {
|
|
|
|
case cmdAnnounce:
|
|
|
|
err = p.cmdAnnounce(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case respAnnounce:
|
|
|
|
response = p.respAnnounce(ctx)
|
|
|
|
case respData:
|
|
|
|
err = p.respData(ctx, response)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case handleResponse:
|
|
|
|
err = p.handleResponse(ctx, response)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown protocol state %s", p.state)
|
2022-11-29 13:05:10 +00:00
|
|
|
}
|
2022-12-01 20:36:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
func (p *ClientProtocol) cmdAnnounce(ctx context.Context) error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
2022-12-04 12:47:51 +00:00
|
|
|
case command, ok := <-p.commands:
|
|
|
|
if !ok {
|
2022-12-01 20:36:10 +00:00
|
|
|
return errCommandExpected
|
|
|
|
}
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
err := p.handler.Send(ctx, command, p.out)
|
|
|
|
if err != nil {
|
|
|
|
p.logger.WithError(err).Error("could not send command announce")
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-12-04 12:47:51 +00:00
|
|
|
|
|
|
|
p.logger.WithFields(map[string]interface{}{
|
|
|
|
"command": command.Announce,
|
|
|
|
"buffer length": len(p.commands),
|
|
|
|
}).Trace("handled command")
|
2022-12-01 20:36:10 +00:00
|
|
|
}
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
p.state = respAnnounce
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
func (p *ClientProtocol) respAnnounce(ctx context.Context) *Response {
|
|
|
|
response, err := p.handler.ResponseAnnounce(ctx, p.in)
|
|
|
|
if err != nil {
|
|
|
|
p.logger.WithError(err).Error("could not handle response announce")
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
p.state = cmdAnnounce
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
p.state = respData
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
return response
|
|
|
|
}
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
func (p *ClientProtocol) respData(ctx context.Context, response *Response) error {
|
|
|
|
if response == nil || response.Announce == nil {
|
2022-12-04 18:17:36 +00:00
|
|
|
return ErrResponseExpected
|
2022-12-01 20:36:10 +00:00
|
|
|
}
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
err := p.handler.ResponseData(ctx, p.in, response)
|
|
|
|
if err != nil {
|
|
|
|
p.logger.WithError(err).Error("could not handle response data")
|
2022-11-29 13:05:10 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
if errors.Is(err, ErrResponseDataTimeoutExpired) {
|
2022-11-29 13:05:10 +00:00
|
|
|
p.state = cmdAnnounce
|
2022-12-01 20:36:10 +00:00
|
|
|
} else {
|
|
|
|
p.state = respAnnounce
|
2022-11-29 13:05:10 +00:00
|
|
|
}
|
2022-12-01 20:36:10 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p.state = handleResponse
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ClientProtocol) handleResponse(ctx context.Context, response *Response) error {
|
|
|
|
if response == nil || response.Announce == nil || response.Response == nil {
|
2022-12-04 18:17:36 +00:00
|
|
|
return ErrResponseExpected
|
2022-12-01 20:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := p.handler.HandleResponse(ctx, response)
|
|
|
|
if err != nil {
|
|
|
|
p.logger.WithError(err).Error("could not handle response")
|
|
|
|
|
|
|
|
p.state = cmdAnnounce
|
|
|
|
|
|
|
|
return nil
|
2022-11-29 13:05:10 +00:00
|
|
|
}
|
2022-12-01 20:36:10 +00:00
|
|
|
|
|
|
|
p.state = cmdAnnounce
|
|
|
|
|
|
|
|
return nil
|
2022-11-29 13:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(
|
|
|
|
handler ClientHandler,
|
2022-12-04 12:47:51 +00:00
|
|
|
commands <-chan *Command,
|
|
|
|
in <-chan []byte,
|
|
|
|
out chan<- []byte,
|
2022-11-29 13:05:10 +00:00
|
|
|
logger *logrus.Logger,
|
|
|
|
) *ClientProtocol {
|
|
|
|
return &ClientProtocol{
|
|
|
|
handler: handler,
|
|
|
|
commands: commands,
|
|
|
|
in: in,
|
|
|
|
out: out,
|
|
|
|
logger: logger,
|
|
|
|
state: cmdAnnounce,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
// Framer handles bytes on the wire by adding or removing framing information.
|
|
|
|
type Framer interface {
|
|
|
|
// ReadFrames reads data frames and publishes unframed data to the channel.
|
2022-12-04 12:47:51 +00:00
|
|
|
ReadFrames(context.Context, io.Reader, chan<- []byte) error
|
2022-12-01 20:36:10 +00:00
|
|
|
// WriteFrames takes data from the channel and writes framed data to the writer.
|
2022-12-04 12:47:51 +00:00
|
|
|
WriteFrames(context.Context, io.Writer, <-chan []byte) error
|
2022-12-01 20:36:10 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 08:57:23 +00:00
|
|
|
const bufferSize = 1024
|
|
|
|
const readInterval = 50 * time.Millisecond
|
|
|
|
|
|
|
|
type COBSFramer struct {
|
2022-12-02 08:09:52 +00:00
|
|
|
logger *logrus.Logger
|
|
|
|
encoder cobs.Encoder
|
2022-11-29 08:57:23 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 18:17:36 +00:00
|
|
|
var COBSConfig = cobs.Config{SpecialByte: COBSDelimiter, Delimiter: true, EndingSave: true}
|
|
|
|
|
2022-12-02 08:09:52 +00:00
|
|
|
func NewCOBSFramer(logger *logrus.Logger) (*COBSFramer, error) {
|
2022-12-04 18:17:36 +00:00
|
|
|
encoder, err := cobs.NewEncoder(COBSConfig)
|
2022-12-02 08:09:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not setup encoder: %w", err)
|
2022-11-29 08:57:23 +00:00
|
|
|
}
|
2022-12-02 08:09:52 +00:00
|
|
|
|
|
|
|
return &COBSFramer{
|
|
|
|
encoder: encoder,
|
|
|
|
logger: logger,
|
|
|
|
}, nil
|
2022-11-29 08:57:23 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 12:47:51 +00:00
|
|
|
func (c *COBSFramer) ReadFrames(ctx context.Context, reader io.Reader, frameChan chan<- []byte) error {
|
2022-11-29 08:57:23 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
raw, data, frame []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
buffer := &bytes.Buffer{}
|
|
|
|
|
|
|
|
for {
|
2022-12-01 20:36:10 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
default:
|
2022-12-02 11:54:07 +00:00
|
|
|
raw, err = c.readRaw(ctx, reader)
|
2022-12-01 20:36:10 +00:00
|
|
|
if err != nil {
|
|
|
|
close(frameChan)
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
if len(raw) == 0 {
|
|
|
|
time.Sleep(readInterval)
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
c.logger.Tracef("read %d raw bytes", len(raw))
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
buffer.Write(raw)
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
for {
|
2022-12-04 18:17:36 +00:00
|
|
|
data, err = buffer.ReadBytes(COBSDelimiter)
|
2022-12-01 20:36:10 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
buffer.Write(data)
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
// this is a safety measure, buffer.ReadBytes should only return io.EOF
|
|
|
|
return fmt.Errorf("could not read from buffer: %w", err)
|
2022-11-29 08:57:23 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 08:09:52 +00:00
|
|
|
if err = c.encoder.Verify(data); err != nil {
|
2022-12-01 20:36:10 +00:00
|
|
|
c.logger.WithError(err).Warnf("skipping invalid frame of %d bytes", len(data))
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-12-02 08:09:52 +00:00
|
|
|
frame = c.encoder.Decode(data)
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
c.logger.Tracef("frame decoded to length %d", len(frame))
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
frameChan <- frame
|
|
|
|
}
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
c.logger.Tracef("read buffer is now %d bytes long", buffer.Len())
|
2022-11-29 08:57:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 11:54:07 +00:00
|
|
|
func (c *COBSFramer) readRaw(ctx context.Context, reader io.Reader) ([]byte, error) {
|
|
|
|
result := make(chan []byte)
|
|
|
|
errChan := make(chan error)
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-12-02 11:54:07 +00:00
|
|
|
go func() {
|
|
|
|
buf := make([]byte, bufferSize)
|
2022-11-29 09:29:09 +00:00
|
|
|
|
2022-12-02 11:54:07 +00:00
|
|
|
count, err := reader.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
if !errors.Is(err, io.EOF) {
|
|
|
|
errChan <- fmt.Errorf("could not read data: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-12-02 11:54:07 +00:00
|
|
|
result <- buf[:count]
|
|
|
|
}()
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-12-02 11:54:07 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, nil
|
|
|
|
case raw := <-result:
|
|
|
|
return raw, nil
|
|
|
|
case err := <-errChan:
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-29 08:57:23 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 12:47:51 +00:00
|
|
|
func (c *COBSFramer) WriteFrames(ctx context.Context, writer io.Writer, frameChan <-chan []byte) error {
|
2022-11-29 08:57:23 +00:00
|
|
|
for {
|
2022-12-01 20:36:10 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case frame := <-frameChan:
|
|
|
|
if frame == nil {
|
|
|
|
c.logger.Debug("channel closed")
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-12-02 08:09:52 +00:00
|
|
|
encoded := c.encoder.Encode(frame)
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-12-03 12:33:37 +00:00
|
|
|
err := c.writeRaw(ctx, writer, encoded)
|
2022-12-01 20:36:10 +00:00
|
|
|
if err != nil {
|
2022-12-03 12:33:37 +00:00
|
|
|
return err
|
2022-12-01 20:36:10 +00:00
|
|
|
}
|
2022-12-03 12:33:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *COBSFramer) writeRaw(ctx context.Context, writer io.Writer, raw []byte) error {
|
|
|
|
errChan := make(chan error)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
n, err := io.Copy(writer, bytes.NewReader(raw))
|
|
|
|
if err != nil {
|
|
|
|
errChan <- fmt.Errorf("could not write data: %w", err)
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-12-03 12:33:37 +00:00
|
|
|
return
|
2022-11-29 08:57:23 +00:00
|
|
|
}
|
2022-12-03 12:33:37 +00:00
|
|
|
|
|
|
|
c.logger.WithField("count", n).Tracef("wrote bytes")
|
|
|
|
|
|
|
|
close(errChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case err := <-errChan:
|
|
|
|
return err
|
2022-11-29 08:57:23 +00:00
|
|
|
}
|
|
|
|
}
|