BattleClient.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using CommonNetwork.Sockets;
  5. using CommonLang.Protocol;
  6. using RoomService.Net.BsServer;
  7. using CommonLang.Log;
  8. using RoomService.Net.BsClient;
  9. using CommonAI.ZoneClient;
  10. using CommonAI.Zone;
  11. using CommonLang.IO;
  12. using CommonAI.Zone.ZoneEditor;
  13. using CommonAI.ZoneServer;
  14. using CommonLang.Concurrent;
  15. using CommonNetwork.Net;
  16. using CommonLang.Property;
  17. using CommonLang;
  18. using CommonAI.Zone.Helper;
  19. namespace CommonAIClient.Client
  20. {
  21. public abstract class BattleClient : AbstractBattle
  22. {
  23. private static Logger log = LoggerFactory.GetLogger("BattleClient");
  24. protected readonly PlayerWillConnectResponseB2R room;
  25. protected readonly PlayerWillConnectRequestR2B testToken;
  26. private KickedByServerNotifyB2C kicked;
  27. private Ping send_ping = new Ping();
  28. private TimeInterval<int> ping_task = new TimeInterval<int>(2000);
  29. //private int current_ping = 0;
  30. public string token { get; private set; }
  31. public string data_root { get; private set; }
  32. public abstract INetSession Session { get; }
  33. public string PlayerUUID { get { return room.PlayerUUID; } }
  34. public MessageFactoryGenerator MessageFactory { get; private set; }
  35. public override long RecvPackages { get { return Session.TotalRecvPackages; } }
  36. public override long SendPackages { get { return Session.TotalSentPackages; } }
  37. public override int CurrentPing { get { return this.Layer.CurrentPing; } }
  38. public int PingIntervalMS
  39. {
  40. get
  41. {
  42. return ping_task.IntervalTimeMS;
  43. }
  44. set
  45. {
  46. ping_task = new TimeInterval<int>(Math.Max(1000, value));
  47. }
  48. }
  49. public override KickedByServerNotifyB2C KickMessage
  50. {
  51. get { return kicked; }
  52. }
  53. public override bool IsNet { get { return true; } }
  54. public BattleClient(
  55. EditorTemplates datas,
  56. MessageFactoryGenerator msgFactory,
  57. PlayerWillConnectResponseB2R room,
  58. PlayerWillConnectRequestR2B testToken,
  59. string token)
  60. : base(datas)
  61. {
  62. this.room = room;
  63. this.testToken = testToken;
  64. this.token = token;
  65. this.MessageFactory = msgFactory;
  66. this.Layer.ActorSyncMode = SyncMode.MoveByClient_PreSkillByClient;
  67. }
  68. protected override void Disposing()
  69. {
  70. base.Disposing();
  71. this.mHandleMessage = null;
  72. this.mDisconnectd = null;
  73. this.mConnected = null;
  74. this.mHandleError = null;
  75. }
  76. //----------------------------------------------------------------------------------------------------
  77. abstract public void Start();
  78. abstract public void Stop();
  79. //----------------------------------------------------------------------------------------------------
  80. /// <summary>
  81. /// 发送单位控制命令
  82. /// </summary>
  83. /// <param name="action"></param>
  84. public override void SendAction(CommonAI.Zone.Action action)
  85. {
  86. Session.Send(action);
  87. }
  88. /// <summary>
  89. /// 请求离开房间
  90. /// </summary>
  91. public void SendLeaveRoom()
  92. {
  93. LeaveRoomRequestC2B req = new LeaveRoomRequestC2B();
  94. Session.Send(req);
  95. }
  96. /// <summary>
  97. /// 发送聊天
  98. /// </summary>
  99. public void SendChatMessage(string message, ChatMessageType type = ChatMessageType.PlayerToForce)
  100. {
  101. ChatAction req = new ChatAction();
  102. req.Message = message;
  103. req.To = type;
  104. Session.Send(req);
  105. }
  106. //----------------------------------------------------------------------------------------------------
  107. public override void Update()
  108. {
  109. base.Update();
  110. if (ping_task.Update(Layer.CurrentIntervalMS))
  111. {
  112. send_ping.Begin();
  113. SendAction(send_ping);
  114. }
  115. }
  116. //----------------------------------------------------------------------------------------------------
  117. protected virtual void callback_sessionOpened(INetSession session)
  118. {
  119. this.QueueTask((AbstractBattle client) =>
  120. {
  121. BattleClient bc = client as BattleClient;
  122. log.Info("sessionOpened : " + session);
  123. if (bc.mConnected != null)
  124. {
  125. bc.mConnected.Invoke(bc);
  126. }
  127. EnterRoomRequestC2B req = new EnterRoomRequestC2B();
  128. req.RoomID = bc.room.Room.RoomID;
  129. req.PlayerUUID = bc.room.PlayerUUID;
  130. req.Token = bc.token;
  131. req.TestToken = bc.testToken;
  132. session.Send(req);
  133. });
  134. }
  135. protected virtual void callback_sessionClosed(INetSession session)
  136. {
  137. this.QueueTask((AbstractBattle client) =>
  138. {
  139. BattleClient bc = client as BattleClient;
  140. log.Info("sessionClosed : " + session);
  141. if (bc.mDisconnectd != null)
  142. {
  143. bc.mDisconnectd.Invoke(bc);
  144. }
  145. });
  146. }
  147. protected virtual void callback_messageReceived(INetSession session, object data)
  148. {
  149. // if (data is Pong)
  150. // {
  151. // uint ctime = (uint)CUtils.CurrentTimeMS;;
  152. // Pong pong = data as Pong;
  153. // this.current_ping = (int)(ctime - pong.ClientTimeDayOfMS);
  154. // }
  155. if (data is KickedByServerNotifyB2C)
  156. {
  157. this.kicked = data as KickedByServerNotifyB2C;
  158. }
  159. if (data is EnterRoomResponseB2C)
  160. {
  161. EnterRoomResponseB2C enter = data as EnterRoomResponseB2C;
  162. if (enter.Result == EnterRoomResponseB2C.RESULT_OK)
  163. {
  164. CreateRoomInfoR2B cr = enter.RoomData as CreateRoomInfoR2B;
  165. }
  166. }
  167. this.Layer.ProcessMessage(data as IMessage);
  168. this.QueueTask((AbstractBattle client) =>
  169. {
  170. BattleClient bc = this as BattleClient;
  171. if (bc.mHandleMessage != null)
  172. {
  173. bc.mHandleMessage.Invoke(bc, data as IMessage);
  174. }
  175. });
  176. }
  177. protected virtual void callback_messageSent(INetSession session, object data)
  178. {
  179. }
  180. protected virtual void callback_onError(INetSession session, Exception err)
  181. {
  182. log.Warn("onError : " + err.Message, err);
  183. this.QueueTask((AbstractBattle client) =>
  184. {
  185. BattleClient bc = client as BattleClient;
  186. if (bc.mHandleError != null)
  187. {
  188. bc.mHandleError.Invoke(bc, err);
  189. }
  190. });
  191. }
  192. #region Delegate
  193. public delegate void OnHandleMessage(BattleClient bc, IMessage msg);
  194. public delegate void OnDisconnectd(BattleClient bc);
  195. public delegate void OnConnected(BattleClient bc);
  196. public delegate void OnError(BattleClient bc, Exception err);
  197. public event OnHandleMessage HandleMessage { add { mHandleMessage += value; } remove { mHandleMessage -= value; } }
  198. public event OnDisconnectd Disconnectd { add { mDisconnectd += value; } remove { mDisconnectd -= value; } }
  199. public event OnConnected Connected { add { mConnected += value; } remove { mConnected -= value; } }
  200. public event OnError HandleError { add { mHandleError += value; } remove { mHandleError -= value; } }
  201. private OnHandleMessage mHandleMessage;
  202. private OnDisconnectd mDisconnectd;
  203. private OnConnected mConnected;
  204. private OnError mHandleError;
  205. #endregion
  206. }
  207. }