-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (87 loc) · 1.84 KB
/
main.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
package main
import (
"flag"
"fmt"
"log/slog"
"net"
"strings"
)
func init() {
Credentials = make(map[string]string)
}
func main() {
debug := flag.Bool("debug", false, "sets log level to debug")
showHelp := flag.Bool("help", false, "show help info")
flag.Parse()
if *showHelp {
help := `rlpa-server
Arguments:
-debug enable debug output
-help show help info
Environment Variables:
LPAC_FOLDER lpac binary folder name
SOCKET_PORT rlpa socket port
API_PORT http management api port
`
print(help)
return
}
slog.SetLogLoggerLevel(slog.LevelInfo)
if *debug {
slog.SetLogLoggerLevel(slog.LevelDebug)
}
err := InitConfig()
if err != nil {
panic(err)
}
go HttpServer()
addr := fmt.Sprint("0.0.0.0:", CFG.SocketPort)
listener, err := net.Listen("tcp", addr)
if err != nil {
panic(err)
}
slog.Info("Start listening on tcp://" + addr)
defer func(listener net.Listener) {
errClose := listener.Close()
if errClose != nil {
slog.Error("Failed to close socket listener")
}
}(listener)
for {
conn, err := listener.Accept()
slog.Info("Accepted " + conn.RemoteAddr().String())
if err != nil {
slog.Error(err.Error())
continue
}
go handleConnection(conn)
}
}
func handleConnection(conn net.Conn) {
client := NewRLPAClient(conn)
for {
// 接受 Packet
err := client.Packet.Recv(conn)
if err != nil {
if strings.Contains(err.Error(), "EOF") {
client.Close(ResultClientDisconnect)
} else {
slog.Error("packet recv: "+err.Error(), "client", client.RemoteAddr())
client.Close(ResultError)
}
return
}
if !client.Packet.IsFinished() {
continue
}
// 处理
err = client.ProcessPacket()
if err != nil {
slog.Error("packet process: "+err.Error(), "client", client.RemoteAddr())
client.Close(ResultError)
return
}
// 重置
client.Packet = NewRLPAPacket(0, []byte{})
}
}