You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

274 lines
9.9 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Sog;
using ProtoCSStruct;
namespace Operation
{
public class RealmOperationRes
{
public int code;
public string msg;
public int realmId;
}
public class Realmlist : BaseReloadableService
{
private static long m_lastTickTime = 0;
public static List<RealmBriefInfo> allRealm = new List<RealmBriefInfo>();
public static Dictionary<uint, List<RealmOperationRes>> realmOperationDic = new Dictionary<uint, List<RealmOperationRes>>();
public static Dictionary<string, Dictionary<int, List<RealmBriefInfo>>> realmDic = new Dictionary<string, Dictionary<int, List<RealmBriefInfo>>>();
private static long lastForceGetAllRealmTime = 0;
public static string svnVersion;
public override void Dispose()
{
}
public override int GetServiceType()
{
return OperationServiceType.RealmlistSvc;
}
public void OnRealmBriefRes(uint serverID, StructPacket packet)
{
ref var res = ref packet.GetMessage<SSRealmBriefRes>();
var svrData = OperationServerUtils.GetOperationServerData();
for (int i = 0; i < res.RealmList.Count; i++)
{
var realm = new RealmBriefInfo(ref res.RealmList[i]);
svrData.m_allRealm[realm.realmId] = realm;
}
if (!res.RealmListMd5.IsEmpty())
{
svrData.recvRealmListMd5 = res.RealmListMd5.GetString();
}
TraceLog.Trace("OnRealmBriefRes count {0} svrData.recvRealmListMd5 {1}", svrData.m_allRealm.Count, svrData.recvRealmListMd5);
}
public void OnRealmAllbriefRes(uint serverID, StructPacket packet)
{
ref var res = ref packet.GetMessage<SSRealmAllBriefRes>();
for (int i = 0; i < res.RealmList.Count; i++)
{
var realm = new RealmBriefInfo(ref res.RealmList[i]);
if (!realmDic.ContainsKey(realm.areaName))
{
Dictionary<int, List<RealmBriefInfo>> m_tempDic = new Dictionary<int, List<RealmBriefInfo>>();
realmDic.Add(realm.areaName, m_tempDic);
}
if (!realmDic[realm.areaName].ContainsKey(realm.WorldId))
{
List<RealmBriefInfo> m_tempList = new List<RealmBriefInfo>();
realmDic[realm.areaName].Add(realm.WorldId, m_tempList);
}
realmDic[realm.areaName][realm.WorldId].Add(realm);
}
if (res.IsEnd == 1)
{
HttpRequestInfo m_request = null;
try
{
var id = res.Guid;
TraceLog.Trace("OnRealmAllbriefRes success id {0}", id);
m_request = OperationServer.m_httpService.m_requestList.Find(f => f.Http.id == id);
LitJson.JsonData builder = new LitJson.JsonData();
HttpQueryParams queryParams = new HttpQueryParams(m_request.Http.httpRequest.Url);
Realm.OnResMsg(id, builder, m_request.Http.httpResponse, m_request.Http.httpRequest, queryParams, realmDic);
m_request.Http.httpResponse.ContentAsUTF8 = builder.ToJson();
}
catch (Exception ex)
{
TraceLog.Error("Realmlist.OnAllRealmRes Error Msg {0}", ex.Message);
}
finally
{
if (m_request != null)
{
m_request.ProcessEnd = true;
}
realmDic.Clear();
}
}
}
//查询最大bigrealmid回包
public void OnAllRealmRes(uint serverID, StructPacket packet)
{
HttpRequestInfo m_request = null;
try
{
ref var res = ref packet.GetMessage<SSAllRealmRes>();
var id = res.Guid;
m_request = OperationServer.m_httpService.m_requestList.Find(f => f.Http.id == id);
LitJson.JsonData builder = new LitJson.JsonData();
HttpQueryParams queryParams = new HttpQueryParams(m_request.Http.httpRequest.Url);
Realm.OnResMsg(id, builder, m_request.Http.httpResponse, m_request.Http.httpRequest, queryParams, ref res);
m_request.Http.httpResponse.ContentAsUTF8 = builder.ToJson();
}
catch (Exception ex)
{
TraceLog.Error("Realmlist.OnAllRealmRes Error Msg {0}", ex.Message);
}
finally
{
if (m_request != null)
{
m_request.ProcessEnd = true;
}
}
}
//新增账号信息回包
public void OnAddRealmRes(uint serverID, StructPacket packet)
{
HttpRequestInfo m_request = null;
try
{
ref var res = ref packet.GetMessage<SSAddRealmRes>();
var id = res.Id;
m_request = OperationServer.m_httpService.m_requestList.Find(f => f.Http.id == id);
LitJson.JsonData builder = new LitJson.JsonData();
HttpQueryParams queryParams = new HttpQueryParams(m_request.Http.httpRequest.Url);
Realm.OnResMsg(builder, m_request.Http.httpResponse, m_request.Http.httpRequest, queryParams, ref res);
m_request.Http.httpResponse.ContentAsUTF8 = builder.ToJson();
}
catch (Exception ex)
{
TraceLog.Error("Realmlist.OnAddRealmRes Error Msg {0}", ex.Message);
}
finally
{
if (m_request != null)
{
m_request.ProcessEnd = true;
}
}
}
public void OnRealmOperationRes(uint serverID, StructPacket packet)
{
ref var res = ref packet.GetMessage<SSRealmOperationRes>();
var id = res.Id;
var realmOperationRes = new RealmOperationRes();
realmOperationRes.code = res.Code;
realmOperationRes.msg = res.Msg.ToString();
realmOperationRes.realmId = res.RealmId;
if (realmOperationDic.ContainsKey(id))
{
realmOperationDic[id].Add(realmOperationRes);
}
else
{
List<RealmOperationRes> list = new List<RealmOperationRes> { realmOperationRes };
realmOperationDic.Add(id, list);
}
if (res.End == 1)
{
HttpRequestInfo m_request = null;
try
{
var resList = realmOperationDic[id];
m_request = OperationServer.m_httpService.m_requestList.Find(f => f.Http.id == id);
LitJson.JsonData builder = new LitJson.JsonData();
HttpQueryParams queryParams = new HttpQueryParams(m_request.Http.httpRequest.Url);
Realm.OnResMsg(builder, m_request.Http.httpResponse, m_request.Http.httpRequest, queryParams, resList);
m_request.Http.httpResponse.ContentAsUTF8 = builder.ToJson();
}
catch (Exception ex)
{
Realm.realmIdDictory.Remove(res.RealmId);
TraceLog.Error("Realmlist.OnAddRealmRes Error Msg {0}", ex.Message);
}
finally
{
if (m_request != null)
{
m_request.ProcessEnd = true;
}
realmOperationDic.Remove(id);
}
}
else
{
Realm.realmIdDictory.Remove(res.RealmId);
}
}
public void OnRealmOnlinePlayer(uint serverID, StructPacket packet)
{
ref var req = ref packet.GetMessage<SSRealmOnlinePlayerToOpsvr>();
var realmOnlineInfo = OperationServerUtils.GetOperationServerData().m_allRealmOnlineInfo;
for (int i = 0; i < req.RealmList.Count; i++)
{
if(!realmOnlineInfo.ContainsKey(req.RealmList[i].RealmID))
{
realmOnlineInfo.Add(req.RealmList[i].RealmID, req.RealmList[i].OnlinePlayer);
}
realmOnlineInfo[req.RealmList[i].RealmID] = req.RealmList[i].OnlinePlayer;
}
}
public static void OnTick(long nowMs)
{
long nowSec = nowMs / 1000;
int timeReqInterval = 60;
//缺省一分钟一次,如果没成功拉到过列表,则10秒一次
if (string.IsNullOrEmpty(OperationServerUtils.GetOperationServerData().recvRealmListMd5))
{
timeReqInterval = 10;
}
//60秒一次
if (nowSec - m_lastTickTime < timeReqInterval)
{
return;
}
m_lastTickTime = nowSec;
var realmReq = new SSRealmBriefReq { GameSvrId = OperationServerUtils.GetAppID() };
//为了以防万一,每10分钟强制拉一次全量
if (nowSec - lastForceGetAllRealmTime >= 600)
{
lastForceGetAllRealmTime = nowSec;
}
else
{
realmReq.RealmListMd5.SetString(OperationServerUtils.GetOperationServerData().recvRealmListMd5);
}
OperationServerUtils.GetPacketSender().SendToRealmlistServer((int)SSMsgID.RealmBriefReq, ref realmReq, 0);
if(RoleInfoMap.needSave)
{
RoleInfoMap.SaveDataToFile();
}
}
}
}