using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using ProtoCSStruct; using Sog; namespace Mail { public static class MailServerUtils { public static string GetMailTableName(long uid) { int index = TableIndexCalc.CalcRoleTableIndex(uid); return "tbmail_" + index.ToString(); } public static MailServerData GetMailServerData() { return ServerDataObjMgr.GetDataObj(MailDataObjType.MailServerData); } public static ProtoCSStructPacker GetProtoPacker() { return ProtoPackerFactory.Instance.GetProtoCSStructPacker(); } public static StructPacketSender GetPacketSender() { return GetMailServerData().m_packetSender; } public static long GetTimeSecond() { return GetMailServerData().m_app.Time.GetTimeSecond(); } public static uint GetAppID() { return GetMailServerData().m_app.ServerID; } public static ServerApp GetApp() { return GetMailServerData().m_app; } /// /// 获取服务器配置方法 /// public static MailServerConfig GetServerConfig() { return (MailServerConfig)ServerConfigMgr.Instance.m_serverConfig; } public static void ParseDBConfig(ServerApp app, out int dbtype, out string dbname, out string dbip) { MailServerConfig serverConfig = GetServerConfig(); // 优先使用参数里传入的db配置, 方便每个人独立修改 string dbtypestr = app.GetCluster().GetAppParamByKey("mail_dbtype"); if (string.IsNullOrEmpty(dbtypestr) || !int.TryParse(dbtypestr, out dbtype)) { dbtype = serverConfig.dbtype; } dbip = app.GetCluster().GetAppParamByKey("mail_dbip"); if (string.IsNullOrEmpty(dbip)) { dbip = serverConfig.dbip; } if (dbtype == 1) // mongo db 不再使用 { TraceLog.Error("MailServerUtils.ParseDBConfig invalid dbtype 1"); } // dbname不让单独修改, 大家统一简单些 dbname = serverConfig.dbname; } public static PlayerInfoMail GetPlayerInfo(long uid) { var players = MailServerUtils.GetMailServerData().m_playerTable; if(players.TryGetValue(uid, out PlayerInfoMail playerInfo)) { return playerInfo; } return null; } public static void SendMsgToSelf(uint remoteAppID, int iMsgID, ref T structMessage, long iObjectID, uint headserverID = 0) where T : struct, IStructMessage { TraceLog.TraceDetail("SendMsgToSelf msgId {0} message {1}", iMsgID, structMessage.ToString()); StructPacket packet = new StructPacket(); packet.Header.Type = iMsgID; packet.Header.ObjectID = iObjectID; packet.Header.ServerID = headserverID; StructMessageParser parser = new StructMessageParser(); packet.Parser = parser; ref T tmsg = ref parser.GetMessage(); tmsg = structMessage; var data = GetMailServerData(); data.m_threadSafePacketQueue.Enqueue(new Sog.Service.StructPacketData(remoteAppID, packet)); } } }