1 Star 0 Fork 0

wuzpdev / goworld

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ClientProxy.go 2.33 KB
一键复制 编辑 原始数据 按行查看 历史
seis 提交于 2018-06-19 09:10 . different way of handing client disconnect
package main
import (
"fmt"
"time"
"github.com/xiaonanln/goworld/engine/common"
"github.com/xiaonanln/goworld/engine/config"
"github.com/xiaonanln/goworld/engine/consts"
"github.com/xiaonanln/goworld/engine/gwioutil"
"github.com/xiaonanln/goworld/engine/gwlog"
"github.com/xiaonanln/goworld/engine/netutil"
"github.com/xiaonanln/goworld/engine/post"
"github.com/xiaonanln/goworld/engine/proto"
)
type clientSyncInfo struct {
EntityID common.EntityID
X, Y, Z float32
Yaw float32
}
func (info *clientSyncInfo) IsEmpty() bool {
return info.EntityID == ""
}
// ClientProxy is a game client connections managed by gate
type ClientProxy struct {
*proto.GoWorldConnection
clientid common.ClientID
filterProps map[string]string
clientSyncInfo clientSyncInfo
heartbeatTime time.Time
ownerEntityID common.EntityID // owner entity's ID
}
func newClientProxy(conn netutil.Connection, cfg *config.GateConfig) *ClientProxy {
gwc := proto.NewGoWorldConnection(netutil.NewBufferedConnection(conn), cfg.CompressConnection, cfg.CompressFormat)
return &ClientProxy{
GoWorldConnection: gwc,
clientid: common.GenClientID(), // each client has its unique clientid
filterProps: map[string]string{},
}
}
func (cp *ClientProxy) String() string {
return fmt.Sprintf("ClientProxy<%s@%s>", cp.clientid, cp.RemoteAddr())
}
//func (cp *ClientProxy) SendPacket(packet *netutil.Packet) error {
// err := cp.GoWorldConnection.SendPacket(packet)
// if err != nil {
// return err
// }
// return cp.Flush("ClientProxy")
//}
func (cp *ClientProxy) serve() {
defer func() {
cp.Close()
// tell the gate service that this client is down
post.Post(func() {
gateService.onClientProxyClose(cp)
})
if err := recover(); err != nil && !netutil.IsConnectionError(err.(error)) {
gwlog.TraceError("%s error: %s", cp, err.(error))
} else {
gwlog.Debugf("%s disconnected", cp)
}
}()
cp.SetAutoFlush(consts.CLIENT_PROXY_WRITE_FLUSH_INTERVAL)
//cp.SendSetClientClientID(cp.cp) // set the cp on the client side
for {
var msgtype proto.MsgType
pkt, err := cp.Recv(&msgtype)
if pkt != nil {
gateService.clientPacketQueue <- clientProxyMessage{cp, proto.Message{msgtype, pkt}}
} else if err != nil && !gwioutil.IsTimeoutError(err) {
if netutil.IsConnectionError(err) {
break
} else {
panic(err)
}
}
}
}
1
https://gitee.com/wuzpdev/goworld.git
git@gitee.com:wuzpdev/goworld.git
wuzpdev
goworld
goworld
v0.1.6

搜索帮助