add the concept of routes input/output

This commit is contained in:
Joel Wetzell
2025-11-19 18:33:04 -06:00
parent 8e0f25abe9
commit 0e903eba2f
11 changed files with 193 additions and 35 deletions

View File

@@ -1,6 +1,7 @@
package framing
type Framer interface {
Frame([]byte) [][]byte
Decode([]byte) [][]byte
Encode([]byte) []byte
Clear()
}

View File

@@ -13,7 +13,7 @@ func NewByteSeparatorFramer(separator []byte) *ByteSeparatorFramer {
return &ByteSeparatorFramer{separator: separator, buffer: []byte{}}
}
func (bsf *ByteSeparatorFramer) Frame(data []byte) [][]byte {
func (bsf *ByteSeparatorFramer) Decode(data []byte) [][]byte {
messages := [][]byte{}
bsf.buffer = append(bsf.buffer, data...)
@@ -28,6 +28,10 @@ func (bsf *ByteSeparatorFramer) Frame(data []byte) [][]byte {
return messages
}
func (bsf *ByteSeparatorFramer) Encode(data []byte) []byte {
return append(data, bsf.separator...)
}
func (bsf *ByteSeparatorFramer) Clear() {
bsf.buffer = []byte{}
}

View File

@@ -8,8 +8,9 @@ func NewSlipFramer() *SlipFramer {
return &SlipFramer{buffer: []byte{}}
}
func (sf *SlipFramer) Frame(data []byte) [][]byte {
func (sf *SlipFramer) Decode(data []byte) [][]byte {
messages := [][]byte{}
END := byte(0xc0)
ESC := byte(0xdb)
ESC_END := byte(0xdc)
@@ -47,6 +48,29 @@ func (sf *SlipFramer) Frame(data []byte) [][]byte {
return messages
}
func (sf *SlipFramer) Encode(data []byte) []byte {
END := byte(0xc0)
ESC := byte(0xdb)
ESC_END := byte(0xdc)
ESC_ESC := byte(0xdd)
var encodedBytes = []byte{END}
for _, byteToEncode := range data {
switch byteToEncode {
case END:
encodedBytes = append(encodedBytes, ESC_END)
case ESC:
encodedBytes = append(encodedBytes, ESC_ESC)
default:
encodedBytes = append(encodedBytes, byteToEncode)
}
}
encodedBytes = append(encodedBytes, END)
return encodedBytes
}
func (sf *SlipFramer) Clear() {
sf.buffer = []byte{}
}