-
Notifications
You must be signed in to change notification settings - Fork 4
/
conn.go
101 lines (85 loc) · 2.09 KB
/
conn.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
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
100
101
package t1k
import (
"net"
"net/http"
"github.com/chaitin/t1k-go/detection"
"github.com/chaitin/t1k-go/t1k"
"github.com/chaitin/t1k-go/misc"
)
type conn struct {
socket net.Conn
server *Server
failing bool
}
func makeConn(socket net.Conn, server *Server) *conn {
return &conn{
socket: socket,
server: server,
failing: false,
}
}
func (c *conn) onErr(err error) {
if err != nil {
// re-open socket to recover from possible error state
c.socket.Close()
sock, errConnect := c.server.socketFactory()
if errConnect != nil {
c.failing = true
return
}
c.socket = sock
}
}
func (c *conn) Close() {
if c.socket == nil {
return
}
c.socket.Close()
}
func (c *conn) DetectRequestInCtx(dc *detection.DetectionContext) (*detection.Result, error) {
ret, err := DetectRequestInCtx(c.socket, dc)
c.onErr(err)
return ret, err
}
func (c *conn) DetectResponseInCtx(dc *detection.DetectionContext) (*detection.Result, error) {
ret, err := DetectResponseInCtx(c.socket, dc)
c.onErr(err)
return ret, misc.ErrorWrap(err, "")
}
func (c *conn) Detect(dc *detection.DetectionContext) (*detection.Result, *detection.Result, error) {
retReq, retRsp, err := Detect(c.socket, dc)
c.onErr(err)
return retReq, retRsp, misc.ErrorWrap(err, "")
}
func (c *conn) DetectHttpRequest(req *http.Request) (*detection.Result, error) {
ret, err := DetectHttpRequest(c.socket, req)
c.onErr(err)
return ret, err
}
func (c *conn) DetectRequest(req detection.Request) (*detection.Result, error) {
ret, err := DetectRequest(c.socket, req)
c.onErr(err)
return ret, err
}
func (c *conn) Heartbeat() {
err := DoHeartbeat(c.socket)
c.onErr(err)
}
func (c *conn) WriteSection(sec t1k.Section) error {
err := t1k.WriteSection(sec, c.socket)
return misc.ErrorWrap(err, "")
}
func (c *conn) ReadSection() (t1k.Section, error) {
sec, err := t1k.ReadSection(c.socket)
if err != nil {
return nil, misc.ErrorWrap(err, "")
}
return sec, nil
}
func (c *conn) ReadFullSection() (t1k.Section, error) {
sec, err := t1k.ReadFullSection(c.socket)
if err != nil {
return nil, misc.ErrorWrap(err, "")
}
return sec, nil
}