-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter.go
104 lines (84 loc) · 2.03 KB
/
router.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
package server
import (
"fmt"
"os"
"strings"
"sync"
"time"
)
type Router struct {
servers map[int]*Server
CleanupF func() error
ServerPath string
startTime time.Time
running bool
offlineClients map[string]offlineClient
isInternalConn func(remoteAddress string) bool
TaskMgr *TaskManager
logFile *os.File
logs []Log
mLog *sync.Mutex
}
func NewRouter(logFile *os.File, serverPath string) (router *Router, err error) {
router = new(Router)
router.servers = make(map[int]*Server)
if logFile == nil {
router.logFile = os.Stdout
} else {
router.logFile = logFile
}
router.mLog = new(sync.Mutex)
if serverPath == "" {
serverPath, err = os.Getwd()
if err != nil {
return nil, fmt.Errorf("serverPath error: %w", err)
}
}
serverPath = strings.ReplaceAll(serverPath, "\\", "/")
router.ServerPath = serverPath
router.offlineClients = make(map[string]offlineClient)
router.isInternalConn = func(remoteAddress string) bool { return false }
router.newTaskManager()
router.startTime = time.Now()
router.plainPrintf(WriteLogStart(router.startTime))
return
}
func (router *Router) SetInternalConnFilter(f func(remoteAddress string) bool) *Router {
if f != nil {
router.isInternalConn = f
}
return router
}
func (router *Router) Server(port int) *Server {
return router.servers[port]
}
func (router *Router) Start() () {
for _, srv := range router.servers {
srv.Start()
}
router.TaskMgr.start()
router.running = true
return
}
func (router *Router) Stop() () {
router.Log(LOG_LEVEL_INFO, "Router shutdown procedure started")
router.running = false
router.TaskMgr.stop()
for _, srv := range router.servers {
srv.Shutdown()
}
if router.CleanupF != nil {
router.CleanupF()
}
os.Remove(router.ServerPath + "/PID.txt")
router.plainPrintf(WriteLogClosure(time.Now()))
return
}
func (router *Router) StopServer(port int) error {
srv := router.servers[port]
if srv == nil {
return fmt.Errorf("server with port %d not found", port)
}
srv.Shutdown()
return nil
}