mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-09-10 07:29:25 +00:00
make sure to use a distinct framer for both directions of communication
This commit is contained in:
@@ -56,12 +56,14 @@ func init() {
|
|||||||
return nil, fmt.Errorf("serial.client framing error: %w", err)
|
return nil, fmt.Errorf("serial.client framing error: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
framer := framer.GetFramer(framingMethodString)
|
inFramer := framer.GetFramer(framingMethodString)
|
||||||
|
|
||||||
if framer == nil {
|
if inFramer == nil {
|
||||||
return nil, fmt.Errorf("serial.client unknown framing method: %s", framingMethodString)
|
return nil, fmt.Errorf("serial.client unknown framing method: %s", framingMethodString)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
outFramer := framer.GetFramer(framingMethodString)
|
||||||
|
|
||||||
baudRateInt, err := params.GetInt("baudRate")
|
baudRateInt, err := params.GetInt("baudRate")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("serial.client baudRate error: %w", err)
|
return nil, fmt.Errorf("serial.client baudRate error: %w", err)
|
||||||
@@ -71,7 +73,7 @@ func init() {
|
|||||||
BaudRate: baudRateInt,
|
BaudRate: baudRateInt,
|
||||||
}
|
}
|
||||||
|
|
||||||
return &SerialClient{config: config, Port: portString, Framer: framer, Mode: &mode, logger: CreateLogger(config)}, nil
|
return &SerialClient{config: config, Port: portString, inFramer: inFramer, outFramer: outFramer, Mode: &mode, logger: CreateLogger(config)}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -81,7 +83,8 @@ type SerialClient struct {
|
|||||||
ctx context.Context
|
ctx context.Context
|
||||||
inputHandler common.InputHandler
|
inputHandler common.InputHandler
|
||||||
Port string
|
Port string
|
||||||
Framer framer.Framer
|
inFramer framer.Framer
|
||||||
|
outFramer framer.Framer
|
||||||
Mode *serial.Mode
|
Mode *serial.Mode
|
||||||
port serial.Port
|
port serial.Port
|
||||||
logger *slog.Logger
|
logger *slog.Logger
|
||||||
@@ -142,13 +145,13 @@ func (sc *SerialClient) Start(ctx context.Context, inputHandler common.InputHand
|
|||||||
byteCount, err := sc.port.Read(buffer)
|
byteCount, err := sc.port.Read(buffer)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sc.Framer.Clear()
|
sc.inFramer.Clear()
|
||||||
break READ
|
break READ
|
||||||
}
|
}
|
||||||
|
|
||||||
if sc.Framer != nil {
|
if sc.inFramer != nil {
|
||||||
if byteCount > 0 {
|
if byteCount > 0 {
|
||||||
messages := sc.Framer.Decode(buffer[0:byteCount])
|
messages := sc.inFramer.Decode(buffer[0:byteCount])
|
||||||
for _, message := range messages {
|
for _, message := range messages {
|
||||||
if sc.inputHandler != nil {
|
if sc.inputHandler != nil {
|
||||||
sc.inputHandler(sc.ctx, sc.Id(), message)
|
sc.inputHandler(sc.ctx, sc.Id(), message)
|
||||||
@@ -175,7 +178,7 @@ func (sc *SerialClient) Output(ctx context.Context, payload any) error {
|
|||||||
return errors.New("serial.client can only output bytes")
|
return errors.New("serial.client can only output bytes")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := sc.port.Write(sc.Framer.Encode(payloadBytes))
|
_, err := sc.port.Write(sc.outFramer.Encode(payloadBytes))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,4 +191,12 @@ func (sc *SerialClient) Stop() {
|
|||||||
if sc.port != nil {
|
if sc.port != nil {
|
||||||
sc.port.Close()
|
sc.port.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if sc.inFramer != nil {
|
||||||
|
sc.inFramer.Clear()
|
||||||
|
}
|
||||||
|
|
||||||
|
if sc.outFramer != nil {
|
||||||
|
sc.outFramer.Clear()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,19 +66,22 @@ func init() {
|
|||||||
return nil, fmt.Errorf("net.tcp.client framing error: %w", err)
|
return nil, fmt.Errorf("net.tcp.client framing error: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
framer := framer.GetFramer(framingMethodString)
|
inFramer := framer.GetFramer(framingMethodString)
|
||||||
|
|
||||||
if framer == nil {
|
if inFramer == nil {
|
||||||
return nil, fmt.Errorf("net.tcp.client unknown framing method: %s", framingMethodString)
|
return nil, fmt.Errorf("net.tcp.client unknown framing method: %s", framingMethodString)
|
||||||
}
|
}
|
||||||
return &TCPClient{framer: framer, Addr: addr, config: config, logger: CreateLogger(config)}, nil
|
|
||||||
|
outFramer := framer.GetFramer(framingMethodString)
|
||||||
|
return &TCPClient{inFramer: inFramer, outFramer: outFramer, Addr: addr, config: config, logger: CreateLogger(config)}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
type TCPClient struct {
|
type TCPClient struct {
|
||||||
config config.ModuleConfig
|
config config.ModuleConfig
|
||||||
framer framer.Framer
|
inFramer framer.Framer
|
||||||
|
outFramer framer.Framer
|
||||||
conn *net.TCPConn
|
conn *net.TCPConn
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
inputHandler common.InputHandler
|
inputHandler common.InputHandler
|
||||||
@@ -135,9 +138,9 @@ CONNECT_RETRY:
|
|||||||
break READ
|
break READ
|
||||||
}
|
}
|
||||||
|
|
||||||
if tc.framer != nil {
|
if tc.inFramer != nil {
|
||||||
if byteCount > 0 {
|
if byteCount > 0 {
|
||||||
messages := tc.framer.Decode(buffer[0:byteCount])
|
messages := tc.inFramer.Decode(buffer[0:byteCount])
|
||||||
for _, message := range messages {
|
for _, message := range messages {
|
||||||
if tc.inputHandler != nil {
|
if tc.inputHandler != nil {
|
||||||
tc.inputHandler(tc.ctx, tc.Id(), message)
|
tc.inputHandler(tc.ctx, tc.Id(), message)
|
||||||
@@ -172,7 +175,7 @@ func (tc *TCPClient) Output(ctx context.Context, payload any) error {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("net.tcp.client is only able to output bytes")
|
return errors.New("net.tcp.client is only able to output bytes")
|
||||||
}
|
}
|
||||||
_, err := tc.conn.Write(tc.framer.Encode(payloadBytes))
|
_, err := tc.conn.Write(tc.inFramer.Encode(payloadBytes))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,4 +188,12 @@ func (tc *TCPClient) Stop() {
|
|||||||
if tc.conn != nil {
|
if tc.conn != nil {
|
||||||
tc.conn.Close()
|
tc.conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if tc.inFramer != nil {
|
||||||
|
tc.inFramer.Clear()
|
||||||
|
}
|
||||||
|
|
||||||
|
if tc.outFramer != nil {
|
||||||
|
tc.outFramer.Clear()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ func init() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &TCPServer{InFramer: inFramer, OutFramer: outFramer, framerType: framingMethodString, Addr: addr, config: moduleConfig, logger: CreateLogger(moduleConfig)}, nil
|
return &TCPServer{inFramer: inFramer, outFramer: outFramer, framerType: framingMethodString, Addr: addr, config: moduleConfig, logger: CreateLogger(moduleConfig)}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -94,8 +94,8 @@ type tcpConnection struct {
|
|||||||
type TCPServer struct {
|
type TCPServer struct {
|
||||||
config config.ModuleConfig
|
config config.ModuleConfig
|
||||||
Addr *net.TCPAddr
|
Addr *net.TCPAddr
|
||||||
InFramer framer.Framer
|
inFramer framer.Framer
|
||||||
OutFramer framer.Framer
|
outFramer framer.Framer
|
||||||
framerType string
|
framerType string
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
inputHandler common.InputHandler
|
inputHandler common.InputHandler
|
||||||
@@ -149,9 +149,9 @@ func (ts *TCPServer) handleClient(client *net.TCPConn) {
|
|||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if ts.InFramer != nil {
|
if ts.inFramer != nil {
|
||||||
if byteCount > 0 {
|
if byteCount > 0 {
|
||||||
messages := ts.InFramer.Decode(buffer[0:byteCount])
|
messages := ts.inFramer.Decode(buffer[0:byteCount])
|
||||||
for _, message := range messages {
|
for _, message := range messages {
|
||||||
if ts.inputHandler != nil {
|
if ts.inputHandler != nil {
|
||||||
ts.inputHandler(ts.ctx, ts.Id(), message)
|
ts.inputHandler(ts.ctx, ts.Id(), message)
|
||||||
@@ -219,11 +219,11 @@ func (ts *TCPServer) Output(ctx context.Context, payload any) error {
|
|||||||
defer ts.connectionsMu.Unlock()
|
defer ts.connectionsMu.Unlock()
|
||||||
var errorString strings.Builder
|
var errorString strings.Builder
|
||||||
|
|
||||||
if ts.OutFramer == nil {
|
if ts.outFramer == nil {
|
||||||
return errors.New("no output framer configured")
|
return errors.New("no output framer configured")
|
||||||
}
|
}
|
||||||
|
|
||||||
outputBytes := ts.OutFramer.Encode(payloadBytes)
|
outputBytes := ts.outFramer.Encode(payloadBytes)
|
||||||
|
|
||||||
for _, connection := range ts.connections {
|
for _, connection := range ts.connections {
|
||||||
_, err := connection.conn.Write(outputBytes)
|
_, err := connection.conn.Write(outputBytes)
|
||||||
|
|||||||
Reference in New Issue
Block a user