Skip to content

Commit

Permalink
fix dead lock bug, add Login Command
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Oct 20, 2015
1 parent 6be050f commit b217972
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 29 deletions.
13 changes: 7 additions & 6 deletions base/CmdDispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,36 @@

namespace MyNameSpace
{
using CallBackFunT = std::function< bool(const Command::BaseCommand *, uint32_t, int)>;
class Dispatcher
{
public:
Dispatcher(const std::string & name) : mName(name){}
~Dispatcher(){}
const std::string & getName() const { return mName;}
void regCallback(uint32_t cmdId, std::function< bool(const Command::BaseCommand *, uint32_t) > fun)
void regCallback(uint32_t cmdId, CallBackFunT fun)
{
// funTable.insert(std::make_pair<uint32_t, std::function<bool <Command::BaseCommand *, uint32_t> > >(cmdId, fun));
funTable[cmdId] = fun;
}

bool dispatcher(const Command::BaseCommand *cmd, uint32_t cmdLen)
bool dispatcher(const Command::BaseCommand *cmd, uint32_t cmdLen, int taskId)
{
std::function<bool (const Command::BaseCommand *, uint32_t) > fun = funTable[cmd->mCmdId];
CallBackFunT fun = funTable[cmd->mCmdId];
if (fun)
{
return fun(cmd, cmdLen);
return fun(cmd, cmdLen, taskId);
}
else
{
std::cerr<<"cmdId:"<<cmd->mCmdId<<"not regeister callback"<<std::endl;
std::cerr<<"cmdId:"<<cmd->mCmdId<<"not regeister callback"<<" taskId: "<<taskId<<std::endl;
}
return false;
}

private:
// std::unordered_map<uint32_t, std::function< bool(BaseCommand *, uint32_t) > > funTable;
std::map<uint32_t, std::function< bool(const Command::BaseCommand *, uint32_t) > > funTable;
std::map<uint32_t, CallBackFunT> funTable;
std::string mName;
};
}
Expand Down
24 changes: 24 additions & 0 deletions base/MySockTaskManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,28 @@ namespace MyNameSpace
}
return true;
}

MySockTask* MySockTaskManager::getTaskByIdWithOutLock(int id)
{
Container_IT it;
// MyScopeLock lock(mLock);
it = mTasks.find(id);
if (mTasks.end() == it)
{
return NULL;
}
return it->second;
}

MySockTask* MySockTaskManager::getTaskByIdWithLock(int id)
{
Container_IT it;
MyScopeLock lock(mLock);
it = mTasks.find(id);
if (mTasks.end() == it)
{
return NULL;
}
return it->second;
}
}
2 changes: 2 additions & 0 deletions base/MySockTaskManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ namespace MyNameSpace
void addTask(MySockTask *task);
void removeTask(MySockTask *task);
bool doProcessMsg();
MySockTask* getTaskByIdWithOutLock(int id);
MySockTask* getTaskByIdWithLock(int id);
private:
typedef std::map<int, MySockTask*> Container;
typedef std::map<int, MySockTask*>::iterator Container_IT;
Expand Down
7 changes: 5 additions & 2 deletions base/MySockTaskPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,11 @@ namespace MyNameSpace
std::set<MySockTask *>::iterator iter = taskSet.begin();
for (; iter != taskSet.end(); ++iter)
{
MySockTaskManager::getInstance().removeTask(*iter);
delete *iter;
if (NULL != *iter)
{
MySockTaskManager::getInstance().removeTask(*iter);
delete *iter;
}
}
taskSet.clear();
}
Expand Down
4 changes: 2 additions & 2 deletions client/MyClientTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ namespace MyNameSpace
{
case Command::COMMAND_TYPE::INNER:
{
mInnerDispatcher->dispatcher(pCmd, len);
mInnerDispatcher->dispatcher(pCmd, len, getId());
}
break;
case Command::COMMAND_TYPE::OUTTER:
{
mOutterDispatcher->dispatcher(pCmd, len);
mOutterDispatcher->dispatcher(pCmd, len, getId());
}
break;
default:
Expand Down
5 changes: 3 additions & 2 deletions proto/BaseCmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
#ifndef __BASECMD_H_
#define __BASECMD_H_

#pragma pack(1)
#include <cstdint>

//#pragma pack(1)

namespace MyNameSpace
{
Expand All @@ -37,7 +39,6 @@ namespace MyNameSpace
}
uint32_t mCmdId;
COMMAND_TYPE mType; //0 InnerMessage, 1 OutterMessage,也就是服务器内部之间的消息是0,服务器和客户端之间的消息是1
char data[0];
}__attribute__ ((packed));
}
}
Expand Down
6 changes: 4 additions & 2 deletions proto/CmdNumber.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ namespace MyNameSpace
{
namespace Command
{
const uint32_t REQ_LOADBALANCE_CMD = 1;
const uint32_t RTN_LOADBALANCE_CMD = 2;
const uint32_t REQ_LOGIN_CMD = 1;
const uint32_t RTN_LOGIN_CMD = 2;
const uint32_t REQ_LOADBALANCE_CMD = 3;
const uint32_t RTN_LOADBALANCE_CMD = 4;
}
}

