using System; using Sog; using Sog.Log; using Sog.Service; using ProtoCSStruct; using System.Collections.Generic; namespace PlayerOp { public class PlayerOpMsgHandler : BaseReloadableService { private ServerApp m_app; private static int seq = 0; public override int GetServiceType() { return PlayerOpServiceType.PlayerOpMsgHandler; } //销毁的时候置空 public override void Dispose() { m_app = null; } public PlayerOpMsgHandler(ServerApp app) { m_app = app; } public ServerApp GetApp() { return m_app; } public void HandlerMessage(uint remoteAppID, MessageData message) { StructPacket packet; bool bSuccess = PlayerOpServerUtils.GetProtoPacker().UnpackMessage(message, out packet); if (bSuccess == false) { TraceLog.Error("PlayerOpMsgHandler.HandlerMessage unpack msg failed {0}, remoteAppID {1}", message.Header.Type, remoteAppID); return; } //预先判断,提高效率 if (TraceLog.GetLogLevel() <= (int)Sog.Log.LogLevel.TraceDetail) { TraceLog.TraceDetail("PlayerOpMsgHandler.HandlerMessage 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()); } packet = packet.Clone(); TraceLog.Trace("PlayerOpMsgHandler.HandlerMessage pack msgId {0} remoteAppID {1}", packet.MsgID, remoteAppID); switch (packet.MsgID) { case (int)SSGameMsgID.ExchangeCodeTakeReq: PlayerOpServerUtils.GetExchangeSvc().OnExchangeCodeTakeReq(remoteAppID, packet); break; case (int)SSGameMsgID.ExchangeSaveDbReq: case (int)SSGameMsgID.ExchangeStatusChangeReq: case (int)SSGameMsgID.ExchangeQueryDbReq: AddMessageTaskDistributor(remoteAppID, packet); break; default: TraceLog.Error("PlayerOpMsgHandler.HandlerMessage unknow MsgID {0}", packet.MsgID); break; } } //按轮询分配 public static void AddMessageTaskDistributor(uint serverID, StructPacket packet) { int iTaskIndex = (int)(seq++ % MessageTaskHandler.DBWorkThreadCount); TraceLog.Debug("PlayerOpMsgHandler.AddMessageTaskDistributor from server iTaskIndex {0} seq {1}", iTaskIndex, seq - 1); MessageTaskDistributor.Instance.Distribute(serverID, packet, iTaskIndex); } } }