123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System;
- namespace Engine.Http
- {
- /// <summary>接受消息委托</summary>
- public delegate bool HandleHttpMessage(HttpResponse httpResponse);
- /// <summary>http控制器</summary>
- internal class HttpManager
- {
- #region 单利
- private static HttpManager _Instance;
- /// <summary>单利函数</summary>
- public static HttpManager GetInstance()
- {
- if (_Instance == null)
- {
- _Instance = new HttpManager();
- }
- return _Instance;
- }
- #endregion
- public const float ResponseOverTimeLimit = 80000;
- /// <summary>接受到消息事件</summary>
- private HandleHttpMessage onHandleHttpMessage = null;
- /// <summary>请求列表</summary>
- private List<HttpMsg> mMsgList = new List<HttpMsg>();
- /// <summary>是否有消息正在请求中,优化Update函数</summary>
- private bool IsMsgRquestIng = false;
-
- /// <summary>添加接受消息事件</summary>
- public void AddHttpMessageEvent(HandleHttpMessage handleHttpMessage)
- {
- onHandleHttpMessage += handleHttpMessage;
- }
- /// <summary>删除接受消息事件</summary>
- public void DelHttMessageEvent(HandleHttpMessage handleHttpMessage)
- {
- onHandleHttpMessage -= handleHttpMessage;
- }
- /// <summary>发送消息</summary>
- public void SendMessage(HttpRequest httpRequest, Action<HttpResponse> callback = null)
- {
- //获得消息对象
- HttpMsg httpMsg = OnGetHttpMsg();
- //发送请求
- httpMsg.SendMsg(httpRequest, callback);
- //修改等待标记
- IsMsgRquestIng = true;
- }
- /// <summary>得到一个HttpMsg对象</summary>
- private HttpMsg OnGetHttpMsg()
- {
- HttpMsg httpMsg = null;
- int msgCount = mMsgList.Count;
- for (int i = 0; i < msgCount; i++)
- {
- if (!mMsgList[i].IsUseIng)
- {
- httpMsg = mMsgList[i];
- break;
- }
- }
- if (httpMsg==null)
- {
- httpMsg = new HttpMsg();
- mMsgList.Add(httpMsg);
- }
- return httpMsg;
- }
- /// <summary>帧频函数</summary>
- public void OnUpdate(int nTime)
- {
- //不存在消息处理逻辑,跳出,缓存全部的已经收到消息
- if (!IsMsgRquestIng)
- {
- return;
- }
- int msgCount = mMsgList.Count;
- for (int i = 0; i < msgCount; i++)
- {
- if (mMsgList[i].IsUseIng)
- {
- mMsgList[i].OnUpdate(nTime);
- }
- }
- }
- /// <summary>帧频函数</summary>
- public void OnFixedUpdate(int nTime)
- {
- //不存在消息处理逻辑,跳出,缓存全部的已经收到消息
- if (onHandleHttpMessage == null || !IsMsgRquestIng)
- {
- return;
- }
- bool isRequestIng = false;
- int msgCount = mMsgList.Count;
- for (int i = 0; i < msgCount; i++)
- {
- //没用使用中直接跳过
- if (!mMsgList[i].IsUseIng)
- { continue; }
- //执行完成逻辑
- if (mMsgList[i].IsRequestComplete)
- {
- //执行回调
- onHandleHttpMessage(mMsgList[i].mHttpResponse);
- mMsgList[i].SendCallBack();
- //释放消息对象
- mMsgList[i].Reset();
- //修改等待标记
- isRequestIng = true;
- }
- //修改等待标记
- else
- {
- if(mMsgList[i].IsTimeout)
- mMsgList[i].Reset();
- isRequestIng = true;
- }
- }
- //修改消息检查标记
- IsMsgRquestIng = isRequestIng;
- }
- }
- }
|