using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Sog; using Sog.Service; using ProtoCSStruct; namespace GameDB { public class GameDBMsgHandler : BaseReloadableService { public override int GetServiceType() { return GameDBServiceType.GameDBMsgHandler; } //销毁的时候置空 public override void Dispose() { } public void HandlerMessage(uint remoteAppID, MessageData message) { StructPacket packet; bool bSuccess = GameDBServerUtils.GetProtoPacker().UnpackMessage(message, out packet); if (bSuccess == false) { TraceLog.Error("GameDBMsgHandler.HandlerMessage ,unpack msg failed {0}, remoteAppID {1}", message.Header.Type, remoteAppID); return; } packet = packet.Clone(); //预先判断,提高效率 if (TraceLog.GetLogLevel() <= (int)Sog.Log.LogLevel.TraceDetail && TraceLog.IsSkipLogMsgID(packet.MsgID) == false) { TraceLog.TraceDetail("GameDBMsgHandler 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()); } long uid = GetUidFromPacket(packet); //long reamId = AccountUtils.GetGameIdFromUserId(uid); int iTaskIndex = 0; switch (packet.MsgID) { case (int)SSGameMsgID.AddServerTimeSync: OnAddServerTimeSync(remoteAppID, packet); //不往后走了,直接return return; case (int)SSGameMsgID.QueryMailReq://已废弃走此路线,通过MailServer处理 case (int)SSGameMsgID.MailOpReq: TraceLog.Error("GameDBMsgHandler.HandlerMessage error mail MsgID {0}", packet.MsgID); return; default: TraceLog.Debug("GameDBMsgHandler.HandlerMessage GetUidFromPacket MsgID {0} uid {1}", packet.MsgID, uid); iTaskIndex = (int)(uid % MessageTaskHandler.GameDBWorkThreadCount); break; } MessageTaskDistributor.Instance.Distribute(remoteAppID, packet, iTaskIndex); } private long GetUidFromPacket(StructPacket packet) { switch (packet.MsgID) { case (int)SSGameMsgID.QueryRoleDbReq: ref SSQueryRoleDbReq queryRoleReq = ref packet.GetMessage(); return queryRoleReq.Uid; case (int)SSGameMsgID.SaveRoleReq: ref SSSaveRoleReq saveRoleReq = ref packet.GetMessage(); return saveRoleReq.RoleBase.Uid; case (int)SSGameMsgID.FirebaseidSaveDbReq: return packet.ObjectID; case (int)SSGameMsgID.FirebaseidQueryDbReq: return packet.ObjectID; case (int)SSGameMsgID.GmSetFreezeTime: return packet.ObjectID; case (int)SSGameMsgID.GmQueryUserPayRecordReq: return packet.ObjectID; case (int)SSGameMsgID.EnableSendMsgReq: ref SSEnableSendMsgReq enableMsgReq = ref packet.GetMessage(); return enableMsgReq.Uid; //friend case (int)SSGameMsgID.QueryFriendListReq: ref SSQueryFriendListReq queryFriendListReq = ref packet.GetMessage(); return queryFriendListReq.Uid; case (int)SSGameMsgID.QueryFriendInfoCacheReq: ref SSQueryFriendInfoCacheReq queryFriendInfoCacheReq = ref packet.GetMessage(); return queryFriendInfoCacheReq.Uid; case (int)SSGameMsgID.SaveFriendListReq: ref SSSaveFriendListReq saveFriendListReq = ref packet.GetMessage(); return saveFriendListReq.Uid; //rank case (int)SSGameMsgID.QueryRankDbReq: return packet.ObjectID; case (int)SSGameMsgID.SaveRankDbReq: return packet.ObjectID; case (int)SSGameMsgID.QueryActRankDbReq: return packet.ObjectID; case (int)SSGameMsgID.SaveActRankDataReq: return packet.ObjectID; case (int)SSGameMsgID.PlayerQueryOfflineRoleReq: return packet.ObjectID; case (int)SSGameMsgID.PlayerSearchRoledbBynameReq: return packet.ObjectID; case (int)SSGameMsgID.GmQueryUserRoleReq: case (int)SSGameMsgID.OperationCostItemReq: //case (int)SSGameMsgID.QueryArenaRankReq: case (int)SSGameMsgID.QueryBattleReplayDbReq: case (int)SSGameMsgID.SaveBattleReplayDbReq: case (int)SSGameMsgID.DeleteBattleReplayDbReq: case (int)SSGameMsgID.AddServerTimeSync: case (int)SSGameMsgID.OperationUpdateTaskReq: case (int)SSGameMsgID.GmUpdateUserRoleReq: case (int)SSGameMsgID.GmImportUserRoleReq: return packet.ObjectID; case (int)SSGameMsgID.WorldglobaldataDbOpReq: return packet.ObjectID; default: TraceLog.Error("GameDBMsgHandler.GetUidFromPacket unknow MsgID {0}", packet.MsgID); break; } //其他情况一律返回0 return 0; } public void OnAddServerTimeSync(uint remoteAppID, StructPacket packet) { ref SSAddServerTimeSync sync = ref packet.GetMessage(); TraceLog.Debug("GameDBMsgHandler.OnAddServerTimeSync serverid {0} offset {1}" , ServerIDUtils.IDToString(remoteAppID), sync.Offset); GameDBServerUtils.GetApp().Time.SetOffset(sync.Offset); GameDBServerUtils.GetApp().Time.UpdateTime(); TraceLog.Debug("GameDBMsgHandler.OnAddServerTimeSync serverid {0} offset {1} set time success" , ServerIDUtils.IDToString(remoteAppID), sync.Offset); } } }