-
Notifications
You must be signed in to change notification settings - Fork 128
/
backflow_frame.go
70 lines (57 loc) 路 1.53 KB
/
backflow_frame.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package frame
import (
"github.com/yomorun/y3"
)
// BackflowFrame is a Y3 encoded bytes
// It's used to receive stream function processed result
type BackflowFrame struct {
Tag byte
Carriage []byte
}
// NewBackflowFrame creates a new BackflowFrame with a given tag and carriage
func NewBackflowFrame(tag byte, carriage []byte) *BackflowFrame {
return &BackflowFrame{
Tag: tag,
Carriage: carriage,
}
}
// Type gets the type of Frame.
func (f *BackflowFrame) Type() Type {
return TagOfBackflowFrame
}
// SetCarriage sets the user's raw data
func (f *BackflowFrame) SetCarriage(buf []byte) *BackflowFrame {
f.Carriage = buf
return f
}
// Encode to Y3 encoded bytes
func (f *BackflowFrame) Encode() []byte {
carriage := y3.NewPrimitivePacketEncoder(f.Tag)
carriage.SetBytesValue(f.Carriage)
node := y3.NewNodePacketEncoder(byte(TagOfBackflowFrame))
node.AddPrimitivePacket(carriage)
return node.Encode()
}
// GetDataTag return the Tag of user's data
func (f *BackflowFrame) GetDataTag() byte {
return f.Tag
}
// GetCarriage return data
func (f *BackflowFrame) GetCarriage() []byte {
return f.Carriage
}
// DecodeToBackflowFrame decodes Y3 encoded bytes to BackflowFrame
func DecodeToBackflowFrame(buf []byte) (*BackflowFrame, error) {
nodeBlock := y3.NodePacket{}
_, err := y3.DecodeToNodePacket(buf, &nodeBlock)
if err != nil {
return nil, err
}
payload := &BackflowFrame{}
for _, v := range nodeBlock.PrimitivePackets {
payload.Tag = v.SeqID()
payload.Carriage = v.GetValBuf()
break
}
return payload, nil
}