123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754 |
- using System.Collections.Generic;
- using Engine.Http;
- //using ThoughtWorks.QRCode.Codec;
- using System.Net.NetworkInformation;
- using UnityEngine;
- using System.IO;
- //using System.Drawing;
- using System.Text;
- //using System.Drawing.Imaging;
- using System;
- using LitJson;
- public delegate void HanlderNetException(int command, string msg);
- public delegate void HandlerNetDistributeQRCode(Texture2D texture);
- namespace Engine.Http
- {
- public class HttpPlayer
- {
- public string name;
- public int pid;
- public int machineID;
- public int score;
- public string wechat;
- public string avatarFile;
- public int rank;
- public Dictionary<int, PlayerStage> playerStages = new Dictionary<int, PlayerStage>();
- public HttpPlayer()
- {
- }
- public void SetPlayerData(JsonData data)
- {
- name = data["name"].ToString();
- wechat = data["wechat"].ToString();
- avatarFile = data["avatar_file"].ToString();
- score = System.Convert.ToInt32(data["score"].ToString());
- pid = (int)data["pid"];
- machineID = (int)data["machine_id"];
- }
- public void SetRankData(JsonData data)
- {
- name = data["name"].ToString();
- score = System.Convert.ToInt32(data["score"].ToString());
- if(data["rank"] != null)
- {
- rank = (int)data["rank"];
- }
- }
- public void AddStagePoint(int stageID, int star, int score, string stageTime)
- {
- PlayerStage stage;
- if(!playerStages.ContainsKey(stageID))
- {
- stage = new PlayerStage();
- playerStages.Add(stageID, stage);
- }
- else
- {
- stage = playerStages[stageID];
- }
- stage.score = score;
- stage.star = star;
- stage.stageTime = stageTime;
- }
- }
- public class PlayerStage
- {
- public int star;
- public int score;
- public string stageTime;
- }
- /// <summary>
- /// 网络通信分发类
- ///
- /// add by ch.ji
- ///
- /// </summary>
- public class HttpDistribute : System.Object
- {
- /// <summary>二维码分辨率精度</summary>
- public const int QR_CODE_SCALE = 1;
- #region 单例
- /// <summary>单利对象</summary>
- private static HttpDistribute instance;
- /// <summary>获得单利对象</summary>
- public static HttpDistribute Instance
- {
- get
- {
- if (instance == null)
- {
- return instance = new HttpDistribute();
- }
- return instance;
- }
- }
- #endregion
- #region 网络通讯基本参数配置
- private string mGateWay = "";
- private string mPort = "";
- private string mHost = "";
- private string mHostBMSGame = "";
- private int mGameType = 0;
- private int mMachineID = 0;
- private int mCurrentPID = -1;
- private int mHID = -1;
- #endregion
- #region 私有属性
- /// <summary>是否开启轮询方式获取等待中的玩家</summary>
- private bool IsPollingWaitingPlayers = true;
- /// <summary>轮询获取等待中玩家的周期</summary>
- private int mPWPTimeMax = 10 * 1000;
- /// <summary>心跳检测连接的的时间周期</summary>
- private int mNetTicTimeMax = 10 * 1000;
- /// <summary>网络连接是否已断开</summary>
- private bool isConnecting = false;
- /// <summary>http网络请求管理器引用</summary>
- private HttpManager httpManager = HttpManager.GetInstance();
- /// <summary>HTTP网络请求缓存</summary>
- private Dictionary<string, HttpRequest> httpRequestCache = new Dictionary<string, HttpRequest>();
- /// <summary>等待中的玩家列表</summary>
- private ObjectArray mWaitingPlayers = new ObjectArray(1000);
- /// <summary>排行榜</summary>
- private ObjectArray mRankPlayers = new ObjectArray(20);
- /// <summary>当前正玩游戏的玩家(列表中第一位)</summary>
- private HttpPlayer mCurrentPlayer = new HttpPlayer();
- /// <summary>与服务端通信使用的物理地址</summary>
- private string mMacAddress;
- #endregion
- #region 外部可设置的回调函数
- /// <summary>请求返回值异常回调函数</summary>
- private HanlderNetException mOnNetExceptionHandler;
- /// <summary>获取二维码的外部回调函数</summary>
- private HandlerNetDistributeQRCode mOnQRCodeHandler;
- /// <summary>轮询获取到等待中玩家的外部回调函数</summary>
- private HandleHttpMessage mPollingWaitingPlayers;
- /// <summary>HTTP请求外部回调函数</summary>
- private HandleHttpMessage mHttpMessage;
- #endregion
- /// <summary>
- /// 构造函数
- /// </summary>
- public HttpDistribute()
- {
- }
- /// <summary>
- /// 初始化http网络请求分发器,入口
- /// </summary>
- /// <param name="gateWay"></param>
- /// <param name="port"></param>
- /// <param name="type"></param>
- /// <param name="callback"></param>
- public void Init(string gateWay, int type, int port = -1)
- {
- mGateWay = gateWay;
- mPort = port.ToString();
- mHost = (port == -1) ? mGateWay : mGateWay + mPort;
- mHostBMSGame = mHost + "/bms/game/";
- mGameType = type;
- httpManager.AddHttpMessageEvent(OnNetDistributeSend);
- }
- #region 添加和删除外部回调函数
- public void AddQRCodeHandler(HandlerNetDistributeQRCode handler)
- {
- mOnQRCodeHandler += handler;
- }
- public void RemoveQRCodeHandler(HandlerNetDistributeQRCode handler)
- {
- mOnQRCodeHandler -= handler;
- }
- public void AddWaitingPlayersHanlder(HandleHttpMessage handler)
- {
- mPollingWaitingPlayers += handler;
- }
- public void RemoveWaitingPlayersHanlder(HandleHttpMessage handler)
- {
- mPollingWaitingPlayers -= handler;
- }
- public void AddHttpMessageHandler(HandleHttpMessage handler)
- {
- mHttpMessage += handler;
- }
- public void RemoveHttpMessageHandler(HandleHttpMessage handler)
- {
- mHttpMessage -= handler;
- }
- public void AddNetExceptionHandler(HanlderNetException handler)
- {
- mOnNetExceptionHandler += handler;
- }
- public void RemoveNetExceptionHandler(HanlderNetException handler)
- {
- mOnNetExceptionHandler -= handler;
- }
- #endregion
- private string GetHostBMSGameURL(string serverMethodName)
- {
- StringBuilder url = new StringBuilder(mHostBMSGame);
- url.Append(serverMethodName);
- return url.ToString();
- }
- private bool hasRequestCache(string url)
- {
- return httpRequestCache.ContainsKey(url);
- }
- /// <summary>
- /// 发送获取支付二维码的http请求
- /// </summary>
- /// <param name="callback"></param>
- public void GetPayQRCodeHTTP()
- {
- string url = GetHostBMSGameURL(ServerMethod.SERVER_QRCODE);
- HttpRequest request = CreateRequest(url, (int)HTTPClientCommand.CMD_GET_QRCODE);
- Send(request);
- }
- #region 获取二维码
- /// <summary>
- /// 获取支付二维码的回调函数
- /// </summary>
- /// <param name="result"></param>
- private void OnGetQRCode(HttpResponse result)
- {
- /*mMacAddress = result.ReadJsonData("mac").ToString();
- string QRData = result.ReadJsonData("code").ToString();
- QRCodeEncoder encoder = new QRCodeEncoder();
- encoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//编码方式(注意:BYTE能支持中文,ALPHA_NUMERIC扫描出来的都是数字)
- encoder.QRCodeScale = QR_CODE_SCALE;//大小(值越大生成的二维码图片像素越高)
- encoder.QRCodeVersion = 0;//版本(注意:设置为0主要是防止编码的字符串太长时发生错误)
- encoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;//错误效验、错误更正(有4个等级)
- Bitmap bmp = encoder.Encode(QRData, System.Text.Encoding.UTF8);
- Texture2D texture = CreateQRCodeTexture(bmp);
- if (mOnQRCodeHandler != null)
- mOnQRCodeHandler(texture);
- isConnecting = true;
- IsPollingWaitingPlayers = true;//开始轮询检测等待中的玩家
- PollingWaitingPlayers(null);*/
- }
- /// <summary>
- /// 创建支付二维码
- /// </summary>
- /// <param name="bmp"></param>
- /// <returns></returns>
- /*private Texture2D CreateQRCodeTexture(Bitmap bmp)
- {
- Texture2D texture = new Texture2D(bmp.Width, bmp.Height, TextureFormat.RGBA32, false);
- UnityEngine.Color zeroColor = new UnityEngine.Color(0, 0, 0, 1f);
- for (int i = 0; i < bmp.Width; i++)
- {
- for (int j = 0; j < bmp.Height; j++)
- {
- System.Drawing.Color sysColor = bmp.GetPixel(i, j);
- int r = (int)sysColor.R;
- int g = (int)sysColor.G;
- int b = (int)sysColor.B;
- int a = (int)sysColor.A;
- if (r != 0 || g != 0 || b != 0 || a != 0)
- {
- float fr = (r != 0) ? r / 255f : 0f;
- float fg = (g != 0) ? g / 255f : 0f;
- float fb = (b != 0) ? b / 255f : 0f;
- float fa = (a != 0) ? a / 255f : 0f;
- UnityEngine.Color color = new UnityEngine.Color(fr, fg, fb, fa);
- texture.SetPixel(i, j, color);
- }
- else
- {
- texture.SetPixel(i, j, zeroColor);
- }
- }
- }
- texture.Apply();
- return texture;
- }*/
- #endregion
- /// <summary>
- /// 发送获取等待中的玩家列表的http请求
- /// </summary>
- /// <param name="isForPolling"></param>
- public void GetWaitingPlayersHTTP()
- {
- GetWaitingPlayersHTTP((int)HTTPClientCommand.CMD_GET_WAITING_PLAYERS);
- }
- #region 获取等待中玩家,包括轮询
- /// <summary>
- /// 轮询获取等待中玩家列表
- /// </summary>
- /// <param name="nTime"></param>
- private void GetWaitingPlayersHTTP(int command)
- {
- string url = GetHostBMSGameURL(ServerMethod.GET_PLAYER_LIST);
- HttpRequest request = null;
- if (!hasRequestCache(url))
- {
- request = CreateRequest(url, command);
- httpRequestCache.Add(url, request);
- } else
- {
- request = httpRequestCache[url];
- }
- if (command != request.Code)
- request.Code = command;
- Send(request);
- httpRequestCache[url].ResetRequestParamContent();
- }
- /// <summary>
- /// 轮询中获取到等待的玩家列表时的处理
- /// </summary>
- /// <param name="result"></param>
- private void OnGetWaitingPlayers(HttpResponse result)
- {
- IsPollingWaitingPlayers = false;
- JsonData data = result.ReadJsonData();
- if (data.IsArray)
- {
- HttpPlayer target = null;
- JsonData player = null;
- mWaitingPlayers.SetEmpty();
- for (int i = 0; i < data.Count; i++)
- {
- if (i >= mWaitingPlayers.Length)
- break;
- player = data[i];
- target = new HttpPlayer();
- target.SetPlayerData(player);
- mWaitingPlayers.Add(target);
- }
- target = mWaitingPlayers.Get<HttpPlayer>(0);
- //target.AddStagePoint(player)//TODO 增加关卡数据的保存逻辑
- IsPollingWaitingPlayers = (target == null);
- if (IsPollingWaitingPlayers)
- {
- mCurrentPID = -1;
- mMachineID = -1;
- TimerManager.Instance.WaitCallBack(mPWPTimeMax, PollingWaitingPlayers);
- } else
- {
- mCurrentPID = target.pid;
- mMachineID = target.machineID;
- TimerManager.Instance.WaitCallBack(mNetTicTimeMax, CheckNetStatuTic);
- }
- }
- if (mPollingWaitingPlayers != null)
- {
- mPollingWaitingPlayers(result);
- }
- }
- private void PollingWaitingPlayers(object param)
- {
- if (isConnecting && IsPollingWaitingPlayers)
- {
- GetWaitingPlayersHTTP((int)HTTPClientCommand.CMD_POLLING_WAITING_PLAYERS);
- }
- }
- #endregion
- /// <summary>
- /// 发送开始游戏的http请求
- /// </summary>
- public void StartGameHTTP()
- {
- if (mCurrentPID == -1)
- return;
- string url = GetHostBMSGameURL(ServerMethod.SERVER_START_GAME);
- HttpRequest request = CreateRequest(url, (int)HTTPClientCommand.CMD_START_GAME);
- request.AddParam("pid", mCurrentPID.ToString());
- request.AddParam("machine_id", mMachineID.ToString());
- Send(request);
- }
- /// <summary>
- /// 游戏结束,发送游戏结果的http请求
- /// </summary>
- public void CommitGameHTTP(int stageID, int score, int machineID = -1, int pID = -1, int hId = -1)
- {
- if (hId == -1)
- hId = mHID;
- if (machineID == -1)
- machineID = mMachineID;
- if (pID == -1)
- pID = mCurrentPID;
- if (pID == -1 || machineID == -1 || hId == -1)
- {
- CDebug.Log("Http Distribute: hid、pid、machine_id 可能不正确,游戏数据提交失败!");
- return;
- }
- StringBuilder content = new StringBuilder();
- JsonWriter writer = new JsonWriter(content);
- writer.WriteObjectStart();
- writer.WritePropertyName("game_type");//必备参数
- writer.Write(mGameType.ToString());
- writer.WritePropertyName("machine_id");//必备参数
- writer.Write(machineID.ToString());
- writer.WritePropertyName("pid");//用户id
- writer.Write(pID.ToString());
- writer.WritePropertyName("stage_id");//关卡id
- writer.Write(stageID.ToString());
- writer.WritePropertyName("score");//关卡分数
- writer.Write(score.ToString());
- writer.WritePropertyName("hid");//区分游玩关卡的次数id
- writer.Write(hId.ToString());
- writer.WriteObjectEnd();
- string jsonParams = content.ToString();
- string url = GetHostBMSGameURL(ServerMethod.SERVER_COMMIT_GAME);
- HttpRequest request = CreateRequest(url, (int)HTTPClientCommand.CMD_COMMIT_GAME, HttpMethod.POST);
- request.SetJSONParamContent(jsonParams);
- Send(request, false);
- }
- /// <summary>
- /// 获取玩家排行榜
- /// </summary>
- /// <param name="pID"></param>
- public void GetRankHTTP(int pID = -1)
- {
- if(pID == -1)
- pID = mCurrentPID;
- if (pID == -1)
- return;
- string url = GetHostBMSGameURL(ServerMethod.SERVER_GET_RANK);
- HttpRequest request = CreateRequest(url, (int)HTTPClientCommand.CMD_GET_RANK);
- request.AddParam("pid", pID.ToString());
- Send(request);
- }
- /// <summary>
- /// 发送HTTP网络请求
- /// </summary>
- /// <param name="request"></param>
- /// <param name="applyBaseParams"></param>
- public void Send(HttpRequest request, bool applyBaseParams = true)
- {
- if (applyBaseParams)
- {
- request.AddParam("mac", MacAddress);//添加基本通信参数
- request.AddParam("game_type", GameTypeParam);
- }
- httpManager.SendMessage(request);//发送网络请求
- }
- #region 网络连接心跳检测
- private void CheckNetStatuTic(object param)
- {
- if (isConnecting && !IsPollingWaitingPlayers)
- {
- CheckNetStatuHTTP();
- }
- }
- private void CheckNetStatuHTTP()
- {
- string url = mHostBMSGame;
- HttpRequest request = null;
- if (!hasRequestCache(url))
- {
- request = CreateRequest(url, (int)HTTPClientCommand.CMD_CHECK_NETSTATU);
- httpRequestCache.Add(url, request);
- }else
- {
- request = httpRequestCache[url];
- }
- Send(request);
- httpRequestCache[url].ResetRequestParamContent();
- }
- private void OnCheckNetStatu(HttpResponse responder)
- {
- if (!responder.isSuccess)
- {
- isConnecting = false;
- //TODO 提示网络已断开
- }else
- {
- if(!IsPollingWaitingPlayers)
- {
- TimerManager.Instance.WaitCallBack(mNetTicTimeMax, CheckNetStatuTic);
- }
- }
- }
- #endregion
- /// <summary>
- /// 处理排行榜返回值
- /// </summary>
- /// <param name="result"></param>
- private void OnGetRank(HttpResponse result)
- {
- JsonData list = result.ReadJsonData("top_list");//{"name":"", "avatar_file":"", "pid":"", "wechat":""}
- JsonData playerData = result.ReadJsonData("player_data");//{"name":"", "avatar_file":"", "pid":"", "wechat":"", "rank":""}
- mCurrentPlayer.SetRankData(playerData);
- mRankPlayers.SetEmpty();
- if (list.IsArray)
- {
- JsonData target = null;
- HttpPlayer player = null;
- for (int i = 0; i < list.Count; i++)
- {
- if (i >= mRankPlayers.Length)
- break;
- target = list[i];
- player = new HttpPlayer();
- player.SetRankData(target);
- mRankPlayers.Add(player);
- }
- CDebug.Log ("排行榜信息拿到");
- //MessageCenter.Instance.Broadcast(GameEnum.MSG_GET_RANK_INFO_CALLBACK);
- }
- }
- #region 每帧检测的内容
- public void OnUpdate(int nTime)
- {
- if (httpManager != null)
- httpManager.OnUpdate(nTime);
- }
- public void OnFixedUpdate(int nTime)
- {
- if (httpManager != null)
- httpManager.OnFixedUpdate(nTime);
- }
- #endregion
- #region 统一发送、响应请求
- private HttpRequest CreateRequest(string url, int command, string method = HttpMethod.GET)
- {
- HttpRequest result = HttpRequest.OnCreateRequest();//构建网络请求对象
- result.URL = url;
- result.Code = command;
- result.httpMethod = method;
- //request.requestTimeout = OnNetDistributeRequestTimeOut;//请求超时的回调函数
- return result;
- }
- /// <summary>
- /// 统一处理网络请求返回值
- /// </summary>
- /// <param name="result"></param>
- /// <returns></returns>
- private bool OnNetDistributeSend(HttpResponse result)
- {
- bool isSuccess = false;
- if (result != null && result.isSuccess)
- {
- isSuccess = true;
- int command = result.Code;
- switch (result.Code)
- {
- case (int)HTTPClientCommand.CMD_GET_QRCODE:
- OnGetQRCode(result);
- break;
- case (int)HTTPClientCommand.CMD_START_GAME:
- mHID = (int)result.ReadJsonData("hid");
- break;
- case (int)HTTPClientCommand.CMD_POLLING_WAITING_PLAYERS:
- OnGetWaitingPlayers(result);
- break;
- case (int)HTTPClientCommand.CMD_CHECK_NETSTATU:
- OnCheckNetStatu(result);
- break;
- case (int)HTTPClientCommand.CMD_GET_RANK:
- OnGetRank(result);
- break;
- case (int)HTTPClientCommand.CMD_COMMIT_GAME:
- GetWaitingPlayersHTTP((int)HTTPClientCommand.CMD_POLLING_WAITING_PLAYERS);//提交完分数后再获取一次玩家的等待列表
- return true;
- default:
- break;
- }
- if (mHttpMessage != null)
- mHttpMessage(result);//网络请求结果分发
- }
- else
- {
- isSuccess = false;
- if(result != null)
- {
- if (result.ResponseMsg != null)
- {
- //TODO 异常消息推送
- CDebug.Log("请求发生异常!=========================================");
- if (mOnNetExceptionHandler != null)
- mOnNetExceptionHandler(result.Code, result.ResponseMsg);
- }
- }else
- {
- CDebug.Log("处理返回值对象为NULL!");
- }
- }
- return isSuccess;
- }
- public void OnNetDistributeRequestTimeOut(HttpRequest request)
- {
- }
- #endregion
- #region 属性 getter/setter
- public string MacAddress
- {
- get
- {
- if(mMacAddress == null)
- {
- NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();
- ObjectArray list = new ObjectArray(nis.Length);
- StringBuilder result = new StringBuilder("");
- string str;
- int i = 0;
- int max = nis.Length;
- while (i < max)
- {
- str = nis[i].GetPhysicalAddress().ToString();
- if (list.IndexOf(str) == -1 && str != "" && str != "00000000000000E0")
- {
- list.Add(str);
- result.Append(str);
- result.Append(",");
- }
- i++;
- }
- result.Remove(result.Length - 1, 1);
- mMacAddress = result.ToString();
- }
- return mMacAddress;
- }
- }
- public string GameTypeParam
- {
- get
- {
- return mGameType.ToString();
- }
- }
- public string Host
- {
- get
- {
- return mHost;
- }
- }
- public int pollingWaitingPlayerTimeMax
- {
- get
- {
- return mPWPTimeMax;
- }
- set
- {
- mPWPTimeMax = value;
- }
- }
- public int netTicTimeMax
- {
- get
- {
- return mNetTicTimeMax;
- }
- set
- {
- mNetTicTimeMax = value;
- }
- }
- public ObjectArray waitingPlayers
- {
- get
- {
- return mWaitingPlayers;
- }
- }
- public ObjectArray rankPlayers
- {
- get
- {
- return mRankPlayers;
- }
- }
- public int hID
- {
- get
- {
- return mHID;
- }
- }
- #endregion
- }
- }
|