public override void BeforeFixedUpdate(int deltaTime) { List <EntityBase> list = GetEntityList(); for (int i = 0; i < list.Count; i++) { ConnectionComponent comp = list[i].GetComp <ConnectionComponent>(); comp.m_isInframe = true; T cmd = (T)comp.GetCommand(m_world.FrameCount); cmd.id = list[i].ID; cmd.frame = m_world.FrameCount; list[i].ChangeComp(cmd); //Debug.Log("USE cmd id "+ list[i].ID + " frame " + cmd.frame + " content " + Serializer.Serialize(cmd)); //到了这一帧还没有发送命令的,给预测一个并广播给所有前端 //if (comp.LastInputFrame < m_world.FrameCount) { for (int j = 0; j < list.Count; j++) { cmd.time = ServiceTime.GetServiceTime(); ConnectionComponent conn = list[j].GetComp <ConnectionComponent>(); ProtocolAnalysisService.SendMsg(conn.m_session, cmd); //Debug.Log("预测并广播 " + cmd.frame); } } } }
public void PushSyncEnity(ConnectionComponent connect, EntityBase entity) { if (connect.m_session != null && !connect.m_session.Connected) { return; } SyncEntityMsg msg = new SyncEntityMsg(); msg.frame = m_world.FrameCount; msg.infos = new List <EntityInfo>(); msg.destroyList = new List <int>(); for (int i = 0; i < connect.m_waitSyncEntity.Count; i++) { msg.infos.Add(CreateEntityInfo(connect.m_waitSyncEntity[i], connect.m_session)); } for (int i = 0; i < connect.m_waitDestroyEntity.Count; i++) { msg.destroyList.Add(connect.m_waitDestroyEntity[i]); } connect.m_waitDestroyEntity.Clear(); connect.m_waitSyncEntity.Clear(); if (msg.infos.Count > 0 || msg.destroyList.Count > 0) { ProtocolAnalysisService.SendMsg(connect.m_session, msg); } }
public static EntityInfo CreateServiceComponentInfo(EntityBase entity, SyncSession session) { EntityInfo Data = new EntityInfo(); Data.id = entity.ID; Data.infos = new List <ComponentInfo>(); //给有连接组件的增加Self组件 if (entity.GetExistComp(ComponentType.ConnectionComponent)) { ConnectionComponent comp = entity.GetComp <ConnectionComponent>(); if (comp.m_session == session) { ComponentInfo info = new ComponentInfo(); info.m_compName = "SelfComponent"; info.content = "{}"; Data.infos.Add(info); } else { ComponentInfo info = new ComponentInfo(); info.m_compName = "TheirComponent"; info.content = "{}"; Data.infos.Add(info); } } return(Data); }
void StartGame(Player[] players) { WorldBase world = WorldManager.CreateWorld <DemoWorld>(); //TODO 模式选择没用了 world.SyncRule = SyncRule.Status; world.m_RandomSeed = new Random().Next(); //随机一个种子 for (int i = 0; i < players.Length; i++) { ConnectionComponent conn = new ConnectionComponent(); conn.m_session = players[i].session; conn.m_playerID = players[i].playerID; SyncComponent sc = new SyncComponent(); world.CreateEntityImmediately("Player" + players[i].playerID, conn, sc); players[i].session.m_connect = conn; world.eventSystem.DispatchEvent(ServiceEventDefine.c_playerJoin, conn.Entity); Debug.Log("Send Game Start"); //派发游戏开始消息 PlayerMatchMsg_c msg = new PlayerMatchMsg_c(); msg.predictTime = 0; msg.isMatched = true; ProtocolAnalysisService.SendMsg(players[i].session, msg); } world.IsStart = true; }
static void ReceviceSameCmdMsg(SyncSession session, SameCommand msg) { //消息确认 AffirmMsg amsg = new AffirmMsg(); amsg.index = msg.frame; amsg.time = msg.time; ProtocolAnalysisService.SendMsg(session, amsg); ConnectionComponent connectComp = session.m_connect; if (connectComp != null) { WorldBase world = connectComp.Entity.World; //取上一帧的数据 T scmd = (T)connectComp.GetCommand(msg.frame - 1).DeepCopy(); if (msg.frame > world.FrameCount) { scmd.frame = msg.frame; connectComp.AddCommand(scmd); BroadcastSameCommand(world, connectComp, msg, true); } else { scmd.frame = world.FrameCount; connectComp.AddCommand(scmd); } ControlSpeed(connectComp, world, msg.frame); } }
public static void SendStartMsg(WorldBase world) { //获取所有的Player组件,并派发 List <EntityBase> list = world.GetEntityList(s_playerFilter); StartSyncMsg startMsg = CreateStartMsg(world); SyncEntityMsg playerInfo = new SyncEntityMsg(); playerInfo.frame = world.FrameCount; playerInfo.infos = new List <EntityInfo>(); for (int i = 0; i < list.Count; i++) { playerInfo.infos.Add(CreatePlayerComponentInfo(list[i])); } for (int i = 0; i < list.Count; i++) { ConnectionComponent cc = list[i].GetComp <ConnectionComponent>(); SyncEntityMsg serviceInfo = CreateServiceMsg(world, cc.m_session); ProtocolAnalysisService.SendMsg(cc.m_session, playerInfo); ProtocolAnalysisService.SendMsg(cc.m_session, serviceInfo); ProtocolAnalysisService.SendMsg(cc.m_session, startMsg); } }
static void ReceviceSyncMsg(SyncSession session, T msg) { //Debug.Log("ReceviceSyncMsg " + msg.id + " content " + Serializer.Serialize(msg)); //消息确认 AffirmMsg amsg = new AffirmMsg(); amsg.index = msg.frame; amsg.time = msg.time; ProtocolAnalysisService.SendMsg(session, amsg); ConnectionComponent connectComp = session.m_connect; if (connectComp != null) { WorldBase world = connectComp.Entity.World; if (msg.frame > world.FrameCount) { //广播这帧 BroadcastCommand(world, connectComp, msg, false); connectComp.AddCommand(msg); } else { //当成最新的一帧来处理 msg.frame = world.FrameCount; connectComp.AddCommand(msg); } ControlSpeed(connectComp, world, msg.frame); } }
public override void BeforeFixedUpdate(int deltaTime) { //Debug.Log("GetEntityList A"); List <EntityBase> list = GetEntityList(); //Debug.Log("list Res -> " + GetEntityList(new string[] { "CommandComponent", "ConnectionComponent" }).Count); //Debug.Log("GetEntityList B " + list.Count); for (int i = 0; i < list.Count; i++) { ConnectionComponent comp = list[i].GetComp <ConnectionComponent>(); T cmd = (T)comp.GetCommand(m_world.FrameCount); cmd.id = list[i].ID; cmd.frame = m_world.FrameCount; list[i].ChangeComp(cmd); //Debug.Log("USE cmd id "+ list[i].ID + " frame " + cmd.frame + " content " + Serializer.Serialize(cmd)); //到了这一帧还没有发送命令的,给预测一个并广播给所有前端 if (comp.LastInputFrame < m_world.FrameCount) { for (int j = 0; j < list.Count; j++) { ConnectionComponent conn = list[j].GetComp <ConnectionComponent>(); ProtocolAnalysisService.SendMsg(conn.m_session, cmd); } } } }
void PushStartSyncMsg() { List <EntityBase> list = GetEntityList(new string[] { "ConnectionComponent" }); for (int i = 0; i < list.Count; i++) { ConnectionComponent comp = list[i].GetComp <ConnectionComponent>(); if (comp.m_isWaitPushStart == true) { comp.m_isWaitPushStart = false; //同步单例组件 foreach (var item in m_world.m_singleCompDict) { if (item.Key != "EntityRecordComponent") { PushSingletonComp(comp.m_session, item.Key); } } PushStartSyncMsg(comp.m_session); } } }
public void OnGameFinsih(params object[] objs) { WorldBase world = (WorldBase)objs[0]; world.eventSystem.DispatchEvent(GameUtils.c_scoreChange, null); List <PlayerComponent> rankList = world.GetSingletonComp <RankComponent>().rankList; int diamond = rankList.Count; for (int i = 0; i < rankList.Count; i++) { ConnectionComponent cc = rankList[i].Entity.GetComp <ConnectionComponent>(); PlayerSettlement_c msg = new PlayerSettlement_c(); msg.rank = i + 1; msg.diamond = diamond--; msg.score = rankList[i].score; msg.historicalHighest = rankList[i].score; if (cc.m_session != null) { cc.m_session.player.Diamond += msg.diamond; ProtocolAnalysisService.SendMsg(cc.m_session, msg); } else { //TODO 将奖励发放给离线玩家 } } }
static int CalcAdvanceFrame(ConnectionComponent connect) { return(2); int frame = connect.rtt / UpdateEngine.IntervalTime + 1; return(frame); }
public NetworkNodeAgent(NetworkAddress networkAddress, string networkManagmentSystemIpAddress, int networkManagmentSystemListeningPort) { _managmentPlaneConnectionComponent = new ConnectionComponent(networkAddress, networkManagmentSystemIpAddress, networkManagmentSystemListeningPort, ConnectionManagerType.NetworkManagementSystem); _managmentPlaneConnectionComponent.ObjectReceived += Receive; _managmentPlaneConnectionComponent.UpdateState += (sender, state) => OnUpdateState(state); }
public void AddRecord(ConnectionComponent connect) { m_disConnectDict.Add(connect.m_session.player.playerID, connect); connect.m_session = null; connect.Entity.World.eventSystem.DispatchEvent(ServiceEventDefine.c_playerExit, connect.Entity); }
public static ConnectionComponent CreateReceivingConnection( LevelComponent level, Point cell, ConnectionComponent incoming) { incoming.TargetLevelCell = cell; var incomingPosition = incoming.Entity.Position; return(CreateConnection(level, cell, incomingPosition.LevelId, incomingPosition.LevelCell)); }
static void ReceviceVerificationMsg(SyncSession session, VerificationMsg msg) { ConnectionComponent connectComp = session.m_connect; if (connectComp != null) { WorldBase world = connectComp.Entity.World; } }
public EntityInfo CreateEntityInfo(EntityBase entity, SyncSession session) { EntityInfo Data = new EntityInfo(); Data.id = entity.ID; Data.infos = new List <ComponentInfo>(); foreach (var c in entity.comps) { if (c == null) { continue; } Type type = c.GetType(); if (!type.IsSubclassOf(typeof(ServiceComponent)) && type != typeof(SyncComponent)) { try { ComponentInfo info = new ComponentInfo(); info.m_compName = type.Name; info.content = Serializer.Serialize(c); Data.infos.Add(info); } catch (StackOverflowException e) { Debug.LogError("Serializer error " + type.FullName + " " + e.ToString()); } } } //给有连接组件的增加Self组件 if (entity.GetExistComp(ComponentType.ConnectionComponent)) { ConnectionComponent comp = entity.GetComp <ConnectionComponent>(); if (comp.m_session == session) { ComponentInfo info = new ComponentInfo(); info.m_compName = "SelfComponent"; info.content = "{}"; Data.infos.Add(info); } else { ComponentInfo info = new ComponentInfo(); info.m_compName = "TheirComponent"; info.content = "{}"; Data.infos.Add(info); } } return(Data); }
public override void NoRecalcLateFixedUpdate(int deltaTime) { if (!isDebug) { return; } DebugMsg msg = new DebugMsg(); msg.frame = m_world.FrameCount; msg.infos = new List <EntityInfo>(); for (int i = 0; i < m_world.m_entityList.Count; i++) { EntityBase eb = m_world.m_entityList[i]; EntityInfo einfo = new EntityInfo(); einfo.id = eb.ID; einfo.infos = new List <ComponentInfo>(); foreach (var item in eb.m_compDict) { if (item.Value.GetType().IsSubclassOf(typeof(PlayerCommandBase))) { CommandComponent cc = (CommandComponent)item.Value; ComponentInfo info = new ComponentInfo(); cc.time = 0; info.m_compName = item.Value.GetType().Name; info.content = Serializer.Serialize(item.Value); einfo.infos.Add(info); } else if (IsFilter(item.Value.GetType().Name)) { ComponentInfo info = new ComponentInfo(); info.m_compName = item.Value.GetType().Name; info.content = Serializer.Serialize(item.Value); einfo.infos.Add(info); } } if (einfo.infos.Count > 0) { msg.infos.Add(einfo); } } List <EntityBase> list = GetEntityList(); for (int i = 0; i < list.Count; i++) { ConnectionComponent cc = list[i].GetComp <ConnectionComponent>(); ProtocolAnalysisService.SendMsg(cc.m_session, msg); } }
static void ReceviceSyncMsg(SyncSession session, T msg) { //Debug.Log("ReceviceSyncMsg " + msg.id + " content " + Serializer.Serialize(msg)); //消息确认 AffirmMsg amsg = new AffirmMsg(); amsg.index = msg.frame; amsg.time = msg.time; ProtocolAnalysisService.SendMsg(session, amsg); ConnectionComponent connectComp = session.m_connect; if (connectComp != null) { WorldBase world = connectComp.Entity.World; if (world.FrameCount <= msg.frame + 4) { connectComp.AddCommand(msg); } //msg.frame = world.FrameCount + 1; // //广播这帧 // if(connectComp.AddCommand(msg)) // { // //BroadcastCommand(world, connectComp, msg, false); // } //} //else //{ // //Debug.Log("丢弃掉 " + msg.frame); // //直接丢弃掉落后帧 // //Debug.Log("帧相等! " + msg.frame); // //if (!connectComp.m_isInframe) // //{ // // //当成最新的一帧来处理 // // msg.frame = world.FrameCount + 1; // // connectComp.AddCommand(msg); // //} // //else // //{ // // Debug.Log("在一帧之内插入了数据! " + msg.frame); // // msg.frame = world.FrameCount + 1; // // connectComp.AddCommand(msg); // //} //} ControlSpeed(connectComp, world, msg.frame); } }
static int CalcAdvanceFrame(ConnectionComponent connect) { int frame = connect.rtt / UpdateEngine.IntervalTime + 1; frame = Math.Min(7, frame); frame = Math.Max(2, frame); return(frame); }
static void ReceviceAffirmMsg(SyncSession session, AffirmMsg msg) { ConnectionComponent commandComp = session.m_connect; int nowTime = ServiceTime.GetServiceTime(); commandComp.rtt = nowTime - msg.time; //Debug.Log(" 收到确认消息 frame: " + msg.index + " id: " + commandComp.Entity.ID + " rtt " + commandComp.rtt); }
public override void EndFrame(int deltaTime) { List <EntityBase> list = GetEntityList(); for (int i = 0; i < list.Count; i++) { ConnectionComponent comp = list[i].GetComp <ConnectionComponent>(); comp.m_isInframe = false; } }
void PushSingleComponentData(ConnectionComponent comp) { //同步单例组件 foreach (var item in m_world.m_singleCompDict) { if (item.Key != "EntityRecordComponent") { PushSingletonComp(comp.m_session, item.Key); } } }
protected PathComputationServer(NetworkAddress networkAddress, string ipAddress, int signallingCloudListeningPort) { SignallingCloudListeningPort = signallingCloudListeningPort; NetworkAddress = networkAddress; _controlPlaneConnectionComponent = new ConnectionComponent(networkAddress, ipAddress, signallingCloudListeningPort, ConnectionManagerType.SignallingCloud); _controlPlaneConnectionComponent.UpdateState += (sender, state) => OnUpdateState(state); _controlPlaneConnectionComponent.ObjectReceived += OnSignallingMessageReceived; }
static void ReceviceAffirmMsg(SyncSession session, AffirmMsg msg) { ConnectionComponent commandComp = session.m_connect; commandComp.ClearForecast(msg.frame); int nowTime = ServiceTime.GetServiceTime(); commandComp.rtt = nowTime - msg.time; //Debug.Log("rtt " + commandComp.rtt); }
static void BroadcastSameCommand(WorldBase world, ConnectionComponent connectComp, SameCommand cmd, bool includeSelf) { cmd.time = ServiceTime.GetServiceTime(); List <EntityBase> list = world.GetEntiyList(new string[] { "ConnectionComponent" }); for (int i = 0; i < list.Count; i++) { ConnectionComponent cp = list[i].GetComp <ConnectionComponent>(); ProtocolAnalysisService.SendMsg(cp.m_session, cmd); } }
static void SendPursueMsg(ConnectionComponent connectComp, float speed) { if (connectComp.UpdateSpeed != speed) { PursueMsg pmsg = new PursueMsg(); pmsg.updateSpeed = speed; pmsg.advanceCount = CalcAdvanceFrame(connectComp); connectComp.UpdateSpeed = speed; ProtocolAnalysisService.SendMsg(connectComp.m_session, pmsg); } }
public NameServer(string ipAddress, int signallingCloudListeningPort) { _directory = new Directory(Address); Initialize(_directory); _policy = new Policy(Address); Initialize(_policy); _controlPlaneConnectionComponent = new ConnectionComponent(Address, ipAddress, signallingCloudListeningPort, ConnectionManagerType.SignallingCloud); _controlPlaneConnectionComponent.UpdateState += (sender, state) => OnUpdateState(state); _controlPlaneConnectionComponent.ObjectReceived += OnSignallingMessageReceived; }
public void SetAllSync(SyncComponent connectionComp) { List <EntityBase> list = GetEntityList(new string[] { "ConnectionComponent" }); for (int i = 0; i < list.Count; i++) { ConnectionComponent comp = list[i].GetComp <ConnectionComponent>(); if (!connectionComp.m_waitSyncList.Contains(comp)) { connectionComp.m_waitSyncList.Add(comp); } } }
void OnPlayerExit(EntityBase entity, params object[] objs) { ConnectionComponent comp = entity.GetComp <ConnectionComponent>(); comp.m_isWaitPushStart = false; SyncComponent sc = entity.GetComp <SyncComponent>(); SetAllSync(sc); //TODO 将来改成推送移除连接组件 //PushDestroyEntity(sc, entity); }
public override void NoRecalcLateFixedUpdate(int deltaTime) { if (!isDebug) { return; } Debug.Log("SyncDebugSystem " + m_world.FrameCount); DebugMsg msg = new DebugMsg(); msg.frame = m_world.FrameCount; msg.infos = new List <EntityInfo>(); for (int i = 0; i < m_world.m_entityList.Count; i++) { EntityBase eb = m_world.m_entityList[i]; EntityInfo einfo = new EntityInfo(); einfo.id = eb.ID; einfo.infos = new List <ComponentInfo>(); foreach (var item in eb.m_compDict) { if (item.Value.GetType().IsSubclassOf(typeof(MomentComponentBase))) { ComponentInfo info = new ComponentInfo(); info.m_compName = item.Value.GetType().Name; info.content = Serializer.Serialize(item.Value); einfo.infos.Add(info); if (info.m_compName == "MoveComponent" || info.m_compName == "CommandComponent") { Debug.Log(".id " + einfo.id + " m_compName " + info.m_compName + " content : " + info.content); } } } msg.infos.Add(einfo); } List <EntityBase> list = GetEntityList(); for (int i = 0; i < list.Count; i++) { ConnectionComponent cc = list[i].GetComp <ConnectionComponent>(); ProtocolAnalysisService.SendMsg(cc.m_session, msg); } }