123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- using CommonAI.Zone.ZoneEditor;
- using CommonLang;
- namespace CommonTickBattle.Battle.Host
- {
- public class BattleHost : Battle
- {
- private HashMap<string, ISession> mPlayers = new HashMap<string, ISession>();
- public BattleHost(EditorTemplates data_root, TBConfig cfg)
- : base(data_root, cfg)
- {
- }
- protected override void OnDispose()
- {
- base.OnDispose();
- foreach (var player in mPlayers.Values)
- {
- player.OnReceived -= OnReceivedRequest;
- }
- mPlayers.Clear();
- }
-
-
-
-
- public void BindSession(ISession player)
- {
- lock (this)
- {
- mPlayers.Add(player.PlayerUUID, player);
- player.OnReceived += OnReceivedRequest;
- }
- }
-
-
-
-
-
- protected virtual void OnReceivedRequest(ISession player, TBRequest req)
- {
- if (this.PushAction(req))
- {
- TBResponse rsp = new TBResponse();
- rsp.MessageID = req.MessageID;
- rsp.result = TBResponse.Result.OK;
- player.Send(rsp);
- TBNotify ntf = new TBNotify();
- ntf.action = req.action;
- ntf.action_tick = req.action_tick;
- ntf.player_uuid = player.PlayerUUID;
- this.Broadcast(ntf);
- }
- else
- {
- TBResponse rsp = new TBResponse();
- rsp.MessageID = req.MessageID;
- rsp.result = TBResponse.Result.TimeOut;
- player.Send(rsp);
- }
- }
-
-
-
-
- protected virtual void Broadcast(TBMessage ntf)
- {
- foreach (var p in mPlayers.Values)
- {
- p.Send(ntf);
- }
- }
-
-
-
-
-
- protected virtual bool PushAction(TBRequest req)
- {
- lock (this)
- {
- if (req.send_tick + this.RequestLaggingTick < this.CurrentTick)
- {
-
- log.ErrorFormat("客户端发送请求已超时 : CT={0} : {1}", req.send_tick, req.action);
- return false;
- }
- else if (req.action_tick < (this.CurrentTick + this.ResponseLaggingTick))
- {
-
- log.ErrorFormat("客户端请求动作时间已超时 : AT={0} : {1}", req.action_tick, req.action);
- return false;
- }
- else
- {
- return base.PushAction(req.action, req.action_tick);
- }
- }
- }
- }
- }
|