Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ylmbtm committed Sep 24, 2020
1 parent ac0001a commit e041ceb
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 21 deletions.
23 changes: 22 additions & 1 deletion Server/Src/ServerEngine/CommonConvert.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "stdafx.h"
#include "CommonConvert.h"


INT32 CommonConvert::StringToInt(char* pStr)
{
if(pStr == NULL)
Expand Down Expand Up @@ -600,3 +599,25 @@ BOOL CommonConvert::StringTrim(std::string& strValue)
}
return TRUE;
}

BOOL CommonConvert::StrCopy(char* pszDest, const char* pszSrc, INT32 nLen)
{
if (pszDest == NULL || pszSrc == NULL)
{
return FALSE;
}

if (nLen <= 0)
{
return FALSE;
}

std::strncpy(pszDest, pszSrc, nLen - 1);

if (strlen(pszSrc) >= nLen)
{
return FALSE;
}

return TRUE;
}
2 changes: 2 additions & 0 deletions Server/Src/ServerEngine/CommonConvert.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ BOOL HasSymbol(const char* pStr, const char* pszSymbol);

BOOL StringTrim(std::string& strValue);

BOOL StrCopy(char* pszDest, const char* pszSrc, INT32 nLen);

}


Expand Down
26 changes: 24 additions & 2 deletions Server/Src/ServerEngine/CommonFunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,9 +637,31 @@ BOOL CommonFunc::IsProcessExist(UINT64 dwPid)
return TRUE;
}

INT32 CommonFunc::Min(INT32 nValue1, INT32 nValue2)
UINT32 CommonFunc::GetProcessID(std::string strProcName)
{
return (nValue1 < nValue2) ? nValue1 : nValue2;
#ifdef WIN32
UINT32 dwProcID = 0;
HANDLE Snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 stProcessEntry;
stProcessEntry.dwSize = sizeof(PROCESSENTRY32);
BOOL bRet = Process32First(Snapshot, &stProcessEntry);
while (bRet)
{
if(stricmp(strProcName.c_str(), stProcessEntry.szExeFile) == 0)
{
dwProcID = stProcessEntry.th32ProcessID;
break;
}

bRet = Process32Next(Snapshot, &stProcessEntry);
}

CloseHandle(Snapshot);

return dwProcID;
#else
return 0;
#endif
}

BOOL CommonFunc::IsAlreadyRun(std::string strSignName)
Expand Down
2 changes: 1 addition & 1 deletion Server/Src/ServerEngine/CommonFunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ BOOL KillProcess(UINT64 dwPid);

BOOL IsProcessExist(UINT64 dwPid);

INT32 Min(INT32 nValue1, INT32 nValue2);
UINT32 GetProcessID(std::string strProcName);

BOOL IsAlreadyRun(std::string strSignName);

Expand Down
26 changes: 18 additions & 8 deletions Server/Src/ServerEngine/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,18 +413,25 @@ BOOL CConnection::CheckHeader(CHAR* m_pPacket)
return FALSE;
}

if (pHeader->dwMsgID > 4999999)
if (pHeader->dwMsgID > 399999 || pHeader->dwMsgID == 0)
{
return FALSE;
}

UINT32 dwPktChkNo = pHeader->dwPacketNo - (pHeader->dwMsgID ^ pHeader->dwSize);

if (dwPktChkNo <= 0)
{
return FALSE;
}

if(m_nCheckNo == 0)
{
m_nCheckNo = pHeader->dwPacketNo - (pHeader->dwMsgID ^ pHeader->dwSize) + 1;
m_nCheckNo = dwPktChkNo + 1;
return TRUE;
}

if(pHeader->dwPacketNo == (pHeader->dwMsgID ^ pHeader->dwSize) + m_nCheckNo)
if(m_nCheckNo == dwPktChkNo)
{
m_nCheckNo += 1;
return TRUE;
Expand Down Expand Up @@ -515,10 +522,10 @@ BOOL CConnection::DoSend()
int nRet = WSASend(m_hSocket, &DataBuf, 1, &dwSendBytes, 0, (LPOVERLAPPED)&m_IoOverlapSend, NULL);
if(nRet == 0) //发送成功
{
if(dwSendBytes < DataBuf.len)
{
CLog::GetInstancePtr()->LogError("发送线程:直接发送数据成功send:%d--Len:%d!", dwSendBytes, DataBuf.len);
}
//if(dwSendBytes < DataBuf.len)
//{
// CLog::GetInstancePtr()->LogError("发送线程:直接发送数据成功send:%d--Len:%d!", dwSendBytes, DataBuf.len);
//}
}
else if( nRet == -1 ) //发送出错
{
Expand Down Expand Up @@ -662,7 +669,10 @@ CConnection* CConnectionMgr::GetConnectionByID( UINT32 dwConnID )

CConnection* pConnect = m_vtConnList.at(dwIndex == 0 ? (m_vtConnList.size() - 1) : (dwIndex - 1));

ERROR_RETURN_NULL(pConnect->GetConnectionID() == dwConnID);
if (pConnect->GetConnectionID() != dwConnID)
{
return NULL;
}

return pConnect;
}
Expand Down
9 changes: 8 additions & 1 deletion Server/Src/ServerEngine/DBInterface/CppMysql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,14 @@ bool CppMySQL3DB::open(const char* host, const char* user, const char* passwd, c
goto EXT;
}

//unsigned int timeout = 2;
//if (0 != mysql_options(m_pMySqlDB, MYSQL_OPT_CONNECT_TIMEOUT, (const char*)&timeout))
//{
// m_nErrno = mysql_errno(m_pMySqlDB);
// m_strError = mysql_error(m_pMySqlDB);
// goto EXT;
//}

//如果连接失败,返回NULL。对于成功的连接,返回值与第1个参数的值相同。
if ( NULL == mysql_real_connect( m_pMySqlDB, host, user, passwd, db, port, NULL, client_flag) )
{
Expand Down Expand Up @@ -507,7 +515,6 @@ int CppMySQL3DB::execSQL(const char* sql)
}
}


