123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using CommonAI.Zone.ZoneEditor;
- using CommonLang;
- using CommonAI.Zone.Helper;
- using CommonLang.Concurrent;
- using CommonLang.Log;
- namespace CommonTickBattle.Battle.Local
- {
- public class BattleLocal : Battle
- {
- private readonly IConnector mConnector;
- public BattleLocal(EditorTemplates data_root, TBConfig cfg, IConnector connector)
- : base(data_root, cfg)
- {
- this.mConnector = connector;
- this.mConnector.OnReceived += OnReceivedMessage;
- }
- protected override void OnDispose()
- {
- this.mConnector.OnReceived -= OnReceivedMessage;
- this.dispose_waiting_request();
- base.OnDispose();
- }
- public override void Update()
- {
- base.Update();
- this.check_request_timeout();
- }
- //------------------------------------------------------------------------------------------------------------------------------
- #region Receive
- /// <summary>
- /// 收到服务器发来的协议
- /// </summary>
- /// <param name="connector"></param>
- /// <param name="req"></param>
- protected virtual void OnReceivedMessage(IConnector connector, TBMessage req)
- {
- if (req is TBResponse)
- {
- this.on_received_response(req as TBResponse);
- }
- else if (req is TBNotify)
- {
- this.OnReceivedTBNotify(req as TBNotify);
- }
- else if (req is TBSyncTime)
- {
- this.OnReceivedTBSyncTime(req as TBSyncTime);
- }
- else if (req is TBSyncHistory)
- {
- this.OnReceivedTBSyncHistory(req as TBSyncHistory);
- }
- }
- protected virtual void OnReceivedTBNotify(TBNotify ntf)
- {
- base.PushAction(ntf.action, ntf.action_tick);
- }
- protected virtual void OnReceivedTBSyncTime(TBSyncTime syn)
- {
- }
- protected virtual void OnReceivedTBSyncHistory(TBSyncHistory syn)
- {
- }
- #endregion
- //------------------------------------------------------------------------------------------------------------------------------
- #region Request
- /// <summary>
- /// 发送请求
- /// </summary>
- /// <param name="msg"></param>
- /// <param name="handler"></param>
- /// <param name="timeout"></param>
- /// <param name="timeOutMS"></param>
- /// <returns></returns>
- public Request SendRequest(CommonAI.Zone.Action action, Action<Request> handler = null, Action<Request> timeout = null)
- {
- Request req = new Request(this, action, handler, timeout);
- lock (mListenRequests)
- {
- mListenRequests.Add(req.MessageID, req);
- }
- mConnector.Send(req.RequestMessage);
- return req;
- }
- private readonly HashMap<int, object> mListenRequests = new HashMap<int, object>();
- private readonly AtomicInteger mMessageIDGen = new AtomicInteger(1);
- private void dispose_waiting_request()
- {
- lock (mListenRequests)
- {
- foreach (Request req in mListenRequests.Values)
- {
- req.onDispose();
- }
- mListenRequests.Clear();
- }
- }
- private void check_request_timeout()
- {
- long curTime = CUtils.CurrentTimeMS;
- lock (mListenRequests)
- {
- if (mListenRequests.Count > 0)
- {
- using (var removing = ListObjectPool<Request>.AllocAutoRelease())
- {
- foreach (Request req in mListenRequests.Values)
- {
- if (req.IsTimeOut)
- {
- removing.Add(req);
- }
- }
- if (removing.Count > 0)
- {
- foreach (Request remove in removing)
- {
- mListenRequests.RemoveByKey(remove.MessageID);
- remove.onTimeout();
- }
- }
- }
- }
- }
- }
- private bool on_received_response(TBResponse rsp)
- {
- Request request = null;
- lock (mListenRequests)
- {
- request = mListenRequests.RemoveByKey(rsp.MessageID) as Request;
- }
- if (request != null)
- {
- request.onRecivedMessage(rsp);
- return true;
- }
- return false;
- }
- public class Request
- {
- internal static Logger log = LoggerFactory.GetLogger("ZoneLayer.Request");
- private Action<Request> mHandler;
- private Action<Request> mTimeout;
- public BattleLocal Layer { get; private set; }
- public ulong SendTick { get; private set; }
- public ulong EndTick { get; private set; }
- public int MessageID { get; private set; }
- public TBRequest RequestMessage { get; protected set; }
- public TBResponse ResponseMessage { get; protected set; }
- public Request(BattleLocal client, CommonAI.Zone.Action action, Action<Request> handler = null, Action<Request> timeout = null)
- {
- this.Layer = client;
- this.RequestMessage = new TBRequest();
- this.RequestMessage.action = action;
- this.MessageID = RequestMessage.MessageID = client.mMessageIDGen.GetAndIncrement();
- this.SendTick = RequestMessage.send_tick = client.CurrentTick;
- this.EndTick = RequestMessage.action_tick = client.CurrentTick + client.RequestLaggingTick + client.ResponseLaggingTick;
- this.mHandler = handler;
- this.mTimeout = timeout;
- }
- public bool IsTimeOut
- {
- get { return Layer.CurrentTick > EndTick; }
- }
- internal void onDispose()
- {
- mHandler = null;
- mTimeout = null;
- }
- internal void onRecivedMessage(TBResponse msg)
- {
- ResponseMessage = msg;
- if (mHandler != null)
- {
- try
- {
- mHandler.Invoke(this);
- }
- catch (Exception err)
- {
- log.Error(err.Message, err);
- }
- }
- mHandler = null;
- mTimeout = null;
- }
- internal void onTimeout()
- {
- if (mTimeout != null)
- {
- try
- {
- mTimeout.Invoke(this);
- }
- catch (Exception err)
- {
- log.Error(err.Message, err);
- }
- }
- mHandler = null;
- mTimeout = null;
- }
- }
- #endregion
- }
- }
|