//
// Created by DGuco on 17-7-13.
//
#include <dbmessage.pb.h>
#include <client_comm_engine.h>
#include <my_assert.h>
#include "dbctrl.h"
#include "config.h"
#include "connector.h"
#include "server_comm_engine.h"
#include "message_factory.h"
#include "database_mysql.h"
template<> shared_ptr<CDBCtrl> CSingleton<CDBCtrl>::spSingleton = NULL;
int CDBCtrl::m_iProxyId = 0;
CByteBuff CDBCtrl::m_acRecvBuff = CByteBuff();
CDBCtrl::CDBCtrl()
{
m_iRunFlag = 0;
m_tLastSendKeepAlive = 0;
m_tLastRecvKeepAlive = 0;
m_pNetWork = CNetWork::GetSingletonPtr();
m_pServerConfig = CServerConfig::GetSingletonPtr();
m_pMsgFactory = std::make_shared<CMessageFactory>();
m_pDatabase = std::make_shared<DatabaseMysql>();
}
CDBCtrl::~CDBCtrl()
{
}
void CDBCtrl::SetRunFlag(int iFlag)
{
m_iRunFlag = iFlag;
LOG_INFO("default", "Set DealMsg Flag {}, All Flag Is {}", iFlag, m_iRunFlag);
}
void CDBCtrl::ClearRunFlag(int iFlag)
{
m_iRunFlag = 0;
LOG_INFO("default", "Clear DealMsg Flag {}", iFlag);
}
bool CDBCtrl::IsRunFlagSet(int iFlag)
{
return iFlag == m_iRunFlag;
}
int CDBCtrl::SendMessageTo(CProxyMessage *pMsg)
{
if (pMsg == NULL) {
LOG_ERROR("default", "in CDBHandle::SendMessageTo, pmsg is null");
return -1;
}
CProxyHead *stProxyHead = pMsg->mutable_msghead();
CByteBuff tmBuff;
unsigned short nCodeLength = 0;
ServerInfo *dbInfo = m_pServerConfig->GetServerInfo(enServerType::FE_DBSERVER);
ServerInfo *proxyInfo = m_pServerConfig->GetServerInfo(enServerType::FE_PROXYSERVER);
pbmsg_setproxy(stProxyHead,
enServerType::FE_PROXYSERVER,
proxyInfo->m_iServerId,
enServerType::FE_DBSERVER,
dbInfo->m_iServerId,
GetMSTime(),
enMessageCmd::MESS_REGIST);
int iRet = CServerCommEngine::ConvertMsgToStream(pMsg, &tmBuff, nCodeLength);
if (iRet != 0) {
LOG_ERROR("default", "CDBCtrl::RegisterToProxyServer ConvertMsgToStream failed, iRet = {}.", iRet);
return 0;
}
//int nSendReturn = m_pProxySvrdConns[ m_current_proxy_index ].GetSocket()->SendOneCode( nCodeLength, abyCodeBuf );
IBufferEvent *tmpConnector = m_pNetWork->FindConnector(CDBCtrl::m_iProxyId);
if (tmpConnector == NULL) {
LOG_ERROR("default", "Connection to proxyserver has gone");
return -1;
}
int nSendReturn = tmpConnector->Send(tmBuff.GetData(), nCodeLength);
if (nSendReturn < 0) {
LOG_ERROR("default", "Send Code(len:{}) To Proxy faild(error={})", nCodeLength, nSendReturn);
return -1;
}
CGooMess *pUnknownMessagePara = (CGooMess *) pMsg->msgpara();
MY_ASSERT(pUnknownMessagePara != NULL, return 0);
const ::google::protobuf::Descriptor *pDescriptor = pUnknownMessagePara->GetDescriptor();
LOG_DEBUG("default", "SendMessageTo: MsgName[{}] ProxyHead[{}] MsgHead[{}] MsgPara[{}]",
pDescriptor->name().c_str(), stProxyHead->ShortDebugString().c_str(),
pMsg->ShortDebugString().c_str(), ((CGooMess *) pMsg->msgpara())->ShortDebugString().c_str());
return 0;
}
// 执行相应的指令
int CDBCtrl::Event(CProxyMessage *pMsg)
{
if (pMsg == NULL) {
LOG_ERROR("default", "In CDBHandle::Event, input msg null");
return -1;
}
CGooMess *pUnknownMessagePara = (CGooMess *) pMsg->msgpara();
MY_ASSERT(pUnknownMessagePara != NULL, return 0);
const ::google::protobuf::Descriptor *pDescriptor = pUnknownMessagePara->GetDescriptor();
LOG_DEBUG("default", "ReceveMsg: MsgName[{}] MsgHead[{}] MsgPara[{}]",
pDescriptor->name().c_str(),
pMsg->ShortDebugString().c_str(),
((CGooMess *) pMsg->msgpara())->ShortDebugString().c_str());
switch (pMsg->msghead().messageid()) {
case CMsgExecuteSqlRequest::MsgID: // 服务器执行SQL请求
{
ProcessExecuteSqlRequest(pMsg);
break;
}
default: {
break;
}
}
return 0;
}
// 执行相应的指令
int CDBCtrl::ProcessExecuteSqlRequest(CProxyMessage *pMsg)
{
if (pMsg == NULL) {
LOG_ERROR("default", "Error: [{}][{}][{}], invalid input.\n", __MY_FILE__, __LINE__, __FUNCTION__);
return -1;
}
CMsgExecuteSqlRequest *pReqMsg = (CMsgExecuteSqlRequest * )(pMsg->msgpara());
if (pReqMsg == NULL) {
LOG_ERROR("default", "Error: [{}][{}][{}], msgpara null.\n", __MY_FILE__, __LINE__, __FUNCTION__);
return -1;
}
if (pReqMsg->sql().length() <= 0) {
LOG_ERROR("default", "Error: [{}][{}][{}], sql len({}) invalid.\n",
__MY_FILE__,
__LINE__,
__FUNCTION__,
pReqMsg->sql().length());
return -1;
}
LOG_ERROR("default", "{} \n", pReqMsg->ShortDebugString().c_str());
string sqlStr = "";
if (pReqMsg->hasblob() == HASBLOB) {
// 有blob字段的处理
sqlStr += pReqMsg->sql();
if (pReqMsg->bufsize() > 0) {
if (pReqMsg->bufsize() >= MAX_PACKAGE_LEN) {
// blob字段超出长度
LOG_ERROR("default", "Error: [{}][{}][{}], exec sql {} faild. sql_len:{} > MAX_PACKAGE_LEN\n",
__MY_FILE__,
__LINE__,
__FUNCTION__,
pReqMsg->sql().c_str(),
pReqMsg->bufsize());
return -1;
}
char sqlBuff[MAX_PACKAGE_LEN * 2 + 1] = {0};
int len = m_pDatabase->escape_string(sqlBuff, pReqMsg->buffer().c_str(), pReqMsg->bufsize());
if (len <= 0) {
LOG_ERROR("default",
"Error: [{}][{}][{}], escape_string error!!!!\n",
__MY_FILE__,
__LINE__,
__FUNCTION__);
return -1;
}
sqlStr += sqlBuff;
}
sqlStr += pReqMsg->sqlwhere();
}
else {
// 没有blob字段的处理
sqlStr += pReqMsg->sql();
}
// 需要回执
if (MUSTCALLBACK == pReqMsg->callback()) {
CProxyMessage tMsg; // 该消息为回执
CMsgExecuteSqlResponse tSqlResMsg; // tSqlResMsg 为消息体
tMsg.Clear();
tSqlResMsg.Clear();
tSqlResMsg.set_logictype(pReqMsg->logictype());
tSqlResMsg.set_sessionid(pReqMsg->sessionid());
tSqlResMsg.set_timestamp(pReqMsg->timestamp());
tSqlResMsg.set_teamid(pReqMsg->teamid());
LOG_ERROR("default", "Execute SQL: {}", sqlStr.c_str()); // 用于在日志中查看SQL查询语句
CProxyHead *pTmpHead = tMsg.mutable_msghead();
pTmpHead->set_messageid(CMsgExecuteSqlResponse::MsgID);
if (pMsg->has_msghead()) {
if (pMsg->mutable_msghead()->has_msghead()) {
CClientCommEngine::CopyMesHead(pMsg->mutable_msghead()->mutable_msghead(),
pTmpHead->mutable_msghead());
}
}
tMsg.set_msgpara((uint64) &tSqlResMsg);
if (pReqMsg->sqltype() == SELECT || pReqMsg->sqltype() == CALL) {
QueryResult *res = NULL;
if (pReqMsg->sqltype() == SELECT) {
res = m_pDatabase->Query(sqlStr.c_str(), sqlStr.length());
}
else {
res = m_pDatabase->QueryForprocedure(sqlStr.c_str(), sqlStr.length(), pReqMsg->outnumber());
}
if (res == NULL) // 无返回结果
{
LOG_ERROR("default", "sql {} exec, but no resultset returned", sqlStr.c_str());
tSqlResMsg.set_resultcode(0);
}
else // 有返回结果
{
tSqlResMsg.set_resultcode(1);
tSqlResMsg.set_rowcount(res->GetRowCount()); // 行
tSqlResMsg.set_colcount(res->GetFieldCount()); // 列
//TODO: 列名 + 列类型 暂时不赋值
if (res->GetRowCount() > 0) {
// 列值 和 值长度
do {
Field *pField = res->Fetch();
if (pField == NULL) {
LOG_ERROR("default", "ERROR: do sql {} success, row[{}], col[{}], but some row is null\n",
sqlStr.c_str(),
res->GetRowCount(),
res->GetFieldCount());
//TODO: 出错设为0
tSqlResMsg.set_rowcount(0);
break;
}
for (int j = 0; j < (int) res->GetFieldCount(); j++) {
tSqlResMsg.add_fieldvalue(pField[j].GetString(), pField[j].GetValueLen());
tSqlResMsg.add_fieldvaluelen(pField[j].GetValueLen());
}
}
while (res->NextRow());
}
}
// 安全释放结果集
ReleaseResult(res);
}
else // 非select操作
{
bool bExecResult = m_pDatabase->RealDirectExecute(sqlStr.c_str(), sqlStr.length()); // 执行操作
tSqlResMsg.set_resultcode(bExecResult ? 1 : 0);
}
// 回复客户端
SendMessageTo(&tMsg);
}
else {
// 同步执行
if (m_pDatabase->RealDirectExecute(sqlStr.
没有合适的资源?快使用搜索试试~ 我知道了~
用libevent和共享内存实现的一个游戏server
共655个文件
h:469个
json:65个
cpp:62个
需积分: 1 0 下载量 4 浏览量
2023-07-30
09:37:23
上传
评论
收藏 60.52MB ZIP 举报
温馨提示
用libevent和共享内存实现的一个游戏server
资源推荐
资源详情
资源评论
收起资源包目录
用libevent和共享内存实现的一个游戏server (655个子文件)
make.bat 107B
cmake.bat 107B
applink.c 4KB
player.pb.cc 318KB
common.pb.cc 82KB
message.pb.cc 61KB
dbmessage.pb.cc 47KB
dbctrl.cpp 17KB
client_handle.cpp 17KB
database_mysql.cpp 16KB
parameter_config.cpp 15KB
timer.cpp 14KB
code_queue.cpp 12KB
proxy_ctrl.cpp 12KB
client_comm_engine.cpp 11KB
server_handle.cpp 10KB
net_manager.cpp 10KB
game_server.cpp 9KB
server_tool.cpp 9KB
db_module.cpp 7KB
server_comm_engine.cpp 7KB
net_work.cpp 6KB
byte_buff.cpp 6KB
socket.cpp 5KB
buffev_interface.cpp 5KB
message_dispatcher.cpp 4KB
base.cpp 4KB
shm.cpp 4KB
share_mem.cpp 3KB
log.cpp 3KB
database.cpp 3KB
thread_pool.cpp 3KB
mes_handle.cpp 3KB
module_manager.cpp 3KB
connector.cpp 3KB
statistics.cpp 3KB
listener.cpp 3KB
message_factory.cpp 2KB
mythread.cpp 2KB
file_listener.cpp 2KB
query_result_mysql.cpp 2KB
TestLibReflection.cpp 2KB
config.cpp 2KB
acceptor.cpp 2KB
performance.cpp 2KB
ccrypto.cpp 2KB
config_handle.cpp 2KB
core_module.cpp 2KB
timer_event.cpp 1KB
gate_ctrl.cpp 1KB
sceneobjmanager.cpp 1KB
main.cpp 1KB
field.cpp 1KB
event_reactor.cpp 1KB
object_manager.cpp 1023B
system_signal.cpp 944B
main.cpp 923B
message_factory.cpp 914B
json_interface.cpp 804B
main.cpp 788B
playerdata.cpp 767B
clock.cpp 693B
main.cpp 503B
logic_module.cpp 458B
player.cpp 323B
runflag.cpp 258B
playerbase.cpp 220B
playercity.cpp 203B
message_interface.cpp 109B
.cproject 5KB
cspipefile 0B
dbpipefile 0B
protoc.exe 9.92MB
gameserver 36.4MB
.gitignore 321B
protobuf-all-3.20.0.tar.gz 7.45MB
libevent-2.0.22-stable.tar.gz 835KB
descriptor.pb.h 581KB
player.pb.h 312KB
obj_mac.h 223KB
ssl.h 121KB
document.h 116KB
format.h 115KB
schema.h 101KB
evp.h 101KB
descriptor.h 98KB
x509v3.h 92KB
reader.h 91KB
type.pb.h 89KB
wire_format_lite.h 83KB
common.pb.h 79KB
cryptoerr_legacy.h 79KB
plugin.pb.h 75KB
coded_stream.h 72KB
tls1.h 70KB
x509.h 70KB
extension_set.h 69KB
repeated_ptr_field.h 68KB
message.h 68KB
ec.h 66KB
共 655 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
yanglamei1962
- 粉丝: 1930
- 资源: 336
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功