Expand Down
49 changes: 49 additions & 0 deletions proto/LoginProcessCmd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* =====================================================================================
*
* Filename: LoginProcessCmd.h
*
* Description: Login Command
*
* Version: 1.0
* Created: 10/20/2015 11:46:56 AM
* Revision: none
* Compiler: gcc
*
* Author: huangyun (hy), [email protected]
* Organization:
*
* =====================================================================================
*/

#ifndef __LOGINPROCESSCMD_H_
#define __LOGINPROCESSCMD_H_
#include "CmdNumber.h"
#include "BaseCmd.h"
#include <cstring>

namespace MyNameSpace
{
namespace Command
{
const int MAX_ACCOUNT_LEN = 48;
struct ReqLogin : BaseCommand
{
ReqLogin() : BaseCommand(REQ_LOGIN_CMD, COMMAND_TYPE::OUTTER)
{
memset(this, 0x0, sizeof(*this));
}
char account[MAX_ACCOUNT_LEN];
}__attribute__ ((packed));

struct RtnLogin : BaseCommand
{
RtnLogin() : BaseCommand(RTN_LOGIN_CMD, COMMAND_TYPE::OUTTER)
{
res = 0;
}
int res;
}__attribute__ ((packed));
}
}
#endif
6 changes: 2 additions & 4 deletions server/MyBaseServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ namespace MyNameSpace
{
class MyBaseServer
{
using CallBackFunT = std::function< bool(const Command::BaseCommand *, uint32_t)>;
public:
MyBaseServer() : mComplete(false), mInnerDispatcher("InnerDispatcher"), mOutterDispatcher("OutterDispatcher")
{

}
virtual bool reload(); //for hup signal
virtual bool init(int port);
Expand All @@ -30,8 +28,8 @@ namespace MyNameSpace
}
void mainLoop();
protected:
virtual void regInnerCallBack(uint32_t cmdId, CallBackFunT fun); //内部消息分发器,也就是服务器之间的消息
virtual void regOutterCallBack(uint32_t cmdId, CallBackFunT fun); //外部消息分发器,也就是服务端客户端之间的消息
void regInnerCallBack(uint32_t cmdId, CallBackFunT fun); //内部消息分发器,也就是服务器之间的消息
void regOutterCallBack(uint32_t cmdId, CallBackFunT fun); //外部消息分发器,也就是服务端客户端之间的消息
private:
int serverProcess();
bool isFini()
Expand Down
8 changes: 3 additions & 5 deletions server/MyServerTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,18 @@ namespace MyNameSpace
if (mInnerDispatcher && mOutterDispatcher)
{
const Command::BaseCommand *pCmd = reinterpret_cast<const Command::BaseCommand *>(msg);
// std::cerr<<len<<" : "<<sizeof (pCmd->mCmdId)<<" : "<<sizeof(pCmd->mType)<<" : "<<len - (sizeof (pCmd->mCmdId) + sizeof(pCmd->mType))<<std::endl;
std::string test(pCmd->data, len - (sizeof (pCmd->mCmdId) + sizeof(pCmd->mType)));
// std::cout<<"msg: "<<msg<<" Id:"<<pCmd->mCmdId<<" type: "<<(char)pCmd->mType<<" data: "<<pCmd->data<<" len: "<<len<<std::endl;
std::cout<<"msg: "<<msg<<" Id:"<<pCmd->mCmdId<<" type: "<<(char)pCmd->mType<<" data: "<<test<<" len: "<<len<<std::endl;
std::cout<<"msg: "<<msg<<" Id:"<<pCmd->mCmdId<<" type: "<<(char)pCmd->mType<<" len: "<<len<<std::endl;
switch(pCmd->mType)
{
case Command::COMMAND_TYPE::INNER:
{
mInnerDispatcher->dispatcher(pCmd, len);
mInnerDispatcher->dispatcher(pCmd, len, getId());
}
break;
case Command::COMMAND_TYPE::OUTTER:
{
mOutterDispatcher->dispatcher(pCmd, len);
mOutterDispatcher->dispatcher(pCmd, len, getId());
}
break;
default:
Expand Down
40 changes: 40 additions & 0 deletions test/LoginProcess.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* =====================================================================================
*
* Filename: LoginProcess.cpp
*
* Description: Login Process
*
* Version: 1.0
* Created: 10/20/2015 11:38:24 AM
* Revision: none
* Compiler: gcc
*
* Author: huangyun (hy), [email protected]
* Organization:
*
* =====================================================================================
*/

#include "LoginProcess.h"
#include "LoginProcessCmd.h"
#include "MySockTask.h"
#include "MySockTaskManager.h"
#include "LoginProcessCmd.h"

namespace MyNameSpace
{
bool LoginProcess::ReqLogin(const Command::BaseCommand *cmd, uint32_t len, int taskId)
{
MySockTask* task = MySockTaskManager::getInstance().getTaskByIdWithOutLock(taskId);
std::cout<<taskId<<std::endl;
if (NULL != task)
{
Command::RtnLogin rtn;
rtn.res = 1;
std::cout<<"return message! len:"<<sizeof(rtn)<<std::endl;
task->sendDataWithBuffer(reinterpret_cast<const char *>(&rtn), sizeof(rtn));
}
return true;
}
}
35 changes: 35 additions & 0 deletions test/LoginProcess.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* =====================================================================================
*
* Filename: LoginProcess.h
*
* Description: Login Process
*
* Version: 1.0
* Created: 10/20/2015 10:46:40 AM
* Revision: none
* Compiler: gcc
*
* Author: huangyun (hy), [email protected]
* Organization:
*
* =====================================================================================
*/


#ifndef __LOGINPROCESS_H_
#define __LOGINPROCESS_H_
#include "BaseCmd.h"
namespace MyNameSpace
{
class LoginProcess
{
public:
LoginProcess() {}
~LoginProcess() {}
public:
bool ReqLogin(const Command::BaseCommand *cmd, uint32_t len, int taskId);

};
#endif
}
9 changes: 6 additions & 3 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
CXXFLAGS=-Wall -std=c++11 -g -Wparentheses -Wl,-rpath,../lib
#CXXFLAGS=-Wall -std=c++11 -g -gstabs+
INCLUDE= -I../base -I../server -I../client -I../lib/tinyxml
INCLUDE= -I../base -I../server -I../client -I../proto -I../lib/tinyxml
LDFLAGS= -L../lib -lhyServer -L../lib -lhyClient -L../lib/tinyxml -ltinyxml
.PHONY : all
all : server
server : main.o Server.o XmlconfigParse.o
g++ $(CXXFLAGS) $(INCLUDE) main.o Server.o XmlconfigParse.o -o $@ $(LDFLAGS)
server : main.o Server.o XmlconfigParse.o LoginProcess.o
g++ $(CXXFLAGS) $(INCLUDE) main.o Server.o LoginProcess.o XmlconfigParse.o -o $@ $(LDFLAGS)
main.o : main.cpp
g++ $(CXXFLAGS) $(INCLUDE) -c $< -o $@
Server.o : Server.cpp Server.h
g++ $(CXXFLAGS) $(INCLUDE) -c $< -o $@

XmlconfigParse.o : XmlconfigParse.cpp XmlconfigParse.h
g++ $(CXXFLAGS) $(INCLUDE) -c $< -o $@
LoginProcess.o : LoginProcess.cpp LoginProcess.h
g++ $(CXXFLAGS) $(INCLUDE) -c $< -o $@

clean:
rm -rf server
rm -rf Server.o
rm -rf main.o
rm -rf XmlconfigParse.o
rm -rf LoginProcess.o
7 changes: 5 additions & 2 deletions test/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "MyServerTask.h"
#include "MyThread.h"
#include "XmlconfigParse.h"
#include "CmdNumber.h"

namespace MyNameSpace
{
Expand Down Expand Up @@ -83,21 +84,23 @@ namespace MyNameSpace
std::cout<<"ip: "<<iter.ip<<" id: "<<iter.id<<" port: "<<iter.port<<" type: "<<iter.type<<std::endl;
newClient(iter.ip.c_str(), iter.port, iter.id, iter.type);
}
initCallBack();
return true;

}
bool Server::reload()
{

//TODO reload config
return true;
}
void Server::initCallBack()
{
regOutterCallBack(Command::REQ_LOGIN_CMD, std::bind(&LoginProcess::ReqLogin, &loginProcess, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
}
bool Server::newTask(int sock)
{
std::cout<<__FUNCTION__<<"("<<__LINE__<<"): new task"<<std::endl;
// std::cout<<__FUNCTION__<<"("<<__LINE__<<"): new task"<<std::endl;
std::cout<<"new task, id :"<<mServerUniqueId<<std::endl;

MyServerTask *task = new MyServerTask(sock, mServerUniqueId, &mInnerDispatcher, &mOutterDispatcher);
if (NULL == task)
Expand Down
Loading

0 comments on commit b217972

Please sign in to comment.