BattleLocal.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using CommonAI.Zone.ZoneEditor;
  6. using CommonLang;
  7. using CommonAI.Zone.Helper;
  8. using CommonLang.Concurrent;
  9. using CommonLang.Log;
  10. namespace CommonTickBattle.Battle.Local
  11. {
  12. public class BattleLocal : Battle
  13. {
  14. private readonly IConnector mConnector;
  15. public BattleLocal(EditorTemplates data_root, TBConfig cfg, IConnector connector)
  16. : base(data_root, cfg)
  17. {
  18. this.mConnector = connector;
  19. this.mConnector.OnReceived += OnReceivedMessage;
  20. }
  21. protected override void OnDispose()
  22. {
  23. this.mConnector.OnReceived -= OnReceivedMessage;
  24. this.dispose_waiting_request();
  25. base.OnDispose();
  26. }
  27. public override void Update()
  28. {
  29. base.Update();
  30. this.check_request_timeout();
  31. }
  32. //------------------------------------------------------------------------------------------------------------------------------
  33. #region Receive
  34. /// <summary>
  35. /// 收到服务器发来的协议
  36. /// </summary>
  37. /// <param name="connector"></param>
  38. /// <param name="req"></param>
  39. protected virtual void OnReceivedMessage(IConnector connector, TBMessage req)
  40. {
  41. if (req is TBResponse)
  42. {
  43. this.on_received_response(req as TBResponse);
  44. }
  45. else if (req is TBNotify)
  46. {
  47. this.OnReceivedTBNotify(req as TBNotify);
  48. }
  49. else if (req is TBSyncTime)
  50. {
  51. this.OnReceivedTBSyncTime(req as TBSyncTime);
  52. }
  53. else if (req is TBSyncHistory)
  54. {
  55. this.OnReceivedTBSyncHistory(req as TBSyncHistory);
  56. }
  57. }
  58. protected virtual void OnReceivedTBNotify(TBNotify ntf)
  59. {
  60. base.PushAction(ntf.action, ntf.action_tick);
  61. }
  62. protected virtual void OnReceivedTBSyncTime(TBSyncTime syn)
  63. {
  64. }
  65. protected virtual void OnReceivedTBSyncHistory(TBSyncHistory syn)
  66. {
  67. }
  68. #endregion
  69. //------------------------------------------------------------------------------------------------------------------------------
  70. #region Request
  71. /// <summary>
  72. /// 发送请求
  73. /// </summary>
  74. /// <param name="msg"></param>
  75. /// <param name="handler"></param>
  76. /// <param name="timeout"></param>
  77. /// <param name="timeOutMS"></param>
  78. /// <returns></returns>
  79. public Request SendRequest(CommonAI.Zone.Action action, Action<Request> handler = null, Action<Request> timeout = null)
  80. {
  81. Request req = new Request(this, action, handler, timeout);
  82. lock (mListenRequests)
  83. {
  84. mListenRequests.Add(req.MessageID, req);
  85. }
  86. mConnector.Send(req.RequestMessage);
  87. return req;
  88. }
  89. private readonly HashMap<int, object> mListenRequests = new HashMap<int, object>();
  90. private readonly AtomicInteger mMessageIDGen = new AtomicInteger(1);
  91. private void dispose_waiting_request()
  92. {
  93. lock (mListenRequests)
  94. {
  95. foreach (Request req in mListenRequests.Values)
  96. {
  97. req.onDispose();
  98. }
  99. mListenRequests.Clear();
  100. }
  101. }
  102. private void check_request_timeout()
  103. {
  104. long curTime = CUtils.CurrentTimeMS;
  105. lock (mListenRequests)
  106. {
  107. if (mListenRequests.Count > 0)
  108. {
  109. using (var removing = ListObjectPool<Request>.AllocAutoRelease())
  110. {
  111. foreach (Request req in mListenRequests.Values)
  112. {
  113. if (req.IsTimeOut)
  114. {
  115. removing.Add(req);
  116. }
  117. }
  118. if (removing.Count > 0)
  119. {
  120. foreach (Request remove in removing)
  121. {
  122. mListenRequests.RemoveByKey(remove.MessageID);
  123. remove.onTimeout();
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }
  130. private bool on_received_response(TBResponse rsp)
  131. {
  132. Request request = null;
  133. lock (mListenRequests)
  134. {
  135. request = mListenRequests.RemoveByKey(rsp.MessageID) as Request;
  136. }
  137. if (request != null)
  138. {
  139. request.onRecivedMessage(rsp);
  140. return true;
  141. }
  142. return false;
  143. }
  144. public class Request
  145. {
  146. internal static Logger log = LoggerFactory.GetLogger("ZoneLayer.Request");
  147. private Action<Request> mHandler;
  148. private Action<Request> mTimeout;
  149. public BattleLocal Layer { get; private set; }
  150. public ulong SendTick { get; private set; }
  151. public ulong EndTick { get; private set; }
  152. public int MessageID { get; private set; }
  153. public TBRequest RequestMessage { get; protected set; }
  154. public TBResponse ResponseMessage { get; protected set; }
  155. public Request(BattleLocal client, CommonAI.Zone.Action action, Action<Request> handler = null, Action<Request> timeout = null)
  156. {
  157. this.Layer = client;
  158. this.RequestMessage = new TBRequest();
  159. this.RequestMessage.action = action;
  160. this.MessageID = RequestMessage.MessageID = client.mMessageIDGen.GetAndIncrement();
  161. this.SendTick = RequestMessage.send_tick = client.CurrentTick;
  162. this.EndTick = RequestMessage.action_tick = client.CurrentTick + client.RequestLaggingTick + client.ResponseLaggingTick;
  163. this.mHandler = handler;
  164. this.mTimeout = timeout;
  165. }
  166. public bool IsTimeOut
  167. {
  168. get { return Layer.CurrentTick > EndTick; }
  169. }
  170. internal void onDispose()
  171. {
  172. mHandler = null;
  173. mTimeout = null;
  174. }
  175. internal void onRecivedMessage(TBResponse msg)
  176. {
  177. ResponseMessage = msg;
  178. if (mHandler != null)
  179. {
  180. try
  181. {
  182. mHandler.Invoke(this);
  183. }
  184. catch (Exception err)
  185. {
  186. log.Error(err.Message, err);
  187. }
  188. }
  189. mHandler = null;
  190. mTimeout = null;
  191. }
  192. internal void onTimeout()
  193. {
  194. if (mTimeout != null)
  195. {
  196. try
  197. {
  198. mTimeout.Invoke(this);
  199. }
  200. catch (Exception err)
  201. {
  202. log.Error(err.Message, err);
  203. }
  204. }
  205. mHandler = null;
  206. mTimeout = null;
  207. }
  208. }
  209. #endregion
  210. }
  211. }