mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
move framing creation into shared place
This commit is contained in:
@@ -1,7 +1,26 @@
|
||||
package framing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Framer interface {
|
||||
Decode([]byte) [][]byte
|
||||
Encode([]byte) []byte
|
||||
Clear()
|
||||
}
|
||||
|
||||
func GetFramer(framingType string) (Framer, error) {
|
||||
switch framingType {
|
||||
case "CR":
|
||||
return NewByteSeparatorFramer([]byte{'\r'}), nil
|
||||
case "LF":
|
||||
return NewByteSeparatorFramer([]byte{'\n'}), nil
|
||||
case "CRLF":
|
||||
return NewByteSeparatorFramer([]byte{'\r', '\n'}), nil
|
||||
case "SLIP":
|
||||
return NewSlipFramer(), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown framing method: %s", framingType)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,19 +49,10 @@ func init() {
|
||||
return nil, fmt.Errorf("misc.serial.client framing method must be a string")
|
||||
}
|
||||
|
||||
var framer framing.Framer
|
||||
framer, err := framing.GetFramer(framingMethodString)
|
||||
|
||||
switch framingMethodString {
|
||||
case "CR":
|
||||
framer = framing.NewByteSeparatorFramer([]byte{'\r'})
|
||||
case "LF":
|
||||
framer = framing.NewByteSeparatorFramer([]byte{'\n'})
|
||||
case "CRLF":
|
||||
framer = framing.NewByteSeparatorFramer([]byte{'\r', '\n'})
|
||||
case "SLIP":
|
||||
framer = framing.NewSlipFramer()
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown framing method: %s", framingMethodString)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
buadRate, ok := params["baudRate"]
|
||||
|
||||
@@ -61,19 +61,10 @@ func init() {
|
||||
return nil, fmt.Errorf("net.tcp.client framing method must be a string")
|
||||
}
|
||||
|
||||
var framer framing.Framer
|
||||
framer, err := framing.GetFramer(framingMethodString)
|
||||
|
||||
switch framingMethodString {
|
||||
case "CR":
|
||||
framer = framing.NewByteSeparatorFramer([]byte{'\r'})
|
||||
case "LF":
|
||||
framer = framing.NewByteSeparatorFramer([]byte{'\n'})
|
||||
case "CRLF":
|
||||
framer = framing.NewByteSeparatorFramer([]byte{'\r', '\n'})
|
||||
case "SLIP":
|
||||
framer = framing.NewSlipFramer()
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown framing method: %s", framingMethodString)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &TCPClient{framer: framer, Addr: addr, config: config}, nil
|
||||
|
||||
@@ -16,7 +16,7 @@ import (
|
||||
type TCPServer struct {
|
||||
config ModuleConfig
|
||||
Addr *net.TCPAddr
|
||||
FramingMethod string
|
||||
Framer framing.Framer
|
||||
router *Router
|
||||
quit chan interface{}
|
||||
wg sync.WaitGroup
|
||||
@@ -51,6 +51,12 @@ func init() {
|
||||
return nil, fmt.Errorf("net.tcp.server framing method must be a string")
|
||||
}
|
||||
|
||||
framer, err := framing.GetFramer(framingMethodString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ipString := "0.0.0.0"
|
||||
|
||||
ip, ok := params["ip"]
|
||||
@@ -69,7 +75,7 @@ func init() {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &TCPServer{FramingMethod: framingMethodString, Addr: addr, config: config, quit: make(chan interface{})}, nil
|
||||
return &TCPServer{Framer: framer, Addr: addr, config: config, quit: make(chan interface{})}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -92,18 +98,6 @@ func (ts *TCPServer) handleClient(client *net.TCPConn) {
|
||||
ts.connectionsMu.Unlock()
|
||||
slog.Debug("net.tcp.server connection accepted", "id", ts.config.Id, "remoteAddr", client.RemoteAddr().String())
|
||||
defer client.Close()
|
||||
var framer framing.Framer
|
||||
|
||||
switch ts.FramingMethod {
|
||||
case "LF":
|
||||
framer = framing.NewByteSeparatorFramer([]byte{'\n'})
|
||||
case "CR":
|
||||
framer = framing.NewByteSeparatorFramer([]byte{'\r'})
|
||||
case "CRLF":
|
||||
framer = framing.NewByteSeparatorFramer([]byte{'\r', '\n'})
|
||||
case "SLIP":
|
||||
framer = framing.NewSlipFramer()
|
||||
}
|
||||
|
||||
buffer := make([]byte, 1024)
|
||||
ClientRead:
|
||||
@@ -147,9 +141,9 @@ ClientRead:
|
||||
}
|
||||
return
|
||||
}
|
||||
if framer != nil {
|
||||
if ts.Framer != nil {
|
||||
if byteCount > 0 {
|
||||
messages := framer.Decode(buffer[0:byteCount])
|
||||
messages := ts.Framer.Decode(buffer[0:byteCount])
|
||||
for _, message := range messages {
|
||||
if ts.router != nil {
|
||||
ts.router.HandleInput(ts.config.Id, message)
|
||||
|
||||
Reference in New Issue
Block a user