上传战斗服

This commit is contained in:
liwei1dao 2023-03-04 20:59:44 +08:00
parent 71b4c4c185
commit 4e528ac669
2 changed files with 60 additions and 11 deletions

View File

@ -14,25 +14,32 @@ import (
)
func newClient(addr string, mgr IClientMgr, log log.ILogger) (c *client, err error) {
c = &client{addr: addr, mgr: mgr, log: log, seq: 1, state: 1, pending: make(map[uint64]*MessageCall)}
msg := &pb.BattleRpcMessage{
Rid: 0,
Method: "Heartbeat",
}
data, _ := proto.Marshal(msg)
c = &client{addr: addr, mgr: mgr, log: log, seq: 1, state: 1, pending: make(map[uint64]*MessageCall), heartbeatpack: data}
dialer := websocket.Dialer{}
ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
if c.conn, _, err = dialer.DialContext(ctx, addr, nil); err != nil {
return
}
go c.run()
c.heartbeat()
return
}
type client struct {
addr string
mgr IClientMgr
log log.ILogger
conn *websocket.Conn
state int32 //状态 0 关闭 1 运行 2 关闭中
seq uint64
pendingmutex sync.Mutex
pending map[uint64]*MessageCall //高并发回调
addr string
mgr IClientMgr
log log.ILogger
conn *websocket.Conn
state int32 //状态 0 关闭 1 运行 2 关闭中
seq uint64
pendingmutex sync.Mutex
pending map[uint64]*MessageCall //高并发回调
heartbeatpack []byte
}
//校验战斗过程
@ -132,6 +139,22 @@ func (this *client) handleresponse(resp *pb.BattleRpcMessage) {
}
}
func (this *client) heartbeat() {
go func() {
timer := time.NewTicker(time.Second * 30)
locp:
for {
select {
case <-timer.C:
if err := this.conn.WriteMessage(websocket.BinaryMessage, this.heartbeatpack); err != nil {
break locp
}
}
}
timer.Stop()
}()
}
//外部代用关闭
func (this *client) Close() {
if !atomic.CompareAndSwapInt32(&this.state, 1, 2) {

View File

@ -48,6 +48,17 @@ func (this *battleClientMgrComp) Shutdown(c *client) {
this.module.Errorf("战斗校验服务被关闭!")
}
func (this *battleClientMgrComp) reconnect() (err error) {
if this.clinet != nil {
return
}
if this.clinet, err = newClient(this.options.BattleServerAddr, this, this.options.Log); err != nil {
this.module.Errorln(err)
return
}
return
}
//Pvp 战斗指令输出
func (this *battleClientMgrComp) BattleOutCmd(out *pb.BattleOutCmdPush) {
this.module.pvp.PvpOutCmdPush(&pb.PvpOutCmdPush{
@ -64,6 +75,9 @@ func (this *battleClientMgrComp) BattleFinish(out *pb.BattleFinishPush) {
//校验战斗结果
func (this *battleClientMgrComp) CheckBattle(ctx context.Context, req *pb.BattleReport) (reply *pb.BattleCheckResults, err error) {
reply = &pb.BattleCheckResults{}
if err = this.reconnect(); err != nil {
return
}
if err = this.clinet.callBattle(ctx, "Check", req, reply); err != nil {
this.module.Errorln(err)
}
@ -73,7 +87,9 @@ func (this *battleClientMgrComp) CheckBattle(ctx context.Context, req *pb.Battle
//实时pvp战斗创建
func (this *battleClientMgrComp) GetInfo(ctx context.Context, req *pb.BattleGetInfoReq) (reply *pb.BattleGetInfoResp, err error) {
this.module.Debug("GetInfo", log.Field{Key: "req", Value: req})
if err = this.reconnect(); err != nil {
return
}
reply = &pb.BattleGetInfoResp{}
if err = this.clinet.callBattle(ctx, "GetInfo", req, reply); err != nil {
this.module.Errorln(err)
@ -84,7 +100,9 @@ func (this *battleClientMgrComp) GetInfo(ctx context.Context, req *pb.BattleGetI
//实时pvp战斗创建
func (this *battleClientMgrComp) CreateBattle(ctx context.Context, req *pb.BattleCreateServerReq) (reply *pb.BattleCreateServerResp, err error) {
this.module.Debug("CreateBattle", log.Field{Key: "req", Value: req})
if err = this.reconnect(); err != nil {
return
}
reply = &pb.BattleCreateServerResp{}
if err = this.clinet.callBattle(ctx, "Create", req, reply); err != nil {
this.module.Errorln(err)
@ -94,6 +112,10 @@ func (this *battleClientMgrComp) CreateBattle(ctx context.Context, req *pb.Battl
//实时pvp 输入指令输入
func (this *battleClientMgrComp) InCmdBattle(ctx context.Context, req *pb.BattleInCmdReq) (reply *pb.BattleInCmdResp, err error) {
this.module.Debug("InCmdBattle", log.Field{Key: "req", Value: req})
if err = this.reconnect(); err != nil {
return
}
reply = &pb.BattleInCmdResp{}
if err = this.clinet.callBattle(ctx, "InCmd", req, reply); err != nil {
this.module.Errorln(err)
@ -103,6 +125,10 @@ func (this *battleClientMgrComp) InCmdBattle(ctx context.Context, req *pb.Battle
//实时pvp 认输
func (this *battleClientMgrComp) ConcedeBattle(ctx context.Context, req *pb.BattleConcedeReq) (reply *pb.BattleConcedeResp, err error) {
this.module.Debug("ConcedeBattle", log.Field{Key: "req", Value: req})
if err = this.reconnect(); err != nil {
return
}
reply = &pb.BattleConcedeResp{}
if err = this.clinet.callBattle(ctx, "Concede", req, reply); err != nil {
this.module.Errorln(err)