You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
6.0 KiB

1 month ago
/*
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<T>(long UserID,uint GateServerID,long SessionID, int iMsgID, ref T structMessage)
where T : struct, IStructMessage<T>
{
GateService.SendToGate(UserID
, GameServerUtils.GetApp().GetCluster()
, GateServerID
, SessionID
, iMsgID
, ref structMessage);
}
public void SendToPlayer<T>(PlayerSession playerSession, int iMsgID, ref T structMessage)
where T : struct, IStructMessage<T>
{
GateService.SendToGate(playerSession.UserID
, GameServerUtils.GetApp().GetCluster()
, playerSession.GateServerID
, playerSession.SessionID
, iMsgID
, ref structMessage);
}
public void SendToPlayer<T>(PlayerOnGame player, int iMsgID, ref T structMessage)
where T : struct, IStructMessage<T>
{
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<T>(PlayerSession playerSession, StructPacket packet)
where T : struct, IStructMessage<T>
{
GateService.SendToGate(playerSession.UserID
,GameServerUtils.GetApp().GetCluster()
, playerSession.GateServerID
, playerSession.SessionID
, packet.MsgID
, ref packet.GetMessage<T>());
}
public void SendToPlayer<T>(PlayerOnGame player, StructPacket packet)
where T : struct, IStructMessage<T>
{
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<T>(playerSession, packet);
}
public void SendToPlayerByObjectID<T>(StructPacket packet)
where T : struct, IStructMessage<T>
{
var playerSession = GameServerUtils.GetPlayerTableOp().GetPlayerSessionByUid(packet.ObjectID);
if (playerSession != null)
{
SendToPlayer<T>(playerSession, packet);
}
else
{
TraceLog.Trace("DeskMsgHandler.SendToPlayerByObjectID uid {0} session not in table", packet.ObjectID);
}
}
public static void BroadcastToGate<T>(int realmId, int iMsgID, ref T structMessage)
where T : struct, IStructMessage<T>
{
Dictionary<uint, List<long>> 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);
}
}
}
}