Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ylmbtm committed Sep 13, 2020
1 parent c2f62d3 commit e912dcd
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 59 deletions.
53 changes: 38 additions & 15 deletions Server/Src/ServerEngine/CommonFunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ BOOL CommonFunc::SetCurrentWorkDir(std::string strPath)

UINT64 CommonFunc::GetCurrTime()
{
time_t t;

t = time(0);
time_t t = time(0);

return (UINT64)t;
}
Expand All @@ -75,19 +73,17 @@ UINT64 CommonFunc::GetCurMsTime()

tm CommonFunc::GetCurrTmTime()
{
time_t rawtime;
struct tm* timeinfo;
time_t t = (time_t)GetCurrTime();

time (&rawtime);
timeinfo = localtime(&rawtime);
struct tm _tm_time;
_tm_time = *localtime(&t);

return *timeinfo;
return _tm_time;
}

UINT64 CommonFunc::GetDayBeginTime()
{
time_t t;
t = time(0);
time_t t = time(0);
tm* t_tm = localtime(&t);
t_tm->tm_hour = 0;
t_tm->tm_min = 0;
Expand All @@ -98,17 +94,15 @@ UINT64 CommonFunc::GetDayBeginTime()

UINT64 CommonFunc::GetWeekBeginTime()
{
time_t t;
t = time(0);
time_t t = time(0);
tm* t_tm = localtime(&t);
return (UINT64)t - (t_tm->tm_wday == 0 ? 6 : t_tm->tm_wday - 1) * 86400 - t_tm->tm_hour * 3600 - t_tm->tm_min * 60 - t_tm->tm_sec;
}

time_t CommonFunc::YearTimeToSec(INT32 nYear, INT32 nMonth, INT32 nDay, INT32 nHour, INT32 nMin, INT32 nSec)
{
time_t timer;
time(&timer);
tm* t_tm = localtime(&timer);
time_t t = time(0);
tm* t_tm = localtime(&t);

tm newtm;
newtm.tm_year = (nYear < 0) ? t_tm->tm_year : nYear - 1900;
Expand Down Expand Up @@ -614,6 +608,35 @@ BOOL CommonFunc::KillProcess(UINT64 dwPid)
return TRUE;
}

BOOL CommonFunc::IsProcessExist(UINT64 dwPid)
{
#ifdef WIN32
HANDLE hPrc;
if (0 == dwPid)
{
return FALSE;
}

hPrc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)dwPid);
if (hPrc == NULL)
{
return FALSE;
}
CloseHandle(hPrc);
#else
if (kill(dwPid, 0) < 0)
{
return FALSE;
}

if (errno == ESRCH)
{
return FALSE;
}
#endif
return TRUE;
}

INT32 CommonFunc::Min(INT32 nValue1, INT32 nValue2)
{
return (nValue1 < nValue2) ? nValue1 : nValue2;
Expand Down
2 changes: 2 additions & 0 deletions Server/Src/ServerEngine/CommonFunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ BOOL DbgTrace(char* format, ...);

BOOL KillProcess(UINT64 dwPid);

BOOL IsProcessExist(UINT64 dwPid);

INT32 Min(INT32 nValue1, INT32 nValue2);

BOOL IsAlreadyRun(std::string strSignName);
Expand Down
59 changes: 29 additions & 30 deletions Server/Src/ServerEngine/DataBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ class CDataBuffer : public IDataBuffer

virtual ~CDataBuffer(void)
{

}

INT32 GetRef()
{
return m_dwRefCount;
m_nDataLen = 0;
m_nBufSize = SIZE;
m_dwRefCount = 0;
m_pPrev = NULL;
m_pNext = NULL;
m_pManager = NULL;
}

BOOL AddRef()
Expand All @@ -41,9 +41,9 @@ class CDataBuffer : public IDataBuffer

BOOL Release()
{
ERROR_RETURN_FALSE(m_pManager != NULL);
assert(m_pManager != NULL);

ERROR_RETURN_FALSE(m_pManager->ReleaseDataBuff(this));
m_pManager->ReleaseDataBuff(this);

return TRUE;
}
Expand Down Expand Up @@ -156,7 +156,7 @@ class CBufferManager
pDataBuffer->m_pPrev = NULL;
}

