/
proxypkthandler.go
executable file
·57 lines (44 loc) · 1.43 KB
/
proxypkthandler.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 main
import (
"github.com/0x00b/gobbq/engine/entity"
"github.com/0x00b/gobbq/engine/nets"
"github.com/0x00b/gobbq/erro"
"github.com/0x00b/gobbq/proto/bbq"
)
var _ nets.PacketHandler = &Gate{}
func (gt *Gate) isMyPacket(pkt *nets.Packet) bool {
hdr := pkt.Header
dstEty := entity.DstEntity(pkt)
if hdr.GetServiceType() == bbq.ServiceType_Entity ||
hdr.RequestType == bbq.RequestType_RequestRespone {
return gt.IsMyEntity(dstEty)
}
_, ok := gt.EntityMgr.GetService(hdr.GetType())
return ok
}
// 这里处理的是所有来自proxy的消息, 如果SendPacket是同步的,那当一个客户端断连, 发包阻塞时,会阻塞整个proxy消息的处理
func (gt *Gate) HandlePacket(pkt *nets.Packet) error {
if gt.isMyPacket(pkt) {
return gt.Server.EntityMgr.HandlePacket(pkt)
}
hdr := pkt.Header
dstEty := entity.DstEntity(pkt)
// 如果是发给客户端的系统调用,需要拦截
if hdr.Type == entity.BbqSysEntityDesc.TypeName {
entity.DispatchPkt(gt, pkt)
return nil
}
rw, ok := gt.GetClient(dstEty)
if !ok {
return erro.ErrUnknownClient
}
// todo 需要处理一下kcp的断开连接,否则会阻塞在这里,以及read也会阻塞,导致goroutine得不到释放
// https://github.com/skywind3000/kcp/issues/176
return rw.SendPacket(pkt)
}
func (gt *Gate) GetClient(eid entity.EntityID) (*nets.Conn, bool) {
gt.cltMtx.Lock()
defer gt.cltMtx.Unlock()
prw, ok := gt.cltMap[eid]
return prw, ok
}