This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdisconnect.go
72 lines (60 loc) · 1.62 KB
/
disconnect.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
71
72
package spoe
import (
"fmt"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
func (c *conn) disconnectFrame(e spoeError) (Frame, error) {
f := Frame{
frameID: 0,
streamID: 0,
ftype: frameTypeAgentDiscon,
flags: frameFlagFin,
data: make([]byte, c.frameSize),
}
off := 0
n, err := encodeKV(f.data[off:], "status-code", int(e))
if err != nil {
return f, errors.Wrap(err, "disconnect")
}
off += n
n, err = encodeKV(f.data[off:], "message", spoeErrorMessages[e])
if err != nil {
return f, errors.Wrap(err, "disconnect")
}
off += n
f.data = f.data[:off]
return f, nil
}
func (c *conn) handleDisconnect(f Frame) error {
data, _, err := decodeKVs(f.data, -1)
log.Debugf("spoe: Disconnect from %s: %+v", c.Conn.RemoteAddr(), data)
if err != nil {
return errors.Wrap(err, "disconnect")
}
code, ok := data["status-code"].(uint)
if !ok {
message, _ := data["message"].(string)
if message == "" {
message = "unknown error "
}
return fmt.Errorf("disconnect error without status-code and message: %s", message)
}
if spoeError(code) == spoeErrorTimeout || spoeError(code) == spoeErrorNone {
return nil
}
if spoeError(code) == spoeErrorFragNotSupported {
// TODO: understand why we have this message
log.Info("spoe: Disconnect with \"fragmentation not supported\"")
return nil
}
message, okMessage := spoeErrorMessages[spoeError(code)]
if okMessage {
return fmt.Errorf("disconnect error: %s", message)
}
message, _ = data["message"].(string)
if message == "" {
message = "unknown error "
}
return fmt.Errorf("disconnect error (without status-code) : %s", message)
}