RPGClient._.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using DeepCore;
  2. using DeepCore.FuckPomeloClient;
  3. using DeepCore.IO;
  4. using DeepCore.Log;
  5. using DeepMMO.Data;
  6. using DeepMMO.Protocol;
  7. using DeepMMO.Protocol.Client;
  8. using System;
  9. using System.Collections.Generic;
  10. using DeepCore.GameData.Helper;
  11. using DeepCore.GameEvent;
  12. namespace DeepMMO.Client
  13. {
  14. public partial class RPGClient : Disposable
  15. {
  16. protected readonly Logger log;
  17. protected readonly IExternalizableFactory codec;
  18. protected readonly ClientInfo clientInfo;
  19. protected readonly PomeloClient gate_client;
  20. protected readonly PomeloClient game_client;
  21. public IExternalizableFactory NetCodec { get { return codec; } }
  22. public PomeloClient GateClient { get { return gate_client; } }
  23. public PomeloClient GameClient { get { return game_client; } }
  24. public int CurrentPing { get; private set; }
  25. public bool IsAutoUpdateBattle { get; set; }
  26. public int ConnectTimeOut { get; set; }
  27. /// <summary>
  28. /// 曾经链接到游戏后掉线
  29. /// </summary>
  30. public bool IsGameDisconnected
  31. {
  32. get { return (this.GameClient.IsConnected == false && this.last_EnterGameResponse != null); }
  33. }
  34. public RPGClient(IExternalizableFactory codec, ClientInfo client)
  35. {
  36. this.CurrentPing = 0;
  37. this.IsAutoUpdateBattle = false;
  38. this.ConnectTimeOut = 15000;
  39. this.log = LoggerFactory.GetLogger(GetType().Name);
  40. this.codec = codec;
  41. this.clientInfo = client;
  42. this.gate_client = new PomeloClient(codec);
  43. this.game_client = new PomeloClient(codec);
  44. this.game_client.NetHandleResponseImmediately += Game_client_AsyncHandleResponseImmediately;
  45. this.game_client.NetHandleBodyImmediately += Game_client_AsyncHandleBodyImmediately;
  46. this.game_client.OnRequestEnd += Game_client_OnRequestEnd1;
  47. this.game_client.NetError += Game_client_AsyncError;
  48. this.gate_client.NetError += Gate_client_AsyncError;
  49. this.ping_codec = codec.GetCodec(typeof(ClientPing));
  50. this.pong_codec = codec.GetCodec(typeof(ClientPong));
  51. this.Gate_Init();
  52. this.Connect_Init();
  53. this.Area_Init();
  54. this.OnCreateModules();
  55. }
  56. protected virtual void OnCreateModules()
  57. {
  58. AddModule(new EventModule(this));
  59. }
  60. public void Disconnect()
  61. {
  62. this.gate_client.Disconnect();
  63. this.game_client.Disconnect();
  64. }
  65. //----------------------------------------------------------------------------------------------------------
  66. private readonly TypeCodec pong_codec;
  67. private readonly TypeCodec ping_codec;
  68. private void Game_client_AsyncHandleResponseImmediately(DeepCore.FuckPomelo.IRecvMessage protocol)
  69. {
  70. if (protocol.MsgRoute == pong_codec.MessageID)
  71. {
  72. var pong = protocol.ReadBody() as ClientPong;
  73. this.CurrentPing = (int)(DateTime.Now - pong.time).TotalMilliseconds;
  74. }
  75. }
  76. private void Game_client_AsyncHandleBodyImmediately(object message)
  77. {
  78. // if (message is Response response)
  79. // {
  80. // response.EndRead();
  81. // }
  82. }
  83. private void Game_client_OnRequestEnd1(string route, PomeloException error, ISerializable response, object option)
  84. {
  85. if (response is Response rsp)
  86. {
  87. rsp.EndRead();
  88. }
  89. }
  90. //----------------------------------------------------------------------------------------------------------
  91. #region Modules
  92. private HashSet<RPGClientModule> mModules = new HashSet<RPGClientModule>();
  93. public void AddModule(RPGClientModule module)
  94. {
  95. mModules.Add(module);
  96. }
  97. public void RemoveModule(RPGClientModule module)
  98. {
  99. mModules.Remove(module);
  100. module.Dispose();
  101. }
  102. public T GetModel<T>(Predicate<T> predicate = null) where T : RPGClientModule
  103. {
  104. foreach (var m in mModules)
  105. {
  106. if (m is T model && (predicate == null || predicate.Invoke(model)))
  107. {
  108. return model;
  109. }
  110. }
  111. return null;
  112. }
  113. protected virtual void OnGameClientConnected(object arg1, ISerializable arg2)
  114. {
  115. }
  116. protected virtual void OnGameClientEntered(ClientEnterGameResponse enter)
  117. {
  118. foreach (var module in mModules)
  119. {
  120. module.OnStart();
  121. }
  122. }
  123. protected virtual void OnGameClientDisconnected(object arg1, string arg2)
  124. {
  125. foreach (var module in mModules)
  126. {
  127. module.OnStop();
  128. }
  129. }
  130. protected override void Disposing()
  131. {
  132. this.gate_client.Disconnect();
  133. this.game_client.Disconnect();
  134. this.tasks.Clear();
  135. this.timer_tasks.Dispose();
  136. this.Area_Disposing();
  137. this.Connect_Disposing();
  138. this.Gate_Disposing();
  139. event_OnError = null;
  140. foreach (IDisposable module in mModules)
  141. {
  142. module.Dispose();
  143. }
  144. mModules.Clear();
  145. }
  146. #endregion
  147. //----------------------------------------------------------------------------------------------------------
  148. #region Update
  149. private readonly SyncMessageQueue<Action> tasks = new SyncMessageQueue<Action>(new SingleThreadCollectionPool());
  150. private readonly TimeTaskQueue timer_tasks = new TimeTaskQueue(new SingleThreadCollectionPool());
  151. public virtual void QueueTask(Action action)
  152. {
  153. tasks.Enqueue(action);
  154. }
  155. /// <summary>
  156. /// 【线程安全】增加时间任务
  157. /// </summary>
  158. /// <param name="intervalMS"></param>
  159. /// <param name="delayMS"></param>
  160. /// <param name="repeat"></param>
  161. /// <param name="handler"></param>
  162. public TimeTaskMS AddTimeTask(int intervalMS, int delayMS, int repeat, TickHandler handler)
  163. {
  164. return timer_tasks.AddTimeTask(intervalMS, delayMS, repeat, handler);
  165. }
  166. /// <summary>
  167. /// 【线程安全】增加延时回调方法
  168. /// </summary>
  169. /// <param name="delayMS"></param>
  170. /// <param name="handler"></param>
  171. public TimeTaskMS AddTimeDelayMS(int delayMS, TickHandler handler)
  172. {
  173. return timer_tasks.AddTimeDelayMS(delayMS, handler);
  174. }
  175. /// <summary>
  176. /// 【线程安全】增加定时回调方法
  177. /// </summary>
  178. /// <param name="intervalMS"></param>
  179. /// <param name="handler"></param>
  180. public TimeTaskMS AddTimePeriodicMS(int intervalMS, TickHandler handler)
  181. {
  182. return timer_tasks.AddTimePeriodicMS(intervalMS, handler);
  183. }
  184. public virtual void Update(int intervalMS)
  185. {
  186. if (IsAutoUpdateBattle && current_battle != null)
  187. {
  188. current_battle.BeginUpdate(intervalMS);
  189. }
  190. tasks.ProcessMessages((act) =>
  191. {
  192. try { act.Invoke(); } catch (Exception err) { DoError(err); }
  193. });
  194. timer_tasks.Update(intervalMS);
  195. if (gate_client != null)
  196. {
  197. gate_client.Update();
  198. }
  199. if (game_client != null)
  200. {
  201. game_client.Update();
  202. }
  203. if (IsAutoUpdateBattle && current_battle != null)
  204. {
  205. current_battle.Update();
  206. }
  207. foreach (var module in mModules)
  208. {
  209. module.Update(intervalMS);
  210. }
  211. }
  212. private void Game_client_AsyncError(Exception obj)
  213. {
  214. QueueTask(() => { DoError(obj); });
  215. }
  216. private void Gate_client_AsyncError(Exception obj)
  217. {
  218. QueueTask(() => { DoError(obj); });
  219. }
  220. protected virtual void DoError(Exception err)
  221. {
  222. log.Error(err.Message, err);
  223. if (event_OnError != null) event_OnError(err);
  224. }
  225. #endregion
  226. //----------------------------------------------------------------------------------------------------------
  227. #region Events
  228. private Action<Exception> event_OnError;
  229. public event Action<Exception> OnError { add { event_OnError += value; } remove { event_OnError -= value; } }
  230. #endregion
  231. //----------------------------------------------------------------------------------------------------------
  232. }
  233. }