|
|
@ -20,6 +20,7 @@ package protocol
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io"
|
|
|
@ -54,24 +55,26 @@ func (r *Response) String() string {
|
|
|
|
// ServerHandler is responsible for parsing incoming frames and calling commands
|
|
|
|
// ServerHandler is responsible for parsing incoming frames and calling commands
|
|
|
|
type ServerHandler interface {
|
|
|
|
type ServerHandler interface {
|
|
|
|
// CommandAnnounce handles the initial announcement of a command.
|
|
|
|
// CommandAnnounce handles the initial announcement of a command.
|
|
|
|
CommandAnnounce(chan []byte) (*Command, error)
|
|
|
|
CommandAnnounce(context.Context, chan []byte) (*Command, error)
|
|
|
|
// CommandData handles the command data.
|
|
|
|
// CommandData handles the command data.
|
|
|
|
CommandData(chan []byte, *Command) error
|
|
|
|
CommandData(context.Context, chan []byte, *Command) error
|
|
|
|
// HandleCommand executes the command, generating a response.
|
|
|
|
// HandleCommand executes the command, generating a response.
|
|
|
|
HandleCommand(*Command) (*Response, error)
|
|
|
|
HandleCommand(context.Context, *Command) (*Response, error)
|
|
|
|
// Respond generates the response for a command.
|
|
|
|
// Respond generates the response for a command.
|
|
|
|
Respond(*Response, chan []byte) error
|
|
|
|
Respond(context.Context, *Response, chan []byte) error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type ClientHandler interface {
|
|
|
|
type ClientHandler interface {
|
|
|
|
Send(*Command, chan []byte) error
|
|
|
|
Send(context.Context, *Command, chan []byte) error
|
|
|
|
ResponseAnnounce(chan []byte) (*Response, error)
|
|
|
|
ResponseAnnounce(context.Context, chan []byte) (*Response, error)
|
|
|
|
ResponseData(chan []byte, *Response) error
|
|
|
|
ResponseData(context.Context, chan []byte, *Response) error
|
|
|
|
HandleResponse(*Response) error
|
|
|
|
HandleResponse(context.Context, *Response) error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
var (
|
|
|
|
errCommandExpected = errors.New("command must not be nil")
|
|
|
|
errCommandExpected = errors.New("command must not be nil")
|
|
|
|
|
|
|
|
errCommandAnnounceExpected = errors.New("command must have an announcement")
|
|
|
|
|
|
|
|
errCommandDataExpected = errors.New("command must have data")
|
|
|
|
errResponseExpected = errors.New("response must not be nil")
|
|
|
|
errResponseExpected = errors.New("response must not be nil")
|
|
|
|
|
|
|
|
|
|
|
|
ErrResponseAnnounceTimeoutExpired = errors.New("response announce timeout expired")
|
|
|
|
ErrResponseAnnounceTimeoutExpired = errors.New("response announce timeout expired")
|
|
|
@ -115,7 +118,7 @@ type ServerProtocol struct {
|
|
|
|
state protocolState
|
|
|
|
state protocolState
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (p *ServerProtocol) Handle() error {
|
|
|
|
func (p *ServerProtocol) Handle(ctx context.Context) error {
|
|
|
|
var (
|
|
|
|
var (
|
|
|
|
command *Command
|
|
|
|
command *Command
|
|
|
|
response *Response
|
|
|
|
response *Response
|
|
|
@ -123,67 +126,107 @@ func (p *ServerProtocol) Handle() error {
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
|
|
|
close(p.out)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
p.logger.Debugf("handling protocol state %s", p.state)
|
|
|
|
p.logger.Debugf("handling protocol state %s", p.state)
|
|
|
|
|
|
|
|
|
|
|
|
switch p.state {
|
|
|
|
switch p.state {
|
|
|
|
case cmdAnnounce:
|
|
|
|
case cmdAnnounce:
|
|
|
|
command, err = p.handler.CommandAnnounce(p.in)
|
|
|
|
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)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (p *ServerProtocol) commandAnnounce(ctx context.Context) *Command {
|
|
|
|
|
|
|
|
command, err := p.handler.CommandAnnounce(ctx, p.in)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
p.logger.WithError(err).Error("could not handle command announce")
|
|
|
|
p.logger.WithError(err).Error("could not handle command announce")
|
|
|
|
|
|
|
|
|
|
|
|
break
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
p.state = cmdData
|
|
|
|
p.state = cmdData
|
|
|
|
case cmdData:
|
|
|
|
|
|
|
|
if command == nil {
|
|
|
|
return command
|
|
|
|
return errCommandExpected
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (p *ServerProtocol) commandData(ctx context.Context, command *Command) error {
|
|
|
|
|
|
|
|
if command == nil || command.Announce == nil {
|
|
|
|
|
|
|
|
return errCommandAnnounceExpected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = p.handler.CommandData(p.in, command)
|
|
|
|
err := p.handler.CommandData(ctx, p.in, command)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
p.logger.WithError(err).Error("could not handle command data")
|
|
|
|
p.logger.WithError(err).Error("could not handle command data")
|
|
|
|
|
|
|
|
|
|
|
|
p.state = cmdAnnounce
|
|
|
|
p.state = cmdAnnounce
|
|
|
|
|
|
|
|
|
|
|
|
break
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
p.state = handleCommand
|
|
|
|
p.state = handleCommand
|
|
|
|
case handleCommand:
|
|
|
|
|
|
|
|
if command == nil {
|
|
|
|
return nil
|
|
|
|
return errCommandExpected
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (p *ServerProtocol) handleCommand(ctx context.Context, command *Command) (*Response, error) {
|
|
|
|
|
|
|
|
if command == nil || command.Announce == nil || command.Command == nil {
|
|
|
|
|
|
|
|
return nil, errCommandDataExpected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
response, err = p.handler.HandleCommand(command)
|
|
|
|
response, err := p.handler.HandleCommand(ctx, command)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
p.logger.WithError(err).Error("could not handle command")
|
|
|
|
p.logger.WithError(err).Error("could not handle command")
|
|
|
|
|
|
|
|
|
|
|
|
p.state = cmdAnnounce
|
|
|
|
p.state = cmdAnnounce
|
|
|
|
|
|
|
|
|
|
|
|
break
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
p.state = respond
|
|
|
|
p.state = respond
|
|
|
|
case respond:
|
|
|
|
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (p *ServerProtocol) respond(ctx context.Context, response *Response) error {
|
|
|
|
if response == nil {
|
|
|
|
if response == nil {
|
|
|
|
return errResponseExpected
|
|
|
|
return errResponseExpected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = p.handler.Respond(response, p.out)
|
|
|
|
err := p.handler.Respond(ctx, response, p.out)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
p.logger.WithError(err).Error("could not respond")
|
|
|
|
p.logger.WithError(err).Error("could not respond")
|
|
|
|
|
|
|
|
|
|
|
|
p.state = cmdAnnounce
|
|
|
|
p.state = cmdAnnounce
|
|
|
|
|
|
|
|
|
|
|
|
break
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
p.state = cmdAnnounce
|
|
|
|
p.state = cmdAnnounce
|
|
|
|
default:
|
|
|
|
|
|
|
|
return fmt.Errorf("unknown protocol state %s", p.state)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewServer(handler ServerHandler, in, out chan []byte, logger *logrus.Logger) *ServerProtocol {
|
|
|
|
func NewServer(handler ServerHandler, in, out chan []byte, logger *logrus.Logger) *ServerProtocol {
|
|
|
@ -196,14 +239,6 @@ func NewServer(handler ServerHandler, in, out chan []byte, logger *logrus.Logger
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
ReadFrames(io.Reader, chan []byte) error
|
|
|
|
|
|
|
|
// WriteFrames takes data from the channel and writes framed data to the writer.
|
|
|
|
|
|
|
|
WriteFrames(io.Writer, chan []byte) error
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type ClientProtocol struct {
|
|
|
|
type ClientProtocol struct {
|
|
|
|
handler ClientHandler
|
|
|
|
handler ClientHandler
|
|
|
|
commands chan *Command
|
|
|
|
commands chan *Command
|
|
|
@ -212,48 +247,87 @@ type ClientProtocol struct {
|
|
|
|
state protocolState
|
|
|
|
state protocolState
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (p *ClientProtocol) Handle() error {
|
|
|
|
func (p *ClientProtocol) Handle(ctx context.Context) error {
|
|
|
|
var (
|
|
|
|
var (
|
|
|
|
command *Command
|
|
|
|
|
|
|
|
response *Response
|
|
|
|
response *Response
|
|
|
|
err error
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
p.logger.Debugf("handling protocol state %s", p.state)
|
|
|
|
p.logger.Debugf("handling protocol state %s", p.state)
|
|
|
|
|
|
|
|
|
|
|
|
switch p.state {
|
|
|
|
switch p.state {
|
|
|
|
case cmdAnnounce:
|
|
|
|
case cmdAnnounce:
|
|
|
|
command = <-p.commands
|
|
|
|
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)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (p *ClientProtocol) cmdAnnounce(ctx context.Context) error {
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case command := <-p.commands:
|
|
|
|
if command == nil {
|
|
|
|
if command == nil {
|
|
|
|
return errCommandExpected
|
|
|
|
return errCommandExpected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = p.handler.Send(command, p.out)
|
|
|
|
err := p.handler.Send(ctx, command, p.out)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
p.logger.WithError(err).Error("could not send command announce")
|
|
|
|
p.logger.WithError(err).Error("could not send command announce")
|
|
|
|
|
|
|
|
|
|
|
|
break
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
p.state = respAnnounce
|
|
|
|
p.state = respAnnounce
|
|
|
|
case respAnnounce:
|
|
|
|
|
|
|
|
response, err = p.handler.ResponseAnnounce(p.in)
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (p *ClientProtocol) respAnnounce(ctx context.Context) *Response {
|
|
|
|
|
|
|
|
response, err := p.handler.ResponseAnnounce(ctx, p.in)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
p.logger.WithError(err).Error("could not handle response announce")
|
|
|
|
p.logger.WithError(err).Error("could not handle response announce")
|
|
|
|
|
|
|
|
|
|
|
|
p.state = cmdAnnounce
|
|
|
|
p.state = cmdAnnounce
|
|
|
|
|
|
|
|
|
|
|
|
break
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
p.state = respData
|
|
|
|
p.state = respData
|
|
|
|
case respData:
|
|
|
|
|
|
|
|
if response == nil {
|
|
|
|
return response
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (p *ClientProtocol) respData(ctx context.Context, response *Response) error {
|
|
|
|
|
|
|
|
if response == nil || response.Announce == nil {
|
|
|
|
return errResponseExpected
|
|
|
|
return errResponseExpected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = p.handler.ResponseData(p.in, response)
|
|
|
|
err := p.handler.ResponseData(ctx, p.in, response)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
p.logger.WithError(err).Error("could not handle response data")
|
|
|
|
p.logger.WithError(err).Error("could not handle response data")
|
|
|
|
|
|
|
|
|
|
|
@ -263,29 +337,31 @@ func (p *ClientProtocol) Handle() error {
|
|
|
|
p.state = respAnnounce
|
|
|
|
p.state = respAnnounce
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
break
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
p.state = handleResponse
|
|
|
|
p.state = handleResponse
|
|
|
|
case handleResponse:
|
|
|
|
|
|
|
|
if response == nil {
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (p *ClientProtocol) handleResponse(ctx context.Context, response *Response) error {
|
|
|
|
|
|
|
|
if response == nil || response.Announce == nil || response.Response == nil {
|
|
|
|
return errResponseExpected
|
|
|
|
return errResponseExpected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = p.handler.HandleResponse(response)
|
|
|
|
err := p.handler.HandleResponse(ctx, response)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
p.logger.WithError(err).Error("could not handle response")
|
|
|
|
p.logger.WithError(err).Error("could not handle response")
|
|
|
|
|
|
|
|
|
|
|
|
p.state = cmdAnnounce
|
|
|
|
p.state = cmdAnnounce
|
|
|
|
|
|
|
|
|
|
|
|
break
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
p.state = cmdAnnounce
|
|
|
|
p.state = cmdAnnounce
|
|
|
|
default:
|
|
|
|
|
|
|
|
return fmt.Errorf("unknown protocol state %s", p.state)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewClient(
|
|
|
|
func NewClient(
|
|
|
@ -304,6 +380,14 @@ func NewClient(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
ReadFrames(context.Context, io.Reader, chan []byte) error
|
|
|
|
|
|
|
|
// WriteFrames takes data from the channel and writes framed data to the writer.
|
|
|
|
|
|
|
|
WriteFrames(context.Context, io.Writer, chan []byte) error
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const bufferSize = 1024
|
|
|
|
const bufferSize = 1024
|
|
|
|
const readInterval = 50 * time.Millisecond
|
|
|
|
const readInterval = 50 * time.Millisecond
|
|
|
|
|
|
|
|
|
|
|
@ -319,7 +403,7 @@ func NewCOBSFramer(logger *logrus.Logger) *COBSFramer {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *COBSFramer) ReadFrames(reader io.Reader, frameChan chan []byte) error {
|
|
|
|
func (c *COBSFramer) ReadFrames(ctx context.Context, reader io.Reader, frameChan chan []byte) error {
|
|
|
|
var (
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
err error
|
|
|
|
raw, data, frame []byte
|
|
|
|
raw, data, frame []byte
|
|
|
@ -328,6 +412,10 @@ func (c *COBSFramer) ReadFrames(reader io.Reader, frameChan chan []byte) error {
|
|
|
|
buffer := &bytes.Buffer{}
|
|
|
|
buffer := &bytes.Buffer{}
|
|
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
raw, err = c.readRaw(reader)
|
|
|
|
raw, err = c.readRaw(reader)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
close(frameChan)
|
|
|
|
close(frameChan)
|
|
|
@ -354,6 +442,7 @@ func (c *COBSFramer) ReadFrames(reader io.Reader, frameChan chan []byte) error {
|
|
|
|
break
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// this is a safety measure, buffer.ReadBytes should only return io.EOF
|
|
|
|
return fmt.Errorf("could not read from buffer: %w", err)
|
|
|
|
return fmt.Errorf("could not read from buffer: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -372,6 +461,7 @@ func (c *COBSFramer) ReadFrames(reader io.Reader, frameChan chan []byte) error {
|
|
|
|
|
|
|
|
|
|
|
|
c.logger.Tracef("read buffer is now %d bytes long", buffer.Len())
|
|
|
|
c.logger.Tracef("read buffer is now %d bytes long", buffer.Len())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *COBSFramer) readRaw(reader io.Reader) ([]byte, error) {
|
|
|
|
func (c *COBSFramer) readRaw(reader io.Reader) ([]byte, error) {
|
|
|
@ -380,7 +470,7 @@ func (c *COBSFramer) readRaw(reader io.Reader) ([]byte, error) {
|
|
|
|
count, err := reader.Read(buf)
|
|
|
|
count, err := reader.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
return []byte{}, nil
|
|
|
|
return buf[:count], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("could not read data: %w", err)
|
|
|
|
return nil, fmt.Errorf("could not read data: %w", err)
|
|
|
@ -391,10 +481,12 @@ func (c *COBSFramer) readRaw(reader io.Reader) ([]byte, error) {
|
|
|
|
return raw, nil
|
|
|
|
return raw, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *COBSFramer) WriteFrames(writer io.Writer, frameChan chan []byte) error {
|
|
|
|
func (c *COBSFramer) WriteFrames(ctx context.Context, writer io.Writer, frameChan chan []byte) error {
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
frame := <-frameChan
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case frame := <-frameChan:
|
|
|
|
if frame == nil {
|
|
|
|
if frame == nil {
|
|
|
|
c.logger.Debug("channel closed")
|
|
|
|
c.logger.Debug("channel closed")
|
|
|
|
|
|
|
|
|
|
|
@ -410,4 +502,5 @@ func (c *COBSFramer) WriteFrames(writer io.Writer, frameChan chan []byte) error
|
|
|
|
|
|
|
|
|
|
|
|
c.logger.Tracef("wrote %d bytes", n)
|
|
|
|
c.logger.Tracef("wrote %d bytes", n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|