-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgdecoder.go
62 lines (59 loc) · 1.14 KB
/
msgdecoder.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
package qsvencc_tcp
import (
"encoding/binary"
"errors"
"io"
"log"
)
func readByte(r io.Reader) (byte, error) {
rb := []byte{0}
i, err := r.Read(rb)
if err != nil {
return 0, err
}
if i > 1 {
log.Fatal("unexpected read length: ", i)
}
return rb[0], nil
}
func TcpMsgDecode(r io.Reader) (TcpCmd, []byte, error) {
rb, err := readByte(r)
if err != nil {
return QTWReserved, nil, err
}
cmd := TcpCmd(rb)
switch cmd {
case QTWExec:
fallthrough
case QTWPipe:
fallthrough
case QTWPipeIdentifier:
fallthrough
case QTWStderr:
lenb := []byte{0, 0}
_, err = io.ReadFull(r, lenb)
if err != nil {
return QTWReserved, nil, err
}
leni := binary.BigEndian.Uint16(lenb)
content := make([]byte, leni, leni)
_, err = io.ReadFull(r, content)
if err != nil {
return QTWReserved, nil, err
}
return cmd, content, nil
case QTWRun:
fallthrough
case QTWPipeReady:
return cmd, nil, nil
case QTWCmdReturnCode:
retb := make([]byte, 4, 4)
_, err = io.ReadFull(r, retb)
if err != nil {
return QTWReserved, nil, err
}
return cmd, retb, nil
default:
return QTWReserved, nil, errors.New("not supported cmd")
}
}