Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
Former-commit-id: df5bca8f22523ff5174c0abf8b9735bf066fb662 [formerly fae4b9d3b31f4f47870d58322b0044c6c427b76a] [formerly da11148a1c75b423ef5b3ecfb874dcbd0b7c2efb [formerly 2d0d2f85a1bf02b5033c98d39abaffc406072782]]
Former-commit-id: 86c9d5384e1cd070cad4df9cf535b75aa5f8a019 [formerly 54b637a10e68f5d6d52ac08f0495d3f5c7b8211b]
Former-commit-id: d8f65cdcd654e9d631b8e62af42dfdd2c5e3415d
  • Loading branch information
nickyang4github committed Apr 23, 2018
2 parents 719ab71 + b56f3ee commit f654f53
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 72 deletions.
5 changes: 2 additions & 3 deletions Frame/SDK/Core/Base/AFLockFreeQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "SDK/Core/Base/AFPlatform.hpp"
#include "common/readerwriterqueue.h"


template<typename T>
class AFLockFreeQueue
{
Expand All @@ -47,11 +46,11 @@ class AFLockFreeQueue
return mList.try_dequeue(object);
}

int Count()
size_t Count()
{
return mList.size_approx();
}

private:
moodycamel::BlockingReaderWriterQueue <T> mList;
moodycamel::BlockingReaderWriterQueue<T> mList;
};
45 changes: 22 additions & 23 deletions Frame/SDK/Net/AFCNetClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void AFCNetClient::ProcessMsgLogicThread()
ProcessMsgLogicThread(m_pClientObject.get());
}

if(NULL != m_pClientObject.get() && m_pClientObject->NeedRemove())
if(m_pClientObject != nullptr && m_pClientObject->NeedRemove())
{
AFScopeWrLock xGuard(mRWLock);
m_pClientObject.release();
Expand All @@ -64,16 +64,16 @@ void AFCNetClient::ProcessMsgLogicThread(NetObject* pEntity)
}

//Handle Msg;
const int nReceiveCount = pEntity->mqMsgFromNet.Count();
for(size_t i = 0; (i < nReceiveCount); i++)
size_t nReceiveCount = pEntity->mqMsgFromNet.Count();
for(size_t i = 0; i < nReceiveCount; ++i)
{
MsgFromNetInfo* pMsgFromNet(NULL);
MsgFromNetInfo* pMsgFromNet(nullptr);
if(!pEntity->mqMsgFromNet.Pop(pMsgFromNet))
{
break;
}

if(!pMsgFromNet)
if(pMsgFromNet == nullptr)
{
continue;
}
Expand Down Expand Up @@ -109,25 +109,25 @@ void AFCNetClient::ProcessMsgLogicThread(NetObject* pEntity)
}
void AFCNetClient::Initialization(const std::string& strAddrPort, const int nServerID)
{
#ifdef _MSC_VER
#if ARK_PLATFORM == PLATFORM_WIN
WSADATA wsa_data;
WSAStartup(MAKEWORD(2, 2), &wsa_data);
#endif

mstrIPPort = strAddrPort;
mnServerID = nServerID;
#if __cplusplus < 201402L
m_pThread.reset(new evpp::EventLoopThread);
#if defined(HAVE_LANG_CXX14) || defined(HAVE_LANG_CXX17)
m_pThread = std::make_unique<evpp::EventLoopThread>();
#else
m_pThread = std::make_unique<evpp::EventLoopThread>();
m_pThread.reset(new evpp::EventLoopThread);
#endif
m_pThread->set_name("TCPClientThread");
m_pThread->Start();

#if __cplusplus < 201402L
m_pClient.reset(new evpp::TCPClient(m_pThread->loop(), mstrIPPort, "TCPPingPongClient"));
#if defined(HAVE_LANG_CXX14) || defined(HAVE_LANG_CXX17)
m_pClient = std::make_unique<evpp::TCPClient>(m_pThread->loop(), mstrIPPort, "TCPClient");
#else
m_pClient = std::make_unique<evpp::TCPClient>(m_pThread->loop(), mstrIPPort, "TCPPingPongClient");
m_pClient.reset(new evpp::TCPClient(m_pThread->loop(), mstrIPPort, "TCPClient"));
#endif

m_pClient->SetConnectionCallback(std::bind(&AFCNetClient::OnClientConnectionInner,this, std::placeholders::_1));
Expand Down Expand Up @@ -156,11 +156,12 @@ bool AFCNetClient::CloseSocketAll()
return true;
}

