Per-packet source tracking in transport, via/from in info output

This commit is contained in:
Ian Gulliver
2026-04-07 22:30:26 +09:00
parent f96ed20aa0
commit 3d20bf4c33
4 changed files with 41 additions and 37 deletions

View File

@@ -2,11 +2,11 @@ package client
import (
"fmt"
"io"
"slices"
"strings"
"time"
"github.com/theater/picomap/lib/msgpack"
"go.bug.st/serial"
"go.bug.st/serial/enumerator"
)
@@ -37,7 +37,9 @@ func ListSerial() ([]string, error) {
}
type serialTransport struct {
port serial.Port
port serial.Port
portName string
dec *msgpack.Decoder
}
func NewSerial(portName string, timeout time.Duration) (*Client, error) {
@@ -45,7 +47,8 @@ func NewSerial(portName string, timeout time.Duration) (*Client, error) {
if err != nil {
return nil, fmt.Errorf("opening %s: %w", portName, err)
}
return &Client{transport: &serialTransport{port: port}, timeout: timeout}, nil
t := &serialTransport{port: port, portName: portName, dec: msgpack.NewDecoder(port)}
return &Client{transport: t, timeout: timeout}, nil
}
func (t *serialTransport) Send(data []byte) error {
@@ -57,8 +60,12 @@ func (t *serialTransport) SetReadTimeout(timeout time.Duration) {
t.port.SetReadTimeout(timeout)
}
func (t *serialTransport) Reader() io.Reader {
return t.port
func (t *serialTransport) Recv() ([]byte, string, error) {
var raw msgpack.RawMessage
if err := t.dec.Decode(&raw); err != nil {
return nil, "", err
}
return []byte(raw), t.portName, nil
}
func (t *serialTransport) Broadcast() bool { return false }