MsgController.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using XGame.Framework;
  2. namespace XGame.Framework.Network
  3. {
  4. public enum MsgCodeDefine
  5. {
  6. SUCCESS_CODE = 0,
  7. UNIFIED_ERROR_CODE = 1,
  8. }
  9. /// <summary>
  10. /// 网络消息控制器基类
  11. /// </summary>
  12. /// <typeparam name="T">消息类型</typeparam>
  13. public abstract class MsgController<T> : IMsgController where T : IMsgData
  14. {
  15. #if FINAL_RELEASE
  16. public void Process(IMessage msg, IMsgReceiverListener listener)
  17. #else
  18. void IMsgController.Process(IMessage msg, IMsgReceiverListener listener)
  19. #endif
  20. {
  21. if (msg is IMsgPush)
  22. {
  23. OnProcess((T)msg, msg.Context);
  24. }
  25. else if (msg is IMsgResponse resp)
  26. {
  27. var code = resp.GetCode();
  28. if (code == (int)MsgCodeDefine.SUCCESS_CODE)
  29. {
  30. OnProcess((T)msg, msg.Context);
  31. }
  32. else if (this is IMsgFailProcess<T> failProcess)
  33. {
  34. failProcess.FailProcess((T)msg, msg.Context);
  35. }
  36. var args = new ResponseEventArgs()
  37. {
  38. errorCode = code,
  39. info = resp.GetInfo(),
  40. seqId = resp.InstanceID,
  41. protoId = resp.ProtocolID,
  42. };
  43. listener.OnResponseFinish(args);
  44. }
  45. else
  46. {
  47. Log.Error($"[MsgController] 消息类型错误,Type:{msg.GetType()}");
  48. }
  49. }
  50. protected abstract void OnProcess(T message, object context);
  51. }
  52. }