mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 13:25:40 +00:00
change internals to internal
This commit is contained in:
7
internal/framing/framer.go
Normal file
7
internal/framing/framer.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package framing
|
||||
|
||||
type Framer interface {
|
||||
Decode([]byte) [][]byte
|
||||
Encode([]byte) []byte
|
||||
Clear()
|
||||
}
|
||||
37
internal/framing/separator.go
Normal file
37
internal/framing/separator.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package framing
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
)
|
||||
|
||||
type ByteSeparatorFramer struct {
|
||||
buffer []byte
|
||||
separator []byte
|
||||
}
|
||||
|
||||
func NewByteSeparatorFramer(separator []byte) *ByteSeparatorFramer {
|
||||
return &ByteSeparatorFramer{separator: separator, buffer: []byte{}}
|
||||
}
|
||||
|
||||
func (bsf *ByteSeparatorFramer) Decode(data []byte) [][]byte {
|
||||
messages := [][]byte{}
|
||||
|
||||
bsf.buffer = append(bsf.buffer, data...)
|
||||
|
||||
parts := bytes.Split(bsf.buffer, bsf.separator)
|
||||
|
||||
if len(parts) > 0 {
|
||||
bsf.buffer = parts[len(parts)-1]
|
||||
messages = parts[:len(parts)-1]
|
||||
}
|
||||
|
||||
return messages
|
||||
}
|
||||
|
||||
func (bsf *ByteSeparatorFramer) Encode(data []byte) []byte {
|
||||
return append(data, bsf.separator...)
|
||||
}
|
||||
|
||||
func (bsf *ByteSeparatorFramer) Clear() {
|
||||
bsf.buffer = []byte{}
|
||||
}
|
||||
76
internal/framing/slip.go
Normal file
76
internal/framing/slip.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package framing
|
||||
|
||||
type SlipFramer struct {
|
||||
buffer []byte
|
||||
}
|
||||
|
||||
func NewSlipFramer() *SlipFramer {
|
||||
return &SlipFramer{buffer: []byte{}}
|
||||
}
|
||||
|
||||
func (sf *SlipFramer) Decode(data []byte) [][]byte {
|
||||
messages := [][]byte{}
|
||||
|
||||
END := byte(0xc0)
|
||||
ESC := byte(0xdb)
|
||||
ESC_END := byte(0xdc)
|
||||
ESC_ESC := byte(0xdd)
|
||||
|
||||
escapeNext := false
|
||||
for _, packetByte := range data {
|
||||
|
||||
if packetByte == ESC {
|
||||
escapeNext = true
|
||||
continue
|
||||
}
|
||||
|
||||
if escapeNext {
|
||||
if packetByte == ESC_END {
|
||||
sf.buffer = append(sf.buffer, END)
|
||||
} else if packetByte == ESC_ESC {
|
||||
sf.buffer = append(sf.buffer, ESC)
|
||||
}
|
||||
escapeNext = false
|
||||
} else if packetByte == END {
|
||||
if len(sf.buffer) == 0 {
|
||||
// opening END byte, can discard
|
||||
continue
|
||||
} else {
|
||||
message := sf.buffer
|
||||
messages = append(messages, message)
|
||||
}
|
||||
sf.buffer = []byte{}
|
||||
} else {
|
||||
sf.buffer = append(sf.buffer, packetByte)
|
||||
}
|
||||
}
|
||||
|
||||
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{}
|
||||
}
|
||||
Reference in New Issue
Block a user