//func = 3605 public void RequestSceneInfo(ServerClient client, ServiceDesc desc) { var msg = ProtocolExtension.Deserialize <__RPC_Scene_RequestSceneInfo_ARG_int32_serverId_int32_sceneTypeId__>(desc.Data); var serverId = msg.ServerId; var sceneId = msg.SceneTypeId; var key = CalcServerSceneId(serverId, sceneId); var retMsg = new __RPC_Scene_RequestSceneInfo_RET_MsgScenesInfo__(); retMsg.ReturnValue = new MsgScenesInfo(); ConcurrentDictionary <ulong, SceneInfo> infos; if (mFromServerIdAndSceneId2Guid.TryGetValue(key, out infos)) { if (null != infos && !infos.IsEmpty) { foreach (var info in infos) { var sceneInfo = new MsgSceneInfo(); sceneInfo.Guid = info.Value.SceneGuid; sceneInfo.PlayerCount = info.Value.CharacterIds.Count; retMsg.ReturnValue.Info.Add(sceneInfo); } } } desc.Data = ProtocolExtension.Serialize(retMsg); client.SendMessage(desc); }
//func = 3072 public void IsSceneExist(ServerClient client, ServiceDesc desc) { var msg = ProtocolExtension.Deserialize <__RPC_Scene_IsSceneExist_ARG_uint64_sceneGuid__>(desc.Data); var sceneGuid = msg.SceneGuid; var exist = mFromSceneGuid2Server.ContainsKey(sceneGuid); desc.Data = ProtocolExtension.Serialize(exist); client.SendMessage(desc); }
//desc.FuncId == 3002 此场景已经不存在 public void NoHaveScene(ServerClient client, ServiceDesc desc) { var msg = ProtocolExtension.Deserialize <__RPC_Scene_NotifySceneNotExist_ARG_uint64_sceneId_uint64_characterId__>( desc.Data); var characterId = msg.CharacterId; mLogger.Info("Enter Game {0} - NotifySceneNotExist - 1 - {1}", characterId, TimeManager.Timer.ElapsedMilliseconds); var info = mBroker.GetCharacter(characterId); if (info == null) { return; } var serverId = info.SceneInfo.ServerId; var sceneId = info.SceneInfo.SceneId; mLogger.Info("Enter Game {0} - NotifySceneNotExist - 2 - {1}", characterId, TimeManager.Timer.ElapsedMilliseconds); RemoveScene(msg.SceneId); mLogger.Info("Enter Game {0} - NotifySceneNotExist - 3 - {1}", characterId, TimeManager.Timer.ElapsedMilliseconds); var newScene = SelectServerForScene(serverId, sceneId, msg.CharacterId); if (newScene == null) { mLogger.Info("Enter Game {0} - NotifySceneNotExist - 4 - {1}", characterId, TimeManager.Timer.ElapsedMilliseconds); newScene = CreateNewSceneInfo(serverId, sceneId, 0); newScene.PushCharacter(msg.CharacterId); //DebugCounter[1]++; var param = new SceneParam(); param.ObjId = desc.CharacterId; CreateNewScene(newScene, param); } if (newScene.Status == SceneStatus.ReadyToEnter) { mBroker.ChangeScene(msg.CharacterId, newScene); mLogger.Info("Enter Game {0} - NotifySceneNotExist - 5 - {1}", characterId, TimeManager.Timer.ElapsedMilliseconds); } else { newScene.WaitingActions.Add(() => { mLogger.Info("Enter Game {0} - NotifySceneNotExist - 6 - {1}", characterId, TimeManager.Timer.ElapsedMilliseconds); mBroker.ChangeScene(msg.CharacterId, newScene); }); } }
//desc.FuncId == 3039 public void CheckCharacterOnline(ServerClient client, ServiceDesc desc) { var request = ProtocolExtension.Deserialize <__RPC_Scene_SBCheckCharacterOnline_ARG_Uint64Array_toList__>(desc.Data); var reply = new __RPC_Scene_SBCheckCharacterOnline_RET_Int32Array__(); reply.ReturnValue = new Int32Array(); foreach (var id in request.ToList.Items) { reply.ReturnValue.Items.Add(mCharacterInfoManager.ContainsKey(id) ? 1 : 0); } desc.Data = ProtocolExtension.Serialize(reply); client.SendMessage(desc); }
//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); }
//func = 3499 public void SeekSceneIndexByCharacterId(ServerClient client, ServiceDesc desc) { var msg = ProtocolExtension .Deserialize <__RPC_Scene_SBReconnectNotifyScene_ARG_uint64_oldclientId_uint64_newclientId_uint64_characterId__>( desc.Data); var characterId = msg.CharacterId; var clientId = msg.NewclientId; var oldClientId = msg.OldclientId; CommonBroker.CharacterInfo characterCommon; if (mBroker.mCharacterInfoManager.TryGetValue(characterId, out characterCommon)) { var character = characterCommon as CharacterSceneInfo; if (character == null) { return; } var index = mBroker.GetServerIndex(character.Server); if (index == -1) { return; } var gateDesc = new ServiceDesc(); gateDesc.Type = (int)MessageType.ReConnetServerToGate; gateDesc.CharacterId = characterId; gateDesc.ServiceType = (int)ServiceType.Scene; gateDesc.ClientId = clientId; gateDesc.Routing.Add(oldClientId); gateDesc.Routing.Add((ulong)index); character.Gate.Gate.SendMessage(gateDesc); } }
/// <summary> /// This function will be called in multithread, so THREAD SAFE is very important. /// </summary> /// <param name="client"></param> /// <param name="desc"></param> private void ClientMessageReceived(ServerClient client, ServiceDesc desc) { try { Logger.Debug("Received a message from Client {0}, {1}.", client.ClientId, ((IPEndPoint)client.RemoteEndPoint).Address.MapToIPv4()); if (desc.ServiceType == (int)ServiceType.Directory) { switch (desc.FuncId) { case 8000: { var info = ProtocolExtension .Deserialize < __RPC_Directory_CheckVersion_ARG_string_lang_string_platform_string_channel_string_version__ >(desc.Data); var lang = info.Lang.ToLowerInvariant(); var platform = info.Platform.ToLowerInvariant(); var channel = info.Channel.ToLowerInvariant(); var big = info.Version; var sb = new StringBuilder(); sb.Append(lang); sb.Append("."); sb.Append(platform); sb.Append("."); sb.Append(channel); sb.Append("."); sb.Append(big); try { mLock.EnterReadLock(); Content c; if (mContents.TryGetValue(sb.ToString(), out c)) { var ret = new __RPC_Directory_CheckVersion_RET_VersionInfo__(); ret.ReturnValue = new VersionInfo(); ret.ReturnValue.SmallVersion = c.SmallVersion; ret.ReturnValue.AnnoucementURL = c.AnnoucementURL; ret.ReturnValue.ResourceURL = c.ResourceURL; ret.ReturnValue.HasNewVersion = c.HasNewVersion ? 1 : 0; ret.ReturnValue.NewVersionURL = c.NewVersionURL; ret.ReturnValue.ReviewState = c.ReviewState; var length = c.GateAddress.Length; if (length > 0) { if (length == 1) { ret.ReturnValue.GateAddress = c.GateAddress[0]; } else { ret.ReturnValue.GateAddress = c.GateAddress[random.Next(length)]; } } desc.Data = ProtocolExtension.Serialize(ret); client.SendMessage(desc); } else { desc.Data = null; desc.Error = 1; client.SendMessage(desc); } } catch (Exception ex) { Logger.Error(ex, "8000 error."); } finally { mLock.ExitReadLock(); } } break; case 8001: { var info = ProtocolExtension .Deserialize < __RPC_Directory_CheckVersion_ARG_string_lang_string_platform_string_channel_string_version__ >(desc.Data); var lang = info.Lang.ToLowerInvariant(); var platform = info.Platform.ToLowerInvariant(); var channel = info.Channel.ToLowerInvariant(); var big = info.Version; var sb = new StringBuilder(); sb.Append(lang); sb.Append("."); sb.Append(platform); sb.Append("."); sb.Append(channel); sb.Append("."); sb.Append(big); try { mLock.EnterReadLock(); Content c; if (mContents.TryGetValue(sb.ToString(), out c)) { var ret = new __RPC_Directory_CheckVersion_RET_VersionInfo__(); ret.ReturnValue = new VersionInfo(); ret.ReturnValue.SmallVersion = c.SmallVersion; ret.ReturnValue.AnnoucementURL = c.AnnoucementURL; ret.ReturnValue.ResourceURL = c.ResourceURL; ret.ReturnValue.HasNewVersion = c.HasNewVersion ? 1 : 0; ret.ReturnValue.NewVersionURL = c.NewVersionURL; ret.ReturnValue.ReviewState = c.ReviewState; ret.ReturnValue.GateAddress = c.GateAddressString; desc.Data = ProtocolExtension.Serialize(ret); client.SendMessage(desc); } else { desc.Data = null; desc.Error = 1; client.SendMessage(desc); } } catch (Exception ex) { Logger.Error(ex, "8001 error."); } finally { mLock.ExitReadLock(); } } break; case 8002: { var info = ProtocolExtension .Deserialize < __RPC_Directory_CheckVersion3_ARG_string_lang_string_platform_string_channel_string_version__ >(desc.Data); var lang = info.Lang.ToLowerInvariant(); var platform = info.Platform.ToLowerInvariant(); var channel = info.Channel.ToLowerInvariant(); var big = info.Version; var sb = new StringBuilder(); sb.Append(lang); sb.Append("."); sb.Append(platform); sb.Append("."); sb.Append(channel); sb.Append("."); sb.Append(big); try { mLock.EnterReadLock(); Content c; if (mContents.TryGetValue(sb.ToString(), out c)) { var ret = new __RPC_Directory_CheckVersion3_RET_VersionInfo__(); ret.ReturnValue = new VersionInfo(); ret.ReturnValue.SmallVersion = c.SmallVersion; ret.ReturnValue.AnnoucementURL = c.AnnoucementURL; ret.ReturnValue.ResourceURL = c.ResourceURL; ret.ReturnValue.HasNewVersion = c.HasNewVersion ? 1 : 0; ret.ReturnValue.NewVersionURL = c.NewVersionURL; ret.ReturnValue.ReviewState = c.ReviewState; if (c.IpWhiteList.Contains(((IPEndPoint)client.RemoteEndPoint).Address.MapToIPv4().ToString())) { ret.ReturnValue.GateAddress = c.RetargetGateAddress; } else { ret.ReturnValue.GateAddress = c.GateAddressString; } ret.ReturnValue.ForceShowAnn = c.ForceShowAnn; ret.ReturnValue.Isbn = c.Isbn; desc.Data = ProtocolExtension.Serialize(ret); client.SendMessage(desc); } else { desc.Data = null; desc.Error = 1; client.SendMessage(desc); } } catch (Exception ex) { Logger.Error(ex, "8001 error."); } finally { mLock.ExitReadLock(); } } break; default: Logger.Error("Unknown funcion id:{0}", desc.FuncId); break; } } } catch (Exception e) { Logger.Error(e, "ClientMessageReceived"); } }
//funId = 3071 public void MergeSceneByTeam(ServerClient client, ServiceDesc desc) { var msg = ProtocolExtension.Deserialize <__RPC_Scene_MergeSceneByTeam_ARG_IdList_ids__>(desc.Data); // 先按场景Id分组 //var dict = msg.Ids.Ids.Select(i => mBroker.mFromCharacterId2Server[i]).ToLookup(k => k.SceneInfo.SceneId, v => v); var dict = new Dictionary <int, List <CharacterSceneInfo> >(); foreach (var id in msg.Ids.Ids) { var c = mBroker.GetCharacter(id); if (c == null) { continue; } List <CharacterSceneInfo> list; if (!dict.TryGetValue(c.SceneInfo.SceneId, out list)) { list = new List <CharacterSceneInfo>(); dict[c.SceneInfo.SceneId] = list; } list.Add(c); } foreach (var k in dict) { // 只合并非副本场景 if (!IsNormalScene(Table.GetScene(k.Key))) { continue; } var v = dict[k.Key]; // 找到人最少的Server SceneInfo minCharacterSceneInfo = null; foreach (var info in v) { if (minCharacterSceneInfo == null) { minCharacterSceneInfo = info.SceneInfo; continue; } if (info.SceneInfo.CharacterIds.Count < minCharacterSceneInfo.CharacterIds.Count) { minCharacterSceneInfo = info.SceneInfo; } } if (minCharacterSceneInfo == null) { continue; } // 把其他人都移过去 foreach (var info in v) { // 如果可以跨服或者服务器Id相同,才可以合并 if (info.SceneInfo != minCharacterSceneInfo && (info.SceneInfo.SceneRecord.CanCrossServer == 1 || info.SceneInfo.ServerId == minCharacterSceneInfo.ServerId)) { mBroker.ChangeScene(info.CharacterId, minCharacterSceneInfo); } } } }
public virtual void OnSocketListenerMessageReceiveSB(ServerClient client, ServiceDesc desc) { if (desc.FuncId % 1000 == 30) { //SBGetAllOnlineCharacterInServer var info = ProtocolExtension.Deserialize <__RPC_Logic_SBGetAllOnlineCharacterInServer_ARG_int32_serverId__>( desc.Data); ConcurrentDictionary <ulong, int> bag; if (mFromServerId2CharacterId.TryGetValue((uint)info.ServerId, out bag)) { var ret = new __RPC_Logic_SBGetAllOnlineCharacterInServer_RET_Uint64Array__(); ret.ReturnValue.Items.AddRange(bag.Keys); desc.Data = ProtocolExtension.Serialize(ret); client.SendMessage(desc); } } else if (desc.FuncId % 1000 == 40) { // QueryBrokerStatus //var ret = new __RPC_Logic_QueryBrokerStatus_RET_CommonBrokerStatus__(); //var status = ret.ReturnValue = new CommonBrokerStatus(); //status.CommonStatus = new ServerCommonStatus(); //status.CommonStatus.ByteReceivedPerSecond = (uint) mFrontEndServer.ByteReceivedPerSecond; //status.CommonStatus.ByteSendPerSecond = (uint) mFrontEndServer.ByteSendPerSecond; //status.CommonStatus.MessageReceivedPerSecond = (uint) mFrontEndServer.MessageReceivedPerSecond; //status.CommonStatus.MessageSendPerSecond = (uint) mFrontEndServer.MessageSendPerSecond; //status.CommonStatus.ConnectionCount = (uint) mFrontEndServer.ConnectionCount; //status.ConnectionInfo.AddRange(mBackEnds.Select(item => //{ // var conn = new ConnectionStatus(); // conn.ByteReceivedPerSecond = (uint) item.ByteReceivedPerSecond; // conn.ByteSendPerSecond = (uint) item.ByteSendPerSecond; // conn.MessageReceivedPerSecond = (uint) item.MessageReceivedPerSecond; // conn.MessageSendPerSecond = (uint) item.MessageSendPerSecond; // conn.Target = (uint) ((UserData) item.UserData).Id; // conn.Latency = (float) item.Latency.TotalMilliseconds; // return conn; //})); //desc.Data = ProtocolExtension.Serialize(ret); //client.SendMessage(desc); } else if (desc.FuncId % 1000 == 500) { //收到login断线清理 OnSocketListenerMessageReceiveCleanEx(desc); } else if (desc.FuncId % 1000 == 502) { var ret = new __RPC_Scene_SBGetServerCharacterCount_RET_Dict_int_int_Data__(); ret.ReturnValue = new Dict_int_int_Data(); foreach (var s in mFromServerId2CharacterId) { ret.ReturnValue.Data.Add((int)s.Key, s.Value.Count); } desc.Data = ProtocolExtension.Serialize(ret); client.SendMessage(desc); } else if (desc.FuncId % 1000 == 503) { // var info = ProtocolExtension.Deserialize<__RPC_Scene_SBModifyCharacterClientId_ARG_uint64_oldClientId_uint64_newClientId_uint64_characterId__>(desc.Data); // var character = GetCharacterInfo(info.CharacterId); // if (character == null) // { // client.SendMessage(desc); // return; // } // if (character.ClientId != info.OldClientId) // { // Logger.Error("funID%1000=503,brokerClientId={0},oldClientId={1},newClientId={2}", character.ClientId, info.OldClientId, info.NewClientId); // client.SendMessage(desc); // return; // } // // //执行 // ulong brokerId; // mFromClientId2CharacterId.TryRemove(character.ClientId, out brokerId); // mFromClientId2CharacterId.TryAdd(character.ClientId, info.CharacterId); // character.ClientId = info.NewClientId; // var ret = new __RPC_Scene_SBModifyCharacterClientId_RET_int32__(); // ret.ReturnValue = 1; // desc.Data = ProtocolExtension.Serialize(ret); // client.SendMessage(desc); } else { Logger.Error( "OnSocketListenerMessageReceiveSB faild!! ClientId={0},CharacterId={1},FuncId={2},ServiceType={3}", desc.ClientId, desc.CharacterId, desc.FuncId, desc.ServiceType); } }
//desc.FuncId == 3069 一堆人同时进一个场景 private void ChangeSceneByTeam(ServerClient client, ServiceDesc desc) { var msg = ProtocolExtension.Deserialize <__RPC_Scene_SBChangeSceneByTeam_ARG_ChangeSceneInfo_changeSceneData__>( desc.Data); var sceneInfo = mSceneManager.SelectOldScene(msg.ChangeSceneData.SceneGuid, msg.ChangeSceneData.ServerId, msg.ChangeSceneData.SceneId, 0, msg.ChangeSceneData.CheckFull); PlayerLog.WriteLog(888, "ChangeSceneByTeam characterId={0},ServerId={1},SceneId={2},SceneGuid={3}", msg.ChangeSceneData.Guids.GetDataString(), msg.ChangeSceneData.ServerId, msg.ChangeSceneData.SceneId, msg.ChangeSceneData.SceneGuid); // 如果场景不存在,不用造新的 if (sceneInfo == null && msg.ChangeSceneData.SceneId == -1) { PlayerLog.WriteLog(888, "ChangeSceneByTeam not find and not new,guids={0}", msg.ChangeSceneData.Guids.GetDataString()); desc.Data = ProtocolExtension.Serialize(new __RPC_Scene_SBChangeSceneByTeam_RET_uint64__ { ReturnValue = 0 }); client.SendMessage(desc); return; } var param = msg.ChangeSceneData.Pos; if (param == null) { param = new SceneParam(); } param.ObjId = desc.CharacterId; if (sceneInfo == null) { sceneInfo = mSceneManager.CreateNewSceneInfo(msg.ChangeSceneData.ServerId, msg.ChangeSceneData.SceneId, msg.ChangeSceneData.SceneGuid); mSceneManager.CreateNewScene(sceneInfo, param); //sceneInfo.PushCharacter(desc.CharacterId); } desc.Data = ProtocolExtension.Serialize(new __RPC_Scene_SBChangeSceneByTeam_RET_uint64__ { ReturnValue = sceneInfo.SceneGuid }); client.SendMessage(desc); var changeSceneInfos = new List <ChangeSceneInfo>(); foreach (var characterId in msg.ChangeSceneData.Guids) { sceneInfo.CharacterIds.Add(characterId); var info = GetCharacter(characterId); if (info == null) { continue; } var oldSceneInfo = info.SceneInfo; if (oldSceneInfo != sceneInfo) { oldSceneInfo.CharacterIds.Remove(characterId); info.SceneInfo = sceneInfo; } changeSceneInfos.Add(new ChangeSceneInfo { Info = info, OldSceneInfo = oldSceneInfo, NewSceneInfo = sceneInfo }); } var type = msg.ChangeSceneData.Type; var act = new Action(() => { PlayerLog.WriteLog(888, "ChangeSceneByTeam not find! new scene over guids={0}", msg.ChangeSceneData.Guids.GetDataString()); foreach (var changeSceneInfo in changeSceneInfos) { Logger.Info("Enter Game {0} {1} {2} - ChangeSceneByTeam - 2 - {3}", changeSceneInfo.Info.CharacterId, changeSceneInfo.OldSceneInfo.SceneGuid, changeSceneInfo.NewSceneInfo.SceneGuid, TimeManager.Timer.ElapsedMilliseconds); ChangeSceneOver(changeSceneInfo.Info, changeSceneInfo.OldSceneInfo, changeSceneInfo.NewSceneInfo, b => { mSceneManager.NotifyEnterScene(changeSceneInfo.Info, type, param); }); } }); if (sceneInfo.Status == SceneStatus.ReadyToEnter) { act(); } else { sceneInfo.WaitingActions.Add(act); } }
public override void OnSocketListenerMessageReceiveSB(ServerClient client, ServiceDesc desc) { // 换场景 if (desc.FuncId == 3000) { if (!mWaitingEvents.IsAddingCompleted) { mWaitingEvents.Add(() => { ChangeScene(client, desc); }); } } // 销毁场景 else if (desc.FuncId == 3001) { if (!mWaitingEvents.IsAddingCompleted) { mWaitingEvents.Add(() => { mSceneManager.DestroyScene(client, desc); }); } } //查询scene是否存在 else if (desc.FuncId == 3002) { if (!mWaitingEvents.IsAddingCompleted) { mWaitingEvents.Add(() => { mSceneManager.NoHaveScene(client, desc); }); } } // 检查是否在线 else if (desc.FuncId == 3039) { CheckCharacterOnline(client, desc); } //广播表格重载 else if (desc.FuncId == 3050) { if (!mWaitingEvents.IsAddingCompleted) { var info = ProtocolExtension.Deserialize <__RPC_Scene_SBReloadTable_ARG_string_tableName__>(desc.Data); mWaitingEvents.Add(() => { Table.ReloadTable(info.TableName); }); } } else if (desc.FuncId == 3061) { OnNotifyConnected(desc); } else if (desc.FuncId == 3062) { OnNotifyLost(desc); } else if (desc.FuncId == 3063) { if (!mWaitingEvents.IsAddingCompleted) { mWaitingEvents.Add(() => { OnSceneFinished(desc); }); } } else if (desc.FuncId == 3069) { if (!mWaitingEvents.IsAddingCompleted) { mWaitingEvents.Add(() => { ChangeSceneByTeam(client, desc); }); } } else if (desc.FuncId == 3071) { if (!mWaitingEvents.IsAddingCompleted) { mWaitingEvents.Add(() => { mSceneManager.MergeSceneByTeam(client, desc); }); } } else if (desc.FuncId == 3072) { if (!mWaitingEvents.IsAddingCompleted) { mWaitingEvents.Add(() => { mSceneManager.IsSceneExist(client, desc); }); } } else if (desc.FuncId == 3499) { if (!mWaitingEvents.IsAddingCompleted) { mWaitingEvents.Add(() => { mSceneManager.SeekSceneIndexByCharacterId(client, desc); }); } } else if (desc.FuncId == 3605) { if (!mWaitingEvents.IsAddingCompleted) { mWaitingEvents.Add(() => { mSceneManager.RequestSceneInfo(client, desc); }); } } else { base.OnSocketListenerMessageReceiveSB(client, desc); } }