using Org.BouncyCastle.Ocsp; using ProtoCSStruct; using Sog; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Game { public class PlayerMsgCache : IPlayerMsgCache { private Dictionary> cache_SendMsg = new Dictionary>(); private void delete(Dictionary dict, int msgSeq) { List keys = new List(); foreach (var kv in dict) { if (kv.Key < msgSeq) { keys.Add(kv.Key); } } foreach (var key in keys) { dict.Remove(key); } } public override void clear() { cache_SendMsg.Clear(); } public override bool CheckRepeateMsg(PlayerOnGame player, StructPacket packet) { var msgId = packet.Header.Type; if (cache_SendMsg.TryGetValue(msgId, out var dict)) { switch (msgId) { case (int)CSGameMsgID.CbFinishReq: { ref var req = ref packet.GetMessage(); if (dict.TryGetValue(req.MsgSeq, out MsgCache msg)) { CSCBFinishLayerRes res = new CSCBFinishLayerRes(); msg.ParseFrom(ref res); player.SendToClient((int)CSGameMsgID.CbFinishRes, ref res); return true; } //删除小于msgseq的数据 delete(dict, req.MsgSeq); } break; case (int)CSGameMsgID.CbLeaveReq: { ref var req = ref packet.GetMessage(); if (dict.TryGetValue(req.MsgSeq, out var msg)) { CSCBLeaveRes res = new CSCBLeaveRes(); msg.ParseFrom(ref res); player.SendToClient((int)CSGameMsgID.CbLeaveRes, ref res); return true; } //删除小于msgseq的数据 delete(dict, req.MsgSeq); } break; } } return false; } public override void AddSendMsg(PlayerOnGame player, StructPacket packetReq, MsgCache msg) { var msgId = packetReq.Header.Type; if (cache_SendMsg.TryGetValue(msgId, out var dict)) { switch (msgId) { case (int)CSGameMsgID.CbFinishReq: { ref var req = ref packetReq.GetMessage(); dict[req.MsgSeq] = msg; } break; case (int)CSGameMsgID.CbLeaveReq: { ref var req = ref packetReq.GetMessage(); dict[req.MsgSeq] = msg; } break; } } else { dict = new Dictionary(); switch (msgId) { case (int)CSGameMsgID.CbFinishReq: { ref var req = ref packetReq.GetMessage(); dict[req.MsgSeq] = msg; } break; case (int)CSGameMsgID.CbLeaveReq: { ref var req = ref packetReq.GetMessage(); dict[req.MsgSeq] = msg; } break; } cache_SendMsg.Add(msgId, dict); } } } }