OneFrameInputsHandler.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. namespace ET.Client
  3. {
  4. [MessageHandler(SceneType.LockStep)]
  5. public class OneFrameInputsHandler: AMHandler<OneFrameInputs>
  6. {
  7. protected override async ETTask Run(Session session, OneFrameInputs input)
  8. {
  9. Room room = session.DomainScene().GetComponent<Room>();
  10. Log.Debug($"OneFrameInputs: {room.AuthorityFrame + 1} {input.ToJson()}");
  11. FrameBuffer frameBuffer = room.FrameBuffer;
  12. ++room.AuthorityFrame;
  13. // 服务端返回的消息比预测的还早
  14. if (room.AuthorityFrame > room.PredictionFrame)
  15. {
  16. OneFrameInputs authorityFrame = frameBuffer.FrameInputs(room.AuthorityFrame);
  17. input.CopyTo(authorityFrame);
  18. }
  19. else
  20. {
  21. // 服务端返回来的消息,跟预测消息对比
  22. OneFrameInputs predictionInput = frameBuffer.FrameInputs(room.AuthorityFrame);
  23. // 对比失败有两种可能,
  24. // 1是别人的输入预测失败,这种很正常,
  25. // 2 自己的输入对比失败,这种情况是自己发送的消息比服务器晚到了,服务器使用了你的上一次输入
  26. // 回滚重新预测的时候,自己的输入不用变化
  27. if (input != predictionInput)
  28. {
  29. input.CopyTo(predictionInput);
  30. // 回滚到frameBuffer.AuthorityFrame
  31. LSClientHelper.Rollback(room, room.AuthorityFrame);
  32. }
  33. else
  34. {
  35. room.SendHash(room.AuthorityFrame);
  36. }
  37. }
  38. // 回收消息,减少GC
  39. NetServices.Instance.RecycleMessage(input);
  40. await ETTask.CompletedTask;
  41. }
  42. }
  43. }