|
using namespace jxInterSrvComm::DbServerProto;
const char sGlobalVarFile[] = "./data/runtime/globalVar_";
CGlobalVarMgr::CGlobalVarMgr()
{
m_boInited = false;
m_hasModify =false;
//Load();
}
CGlobalVarMgr::~CGlobalVarMgr()
{
Destroy();
}
VOID CGlobalVarMgr::Load()
{
//CDataClient* pDbClient = GetLogicServer()->GetDbClient();
//if (!m_boInited && pDbClient->connected())
//{
// CDataPacket& DataPacket =pDbClient->allocProtoPacket(dcLoadVar);
// DataPacket << (int)0;
// GetLogicServer()->GetDbClient()->flushProtoPacket(DataPacket);
//}
///改成读本地文件
char sFile[256];
sprintf_s(sFile,sizeof(sFile),"%s%d.bin",sGlobalVarFile,GetLogicServer()->GetServerIndex());
wylib::stream::CMemoryStream ms;
if ( ms.loadFromFile(sFile) > 0 )
{
m_Var.loadFromMemory((char*)ms.getMemory(),(size_t)ms.getSize());
}
m_boInited = true;
}
void CGlobalVarMgr::OnDbRetData( INT_PTR nCmd,INT_PTR nErrorCode,CDataPacketReader &reader )
{
if(nCmd == dcLoadVar )
{
if (nErrorCode == reSucc)
{
m_Var.loadFromMemory(reader.getOffsetPtr(), reader.getAvaliableLength());
m_boInited = true;
}
else
{
OutputMsg(rmError,_T("load globalVar error!errcode=%d"),nErrorCode);
m_boInited = false;
}
}
}
VOID CGlobalVarMgr::RunOne()
{
TICKCOUNT nCurr = _getTickCount();
if (m_timer.CheckAndSet(nCurr))
{
Save();
}
}
void CGlobalVarMgr::Save()
{
if(!m_hasModify || !m_boInited ) return;
DECLARE_FUN_TIME_PROF()
char* buf = NULL;
size_t buf_size = 0;
CMyBufferAllocator* alloc = GetLogicServer()->GetBuffAllocator();
#ifdef _USE_TRY_CATCH
__try
#endif
{
///改成保存到文件
char sFile[256];
sprintf_s(sFile,sizeof(sFile),"%s%d.bin",sGlobalVarFile,GetLogicServer()->GetServerIndex());
CCLVariant *pContinueVar = &m_Var;
const int ADDSIZE = 1048576; //每次增长1兆空间
size_t data_size = 0;
buf = (char*)alloc->_alloc_buffer(ADDSIZE); //预留4K空间
if (!buf) return;
buf_size = ADDSIZE;
size_t savesize = 0;
int nLoopIndex = 0;
while (true)
{
savesize = pContinueVar->saveToMemory(buf + data_size, buf_size - data_size, &pContinueVar);
data_size += savesize;
if (!pContinueVar)
break; // 到最后了
OutputMsg(rmWaning, _T("GLOBAL VAL the var data too large, buf size >= %d"), buf_size);
//这里修改了一处死循环,问题是大小大于4096将无法申请空间
char* new_buf = (char*)alloc->_realloc_buffer(buf, buf_size + ADDSIZE); //每次增长4K
if (!new_buf)
{
alloc->_free_buffer(buf);
buf = NULL;
break;
}
buf = new_buf;
buf_size += ADDSIZE;
nLoopIndex++;
if(nLoopIndex > 10000)
{
OutputMsg(rmError,"LoopIndex CGlobalVarMgr::Save()");
break;
}
}
if (buf && data_size > 0)
{
wylib::stream::CMemoryStream ms;
ms.write(buf, data_size);
ms.saveToFile(sFile);
}
}
#ifdef _USE_TRY_CATCH
__except(CustomExceptHandler(GetExceptionInformation()))
{
}
#endif
m_hasModify =false;
if (buf)
alloc->_free_buffer(buf);
}
这个是c++ 读取本地的bin类型文件。哪位大神可以翻译成易语言的!
globalVar_1.zip
(2.69 KB, 下载次数: 2)
|
|