-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
99 lines (76 loc) · 1.79 KB
/
session.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
package http
import (
"errors"
"html/template"
"net/http"
"github.com/adamluo159/cellnetEx"
"github.com/adamluo159/cellnetEx/peer"
)
type RequestMatcher interface {
Match(method, url string) bool
}
type RespondProc interface {
WriteRespond(*httpSession) error
}
var (
ErrUnknownOperation = errors.New("Unknown http operation")
)
type httpSession struct {
peer.CoreContextSet
*peer.CoreProcBundle
req *http.Request
resp http.ResponseWriter
// 单独保存的保存Peer接口
peerInterface cellnetEx.Peer
t *template.Template
respond bool
err error
}
func (self *httpSession) Match(method, url string) bool {
return self.req.Method == method && self.req.URL.Path == url
}
func (self *httpSession) Request() *http.Request {
return self.req
}
func (self *httpSession) Response() http.ResponseWriter {
return self.resp
}
// 取原始连接
func (self *httpSession) Raw() interface{} {
return nil
}
func (self *httpSession) ID() int64 {
return 0
}
// 取原始连接
func (self *httpSession) Close() {
}
//SetUserData 设置用户数据
func (self *httpSession) SetUserData(data interface{}) {
}
//GetUserData 获取用户数据
func (self *httpSession) GetUserData() interface{} {
return nil
}
// 取会话归属的通讯端
func (self *httpSession) Peer() cellnetEx.Peer {
return self.peerInterface
}
// 发送封包
func (self *httpSession) Send(raw interface{}) {
if proc, ok := raw.(RespondProc); ok {
self.err = proc.WriteRespond(self)
self.respond = true
} else {
self.err = ErrUnknownOperation
}
}
func newHttpSession(acc *httpAcceptor, req *http.Request, response http.ResponseWriter) *httpSession {
return &httpSession{
req: req,
resp: response,
peerInterface: acc,
t: acc.Compile(),
CoreProcBundle: acc.GetBundle(),
}
}