/* Sog 游戏基础库 2016 by zouwei */ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Sog; using Sog.Gate; using ProtoCSStruct; namespace Game { // 实现 SendToPlayer public abstract class BasePacketHandler : IPacketHandler, IReloadableService { protected uint m_serviceSeqID; public uint GetServiceSeqID() { return m_serviceSeqID; } public abstract int GetServiceType(); public abstract void Dispose(); protected BasePacketHandler() { m_serviceSeqID = ServiceMgr.Instance.GenNextSeqID(); } public abstract void HandlerClientPacket(PlayerSession playerSession, StructPacket packet); //处理其他服务器发过来的消息 public abstract void HandlerServerPacket(uint serverID, StructPacket packet); public void SendToPlayer(long UserID,uint GateServerID,long SessionID, int iMsgID, ref T structMessage) where T : struct, IStructMessage { GateService.SendToGate(UserID , GameServerUtils.GetApp().GetCluster() , GateServerID , SessionID , iMsgID , ref structMessage); } public void SendToPlayer(PlayerSession playerSession, int iMsgID, ref T structMessage) where T : struct, IStructMessage { GateService.SendToGate(playerSession.UserID , GameServerUtils.GetApp().GetCluster() , playerSession.GateServerID , playerSession.SessionID , iMsgID , ref structMessage); } public void SendToPlayer(PlayerOnGame player, int iMsgID, ref T structMessage) where T : struct, IStructMessage { if(player == null) { TraceLog.Error("BasePacketHandler.SendToPlayer msgID {0}, player not online", iMsgID); return; } if(player.SessionID == 0) { player.Trace("BasePacketHandler.SendToPlayer uid {0} session is 0, player not online", player.UserID); return; } PlayerSession playerSession = GameServerUtils.GetPlayerTableOp().GetPlayerSession(player.SessionID); if (playerSession == null) { player.Error("BasePacketHandler.SendToPlayer uid {0} session {1} can not find session!", player.UserID, player.SessionID); return; } SendToPlayer(playerSession, iMsgID, ref structMessage); } public void SendToPlayer(PlayerSession playerSession, StructPacket packet) where T : struct, IStructMessage { GateService.SendToGate(playerSession.UserID ,GameServerUtils.GetApp().GetCluster() , playerSession.GateServerID , playerSession.SessionID , packet.MsgID , ref packet.GetMessage()); } public void SendToPlayer(PlayerOnGame player, StructPacket packet) where T : struct, IStructMessage { if (player == null) { TraceLog.Error("BasePacketHandler.SendToPlayer msgID {0}, player not online", packet.MsgID); return; } if (player.SessionID == 0) { player.Trace("BasePacketHandler.SendToPlayer uid {0} session is 0, player not online", player.UserID); return; } PlayerSession playerSession = GameServerUtils.GetPlayerTableOp().GetPlayerSession(player.SessionID); if (playerSession == null) { player.Error("BasePacketHandler.SendToPlayer uid {0} session {1} can not find session!", player.UserID, player.SessionID); return; } SendToPlayer(playerSession, packet); } public void SendToPlayerByObjectID(StructPacket packet) where T : struct, IStructMessage { var playerSession = GameServerUtils.GetPlayerTableOp().GetPlayerSessionByUid(packet.ObjectID); if (playerSession != null) { SendToPlayer(playerSession, packet); } else { TraceLog.Trace("DeskMsgHandler.SendToPlayerByObjectID uid {0} session not in table", packet.ObjectID); } } public static void BroadcastToGate(int realmId, int iMsgID, ref T structMessage) where T : struct, IStructMessage { Dictionary> gateSessionMap; if(realmId > 0) { gateSessionMap = GameServerUtils.GetPlayerTableOp().GetSameRealmIDPlayer(realmId); } else { gateSessionMap = GameServerUtils.GetPlayerTableOp().GetAllPlayer(); } if (gateSessionMap == null || gateSessionMap.Count == 0) { return; } foreach(var item in gateSessionMap) { var gateId = item.Key; var sessionIDList = item.Value; //广播给所有的玩家 while (sessionIDList.Count > GateMsgDefine.TransMultiSessionCountMax) { GateService.BroadcastToGate(0, GameServerUtils.GetApp().GetCluster(), gateId, sessionIDList.Take(GateMsgDefine.TransMultiSessionCountMax).ToList() , iMsgID, ref structMessage); sessionIDList.RemoveRange(0, GateMsgDefine.TransMultiSessionCountMax); } GateService.BroadcastToGate(0, GameServerUtils.GetApp().GetCluster(), gateId, sessionIDList , iMsgID, ref structMessage); } } } }