-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.go
More file actions
99 lines (92 loc) · 2.56 KB
/
decoder.go
File metadata and controls
99 lines (92 loc) · 2.56 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package socket
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"net"
)
const (
FrameLengthFieldLength1 = 1
FrameLengthFieldLength2 = 2
FrameLengthFieldLength4 = 4
FrameLengthFieldLength8 = 8
)
type (
SocketFrameDecoder func(con net.Conn, buffer *bytes.Buffer) (err error)
)
func NewLengthBasedFrameDecoder(order binary.ByteOrder,
lengthFieldOffset int,
lengthFieldLength uint8,
lengthAdjustment int,
initialBytesToStrip int,
) SocketFrameDecoder {
switch lengthFieldLength {
case FrameLengthFieldLength1, FrameLengthFieldLength2, FrameLengthFieldLength4, FrameLengthFieldLength8:
default:
panic(fmt.Sprintf("unsupported lengthFieldLength: %d, expected: 1,2,4,8", lengthFieldLength))
}
read := func(reader io.Reader, num int, buffer []byte) error {
if n, err := reader.Read(buffer); err != nil {
return fmt.Errorf("socket read length error: %w", err)
} else if n != int(num) {
return fmt.Errorf("socket read length not match, expect: %d, was: %d", num, n)
}
return nil
}
return func(conn net.Conn, buffer *bytes.Buffer) (err error) {
// offset
offsetBuf := make([]byte, lengthFieldOffset)
if lengthFieldOffset > 0 {
if err := read(conn, int(lengthFieldOffset), offsetBuf); err != nil {
return err
}
buffer.Write(offsetBuf)
}
// length
lengthBuf := make([]byte, lengthFieldLength)
if err := read(conn, int(lengthFieldLength), lengthBuf); err != nil {
return err
}
buffer.Write(lengthBuf)
// init bytes strip
if initialBytesToStrip > 0 {
for i := 0; i < initialBytesToStrip; i++ {
_, _ = buffer.ReadByte()
}
}
actualLength := ReadFrameLength(order, lengthBuf, lengthFieldLength)
if actualLength == 0 {
return nil
}
if actualLength < 0 {
return fmt.Errorf("socket read unexpected length, expected: %d, was: %d", lengthFieldLength, actualLength)
}
// frame
actualFrameLength := actualLength + int64(lengthAdjustment)
if actualFrameLength == 0 {
return nil
} else {
if n, cerr := io.CopyN(buffer, conn, actualFrameLength); cerr != nil {
return cerr
} else if n != actualFrameLength {
return fmt.Errorf("socket read length not match, expect: %d, was: %d", actualFrameLength, n)
}
return nil
}
}
}
func ReadFrameLength(order binary.ByteOrder, buffer []byte, length uint8) int64 {
switch length {
case FrameLengthFieldLength1:
return int64(buffer[0])
case FrameLengthFieldLength2:
return int64(order.Uint16(buffer))
case FrameLengthFieldLength4:
return int64(order.Uint32(buffer))
case FrameLengthFieldLength8:
return int64(order.Uint64(buffer))
default:
return -1
}
}