bool AFCNetClient::SendMsg(const char* msg, const uint32_t nLen, const AFGUID& xClient)
bool AFCNetClient::SendMsg(const char* msg, const size_t nLen, const AFGUID& xClient)
{
if(m_pClient->conn().get())
if(m_pClient->conn() != nullptr)
{
m_pClient->conn()->Send(msg, nLen);
return true;
}

return false;
Expand All @@ -185,7 +186,7 @@ bool AFCNetClient::DismantleNet(NetObject* pEntity)
pNetInfo->nType = RECIVEDATA;
pNetInfo->strMsg.append(pEntity->GetBuff() + AFIMsgHead::ARK_MSG_HEAD_LENGTH, nMsgBodyLength);
pEntity->mqMsgFromNet.Push(pNetInfo);
int nNewSize = pEntity->RemoveBuff(nMsgBodyLength + AFIMsgHead::ARK_MSG_HEAD_LENGTH);
size_t nNewSize = pEntity->RemoveBuff(nMsgBodyLength + AFIMsgHead::ARK_MSG_HEAD_LENGTH);
}
else
{
Expand Down Expand Up @@ -253,8 +254,6 @@ void AFCNetClient::OnClientConnectionInner(const evpp::TCPConnPtr& conn)
pMsg->xClientID = conn->id();
pMsg->nType = DISCONNECTED;


//娑撹崵鍤庣粙瀣╃瑝閼崇晫娲块幒銉ュ灩闂勩們鈧倷绗夐悞鎯扮箹闁插苯姘ㄩ柌搴濈啊
if(!conn->context().IsEmpty())
{
NetObject* pEntity = evpp::any_cast<NetObject*>(conn->context());
Expand Down Expand Up @@ -297,7 +296,7 @@ void AFCNetClient::OnMessageInner(const evpp::TCPConnPtr& conn, evpp::Buffer* ms
}
}

bool AFCNetClient::SendMsgWithOutHead(const int16_t nMsgID, const char* msg, const uint32_t nLen, const AFGUID & xClientID, const AFGUID& xPlayerID)
bool AFCNetClient::SendMsgWithOutHead(const uint16_t nMsgID, const char* msg, const size_t nLen, const AFGUID & xClientID, const AFGUID& xPlayerID)
{
std::string strOutData;
AFCMsgHead xHead;
Expand All @@ -314,21 +313,21 @@ bool AFCNetClient::SendMsgWithOutHead(const int16_t nMsgID, const char* msg, con
return false;
}

int AFCNetClient::EnCode(const AFCMsgHead& xHead, const char* strData, const uint32_t unDataLen, std::string& strOutData)
int AFCNetClient::EnCode(const AFCMsgHead& xHead, const char* strData, const size_t len, std::string& strOutData)
{
char szHead[AFIMsgHead::ARK_MSG_HEAD_LENGTH] = { 0 };
int nSize = xHead.EnCode(szHead);

strOutData.clear();
strOutData.append(szHead, AFIMsgHead::ARK_MSG_HEAD_LENGTH);
strOutData.append(strData, unDataLen);
strOutData.append(strData, len);

return xHead.GetBodyLength() + AFIMsgHead::ARK_MSG_HEAD_LENGTH;
}

int AFCNetClient::DeCode(const char* strData, const uint32_t unAllLen, AFCMsgHead & xHead)
int AFCNetClient::DeCode(const char* strData, const size_t len, AFCMsgHead & xHead)
{
if(unAllLen < AFIMsgHead::ARK_MSG_HEAD_LENGTH)
if(len < AFIMsgHead::ARK_MSG_HEAD_LENGTH)
{
return -1;
}
Expand All @@ -338,7 +337,7 @@ int AFCNetClient::DeCode(const char* strData, const uint32_t unAllLen, AFCMsgHea
return -2;
}

if(xHead.GetBodyLength() > (unAllLen - AFIMsgHead::ARK_MSG_HEAD_LENGTH))
if(xHead.GetBodyLength() > (len - AFIMsgHead::ARK_MSG_HEAD_LENGTH))
{
return -3;
}
Expand Down
10 changes: 5 additions & 5 deletions Frame/SDK/Net/AFCNetClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class AFCNetClient : public AFINet
}

template<typename BaseType>
AFCNetClient(BaseType* pBaseType, void (BaseType::*handleRecieve)(const AFIMsgHead& xHead, const int, const char*, const uint32_t, const AFGUID&), void (BaseType::*handleEvent)(const NetEventType, const AFGUID&, const int))
AFCNetClient(BaseType* pBaseType, void (BaseType::*handleRecieve)(const AFIMsgHead& xHead, const int, const char*, const size_t, const AFGUID&), void (BaseType::*handleEvent)(const NetEventType, const AFGUID&, const int))
{
mRecvCB = std::bind(handleRecieve, pBaseType, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5);
mEventCB = std::bind(handleEvent, pBaseType, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
Expand All @@ -64,7 +64,7 @@ class AFCNetClient : public AFINet
virtual void Update();
virtual void Initialization(const std::string& strAddrPort, const int nServerID);
virtual bool Final();
virtual bool SendMsgWithOutHead(const int16_t nMsgID, const char* msg, const uint32_t nLen, const AFGUID& xClientID = 0, const AFGUID& xPlayerID = 0);
virtual bool SendMsgWithOutHead(const uint16_t nMsgID, const char* msg, const size_t nLen, const AFGUID& xClientID = 0, const AFGUID& xPlayerID = 0);

virtual bool CloseNetObject(const AFGUID& xClient);

Expand All @@ -82,7 +82,7 @@ class AFCNetClient : public AFINet
evpp::Buffer* msg);

private:
bool SendMsg(const char* msg, const uint32_t nLen, const AFGUID& xClient = 0);
bool SendMsg(const char* msg, const size_t nLen, const AFGUID& xClient = 0);

bool DismantleNet(NetObject* pEntity);
void ProcessMsgLogicThread();
Expand All @@ -92,8 +92,8 @@ class AFCNetClient : public AFINet
static void log_cb(int severity, const char* msg);

protected:
int DeCode(const char* strData, const uint32_t unLen, AFCMsgHead& xHead);
int EnCode(const AFCMsgHead& xHead, const char* strData, const uint32_t unDataLen, std::string& strOutData);
int DeCode(const char* strData, const size_t len, AFCMsgHead& xHead);
int EnCode(const AFCMsgHead& xHead, const char* strData, const size_t len, std::string& strOutData);

private:
std::unique_ptr<evpp::EventLoopThread> m_pThread;
Expand Down
28 changes: 14 additions & 14 deletions Frame/SDK/Net/AFCNetServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,17 @@ void AFCNetServer::ProcessMsgLogicThread()

void AFCNetServer::ProcessMsgLogicThread(NetObject* pEntity)
{
//Handle Msg;
const int nReceiveCount = pEntity->mqMsgFromNet.Count();
for(size_t i = 0; (i < nReceiveCount); i++)
//Handle Msg
size_t nReceiveCount = pEntity->mqMsgFromNet.Count();
for(size_t i = 0; i < nReceiveCount; ++i)
{
MsgFromNetInfo* pMsgFromNet(NULL);
if(!pEntity->mqMsgFromNet.Pop(pMsgFromNet))
{
break;
}

if(!pMsgFromNet)
if(pMsgFromNet == nullptr)
{
continue;
}
Expand Down Expand Up @@ -222,7 +222,7 @@ bool AFCNetServer::Final()
return true;
}

bool AFCNetServer::SendMsgToAllClient(const char* msg, const uint32_t nLen)
bool AFCNetServer::SendMsgToAllClient(const char* msg, const size_t nLen)
{
std::map<AFGUID, NetObject*>::iterator it = mmObject.begin();
for(; it != mmObject.end(); ++it)
Expand All @@ -237,7 +237,7 @@ bool AFCNetServer::SendMsgToAllClient(const char* msg, const uint32_t nLen)
return true;
}

bool AFCNetServer::SendMsg(const char* msg, const uint32_t nLen, const AFGUID& xClient)
bool AFCNetServer::SendMsg(const char* msg, const size_t nLen, const AFGUID& xClient)
{
AFScopeRdLock xGuard(mRWLock);

Expand Down Expand Up @@ -290,7 +290,7 @@ bool AFCNetServer::DismantleNet(NetObject* pEntity)
pNetInfo->nType = RECIVEDATA;
pNetInfo->strMsg.append(pEntity->GetBuff() + AFIMsgHead::ARK_MSG_HEAD_LENGTH, nMsgBodyLength);
pEntity->mqMsgFromNet.Push(pNetInfo);
int nRet = pEntity->RemoveBuff(nMsgBodyLength + AFIMsgHead::ARK_MSG_HEAD_LENGTH);
size_t nRet = pEntity->RemoveBuff(nMsgBodyLength + AFIMsgHead::ARK_MSG_HEAD_LENGTH);
}
else
{
Expand Down Expand Up @@ -326,7 +326,7 @@ NetObject* AFCNetServer::GetNetObject(const AFGUID& xClientID)
return NULL;
}

bool AFCNetServer::SendMsgWithOutHead(const int16_t nMsgID, const char* msg, const uint32_t nLen, const AFGUID& xClientID, const AFGUID& xPlayerID)
bool AFCNetServer::SendMsgWithOutHead(const uint16_t nMsgID, const char* msg, const size_t nLen, const AFGUID& xClientID, const AFGUID& xPlayerID)
{
std::string strOutData;
AFCMsgHead xHead;
Expand All @@ -343,7 +343,7 @@ bool AFCNetServer::SendMsgWithOutHead(const int16_t nMsgID, const char* msg, con
return false;
}

bool AFCNetServer::SendMsgToAllClientWithOutHead(const int16_t nMsgID, const char* msg, const uint32_t nLen, const AFGUID& xPlayerID)
bool AFCNetServer::SendMsgToAllClientWithOutHead(const uint16_t nMsgID, const char* msg, const size_t nLen, const AFGUID& xPlayerID)
{
std::string strOutData;
AFCMsgHead xHead;
Expand All @@ -359,21 +359,21 @@ bool AFCNetServer::SendMsgToAllClientWithOutHead(const int16_t nMsgID, const cha
return false;
}

int AFCNetServer::EnCode(const AFCMsgHead& xHead, const char* strData, const uint32_t unDataLen, std::string& strOutData)
int AFCNetServer::EnCode(const AFCMsgHead& xHead, const char* strData, const size_t len, std::string& strOutData)
{
char szHead[AFIMsgHead::ARK_MSG_HEAD_LENGTH] = { 0 };
int nRet = xHead.EnCode(szHead);

strOutData.clear();
strOutData.append(szHead, AFIMsgHead::ARK_MSG_HEAD_LENGTH);
strOutData.append(strData, unDataLen);
strOutData.append(strData, len);

return xHead.GetBodyLength() + AFIMsgHead::ARK_MSG_HEAD_LENGTH;
}

int AFCNetServer::DeCode(const char* strData, const uint32_t unAllLen, AFCMsgHead& xHead)
int AFCNetServer::DeCode(const char* strData, const size_t len, AFCMsgHead& xHead)
{
if(unAllLen < AFIMsgHead::ARK_MSG_HEAD_LENGTH)
if(len < AFIMsgHead::ARK_MSG_HEAD_LENGTH)
{
return -1;
}
Expand All @@ -383,7 +383,7 @@ int AFCNetServer::DeCode(const char* strData, const uint32_t unAllLen, AFCMsgHea
return -2;
}

if(xHead.GetBodyLength() > (unAllLen - AFIMsgHead::ARK_MSG_HEAD_LENGTH))
if(xHead.GetBodyLength() > (len - AFIMsgHead::ARK_MSG_HEAD_LENGTH))
{
return -3;
}
Expand Down
20 changes: 9 additions & 11 deletions Frame/SDK/Net/AFCNetServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class AFCNetServer : public AFINet
}

template<typename BaseType>
AFCNetServer(BaseType* pBaseType, void (BaseType::*handleRecieve)(const AFIMsgHead& xHead, const int, const char*, const uint32_t, const AFGUID&), void (BaseType::*handleEvent)(const NetEventType, const AFGUID&, const int))
AFCNetServer(BaseType* pBaseType, void (BaseType::*handleRecieve)(const AFIMsgHead& xHead, const int, const char*, const size_t, const AFGUID&), void (BaseType::*handleEvent)(const NetEventType, const AFGUID&, const int))
: mnMaxConnect(0)
, mnCpuCount(0)
, mnServerID(0)
Expand All @@ -74,8 +74,8 @@ class AFCNetServer : public AFINet
return true;
}

virtual bool SendMsgWithOutHead(const int16_t nMsgID, const char* msg, const uint32_t nLen, const AFGUID& xClientID, const AFGUID& xPlayerID);
virtual bool SendMsgToAllClientWithOutHead(const int16_t nMsgID, const char* msg, const uint32_t nLen, const AFGUID& xPlayerID);
virtual bool SendMsgWithOutHead(const uint16_t nMsgID, const char* msg, const size_t nLen, const AFGUID& xClientID, const AFGUID& xPlayerID);
virtual bool SendMsgToAllClientWithOutHead(const uint16_t nMsgID, const char* msg, const size_t nLen, const AFGUID& xPlayerID);

virtual bool CloseNetObject(const AFGUID& xClientID);
virtual bool Log(int severity, const char* msg)
Expand All @@ -85,18 +85,16 @@ class AFCNetServer : public AFINet

public:
//From Worker Thread
static void OnMessage(const evpp::TCPConnPtr& conn,
evpp::Buffer* msg, void* pData);
void OnMessageInner(const evpp::TCPConnPtr& conn,
evpp::Buffer* msg);
static void OnMessage(const evpp::TCPConnPtr& conn, evpp::Buffer* msg, void* pData);
void OnMessageInner(const evpp::TCPConnPtr& conn, evpp::Buffer* msg);

//From ListenThread
static void OnClientConnection(const evpp::TCPConnPtr& conn, void* pData);
void OnClientConnectionInner(const evpp::TCPConnPtr& conn);

private:
bool SendMsgToAllClient(const char* msg, const uint32_t nLen);
bool SendMsg(const char* msg, const uint32_t nLen, const AFGUID& xClient);
bool SendMsgToAllClient(const char* msg, const size_t nLen);
bool SendMsg(const char* msg, const size_t nLen, const AFGUID& xClient);
bool AddNetObject(const AFGUID& xClientID, NetObject* pEntity);
bool RemoveNetObject(const AFGUID& xClientID);
NetObject* GetNetObject(const AFGUID& xClientID);
Expand All @@ -108,8 +106,8 @@ class AFCNetServer : public AFINet
bool DismantleNet(NetObject* pEntity);

protected:
int DeCode(const char* strData, const uint32_t unLen, AFCMsgHead& xHead);
int EnCode(const AFCMsgHead& xHead, const char* strData, const uint32_t unDataLen, std::string& strOutData);
int DeCode(const char* strData, const size_t len, AFCMsgHead& xHead);
int EnCode(const AFCMsgHead& xHead, const char* strData, const size_t len, std::string& strOutData);

private:
std::unique_ptr<evpp::TCPServer> m_pTcpSrv;
Expand Down
Loading

0 comments on commit f654f53

Please sign in to comment.