-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
159 lines (150 loc) · 4.11 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"fmt"
_ "github.com/CuteReimu/cellnet-plus/codec/protobuf"
"github.com/CuteReimu/cellnet-plus/codec/raw"
_ "github.com/CuteReimu/cellnet-plus/peer/kcp"
_ "github.com/CuteReimu/cellnet-plus/proc/udp"
"github.com/CuteReimu/udptunnel/pb"
"github.com/davyxu/cellnet"
"github.com/davyxu/cellnet/peer"
_ "github.com/davyxu/cellnet/peer/udp"
"github.com/davyxu/cellnet/proc"
_ "github.com/davyxu/cellnet/proc/tcp"
"github.com/davyxu/cellnet/rpc"
"os"
"os/signal"
"time"
)
type server struct {
port uint32
timeout time.Duration
cache map[int64]*serverClient // from_client_id => server_client
peer cellnet.TCPConnector
}
func (s *server) start(address string) {
ch := make(chan os.Signal, 1)
s.cache = make(map[int64]*serverClient)
queue := cellnet.NewEventQueue()
queue.EnableCapturePanic(true)
s.peer = peer.NewGenericPeer("kcp.Connector", "server", fmt.Sprint(address, ":", *tunnelPort), queue).(cellnet.TCPConnector)
proc.BindProcessorHandler(s.peer, "tcp.ltv", func(ev cellnet.Event) {
switch msg := ev.Message().(type) {
case *cellnet.SessionConnected:
log.Debugln("server connected: ", ev.Session().ID())
go s.heart()
s.createServerRoom()
case *cellnet.SessionConnectError:
log.Errorln("server connect failed: ", msg.String())
time.AfterFunc(3*time.Second, func() { ch <- os.Interrupt })
case *cellnet.SessionClosed:
log.Debugln("session closed: ", ev.Session().ID())
time.AfterFunc(3*time.Second, func() { ch <- os.Interrupt })
case *pb.UdpToc:
cli, ok := s.cache[msg.FromId]
if !ok {
cli = &serverClient{server: s, id: msg.FromId}
cli.start()
s.cache[msg.FromId] = cli
}
cli.lastMsgTime = time.Now()
cli.peer.Session().Send(&raw.Packet{Msg: msg.Data})
}
})
s.peer.Start()
queue.StartLoop()
go s.startTimeoutTimer()
signal.Notify(ch, os.Interrupt)
<-ch
queue.StopLoop()
for _, c := range s.cache {
c.peer.Queue().StopLoop()
}
queue.Wait()
for _, c := range s.cache {
c.peer.Queue().Wait()
}
for _, c := range s.cache {
c.peer.Stop()
}
s.peer.Stop()
}
func (s *server) heart() {
var count int
ch := time.Tick(15 * time.Second)
for {
<-ch
_, err := rpc.CallSync(s.peer.Session(), &pb.HeartTos{}, time.Second*3)
if err != nil {
count++
if count >= 10 {
log.Errorf("服务器已断开,程序即将关闭:%s", err.Error())
s.peer.Session().Close()
break
}
log.Errorf("服务器已断开,请检查网络连接:%s", err.Error())
} else {
count = 0
}
}
}
func (s *server) startTimeoutTimer() {
ch := time.Tick(time.Minute)
for {
<-ch
s.peer.Queue().Post(s.removeTimeoutClient)
}
}
func (s *server) removeTimeoutClient() {
if len(s.cache) == 0 {
return
}
deleteClient := make([]int64, 0, len(s.cache))
now := time.Now()
for _, c := range s.cache {
if c.lastMsgTime.Add(s.timeout).Before(now) {
deleteClient = append(deleteClient, c.id)
p := c.peer
p.Queue().StopLoop()
go func() {
p.Queue().Wait()
p.Stop()
}()
}
}
for _, id := range deleteClient {
delete(s.cache, id)
}
}
func (s *server) createServerRoom() {
rpc.Call(s.peer.Session(), &pb.CreateServerTos{Port: s.port}, time.Second*3, func(raw interface{}) {
if err, ok := raw.(error); ok {
log.Errorln("创建服务器房间失败:", err)
s.peer.Session().Close()
} else if resp, ok := raw.(*pb.CreateServerToc); ok && !resp.Success {
log.Errorln("创建服务器房间失败:", resp)
s.peer.Session().Close()
} else {
log.Infof("创建服务器房间成功,等待客户端加入")
}
})
}
type serverClient struct {
server *server
id int64
peer cellnet.UDPConnector
lastMsgTime time.Time
}
func (c *serverClient) start() {
queue := cellnet.NewEventQueue()
queue.EnableCapturePanic(true)
c.peer = peer.NewGenericPeer("udp.Connector", "server", fmt.Sprint("127.0.0.1:", c.server.port), queue).(cellnet.UDPConnector)
proc.BindProcessorHandler(c.peer, "udp.packet", func(ev cellnet.Event) {
switch msg := ev.Message().(type) {
case *raw.Packet:
c.server.peer.Session().Send(&pb.UdpTos{ToId: c.id, Data: msg.Msg})
}
})
c.peer.Start()
queue.StartLoop()
}