-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.go
116 lines (103 loc) · 1.9 KB
/
server.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package play
import (
"net"
"net/http"
"sync"
"time"
"github.com/gorilla/websocket"
"github.com/leochen2038/play/codec/binders"
"github.com/quic-go/quic-go"
)
const (
SERVER_TYPE_HTTP = 1
SERVER_TYPE_TCP = 2
SERVER_TYPE_SSE = 3
SERVER_TYPE_WS = 4
SERVER_TYPE_H2C = 5
SERVER_TYPE_QUIC = 6
SERVER_TYPE_HTTP3 = 7
)
type IServerHook interface {
OnBoot(server IServer)
OnShutdown(server IServer)
OnConnect(sess *Session, err error)
OnClose(sess *Session, err error)
OnRequest(ctx *Context) error
OnResponse(ctx *Context)
OnFinish(ctx *Context)
}
type IServer interface {
Info() InstanceInfo
Ctrl() *InstanceCtrl
Hook() IServerHook
Packer() IPacker
Transport(*Conn, []byte) error
Network() string
Run(net.Listener, net.PacketConn) error
Close()
}
type IPacker interface {
Receive(c *Conn) (*Request, error)
Pack(c *Conn, res *Response) ([]byte, error)
}
type InstanceInfo struct {
Address string
Name string
Type int
}
type InstanceCtrl struct {
wg sync.WaitGroup
}
func (c *InstanceCtrl) AddTask() {
c.wg.Add(1)
}
func (c *InstanceCtrl) DoneTask() {
c.wg.Done()
}
func (c *InstanceCtrl) WaitTask() {
c.wg.Wait()
}
type Conn struct {
Type int
IsClose bool
Http struct {
Request *http.Request
ResponseWriter http.ResponseWriter
}
Websocket struct {
Message []byte
MessageType int
WebsocketConn *websocket.Conn
}
Tcp struct {
Version byte
Surplus []byte
Conn net.Conn
}
Quic struct {
Version byte
Conn quic.Connection
Stream quic.Stream
}
}
type Request struct {
Version byte
RenderName string
CallerId int
TagId int
TraceId string
SpanId []byte
NonRespond bool
ActionName string
Attach []byte
Deadline time.Time
InputBinder binders.Binder
}
type Response struct {
Version byte
TraceId string
Template string
RenderName string
Error error
Output Output
}