-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathparser.go
43 lines (38 loc) · 1.07 KB
/
parser.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
package ucp
import (
"errors"
"strings"
)
var errInvalidPacket = errors.New("invalid packet")
// parseSessionResp parse the initial login
func parseSessionResp(resp string) error {
removedStxEtx := strings.TrimFunc(resp, func(c rune) bool {
return c == stx || c == etx
})
splitFields := strings.Split(removedStxEtx, delimiter)
if len(splitFields) < openSesRespMinLen {
return errInvalidPacket
}
opType := splitFields[optypeIndex]
ack := splitFields[ackIndex]
if opType != opSessionManagement {
return errInvalidPacket
}
if ack != positiveAck {
errMsg := splitFields[len(splitFields)-errMsgOffset]
errCode := splitFields[len(splitFields)-errCodeOffset]
return &UcpError{errCode, errMsg}
}
return nil
}
func parseResp(resp string) (string, []string, error) {
removedStxEtx := strings.TrimFunc(resp, func(c rune) bool {
return c == stx || c == etx
})
splitFields := strings.Split(removedStxEtx, delimiter)
if len(splitFields) < respMinLen {
return "", []string{}, errInvalidPacket
}
opType := splitFields[optypeIndex]
return opType, splitFields, nil
}