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.
138 lines
4.2 KiB
138 lines
4.2 KiB
/*
|
|
Sog 游戏基础库
|
|
2016 by zouwei
|
|
*/
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Sog
|
|
{
|
|
/*
|
|
* server id
|
|
* 16 8 8
|
|
* worldid,servertypeid,instanceid
|
|
*/
|
|
public static class ServerIDUtils
|
|
{
|
|
//基于服务器id是有限的这个前提,IDToString可以做性能优化,整个服务器组一般来说进程数量不会超过1000个吧
|
|
//加cache性能提高明显,日志中大量使用IDToString,优化还是有必要的
|
|
private const int MAX_CHACHE_ID_COUNT = 1000;
|
|
private static Dictionary<uint, string> m_serverIDCache = new Dictionary<uint, string>(MAX_CHACHE_ID_COUNT);
|
|
|
|
|
|
|
|
public static uint IDToNumber(string strID)
|
|
{
|
|
string[] arrayID = strID.Split('.');
|
|
if(arrayID.Length != 3)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
uint sip1 = UInt32.Parse(arrayID[0]);
|
|
uint sip2 = UInt32.Parse(arrayID[1]);
|
|
uint sip3 = UInt32.Parse(arrayID[2]);
|
|
|
|
uint tmpNumber;
|
|
tmpNumber = sip1 * 256 * 256 + sip2 * 256 + sip3;
|
|
return tmpNumber;
|
|
}
|
|
|
|
//
|
|
public static string IDToString(uint id)
|
|
{
|
|
string strID;
|
|
|
|
//说明,用TryGetValue比判断ContainsKey后再取值效率高,原因是TryGetValue只需要查找一次,ContainsKey后再取值查找了2次
|
|
if(m_serverIDCache.TryGetValue(id, out strID))
|
|
{
|
|
return strID;
|
|
}
|
|
|
|
|
|
uint s1 = id >> 16;
|
|
|
|
uint s2 = (id & 0x0000ffff) >> 8;
|
|
|
|
uint s3 = id & 0x000000ff;
|
|
|
|
//经过测试,string直接+(前提是固定数量的string,编译器会优化)的效率比StringBuilder还高...
|
|
//string.format底层使用的是StringBuilder,效率和StringBuilder是一样的
|
|
/*
|
|
m_stringBuilder.Clear();
|
|
|
|
m_stringBuilder.AppendFormat("{0}.{1}.{2}", s1, s2, s3);
|
|
|
|
m_stringBuilder.Append(s1);
|
|
m_stringBuilder.Append(m_strDot);
|
|
m_stringBuilder.Append(s2);
|
|
m_stringBuilder.Append(m_strDot);
|
|
m_stringBuilder.Append(s3);
|
|
|
|
string strID = m_stringBuilder.ToString();
|
|
*/
|
|
|
|
//string strID = string.Format("{0}.{1}.{2}", s1, s2, s3);
|
|
|
|
strID = s1.ToString() + "." + s2.ToString() + "." + s3.ToString();
|
|
|
|
//最多cache 多少个
|
|
if (m_serverIDCache.Count < MAX_CHACHE_ID_COUNT)
|
|
{
|
|
m_serverIDCache.Add(id, strID);
|
|
}
|
|
|
|
return strID;
|
|
}
|
|
|
|
|
|
//根据一个服务器类型,实例ID获取第0层服务器ID,比如VersionServer,这种
|
|
//0层服务器worldid为0
|
|
public static uint GetLevel0ServerIDByType(int serverType, int instID)
|
|
{
|
|
uint level1serverID = (uint)( (serverType << 8) | instID);
|
|
|
|
return level1serverID;
|
|
}
|
|
|
|
//根据一个服务器类型,实例ID获取第1层服务器ID,比如AccountServer,这种
|
|
public static uint GetLevel1ServerIDByType(uint srcServerId,int serverType, int instID)
|
|
{
|
|
uint level1serverID = (srcServerId & 0xffff0000) | ((uint)serverType << 8) | (uint)instID;
|
|
|
|
return level1serverID;
|
|
}
|
|
|
|
//根据一个服务器类型,实例ID获取第1层服务器ID,比如AccountServer,这种
|
|
public static uint MakeServerID(int worldId, int serverType, int instID)
|
|
{
|
|
uint tmpNumber;
|
|
tmpNumber = (uint)worldId * 256 * 256 + (uint)serverType * 256 + (uint)instID;
|
|
return tmpNumber;
|
|
}
|
|
|
|
public static int GetServerType(uint id)
|
|
{
|
|
int iServerType = (int)((id & 0x0000ff00) >> 8);
|
|
return iServerType;
|
|
}
|
|
|
|
// worldid
|
|
public static uint GetWorldID(uint srcServerId)
|
|
{
|
|
return (srcServerId & 0xffff0000) >> 16;
|
|
}
|
|
|
|
//instid
|
|
public static uint GetInstanceID(uint srcServerId)
|
|
{
|
|
return srcServerId & 0x000000ff;
|
|
}
|
|
|
|
public static bool IsClusterServer(uint serverId)
|
|
{
|
|
return (serverId & 0xffff0000) == 0;
|
|
}
|
|
}
|
|
}
|
|
|