ERROR_RETURN_NULL(pDataBuffer->m_dwRefCount == 0);
assert(pDataBuffer->m_dwRefCount == 0);

pDataBuffer->m_dwRefCount = 1;

Expand All @@ -179,19 +179,21 @@ class CBufferManager

BOOL ReleaseDataBuff(CDataBuffer<SIZE>* pBuff)
{
assert(pBuff != NULL);
if (pBuff == NULL)
{
return FALSE;
}

m_BuffMutex.lock();
pBuff->m_dwRefCount--;
if (pBuff->m_dwRefCount < 0)
assert(pBuff->m_dwRefCount > 0);
if (pBuff->m_dwRefCount <= 0)
{
return FALSE;
}
std::lock_guard<std::mutex> lock(m_BuffMutex);
pBuff->m_dwRefCount--;

if (pBuff->m_dwRefCount == 0)
if (pBuff->m_dwRefCount <= 0)
{
pBuff->m_nDataLen = 0;
//首先从己用中删除
Expand All @@ -206,7 +208,7 @@ class CBufferManager
}
else
{
ERROR_RETURN_FALSE(pBuff->m_pPrev != NULL);
assert(pBuff->m_pPrev != NULL);
pBuff->m_pPrev->m_pNext = pBuff->m_pNext;
if (pBuff->m_pNext != NULL)
{
Expand All @@ -232,9 +234,6 @@ class CBufferManager
}
m_dwBufferCount--;
}

m_BuffMutex.unlock();

return TRUE;
}

Expand Down Expand Up @@ -321,19 +320,19 @@ class CBufferAllocator
public:
IDataBuffer* AllocDataBuff(int nSize);

CBufferManager<64> m_BufferManager64B; //管理64B的内存池
CBufferManager<128> m_BufferManager128B; //管理128B的内存池
CBufferManager<256> m_BufferManager256B; //管理256B的内存池
CBufferManager<512> m_BufferManager512B; //管理512B的内存池
CBufferManager<1024> m_BufferManager1K; //管理1k的内存池
CBufferManager<2048> m_BufferManager2K; //管理2k的内存池
CBufferManager<4096> m_BufferManager4K; //管理4k的内存池
CBufferManager<8192> m_BufferManager8K; //管理8k的内存池
CBufferManager<16384> m_BufferManager16K; //管理16k的内存池
CBufferManager<32768> m_BufferManager32K; //管理32k的内存池
CBufferManager<65536> m_BufferManager64K; //管理64k的内存池

CBufferManager<10 * 1024 * 1014> m_BufferManagerAny; //管理10M的内存, 并不用池管理, 直接申请, 直接释放.
CBufferManager<64> m_BufferManager64B; //管理<=64B的内存池
CBufferManager<128> m_BufferManager128B; //管理<=128B的内存池
CBufferManager<256> m_BufferManager256B; //管理<=256B的内存池
CBufferManager<512> m_BufferManager512B; //管理<=512B的内存池
CBufferManager<1024> m_BufferManager1K; //管理<=1k的内存池
CBufferManager<2048> m_BufferManager2K; //管理<=2k的内存池
CBufferManager<4096> m_BufferManager4K; //管理<=4k的内存池
CBufferManager<8192> m_BufferManager8K; //管理<=8k的内存池
CBufferManager<16384> m_BufferManager16K; //管理<=16k的内存池
CBufferManager<32768> m_BufferManager32K; //管理<=32k的内存池
CBufferManager<65536> m_BufferManager64K; //管理<=64k的内存池

CBufferManager<10 * 1024 * 1014> m_BufferManagerAny; //管理<=10M的内存, 并不用池管理, 直接申请, 直接释放.
};

#endif
2 changes: 0 additions & 2 deletions Server/Src/ServerEngine/IBufferHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ struct NetPacket;
class IDataBuffer
{
public:
virtual INT32 GetRef() = 0;

virtual BOOL AddRef() = 0;

virtual BOOL Release() = 0;
Expand Down
11 changes: 9 additions & 2 deletions Server/Src/ServerEngine/ServiceBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ BOOL ServiceBase::StartNetwork(UINT16 nPortNum, UINT32 nMaxConn, IPacketDispatch
m_dwRecvNum = 0;
m_dwFps = 0;
m_dwSendNum = 0;
m_dwLastMsgID = 0;
return TRUE;
}

Expand All @@ -73,8 +74,6 @@ BOOL ServiceBase::StopNetwork()

CNetManager::GetInstancePtr()->Stop();

CLog::GetInstancePtr()->Close();

return TRUE;
}

