using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Sog; using Sog.Service; using ProtoCSStruct; namespace DB { public class DBMsgHandler : BaseReloadableService { public override int GetServiceType() { return DBServiceType.DBMsgHandler; } //销毁 public override void Dispose() { } public void HandlerMessage(uint remoteAppID, MessageData message) { StructPacket packet; bool bSuccess = DBServerUtils.GetProtoPacker().UnpackMessage(message, out packet); if(bSuccess == false) { TraceLog.Error("HandlerMessage ,unpack msg failed {0}, remoteAppID {1}", message.Header.Type, remoteAppID); return; } //因为需要放入每个线程的队列,所以需要clone 一个新的对象,UnpackMessage出来的消息每次其实是同一个 //如果存在性能问题,可以根据消息类型进行缓冲 packet = packet.Clone(); //预先判断,提高效率 if (TraceLog.GetLogLevel() <= (int)Sog.Log.LogLevel.TraceDetail && TraceLog.IsSkipLogMsgID(packet.MsgID) == false) { TraceLog.TraceDetail("recv message from server {0}, message type {1} length {2} : {3}->{4}" , ServerIDUtils.IDToString(remoteAppID) , message.Header.Type , message.Header.Length , packet.MessageName() , packet.ToString()); } switch (packet.MsgID) { case (int) SSMsgID.QueryAccountDb: OnQueryAccountDBReq(remoteAppID, packet); break; case (int)SSMsgID.UpdateAccountGradeDbReq: OnUpdateAccountGradeDbReq(remoteAppID, packet); break; case (int) SSMsgID.QueryDbSnsFriendAccountReq: OnQueryDbSnsFriendAccountReq(remoteAppID, packet); break; case (int) SSMsgID.BindGuestFacebookDbReq: OnBindGuestFacebookDbReq(remoteAppID, packet); break; case (int) SSMsgID.UnbindGuestFacebookDbReq: OnUnbindGuestFacebookDbReq(remoteAppID, packet); break; case (int) SSMsgID.QueryUidByFbuseridReq: OnQueryUidByFbuseridReq(remoteAppID, packet); break; case (int)SSMsgID.QueryUidByWxidReq: OnQueryUidByWxidReq(remoteAppID, packet); break; case (int) SSMsgID.ChangeFacebookUidDbReq: OnChangeFacebookUidDbReq(remoteAppID, packet); break; case (int) SSMsgID.UpdateAccountLastloginrealm: OnUpdateAccountLastLoginRealm(remoteAppID, packet); break; case (int) SSMsgID.LoginCheckValidReq: OnLoginCheckValidReq(remoteAppID, packet); break; case (int)SSMsgID.RealmRoleBriefReq: OnRealmRoleBriefReq(remoteAppID, packet); break; case (int)SSMsgID.UpdateRealmRoleBrief: OnUpdateRealmRoleBrief(remoteAppID, packet); break; case (int)SSMsgID.QueryRealmUidDataReq: OnQueryRealmUidDataReq(remoteAppID, packet); break; case (int)SSMsgID.SaveRealmUidDataReq: OnSaveRealmUidDataReq(remoteAppID, packet); break; case (int)SSMsgID.SsRealmDbqueryReq: OnQueryRealmDBReq(remoteAppID, packet); break; case (int)SSMsgID.SsRealmDbsaveReq: OnSaveRealmDBReq(remoteAppID, packet); break; case (int)SSMsgID.SsRealmSavePlayernum: OnSSRealmSavePlayerNum(remoteAppID, packet); break; //pay case (int)SSGameMsgID.GmQueryUserPayRecordReq: OnGmQueryUserPayRecordReq(remoteAppID, packet); break; case (int) SSGameMsgID.PayGoogleReq: OnPayGoogleReq(remoteAppID, packet); break; case (int)SSGameMsgID.PayGoogleSuccessReq: OnPayGoogleSuccReq(remoteAppID, packet); break; case (int)SSGameMsgID.PayDbAddDiamondStatusReq: OnPayDBAddDiamondStatusReq(remoteAppID, packet); break; case (int)SSGameMsgID.QueryPayGoogleSuccOrder: OnQueryPayGoogleSuccOrder(remoteAppID, packet); break; case (int)SSGameMsgID.GmQueryUserAccountinfoReq: OnQueryAccountInfoReq(remoteAppID, packet); break; case (int)SSGameMsgID.PayHeroSaveHttpContentReq: OnSaveHttpContentReq(remoteAppID, packet); break; case (int)SSGameMsgID.PayHeroSelectHttpContentReq: OnSelectHttpContentReq(remoteAppID, packet); break; case (int)SSGameMsgID.PayHeroDeleteHttpContentNotify: OnDeleteHttpContentNotify(remoteAppID, packet); break; case (int)SSMsgID.SendGifts: SSSendGift(remoteAppID, packet); break; default: TraceLog.Error("DBMsgHandler unknow MsgID {0}", packet.MsgID); break; } } private int CalcTaskIndex(int accountType, string accountID) { string strHash = AccountUtils.CalcAccountKey(accountType, accountID); int index = AccountUtils.HashStringForIndex(strHash) % (int)MessageTaskDistributor.Instance.TaskCount; return index; } private int CalcRankTaskIndex(int realmId) { int index = 1 % (int)MessageTaskDistributor.Instance.TaskCount; return index; } private void OnQueryAccountDBReq(uint remoteAppID, StructPacket packet) { ref SSQueryAccountInfoDB queryAccountDbReq = ref packet.GetMessage(); int iTaskIndex = CalcTaskIndex(queryAccountDbReq.CSAccountAuthReq.Account.AccountType , queryAccountDbReq.CSAccountAuthReq.Account.AccountID.GetString()); //转给任务线程处理 MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); return; } private void OnQueryDbSnsFriendAccountReq(uint remoteAppID, StructPacket packet) { ref SSQueryDBSnsFriendAccountReq req = ref packet.GetMessage(); int iTaskIndex = CalcTaskIndex(req.AccountType, req.AccountID.GetString()); //转给任务线程处理 MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnBindGuestFacebookDbReq(uint remoteAppID, StructPacket packet) { ref SSBindGuestFacebookDbReq req = ref packet.GetMessage(); int iTaskIndex = CalcTaskIndex(req.AccountInfo.AccountType, req.AccountInfo.AccountID.GetString()); //转给任务线程处理 MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnUnbindGuestFacebookDbReq(uint remoteAppID, StructPacket packet) { ref SSUnbindGuestFacebookDbReq req = ref packet.GetMessage(); int iTaskIndex = CalcTaskIndex(req.AccountInfo.AccountType, req.AccountInfo.AccountID.GetString()); //转给任务线程处理 MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnQueryUidByFbuseridReq(uint remoteAppID, StructPacket packet) { ref var req = ref packet.GetMessage(); int iTaskIndex = CalcTaskIndex((int)AccountType.AccountType_Facebook, req.Fbuserid.GetString()); //转给任务线程处理 MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnQueryUidByWxidReq(uint remoteAppID, StructPacket packet) { ref var req = ref packet.GetMessage(); int iTaskIndex = CalcTaskIndex((int)AccountType.AccountType_WX, req.Wxid.GetString()); //转给任务线程处理 MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnChangeFacebookUidDbReq(uint remoteAppID, StructPacket packet) { ref var req = ref packet.GetMessage(); int iTaskIndex = CalcTaskIndex(req.AccountType,req.AccountID.GetString()); //转给任务线程处理 MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnUpdateAccountLastLoginRealm(uint remoteAppID, StructPacket packet) { ref var req = ref packet.GetMessage(); int iTaskIndex = CalcTaskIndex(req.AccountType, req.AccountID.GetString()); //转给任务线程处理 MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnLoginCheckValidReq(uint remoteAppID, StructPacket packet) { ref var req = ref packet.GetMessage(); int iTaskIndex = CalcTaskIndex(req.AccountInfo.AccountType, req.AccountInfo.AccountID.GetString()); //转给任务线程处理 MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnRealmRoleBriefReq(uint remoteAppID, StructPacket packet) { ref var req = ref packet.GetMessage(); int iTaskIndex = CalcTaskIndex(req.AccountType, req.AccountID.GetString()); //转给任务线程处理 MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnUpdateRealmRoleBrief(uint remoteAppID, StructPacket packet) { ref var req = ref packet.GetMessage(); int iTaskIndex = CalcTaskIndex(req.AccountType, req.AccountID.GetString()); //转给任务线程处理 MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnQueryFriendListReq(uint remoteAppID, StructPacket packet) { ref var req = ref packet.GetMessage(); int iTaskIndex = (int)(req.Uid % (int)MessageTaskDistributor.Instance.TaskCount); MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnQueryFriendInfoCacheReq(uint remoteAppID, StructPacket packet) { ref var req = ref packet.GetMessage(); int iTaskIndex = (int)(req.Uid % (int)MessageTaskDistributor.Instance.TaskCount); MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnSaveFriendListReq(uint remoteAppID, StructPacket packet) { ref var req = ref packet.GetMessage(); int iTaskIndex = (int)(req.Uid % (int)MessageTaskDistributor.Instance.TaskCount); MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnPayGoogleReq(uint remoteAppID, StructPacket packet) { ref var req = ref packet.GetMessage(); int iTaskIndex = (int)(req.Uid % (int)MessageTaskDistributor.Instance.TaskCount); MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnGmQueryUserPayRecordReq(uint remoteAppID, StructPacket packet) { ref var req = ref packet.GetMessage(); int iTaskIndex = (int)(req.UID % (int)MessageTaskDistributor.Instance.TaskCount); MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnPayGoogleSuccReq(uint remoteAppID, StructPacket packet) { ref var req = ref packet.GetMessage(); int iTaskIndex = (int)(req.Uid % (int)MessageTaskDistributor.Instance.TaskCount); MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnPaySendDBRefundReq(uint remoteAppID, StructPacket packet) { ref var req = ref packet.GetMessage(); int iTaskIndex = (int)(req.Uid % (int)MessageTaskDistributor.Instance.TaskCount); MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnPayDBAddDiamondStatusReq(uint remoteAppID, StructPacket packet) { int iTaskIndex = (int)(packet.ObjectID % (int)MessageTaskDistributor.Instance.TaskCount); MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnQueryPayGoogleSuccOrder(uint remoteAppID, StructPacket packet) { int iTaskIndex = (int)(packet.ObjectID % (int)MessageTaskDistributor.Instance.TaskCount); MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnQueryAccountInfoReq(uint remoteAppID, StructPacket packet) { ref SSGmQueryUserAccountInfoReq req = ref packet.GetMessage(); int iTaskIndex = CalcTaskIndex(req.AccountType, req.Account.GetString()); //转给任务线程处理 MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnSaveHttpContentReq(uint remoteAppID, StructPacket packet) { ref var req = ref packet.GetMessage(); int iTaskIndex = (int)(req.Http.Uid % (int)MessageTaskDistributor.Instance.TaskCount); MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnSelectHttpContentReq(uint remoteAppID, StructPacket packet) { // 定期检查(60s或者300s)是否有http记录未成功提交到hero的服务器, 频率非常低, 固定用线程1处理 MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, 0); } private void OnDeleteHttpContentNotify(uint remoteAppID, StructPacket packet) { ref var notify = ref packet.GetMessage(); int iTaskIndex = (int)(notify.Uid % (int)MessageTaskDistributor.Instance.TaskCount); MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void SSSendGift(uint remoteAppID, StructPacket packet) { ref var req = ref packet.GetMessage(); int iTaskIndex = CalcTaskIndex(req.AccountType, req.AccountId.GetString()); MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnUpdateAccountGradeDbReq(uint remoteAppID, StructPacket packet) { ref var req = ref packet.GetMessage(); int iTaskIndex = CalcTaskIndex(req.AccountInfo.AccountType, req.AccountInfo.AccountID.GetString()); //转给任务线程处理 MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnQueryRealmUidDataReq(uint remoteAppID, StructPacket packet) { ref var req = ref packet.GetMessage(); int iTaskIndex = (int)(req.RealmId % (int)MessageTaskDistributor.Instance.TaskCount); //转给任务线程处理 MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnSaveRealmUidDataReq(uint remoteAppID, StructPacket packet) { ref var req = ref packet.GetMessage(); int iTaskIndex = (int)(req.RealmId % (int)MessageTaskDistributor.Instance.TaskCount); //转给任务线程处理 MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnQueryRealmDBReq(uint remoteAppID, StructPacket packet) { ref var req = ref packet.GetMessage(); int iTaskIndex = (int)(1 % (int)MessageTaskDistributor.Instance.TaskCount); //转给任务线程处理 MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnSaveRealmDBReq(uint remoteAppID, StructPacket packet) { ref var req = ref packet.GetMessage(); int iTaskIndex = (int)(1 % (int)MessageTaskDistributor.Instance.TaskCount); //转给任务线程处理 MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private void OnSSRealmSavePlayerNum(uint remoteAppID, StructPacket packet) { ref var req = ref packet.GetMessage(); int iTaskIndex = (int)(1 % (int)MessageTaskDistributor.Instance.TaskCount); //转给任务线程处理 MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } } }