BaseBattleClient.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using CommonAI.Zone;
  2. using CommonAI.ZoneClient;
  3. using CommonAIClient.Client;
  4. using CommonLang;
  5. using CommonLang.Protocol;
  6. using RoomService.Net.BsServer;
  7. using System;
  8. using CommonLang.Log;
  9. namespace Pomelo.DotNetClient.NetClient
  10. {
  11. /// <summary>
  12. /// 战斗服和客户端中间代理层,通信接口
  13. /// </summary>
  14. public class BaseBattleClient : AbstractBattle, IDisposable
  15. {
  16. protected bool BattleOk = false;
  17. protected readonly Logger log;
  18. protected readonly INetClient mClient;
  19. private Ping send_ping = new Ping();
  20. private TimeInterval<int> ping_task = new TimeInterval<int>(2000);
  21. //private int mPing = 0;
  22. private long mRecvPak = 0;
  23. private long mSentPak = 0;
  24. //-----------------------------------------------------------------------------------
  25. public override bool IsNet { get { return true; } }
  26. public override KickedByServerNotifyB2C KickMessage { get { return null; } }
  27. public override int CurrentPing { get { return Layer.CurrentPing; } }
  28. public override long RecvPackages { get { return mRecvPak; } }
  29. public override long SendPackages { get { return mSentPak; } }
  30. public int PingIntervalMS
  31. {
  32. get
  33. {
  34. return ping_task.IntervalTimeMS;
  35. }
  36. set
  37. {
  38. ping_task = new TimeInterval<int>(Math.Max(1000, value));
  39. }
  40. }
  41. //-----------------------------------------------------------------------------------
  42. public BaseBattleClient(INetClient client)
  43. : base(client.DataRoot)
  44. {
  45. this.log = LoggerFactory.GetLogger(GetType().Name);
  46. this.mClient = client;
  47. this.Layer.ActorSyncMode = SyncMode.MoveByClient_PreSkillByClient;
  48. }
  49. protected override void Disposing()
  50. {
  51. mHandleMessage = null;
  52. base.Disposing();
  53. }
  54. //-----------------------------------------------------------------------------------
  55. #region _PomeloNetwork_
  56. /// <summary>
  57. /// 发送战斗协议到服务器
  58. /// </summary>
  59. /// <param name="action"></param>
  60. public override void SendAction(CommonAI.Zone.Action action)
  61. {
  62. if(BattleOk)
  63. {
  64. this.mClient.SendBattleMessage(action);
  65. this.mSentPak++;
  66. }
  67. }
  68. public override void BattleReady(bool bok)
  69. {
  70. BattleOk = bok;
  71. base.BattleReady(bok);
  72. }
  73. public virtual void OnReceivedBattleMessage(IMessage data)
  74. {
  75. this.Layer.ProcessMessage(data);
  76. this.mRecvPak++;
  77. //this.QueueTask((bc) =>
  78. //{
  79. // if (this.mHandleMessage != null)
  80. // {
  81. // this.mHandleMessage.Invoke(this, data);
  82. // }
  83. //});
  84. if (this.mHandleMessage != null)
  85. {
  86. this.mHandleMessage.Invoke(this, data);
  87. }
  88. }
  89. #endregion
  90. //-----------------------------------------------------------------------------------
  91. #region _MainThread_
  92. public override void BeginUpdate(int intervalMS)
  93. {
  94. base.BeginUpdate(intervalMS);
  95. if (ping_task.Update(Layer.CurrentIntervalMS))
  96. {
  97. send_ping.Begin();
  98. Layer.SendAction(send_ping);
  99. }
  100. }
  101. public override void Update()
  102. {
  103. base.Update();
  104. }
  105. #endregion
  106. //-----------------------------------------------------------------------------------
  107. #region _Delegate_
  108. public delegate void OnHandleMessage(BaseBattleClient bc, IMessage msg);
  109. private OnHandleMessage mHandleMessage;
  110. public event OnHandleMessage HandleMessage { add { mHandleMessage += value; } remove { mHandleMessage -= value; } }
  111. #endregion
  112. //-----------------------------------------------------------------------------------
  113. }
  114. }