Expand Down Expand Up @@ -203,7 +202,15 @@ BOOL ServiceBase::Update()
}
else
{
//try
//{
m_dwLastMsgID = item.m_dwMsgID;
m_pPacketDispatcher->DispatchPacket(&item);
//}
//catch (std::exception& e)
//{
// CLog::GetInstancePtr()->LogError("DispatchPacket Message Error %s, MessageID:%d", e.what(), item.m_dwMsgID);
//}

item.m_pDataBuffer->Release();

Expand Down
1 change: 1 addition & 0 deletions Server/Src/ServerEngine/ServiceBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class ServiceBase : public IDataHandler//, public CEventFuncManager
UINT32 m_dwRecvNum;
UINT32 m_dwSendNum;
UINT32 m_dwFps;
UINT32 m_dwLastMsgID;
};


Expand Down
12 changes: 6 additions & 6 deletions Server/Src/ServerEngine/SharedMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,14 @@ template <typename T> class DataWriter : public DataWriterBase
///其次回调新建
if (pBlock->m_bNewBlock)
{
pBlock->m_beforeTime = time(NULL);
pBlock->m_beforeTime = time(0);
if (!pdata->Create(pdb))
{
m_nErrorCount++;
continue;
}
pBlock->m_bNewBlock = false;
pBlock->m_afterTime = time(NULL);
pBlock->m_afterTime = time(0);
hasOprate = true;
nCreateCount++;
continue;
Expand All @@ -359,15 +359,15 @@ template <typename T> class DataWriter : public DataWriterBase

if (bNeedSave)
{
pBlock->m_beforeTime = time(NULL);
pBlock->m_beforeTime = time(0);
if (!pdata->Update(pdb))
{
m_nErrorCount++;
continue;
}
hasOprate = true;
nUpdateCount++;
pBlock->m_afterTime = time(NULL);
pBlock->m_afterTime = time(0);
continue;
}

Expand All @@ -376,15 +376,15 @@ template <typename T> class DataWriter : public DataWriterBase
///释放的时候执行一次保存...如果上次没有保存成功或者,释放前修改了就再保存一次
if ((lastMotifyTime > 0) && (afterTime < beforeTime || lastMotifyTime > beforeTime))
{
pBlock->m_beforeTime = time(NULL);
pBlock->m_beforeTime = time(0);
if (!pdata->Update(pdb))
{
m_nErrorCount++;
continue;
}
hasOprate = true;
nUpdateCount++;
pBlock->m_afterTime = time(NULL);
pBlock->m_afterTime = time(0);
}
m_MemoryPool->DestoryObject(pdata);
nRealseCount++;
Expand Down
5 changes: 5 additions & 0 deletions Server/Src/WatchServer/GameService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ BOOL CGameService::Init()
return FALSE;
}

if (CommonFunc::IsAlreadyRun("WatchServer" + CConfigFile::GetInstancePtr()->GetIntValue("areaid")))
{
return FALSE;
}

CLog::GetInstancePtr()->SetLogLevel(CConfigFile::GetInstancePtr()->GetIntValue("watch_log_level"));

UINT16 nPort = CConfigFile::GetInstancePtr()->GetRealNetPort("watch_svr_port");
Expand Down
6 changes: 2 additions & 4 deletions Server/Src/WatchServer/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ int main(int argc, char* argv[])
{
SetCrashReport("WatchServer");

if (!CGameService::GetInstancePtr()->Init())
if (CGameService::GetInstancePtr()->Init())
{
return 0;
CGameService::GetInstancePtr()->Run();
}

CGameService::GetInstancePtr()->Run();

CGameService::GetInstancePtr()->Uninit();

UnSetCrashReport();
Expand Down

0 comments on commit e912dcd

Please sign in to comment.