mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 05:15:47 +00:00
add concept of framing to net.tcp
This commit is contained in:
6
internals/framing/framer.go
Normal file
6
internals/framing/framer.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package framing
|
||||
|
||||
type Framer interface {
|
||||
Frame([]byte) [][]byte
|
||||
Clear()
|
||||
}
|
||||
33
internals/framing/separator.go
Normal file
33
internals/framing/separator.go
Normal file
@@ -0,0 +1,33 @@
|
||||
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{}
|
||||
}
|
||||
Reference in New Issue
Block a user