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