mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 13:25:40 +00:00
34 lines
613 B
Go
34 lines
613 B
Go
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) Frame(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) Clear() {
|
|
bsf.buffer = []byte{}
|
|
}
|