-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
81 lines (66 loc) · 1.39 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
package plex
import (
"net"
"sync"
"github.com/swxctx/plex/plog"
)
var (
server *plexServer
)
// plexServer
type plexServer struct {
// config
cfg *Config
// tcp listen
listener net.Listener
// conn store cache
store *connStore
// tcp auth func
authFunc func(body string) (bool, string)
// client online or offline callback
onlineStatusSubscribe func(isOnline bool, uid string)
}
// NewServer new plex server
func NewServer(config *Config, fn ...func(body string) (bool, string)) {
// reload config
cfg := reloadConfig(config)
plog.Infof("reload config success.")
// new server
server = &plexServer{
cfg: cfg,
store: newConnStore(cfg.MaxConnection),
}
// auth handler
if len(fn) > 0 {
server.authFunc = fn[0]
}
plog.Infof("new plex server success.")
}
// Start plex server start
func Start() {
plog.Infof("--- server start begin ---")
var (
wg sync.WaitGroup
)
wg.Add(2)
// start http
go func() {
defer wg.Done()
server.startHttpServer()
}()
// start tcp
go func() {
defer wg.Done()
server.startTcpServer()
}()
wg.Wait()
plog.Infof("--- server start end ---")
}
// SetAuthFunc func set outer auth func
func SetAuthFunc(fn func(body string) (bool, string)) {
server.authFunc = fn
}
// SetOnlineStatusSubscribe func client online status change callback
func SetOnlineStatusSubscribe(fn func(isOnline bool, uid string)) {
server.onlineStatusSubscribe = fn
}