//完成请求验证 public static void FinishRequest(UdpPoint point, ReplyHandle handle) { Request request; //尝试移除验证值,如果不存在或者处于竞争状态则抛出警告 if (point.RequestDic.TryRemove(handle.verify, out request)) { //每次成功收到回复都会修正网络状态 Netstat netstat = point.NetstatDic[request.remote]; netstat.timelast = TickTime; netstat.delay = TickTime - request.timeSend; netstat.totalDelay += netstat.delay; //Console.WriteLine("[验证成功]" + point.LocalEndPoint + "[验证值]" + handle.verify + "[本次延迟]" + netstat.delay); request.succeed(request, handle.data); //OutStr += "[验证成功]" + point.LocalEndPoint + "[验证值]" + handle.verify + "[本次延迟]" + netstat.delay +"\n"; } else { string str = "[验证总数]" + point.RequestDic.Count + "[详细]"; foreach (var item in point.RequestDic.Keys) { str += "<" + item + ">"; } str += "[不包含]" + "<" + handle.verify + ">"; //OutStr += str + "\n"; //Console.WriteLine(str); } }
/// <summary> /// 复合指令集束添加请求 /// </summary> public void BunchRequestAdd(RequestHandle handle, IPEndPoint remote, Action <Request, byte[]> succeed, Action <Request> failed) { //int time = TimeMgr.TickTime; //新建请求 Request request = new Request(handle, remote, succeed, failed); request.isSend = false; request.overtime = 1000 / FRAME; request.timeSend = TickTime; request.timeStart = TickTime; request.verify = handle.verify; //添加请求列表 if (RequestDic.TryAdd(handle.verify, request)) { Netstat netstat = NetstatDic.GetOrAdd(remote, new Netstat()); netstat.request += 1; //添加集束任务 BunchTaskAdd(remote, request); //Debug.Log("[注册验证]" + request.verify + "[验证总数]" + RequestDic.Count); } else { #if Debug Loger.LogError("[注册失败]" + handle.verify); #endif } //time = TimeMgr.TickTime - time; //Debug.Log("耗时" + time); }
/// <summary> /// 单独请求服务端数据,成功或失败都会回调函数 /// </summary> public void SingleRequestData(RequestEnum requests, IPEndPoint remote, Action <Request, byte[]> succeed, Action <Request> failed, byte[] data = null) { RequestDataHandle handle = RequestDataHandle.Instance(requests, RandVerify, data); Request request = new Request(handle, remote, succeed, failed); request.isSend = false; request.overtime = 1000 / FRAME; request.timeSend = TickTime; request.timeStart = TickTime; request.verify = handle.verify; //添加请求列表 if (RequestDic.TryAdd(handle.verify, request)) { Netstat netstat = NetstatDic.GetOrAdd(remote, new Netstat()); netstat.request += 1; //注册完成后直接发送 SendTo(handle.ToBytes(), remote, request); //Debug.Log("[注册验证]" + request.verify + "[验证总数]" + RequestDic.Count); } //Console.WriteLine("单独发送数据请求" + remote + "[RequestDataHandle]"); //HandleManager.OutStr += "单独发送"+handle.verify+"数据请求至" + remote + "[RequestDataHandle]" + "\n"; }
private void Run() { /*队列处理*/ int work; int queue; //获取所有接收队列 UdpQueueStruct udpQueue; while (ReceiveQueue.TryDequeue(out udpQueue)) { //将队列中的数据取出放入线程池内处理 ThreadPool.QueueUserWorkItem(new WaitCallback(QueueCallback), udpQueue); ThreadPool.GetAvailableThreads(out work, out queue); if (work >= 10) { break; } } /*队列处理*/ foreach (var item in RequestDic) { //Request item.Value = item.Value; //对于还未发送成功的请求直接跳过,除非超过1秒还未发送 if (!item.Value.isSend && item.Value.overtime > 0) { item.Value.overtime -= 1; continue; } Netstat netstat = NetstatDic[item.Value.remote]; int wait = (int)((3 * netstat.AverageDelay + 50 * item.Value.retry + 100) * (1f + (float)item.Value.retry * 0.5f)) - 0; //Debug.Log("@"+request.GetHashCode()+"等待时间" + wait + "延迟" + netstat.AverageDelay + "重发" + request.retry); //基础等待时间等于延迟的两倍 //每次重连都会延长一半等待时间 if (TickTime - item.Value.timeStart < OVERTIME) { if (TickTime - item.Value.timeSend > wait) { //continue; //丢包重发机制 item.Value.retry += 1; //数据包重发时间 item.Value.timeSend = TickTime;; BunchTaskAdd(item.Value.remote, item.Value.handle); //List<byte> buffer = RetryDic.GetOrAdd(item.Value.remote, new List<byte>()); //buffer.AddRange(item.Value.value); //#if Debug // Loger.LogError("[重发]"+item.Value.verify + "[等待超时]" + wait); //#endif } else { //Debug.Log("等待时间" + wait + "延迟" + netstat.AverageDelay+ "重发"+ request.retry); } } else { Request request; if (RequestDic.TryRemove(item.Value.verify, out request)) { //增加一次丢包 netstat.loss += 1; //发布请求失败事件 Mgr.DisposeRequestFailure(item.Value); request.failed(item.Value); //#if Debug // Loger.LogError("[数据丢包]" + item.Value.verify); //#endif } else { //#if Debug // Loger.LogError("[临界回复]"+item.Value.verify); //#endif } } } //定时合并发送 BunchSend(); if (RequestDic.Count > 0) { ThreadOvertime += 1; } else { ThreadOvertime = 0; } if (ThreadOvertime >= 1000 / FRAME) { ThreadOvertime = 0; //#if Debug // Loger.LogError("[未完成的请求]" + RequestDic.Count); //#endif } }