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.
225 lines
8.1 KiB
225 lines
8.1 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
using Sog;
|
|
using Sog.Service;
|
|
using ProtoCSStruct;
|
|
|
|
|
|
namespace Name
|
|
{
|
|
//每个Task需要一个Handler,一个Handler一个DBOperator
|
|
public class MessageTaskHandler
|
|
{
|
|
private NameOperator m_nameOperator;
|
|
|
|
private long m_lastKeepMysqlConnTimeSecond;
|
|
|
|
//自管理
|
|
private static MessageTaskHandler[] m_allTaskHandler;
|
|
|
|
public static void InitAllTaskHandler()
|
|
{
|
|
//分配消息任务,线程数量=表的数量,免得冲突
|
|
uint iTaskCount = (uint)NameUtils.DBServerWorkThreadCount;
|
|
|
|
TraceLog.Debug("MessageTaskHandler.InitAllHandler messageTaskCount is {0}", iTaskCount);
|
|
|
|
MessageTaskDistributor.Instance.InitTask(iTaskCount);
|
|
|
|
|
|
//配置文件
|
|
NameServerConfig serverConfig = NameServerUtils.GetServerConfig();
|
|
m_allTaskHandler = new MessageTaskHandler[iTaskCount];
|
|
|
|
for (int i = 0; i < m_allTaskHandler.Length; i++)
|
|
{
|
|
m_allTaskHandler[i] = new MessageTaskHandler();
|
|
|
|
m_allTaskHandler[i].m_nameOperator = new NameOperator(serverConfig.dbname, serverConfig.dbip
|
|
, serverConfig.dbuser, serverConfig.dbpassword);
|
|
|
|
MessageTaskObj taskObj = MessageTaskDistributor.Instance.GetTaskByIndex(i);
|
|
taskObj.Handler = m_allTaskHandler[i].HandlerPacket;
|
|
taskObj.IdleTick = m_allTaskHandler[i].OnIdleTick;
|
|
}
|
|
|
|
MessageTaskDistributor.Instance.StartAllTask();
|
|
}
|
|
|
|
|
|
public static void DisposeAllHandler()
|
|
{
|
|
//关闭所有消息处理任务
|
|
MessageTaskDistributor.Instance.CloseAllTask();
|
|
|
|
if (m_allTaskHandler == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//销毁数据库连接,销毁对象
|
|
for (int i = 0; i < m_allTaskHandler.Length; i++)
|
|
{
|
|
m_allTaskHandler[i].m_nameOperator.Dispose();
|
|
m_allTaskHandler[i].m_nameOperator = null;
|
|
}
|
|
|
|
m_allTaskHandler = null;
|
|
}
|
|
|
|
public void OnIdleTick(long idleSecond)
|
|
{
|
|
//5分钟保持连接一次
|
|
if(AppTime.GetNowSysSecond() - m_lastKeepMysqlConnTimeSecond < 300)
|
|
{
|
|
return;
|
|
}
|
|
|
|
m_lastKeepMysqlConnTimeSecond = AppTime.GetNowSysSecond();
|
|
|
|
m_nameOperator.KeepAlive();
|
|
}
|
|
|
|
public void HandlerPacket(uint remoteAppID, StructPacket packet)
|
|
{
|
|
//去掉这里的时间重置逻辑,如果下面实际处理逻辑里没有进行sql查询就返回会造成KeepAlive不执行
|
|
//m_lastKeepMysqlConnTimeSecond = AppTime.GetNowSysSecond();
|
|
|
|
switch (packet.MsgID)
|
|
{
|
|
case (int)SSMsgID.NameQueryReq:
|
|
OnQueryNameDBReq(remoteAppID, packet);
|
|
break;
|
|
case (int)SSMsgID.NameInsertReq:
|
|
OnInsertNameDBReq(remoteAppID, packet);
|
|
break;
|
|
default:
|
|
TraceLog.Error("MessageTaskHandler unknow MsgID {0}", packet.MsgID);
|
|
break;
|
|
|
|
}
|
|
}
|
|
|
|
//角色新创建的时候,检查角色名称是否已经存在
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="remoteAppID"></param>
|
|
/// <param name="packet"></param>
|
|
private void OnQueryNameDBReq(uint remoteAppID, StructPacket packet)
|
|
{
|
|
ref SSNameQueryReq nameQueryReq = ref packet.GetMessage<SSNameQueryReq>();
|
|
|
|
string strName = nameQueryReq.StrName.GetString();
|
|
|
|
string hashName = HashHelper.SHA1String(strName+"_"+nameQueryReq.RealmId);
|
|
|
|
string accountID = nameQueryReq.AccountID.GetString();
|
|
|
|
SSNameQueryRes nameQueryRes = new SSNameQueryRes();
|
|
nameQueryRes.PlayerSessionID = nameQueryReq.PlayerSessionID;
|
|
nameQueryRes.StrName.SetString(strName);
|
|
nameQueryRes.HashName.SetString(hashName);
|
|
nameQueryRes.AccountID.SetString(accountID);
|
|
nameQueryRes.CreateIfNotExist = nameQueryReq.CreateIfNotExist;
|
|
nameQueryRes.RealmId = nameQueryReq.RealmId;
|
|
TraceLog.Debug("OnQueryNameDBReq strName {0} hashName {1} accountID {2}", strName, hashName, accountID);
|
|
|
|
tbName tbName = m_nameOperator.QueryName(hashName, nameQueryReq.RealmId);
|
|
|
|
if (tbName != null)
|
|
{
|
|
nameQueryRes.Count = 1;
|
|
TraceLog.Debug("query success, hashName is {0}", hashName);
|
|
|
|
nameQueryRes.Ret = 1;//存在
|
|
}
|
|
else if(nameQueryReq.CreateIfNotExist)//自动创建角色
|
|
{
|
|
//没有记录,则创建,大部分手游只有一个玩家角色信息(棋牌,卡牌,竞技游戏等等),直接创建user
|
|
TraceLog.Debug("no record, create it ");
|
|
|
|
//int accountTableIndex = TableIndexCalc.CalcNameTableIndex(0, hashName);
|
|
|
|
bool bSuccess = m_nameOperator.InsertName(
|
|
hashName
|
|
,strName
|
|
, accountID
|
|
, nameQueryReq.RealmId
|
|
);
|
|
if (!bSuccess)
|
|
{
|
|
nameQueryRes.Ret = -1;
|
|
//queryAccountDbReq.GameId = 0;
|
|
}
|
|
else
|
|
{
|
|
//queryAccountDbReq.GameId = gameId;
|
|
nameQueryRes.Ret = 0;//不存在,自动创建
|
|
}
|
|
}
|
|
else
|
|
{
|
|
TraceLog.Debug("OnQueryNameDBReq strName {0} hashName {1} not exist, config.CreateIfNotExist is false! "
|
|
, strName, hashName);
|
|
nameQueryRes.Ret = 0;//不存在
|
|
}
|
|
|
|
NameServerUtils.GetPacketSender().SendToServerByID(remoteAppID,
|
|
(int)SSMsgID.NameQueryRes, ref nameQueryRes, packet.ObjectID);
|
|
}
|
|
private void OnInsertNameDBReq(uint remoteAppID, StructPacket packet)
|
|
{
|
|
//ref SSNameInsertReq nameInsertReq = ref packet.GetMessage<SSNameInsertReq>();
|
|
|
|
//string strName = nameInsertReq.StrName.GetString();
|
|
|
|
//string hashName = HashHelper.SHA1String(strName);
|
|
|
|
//string accountID = nameInsertReq.AccountID.GetString();
|
|
|
|
//SSNameInsertRes nameInsertRes = new SSNameInsertRes();
|
|
//nameInsertRes.StrName.SetString(strName);
|
|
//nameInsertRes.HashName.SetString(hashName);
|
|
//nameInsertRes.AccountID.SetString(accountID);
|
|
|
|
//TraceLog.Debug("OnInsertNameDBReq strName {0} hashName {1} accountID {2}", strName, hashName, accountID);
|
|
|
|
//tbName tbName = m_nameOperator.QueryName(hashName);
|
|
|
|
//if (tbName != null)
|
|
//{
|
|
// nameInsertRes.Ret = -1;
|
|
// TraceLog.Debug("query success, hashName is {0}", hashName);
|
|
//}
|
|
//else //自动创建角色
|
|
//{
|
|
// //没有记录,则创建,大部分手游只有一个玩家角色信息(棋牌,卡牌,竞技游戏等等),直接创建user
|
|
// TraceLog.Debug("OnInsertNameDBReq no record, create it ");
|
|
|
|
// int accountTableIndex = TableIndexCalc.CalcNameTableIndex(0, hashName);
|
|
|
|
// bool bSuccess = m_nameOperator.InsertName(
|
|
// hashName
|
|
// , strName
|
|
// , accountID
|
|
// );
|
|
// if (!bSuccess)
|
|
// {
|
|
// nameInsertRes.Ret = -1;
|
|
// //queryAccountDbReq.GameId = 0;
|
|
// }
|
|
// else
|
|
// {
|
|
// //queryAccountDbReq.GameId = gameId;
|
|
// }
|
|
//}
|
|
|
|
//NameServerUtils.GetPacketSender().SendToServerByID(remoteAppID,
|
|
// (int)SSMsgID.NameInsertRes, ref nameInsertRes, packet.ObjectID);
|
|
}
|
|
}
|
|
}
|
|
|