HttpManager.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. namespace Engine.Http
  6. {
  7. /// <summary>接受消息委托</summary>
  8. public delegate bool HandleHttpMessage(HttpResponse httpResponse);
  9. /// <summary>http控制器</summary>
  10. internal class HttpManager
  11. {
  12. #region 单利
  13. private static HttpManager _Instance;
  14. /// <summary>单利函数</summary>
  15. public static HttpManager GetInstance()
  16. {
  17. if (_Instance == null)
  18. {
  19. _Instance = new HttpManager();
  20. }
  21. return _Instance;
  22. }
  23. #endregion
  24. public const float ResponseOverTimeLimit = 80000;
  25. /// <summary>接受到消息事件</summary>
  26. private HandleHttpMessage onHandleHttpMessage = null;
  27. /// <summary>请求列表</summary>
  28. private List<HttpMsg> mMsgList = new List<HttpMsg>();
  29. /// <summary>是否有消息正在请求中,优化Update函数</summary>
  30. private bool IsMsgRquestIng = false;
  31. /// <summary>添加接受消息事件</summary>
  32. public void AddHttpMessageEvent(HandleHttpMessage handleHttpMessage)
  33. {
  34. onHandleHttpMessage += handleHttpMessage;
  35. }
  36. /// <summary>删除接受消息事件</summary>
  37. public void DelHttMessageEvent(HandleHttpMessage handleHttpMessage)
  38. {
  39. onHandleHttpMessage -= handleHttpMessage;
  40. }
  41. /// <summary>发送消息</summary>
  42. public void SendMessage(HttpRequest httpRequest, Action<HttpResponse> callback = null)
  43. {
  44. //获得消息对象
  45. HttpMsg httpMsg = OnGetHttpMsg();
  46. //发送请求
  47. httpMsg.SendMsg(httpRequest, callback);
  48. //修改等待标记
  49. IsMsgRquestIng = true;
  50. }
  51. /// <summary>得到一个HttpMsg对象</summary>
  52. private HttpMsg OnGetHttpMsg()
  53. {
  54. HttpMsg httpMsg = null;
  55. int msgCount = mMsgList.Count;
  56. for (int i = 0; i < msgCount; i++)
  57. {
  58. if (!mMsgList[i].IsUseIng)
  59. {
  60. httpMsg = mMsgList[i];
  61. break;
  62. }
  63. }
  64. if (httpMsg==null)
  65. {
  66. httpMsg = new HttpMsg();
  67. mMsgList.Add(httpMsg);
  68. }
  69. return httpMsg;
  70. }
  71. /// <summary>帧频函数</summary>
  72. public void OnUpdate(int nTime)
  73. {
  74. //不存在消息处理逻辑,跳出,缓存全部的已经收到消息
  75. if (!IsMsgRquestIng)
  76. {
  77. return;
  78. }
  79. int msgCount = mMsgList.Count;
  80. for (int i = 0; i < msgCount; i++)
  81. {
  82. if (mMsgList[i].IsUseIng)
  83. {
  84. mMsgList[i].OnUpdate(nTime);
  85. }
  86. }
  87. }
  88. /// <summary>帧频函数</summary>
  89. public void OnFixedUpdate(int nTime)
  90. {
  91. //不存在消息处理逻辑,跳出,缓存全部的已经收到消息
  92. if (onHandleHttpMessage == null || !IsMsgRquestIng)
  93. {
  94. return;
  95. }
  96. bool isRequestIng = false;
  97. int msgCount = mMsgList.Count;
  98. for (int i = 0; i < msgCount; i++)
  99. {
  100. //没用使用中直接跳过
  101. if (!mMsgList[i].IsUseIng)
  102. { continue; }
  103. //执行完成逻辑
  104. if (mMsgList[i].IsRequestComplete)
  105. {
  106. //执行回调
  107. onHandleHttpMessage(mMsgList[i].mHttpResponse);
  108. mMsgList[i].SendCallBack();
  109. //释放消息对象
  110. mMsgList[i].Reset();
  111. //修改等待标记
  112. isRequestIng = true;
  113. }
  114. //修改等待标记
  115. else
  116. {
  117. if(mMsgList[i].IsTimeout)
  118. mMsgList[i].Reset();
  119. isRequestIng = true;
  120. }
  121. }
  122. //修改消息检查标记
  123. IsMsgRquestIng = isRequestIng;
  124. }
  125. }
  126. }