-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
61 lines (51 loc) · 1.12 KB
/
engine.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
package ngebut
import (
"github.com/panjf2000/gnet/v2"
"io"
"net/http"
)
type engine struct {
gnet.BuiltinEventEngine
addr string
multicore bool
eng gnet.Engine
handler Handler
}
func (hs *engine) OnBoot(eng gnet.Engine) gnet.Action {
hs.eng = eng
return gnet.None
}
func (hs *engine) OnOpen(c gnet.Conn) ([]byte, gnet.Action) {
return nil, gnet.None
}
func (hs *engine) OnTraffic(c gnet.Conn) gnet.Action {
buf, _ := c.Next(-1)
req, err := parseRequest(c, buf)
if err != nil {
return gnet.Close
}
resp := &responseWriter{
conn: c,
Response: &Response{
Status: http.StatusText(http.StatusOK),
StatusCode: http.StatusOK,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: Header{
"Content-Type": []string{"text/plain; charset=utf-8"},
"Date": []string{http.TimeFormat},
"Server": []string{"ngebut"},
},
Body: io.NopCloser(nil),
},
}
hs.handler.ServeHTTP(resp, req)
return gnet.None
}
func (hs *engine) OnShutdown(eng gnet.Engine) {
hs.eng = eng
}
func (hs *engine) OnClose(c gnet.Conn, err error) gnet.Action {
return gnet.None
}