-
Notifications
You must be signed in to change notification settings - Fork 3
/
evnet.go
57 lines (49 loc) · 1.23 KB
/
evnet.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
package evnet
import (
log "github.com/sirupsen/logrus"
"os"
"strings"
)
func Run(eventHandler EventHandler, protoAddr string, optList ...SetOption) {
optArg := loadOptions(optList)
//set Default optArg
if optArg.LogPath != "" {
outputFile, err := os.OpenFile(optArg.LogPath, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0777)
if err != nil {
log.Error("log file open error")
}
log.SetOutput(outputFile)
wd, _ := os.Getwd()
log.Info("save log file at", wd, outputFile.Name())
}
log.SetLevel(optArg.LogLevel)
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
ForceQuote: true,
ForceColors: true,
})
// log.SetFlags(log.Llongfile | log.Lmicroseconds | log.Ldate)
//options working
network, address := parseProtoAddr(protoAddr)
//mainReactor initialize
mr := new(MainReactor)
mr.Init(network, address)
mr.SetEventHandler(eventHandler)
serve(mr)
}
func parseProtoAddr(protoAddr string) (network, address string) {
network = "tcp"
address = strings.ToLower(protoAddr)
if strings.Contains(address, "://") {
pair := strings.Split(address, "://")
network = pair[0]
address = pair[1]
}
return
}
func serve(mr *MainReactor) {
(**mr.eventHandlerPP).OnBoot(mr)
go mr.Loop()
mr.waitForShutdown()
mr.stop()
}