//完成请求验证 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); } }
private void OnWriteBufferCallback(UdpPoint point, IPEndPoint remote, WriteBufferHandle handle) { if (handle.data != null && handle.data.Length > 0) { //Debug.Log("写入=>" + handle.write + "[数据大小]" + handle.data.Length); WriteBufferDic[handle.buffer] = handle.data; } else { //Debug.Log("写入=>" + handle.write + "[没有数据]" + "null"); } FinishRequest(point, handle); }
private void OnReadBufferCallback(UdpPoint point, IPEndPoint remote, ReadBufferHandle handle) { byte[] value; if (!ReadBufferDic.TryGetValue(handle.buffer, out value)) { value = new byte[0]; //Debug.Log("读取=>" + handle.buffer +"[没有数据]"+"null"); } else { //Debug.Log("读取=>" + handle.buffer + "[数据大小]"+value.Length); } point.BunchTaskAdd(remote, WriteBufferHandle.Instance(handle.buffer, handle.verify, value)); //point.ReplyCollect(WriteBufferHandle.Instance(handle.buffer, handle.verify, value).ToBytes()); }
//接收回调函数,执行尽量简短的命令,应该使用一个队列来处理,这里只进行收集工作 private void ReceiveCallback(IAsyncResult iar) { byte[] buffer = null; IPEndPoint remote; UdpPoint point = iar.AsyncState as UdpPoint; if (iar.IsCompleted) { remote = null; try { buffer = point.Client.EndReceive(iar, ref remote); } catch (Exception ex) { Console.WriteLine(ex); } finally { //重新开始接收 if (!point.isClose) { //Debug.Log("[监听]" + point.LocalEndPoint + "[来源]" + remote + "[时刻]"+TimeMgr.TickTime); //point.Iar = point.Client.BeginReceive(ReceiveCallback, point); //加入请求队列 if (buffer != null) { ReceiveQueue.Enqueue(new UdpQueueStruct(buffer, point, remote)); } //HandleManager.OutStr += "接收数据" + remote + "长度"+buffer.Length+"\n"; Client.BeginReceive(ReceiveCallback, this); } else { point.Iar = null; //Debug.Log("结束监听" + point.Client.Client.LocalEndPoint); } } return; } }
private void OnReplyCmdCallback(UdpPoint point, IPEndPoint remote, ReplyCmdHandle handle) { FinishRequest(point, handle); }
private void OnReplyDataCallback(UdpPoint point, IPEndPoint remote, ReplyDataHandle handle) { //Debug.Log("[完成回复]"+ handle.verify); FinishRequest(point, handle); }
public HandleManager(UdpPoint point) { #region 初始化 this.point = point; ReadBufferDic = new Dictionary <BufferEnum, byte[]>(); WriteBufferDic = new Dictionary <BufferEnum, byte[]>(); HandleLengthMapDic = new Dictionary <HandleEnum, int>(); HandleDynamicMapDic = new Dictionary <HandleEnum, bool>(); HandleRequestMapDic = new Dictionary <HandleEnum, bool>(); HandleToByteMapDic = new Dictionary <HandleEnum, byte[]>(); BufferToByteMapDic = new Dictionary <BufferEnum, byte[]>(); RequestToByteMapDic = new Dictionary <RequestEnum, byte[]>(); EnumHandleMapDic = new Dictionary <int, HandleEnum>(); EnumBufferMapDic = new Dictionary <int, BufferEnum>(); EnumRequestMapDic = new Dictionary <int, RequestEnum>(); foreach (var item in Enum.GetValues(typeof(HandleEnum)) as HandleEnum[]) { EnumHandleMapDic[(int)item] = item; HandleToByteMapDic[item] = new byte[4] { (byte)((int)item), (byte)((int)item >> 8), (byte)((int)item >> 16), (byte)((int)item >> 24) }; } foreach (var item in Enum.GetValues(typeof(RequestEnum)) as RequestEnum[]) { EnumRequestMapDic[(int)item] = item; RequestToByteMapDic[item] = new byte[4] { (byte)((int)item), (byte)((int)item >> 8), (byte)((int)item >> 16), (byte)((int)item >> 24) }; } foreach (var item in Enum.GetValues(typeof(BufferEnum)) as BufferEnum[]) { EnumBufferMapDic[(int)item] = item; BufferToByteMapDic[item] = new byte[4] { (byte)((int)item), (byte)((int)item >> 8), (byte)((int)item >> 16), (byte)((int)item >> 24) }; } #endregion #region 功能实现 OnReadBufferHandle += OnReadBufferCallback; OnWriteBufferHandle += OnWriteBufferCallback; OnReplyDataHandle += OnReplyDataCallback; OnReplyCmdHandle += OnReplyCmdCallback; #endregion Bind <ConnectHandle>(); Bind <MessageHandle>(); Bind <ReadBufferHandle>(); Bind <WriteBufferHandle>(); Bind <CmdDataHandle>(); Bind <RequestDataHandle>(); Bind <ReplyDataHandle>(); Bind <RequestCmdHandle>(); Bind <ReplyCmdHandle>(); Bind <InformHandle>(); }
public UdpQueueStruct(byte[] buffer, UdpPoint point, IPEndPoint remote) { this.buffer = buffer; this.point = point; this.remote = remote; }