using System; using System.Collections.Generic; using System.Collections.Concurrent; namespace Sog { /// /// 消息发送器,初始化的时候可决定是否支持线程安全 /// public class PacketSender { private uint m_serverID; // private uint m_accountServerID; // 暂时没发现使用的地方,修改主备后这里不建议使用 private uint m_worldServerID; //private uint m_httpProxyServerID; //private uint m_gameServerID; //private uint m_battleServerID; //private uint m_friendServerID; private Cluster m_cluster; public PacketSender() { } /// /// 初始化,支持线程安全可选项,默认不支持多线程 /// /// /// /// 是否线程安全,如果打开需要注意,参考m_threadSafeSendQueue的说明 public void Init(uint serverID, Cluster cluster) { m_cluster = cluster; m_serverID = serverID; //m_accountServerID = ServerIDUtils.GetLevel0ServerIDByType((int)ServerType.Account, 1); m_worldServerID = ServerIDUtils.GetLevel1ServerIDByType(m_serverID, (int)ServerType.World, 1); //m_httpProxyServerID = ServerIDUtils.GetLevel0ServerIDByType((int)ServerType.HttpProxy, 1); // 默认第一个gamesvr // m_gameServerID = ServerIDUtils.GetLevel1ServerIDByType(m_serverID, (int)ServerType.Game, 1); // m_battleServerID = ServerIDUtils.GetLevel1ServerIDByType(m_serverID, (int)ServerType.Battle, 1); //m_friendServerID = ServerIDUtils.GetLevel1ServerIDByType(m_serverID, (int)ServerType.Friend, 1); } public uint GetWorldServerID() { return m_worldServerID; } public void SendToServerByID(uint dstserverID, int iMsgID, Google.Protobuf.IMessage message, long iObjectID) { RequestPacket reqPacket = new RequestPacket(iMsgID, iObjectID, 0); reqPacket.Message = message; SendToServerByID(dstserverID, reqPacket, iObjectID, 0); } public void SendToServerByID(uint dstserverID, int iMsgID, Google.Protobuf.IMessage message, long iObjectID, uint headserverID) { RequestPacket reqPacket = new RequestPacket(iMsgID, iObjectID, headserverID); reqPacket.Message = message; SendToServerByID(dstserverID, reqPacket, iObjectID, headserverID); } public void SendToServerByID(uint dstserverID, RequestPacket packet) { SendToServerByID(dstserverID, packet, packet.ObjectID, packet.ServerID); } public void SendToServerByID(uint dstserverID, RequestPacket packet, long iObjectID, uint headserverID) { MessageData message = PackMessagePrintf(dstserverID, packet, iObjectID, headserverID); if(message.Buffer.Data == null) { return; } m_cluster.Send(dstserverID, message); } public void Broadcast(int serverType, int iMsgID, Google.Protobuf.IMessage message, long iObjectID, uint headserverID) { RequestPacket reqPacket = new RequestPacket(iMsgID, iObjectID, headserverID); reqPacket.Message = message; Broadcast(serverType, reqPacket,iObjectID, headserverID); } public void Broadcast(int serverType, RequestPacket packet, long iObjectID, uint headserverID) { uint serverID = (uint)(serverType << 8); MessageData message = PackMessagePrintf(serverID, packet, iObjectID, headserverID); if (message.Buffer.Data == null) { return; } m_cluster.Broadcast(serverType, message); } private MessageData PackMessagePrintf(uint dstserverID, RequestPacket packet, long iObjectID, uint headserverID = 0) { MessageData message = new MessageData(); message.Header.Type = packet.MsgID; message.Header.ObjectID = iObjectID; message.Header.ServerID = headserverID; bool bRet = ProtoPackerFactory.Instance.GetProtoBufPacker().PackMessage(packet, ref message); if (bRet == false) { //判断是不是sessionid,sessionid是个很大的数 if (Sog.Service.GateSessionID.IsGateSessionIDOrUserID(iObjectID)) { TraceLog.Error("PackMessage error server {0} msgId {1} iID {2} message {3}" , ServerIDUtils.IDToString(dstserverID), packet.MsgID, iObjectID , packet.Message.GetType().Name); } else { TraceLog.UserError(iObjectID, "PackMessage error server {0} msgId {1} iID {2} message {3}" , ServerIDUtils.IDToString(dstserverID), packet.MsgID, iObjectID , packet.Message.GetType().Name); } return message; } //预先判断,提高效率 if(TraceLog.GetLogLevel() <= Log.LogLevel.TraceDetail && TraceLog.IsSkipLogMsgID(packet.MsgID) == false) { //判断是不是sessionid,sessionid是个很大的数 if (Sog.Service.GateSessionID.IsGateSessionIDOrUserID(iObjectID)) { TraceLog.TraceDetail("PackMessage send to server {0} msgId {1} iID {2} message {3} -> {4}" , ServerIDUtils.IDToString(dstserverID), packet.MsgID, iObjectID , packet.Message.GetType().Name, ((Google.Protobuf.IMessage)packet.Message).ToString()); } else { TraceLog.UserTraceDetail(iObjectID, "PackMessage send to server {0} msgId {1} iID {2} message {3} -> {4}" , ServerIDUtils.IDToString(dstserverID), packet.MsgID, iObjectID , packet.Message.GetType().Name, ((Google.Protobuf.IMessage)packet.Message).ToString()); } } return message; } /* //以下方法都是要废弃的,battleServerID 和 gameServerID 都不唯一了,全部废弃 全部废弃 全部废弃 //目前只有game像battle发送消息 public void SendToBattleServer(UInt64 battleID, int iMsgID, Google.Protobuf.IMessage message, long iObjectID, uint headserverID = 0) { RequestPacket reqPacket = new RequestPacket(iMsgID, iObjectID, headserverID); reqPacket.Message = message; SendToServerByID(TransBattleIDToBattleServerID(battleID), reqPacket); } public void SendToBattleServer(UInt64 battleID, RequestPacket packet) { SendToServerByID(TransBattleIDToBattleServerID(battleID), packet, packet.ObjectID, packet.ServerID); } public void SendToBattleServer(UInt64 battleID, RequestPacket packet, long iObjectID, uint headserverID = 0) { SendToServerByID(TransBattleIDToBattleServerID(battleID), packet, iObjectID, headserverID); } //这里要删除,现在是多台game,有的要改成广播,有的必须指定 public void SendToGameServer(uint gameServerID, int iMsgID, Google.Protobuf.IMessage message, long iObjectID, uint headserverID = 0) { RequestPacket reqPacket = new RequestPacket(iMsgID, iObjectID, headserverID); reqPacket.Message = message; SendToServerByID(gameServerID, reqPacket); } public void SendToGameServer(uint gameServerID, RequestPacket packet) { SendToServerByID(gameServerID, packet, packet.ObjectID, packet.ServerID); } public void SendToGameServer(uint gameServerID, RequestPacket packet, long iObjectID, uint headserverID = 0) { SendToServerByID(gameServerID, packet, iObjectID, headserverID); } private uint TransBattleIDToBattleServerID(UInt64 battleID) { //转换为serverID UInt64 iInstID = (battleID >> 48) & (0xffff); uint battleServerID = ServerIDUtils.MakeServerID(1, (int)ServerType.Battle, (int)iInstID); return battleServerID; } */ } }