return -1;
}

Expand Down
21 changes: 13 additions & 8 deletions Server/Src/ServerEngine/NetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ BOOL CNetManager::WorkThread_Listen()
return FALSE;
}
CommonSocket::SetSocketBlock(hClientSocket, FALSE);
CommonSocket::SetSocketNoDelay(hClientSocket);
//CommonSocket::SetSocketNoDelay(hClientSocket);
CConnection* pConnection = AssociateCompletePort(hClientSocket, FALSE);
if(pConnection != NULL)
{
Expand Down Expand Up @@ -173,19 +173,24 @@ BOOL CNetManager::WorkThread_ProcessEvent(UINT32 nParam)
bRetValue = GetQueuedCompletionStatus(m_hCompletePort, &dwNumOfByte, &CompleteKey, &lpOverlapped, dwWaitTime);
if(!bRetValue)
{
if(lpOverlapped == NULL)
if (lpOverlapped == NULL)
{
if(ERROR_ABANDONED_WAIT_0 == CommonSocket::GetSocketLastError())
if (ERROR_ABANDONED_WAIT_0 == CommonSocket::GetSocketLastError())
{
CLog::GetInstancePtr()->LogError("完成端口被外部关闭!");
return FALSE;
}

if(CommonSocket::GetSocketLastError() == WAIT_TIMEOUT)
if (CommonSocket::GetSocketLastError() == WAIT_TIMEOUT)
{
continue;
}
}
//else
//{
// NetIoOperatorData* pIoPeratorData = (NetIoOperatorData*)lpOverlapped;
// CLog::GetInstancePtr()->LogError(" GetQueuedCmpletionStatus Error: %d-%d-%s", pIoPeratorData->dwOpType, CommonFunc::GetLastError(), CommonFunc::GetLastErrorStr(CommonFunc::GetLastError()).c_str());
//}
}

NetIoOperatorData* pIoPeratorData = (NetIoOperatorData*)lpOverlapped;
Expand Down Expand Up @@ -322,7 +327,7 @@ BOOL CNetManager::WorkThread_ProcessEvent(UINT32 nParam)
}

CommonSocket::SetSocketBlock(m_hCurAcceptSocket, FALSE);
CommonSocket::SetSocketNoDelay(m_hCurAcceptSocket);
//CommonSocket::SetSocketNoDelay(m_hCurAcceptSocket);
CConnection* pConnection = AssociateCompletePort(m_hCurAcceptSocket, FALSE);
if (pConnection != NULL)
{
Expand Down Expand Up @@ -455,7 +460,7 @@ BOOL CNetManager::WorkThread_ProcessEvent(UINT32 nParam)
}

CommonSocket::SetSocketBlock(hClientSocket, FALSE);
CommonSocket::SetSocketNoDelay(hClientSocket);
//CommonSocket::SetSocketNoDelay(hClientSocket);
CConnection* pConnection = AssociateCompletePort(hClientSocket, FALSE);
if (pConnection != NULL)
{
Expand Down Expand Up @@ -673,7 +678,7 @@ CConnection* CNetManager::ConnectTo_Sync( std::string strIpAddr, UINT16 sPort )

CommonSocket::SetSocketBlock(hSocket, TRUE);

CommonSocket::SetSocketNoDelay(hSocket);
//CommonSocket::SetSocketNoDelay(hSocket);

if(!CommonSocket::ConnectSocket(hSocket, strIpAddr.c_str(), sPort))
{
Expand Down Expand Up @@ -714,7 +719,7 @@ CConnection* CNetManager::ConnectTo_Async( std::string strIpAddr, UINT16 sPort )

CommonSocket::SetSocketBlock(hSocket, FALSE);

CommonSocket::SetSocketNoDelay(hSocket);
//CommonSocket::SetSocketNoDelay(hSocket);

#ifdef WIN32
CConnection* pConnection = AssociateCompletePort(hSocket, TRUE);
Expand Down

0 comments on commit e041ceb

Please sign in to comment.