using ProtoCSStruct; using Sog; using Sog.Gate; namespace FrameSync; public class FrameSyncMsgHandler: BaseReloadableService { private ServerApp m_app; public override int GetServiceType() { return FraneSyncType.BattleMsgHandler; } //销毁的时候置空,提高垃圾回收效率 public override void Dispose() { m_app = null; } public FrameSyncMsgHandler(ServerApp app) { m_app = app; } public ServerApp GetApp() { return m_app; } private void HandlerGateMessage(uint remoteAppID, MessageData message) { GateMsgHeader header = new GateMsgHeader(); GateMsgHeaderPacker.UnPack(message.Buffer.Data, ref header); // trans msg to client if (header.OpCode == (int)GateMsgOpCode.Trans) { HandlerClientGateMessage(remoteAppID, message); } if (header.OpCode == (int)GateMsgOpCode.ConnectedReq) { GateClientConnectedReq req = GateClientConnectedReq.ParseFrom(message.Buffer.Data, GateMsgHeaderPacker.GetLength()); OnSessionConnected(remoteAppID, header.SessionID, req); return; } if (header.OpCode == (int)GateMsgOpCode.DisconnectedReq) { OnSessionDisconnected(remoteAppID, header.SessionID); return; } } private void OnSessionConnected(uint gateServerID, long sessionID, GateClientConnectedReq req) { TraceLog.Trace("FrameSyncMsgHandler.OnSessionConnected session {0} ip {1} connected, new PlayerSession", sessionID, req.IPAddress); //回应 GateMsgHeader gateHeader; gateHeader.OpCode = (int)GateMsgOpCode.ConnectedRes; gateHeader.SessionID = sessionID; GateService.NotifyGateServer(m_app, gateServerID, ref gateHeader); } private void OnSessionDisconnected(uint gateServerID, long sessionID) { TraceLog.Debug("FrameSyncMsgHandler.OnSessionDisconnected session {0} disconnected, delete PlayerSession" , sessionID); // m_versionSvc.RemoveClientConnectData(gateServerID, sessionID); MatchSystem.LeaveRoom(gateServerID, sessionID); } private void HandlerClientGateMessage(uint gateServerID, MessageData gateMessage) { long sessionID = gateMessage.Header.ObjectID; int headLength = GateMsgHeaderPacker.GetLength(); MessageData message = new MessageData(); message.Header.Type = gateMessage.Header.Type; message.Header.Length = gateMessage.Header.Length - headLength; if (message.Header.Length < 0) { TraceLog.Trace("FrameSyncMsgHandler.HandlerClientGateMessage recv session {0} message ,gateMessage length error ", sessionID); return; } message.MallocData(message.Header.Length); //message.Data = new byte[message.Header.Length]; Buffer.BlockCopy(gateMessage.Buffer.Data, headLength, message.Buffer.Data, 0, message.Buffer.Length); try { HandlerClientMessage(gateServerID, sessionID, message); } finally { message.FreeData(); } } private void HandlerClientMessage(uint gateServerID, long sessionID, MessageData message) { try { StructPacket packet = null; bool result = false; try { result = FrameServerUtils.GetProtoPacker().UnpackMessage(message, out packet); } catch (Exception ex) { TraceLog.Error("FrameSyncMsgHandler.HandlerClientMessage type {0} UnpackMessage error {1}" , message.Header.Type, ex.Message); return; } //bool result = VersionServerUtils.GetProtoPacker().UnpackMessage(message, out packet); if (result) { //预先判断,提高效率 if (TraceLog.GetLogLevel() <= (int)Sog.Log.LogLevel.TraceDetail && TraceLog.IsSkipLogMsgID(packet.MsgID) == false) { // log message TraceLog.TraceDetail("FrameSyncMsgHandler.HandlerClientMessage recv from session {0} from gateServer {1}, message type {2} length {3} : {4}->{5}" , sessionID , ServerIDUtils.IDToString(gateServerID) , message.Header.Type , message.Header.Length , packet.MessageName() , packet.ToString()); } ProcessClientPacket(gateServerID, sessionID, packet); } } catch (Exception ex) { TraceLog.Error("FrameSyncMsgHandler.HandlerClientMessage Received from session:{0} error:{1}", sessionID, ex.Message); //TraceLog.Exception(ex); throw; } } private void ProcessClientPacket(uint gateServerID, long sessionID, StructPacket packet) { //来自客户端的消息 switch (packet.MsgID) { case (int)CSGameMsgID.Battleping: ref CSBattlePing pingReq = ref packet.GetMessage(); pingReq.ServerTime = m_app.Time.GetTime(); GateService.SendToGate(0, m_app.GetCluster(), gateServerID, sessionID, (int)CSGameMsgID.Battleping, ref pingReq); break; case (int)CSGameMsgID.JoinRoomReq: ref var joinRoom = ref packet.GetMessage(); MatchSystem.JoinOrCreateRoom(ref joinRoom, gateServerID, sessionID); break; case (int)CSGameMsgID.PlayerOpInfoReq: ref var csPlayerOpInfo = ref packet.GetMessage(); MatchSystem.AddPlayerOpInfo(ref csPlayerOpInfo, gateServerID, sessionID); break; } } public void HandlerMessage(uint remoteAppID, MessageData message) { int iServerType = ServerIDUtils.GetServerType(remoteAppID); if (iServerType == (int)ServerType.GateFrameSync) { TraceLog.Trace("GateFrameSync.HandlerMessage recv msg from gate length {0}", message.Header.Length); HandlerGateMessage(remoteAppID, message); } } }