public void RemoveScene(ulong sceneGuid) { SceneInfo sceneInfo; if (mFromSceneGuid2Server.TryRemove(sceneGuid, out sceneInfo)) { if (sceneInfo == null) { mLogger.Error("SceneBroker RemoveScene Guid = {0}", sceneGuid); return; } SceneServerManager.RemoveScene(sceneInfo.Server, 1); ConcurrentDictionary <ulong, SceneInfo> scenes; if (mFromServerIdAndSceneId2Guid.TryGetValue(CalcServerSceneId(sceneInfo.ServerId, sceneInfo.SceneId), out scenes)) { scenes.TryRemove(sceneGuid, out sceneInfo); //scenes.RemoveAll(item => item.SceneGuid == sceneGuid); } if (sceneInfo.CharacterIds.Count > 0) { mLogger.Error("SceneBroker RemoveScene CharacterIds = {0}", sceneInfo.CharacterIds.GetDataString()); } sceneInfo.CharacterIds.Clear(); } }
public override void BackEndsOnConnect(SocketClient client, int index) { if (index == 0) { SceneServerManager.PushServer(client, 2); } else { SceneServerManager.PushServer(client, 0); } }
//desc.FuncId == 3001 销毁场景 public void DestroyScene(ServerClient client, ServiceDesc desc) { var msg = ProtocolExtension.Deserialize <__RPC_Scene_SBDestroyScene_ARG_uint64_guid__>(desc.Data); var ret = new __RPC_Scene_SBDestroyScene_RET_uint64__(); SceneInfo info; if (mFromSceneGuid2Server.TryGetValue(msg.Guid, out info)) { if (info.CharacterIds.Count == 0) { SceneServerManager.RemoveScene(info.Server, 1); ConcurrentDictionary <ulong, SceneInfo> scenes; if (mFromServerIdAndSceneId2Guid.TryGetValue(CalcServerSceneId(info.ServerId, info.SceneId), out scenes)) { SceneInfo info2; scenes.TryRemove(msg.Guid, out info2); //scenes.RemoveAll(item => item.SceneGuid == msg.Guid); mFromSceneGuid2Server.TryRemove(msg.Guid, out info); ret.ReturnValue = 1; } else { // 这种情况不应该发生 mLogger.Error( "mFromSceneGuid2Server and mFromServerIdAndSceneId2Guid not consistent, {0},{1},{2}", info.ServerId, info.SceneId, info.SceneGuid); mFromSceneGuid2Server.TryRemove(msg.Guid, out info); ret.ReturnValue = 1; } } else { ret.ReturnValue = 0; } } else { ret.ReturnValue = 0; } desc.Data = ProtocolExtension.Serialize(ret); client.SendMessage(desc); }
//创造新场景 public void CreateNewScene(SceneInfo sceneInfo, SceneParam param = null) { var content = new __RPC_Scene_BSCreateScene_ARG_int32_serverId_int32_sceneId_uint64_guid_SceneParam_sceneParam__(); content.ServerId = sceneInfo.ServerId; content.SceneId = sceneInfo.SceneId; content.Guid = sceneInfo.SceneGuid; content.SceneParam = param ?? new SceneParam(); var message = new ServiceDesc(); message.FuncId = 3010; message.ServiceType = (int)ServiceType.Scene; message.PacketId = mBroker.GetUniquePacketId(); message.Data = ProtocolExtension.Serialize(content); message.Type = (int)MessageType.BS; mLogger.Info("Notify Scene server CreateNewScene {0}, {1}, {2}", sceneInfo.ServerId, sceneInfo.SceneId, sceneInfo.SceneGuid); ConcurrentDictionary <ulong, SceneInfo> infos; var serverSceneId = CalcServerSceneId(sceneInfo.ServerId, sceneInfo.SceneId); if (mFromServerIdAndSceneId2Guid.TryGetValue(serverSceneId, out infos)) { infos.TryAdd(sceneInfo.SceneGuid, sceneInfo); } else { var temp = new ConcurrentDictionary <ulong, SceneInfo>(); temp.TryAdd(sceneInfo.SceneGuid, sceneInfo); mFromServerIdAndSceneId2Guid.TryAdd(serverSceneId, temp); } mBroker.DebugCounter[17]++; mFromSceneGuid2Server.TryAdd(sceneInfo.SceneGuid, sceneInfo); SceneServerManager.CreateScene(sceneInfo.Server, 1); var act = new Action <bool, ServiceDesc>((b, item) => { if (b) { if (item.Error == 0) { mLogger.Info("Scene server CreateNewScene replied {0}, {1}, {2}", sceneInfo.ServerId, sceneInfo.SceneId, sceneInfo.SceneGuid); sceneInfo.Status = SceneStatus.ReadyToEnter; foreach (var action in sceneInfo.WaitingActions) { try { action(); } catch (Exception ex) { mLogger.Error(ex, "Create new scene callback error."); } } sceneInfo.WaitingActions.Clear(); } else { mLogger.Error("CreateNewScene failed {0}....", item.Error); } } else { mLogger.Error("CreateNewScene timeout...."); } }); mBroker.RegisterCallback(message.PacketId, act); sceneInfo.Server.SendMessage(message); }
//根据Guid获取服务器 public SocketClient GetSceneSocket(ulong guid) { return(SceneServerManager.GetFreeSceneServer()); }