1 Star 0 Fork 32

MichaelFeng/CM-Server

forked from 留天下/CM-Server 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
networkmanager.cpp 1.92 KB
一键复制 编辑 原始数据 按行查看 历史
留天下 提交于 2019-02-22 13:22 . net redo done!
#include"networkmanager.h"
#include"Json/reader.h"
#include"tool.h"
using namespace Json;
DEFINE_SINGLE_ATTRIBUTES(NetWorkManager);
NetWorkManager::NetWorkManager()
{
}
void NetWorkManager::startServer()
{
if(!m_socketManager.StartServer())
{
cout<<"start server success!"<<sizeof(int)<<endl;
}
else
{
cout<<m_socketManager.GetLastError()<<endl;
cout<<"start server failed!"<<endl;
}
}
Slot* NetWorkManager::add(const int & messageId, const EventHandler& evHandler)
{
if (m_signals.find(messageId) != m_signals.end())
return m_signals[messageId].add(evHandler);
else
{
m_signals[messageId] = Signal();
return m_signals[messageId].add(evHandler);
}
}
Slot * NetWorkManager::add(const int & messageId, const std::function<void(Json::Value&)>& func)
{
if (m_signals.find(messageId) != m_signals.end())
return m_signals[messageId].add(func);
else
{
m_signals[messageId] = Signal();
return m_signals[messageId].add(func);
}
}
void NetWorkManager::dispatch(const int & messageId,Json::Value& message)
{
cout<<"NetWorkManager::dispatch"<<message.toStyledString()<<endl;
m_signals[messageId].dispatch(message);
}
void NetWorkManager::send(const int& fd,const int & messageId, const std::map<std::string, std::string>& msg)
{
Json::Value value;
value["MessageId"] = messageId;
for (auto var : msg)
{
if (isNum(var.second))
value[var.first] = std::stof(var.second);
else
value[var.first] = var.second;
}
std::string out = value.toStyledString();
m_socketManager.SendMsg(fd,(char*)out.c_str(), out.length());
}
void NetWorkManager::send(const int& fd,const int & messageId, Json::Value& msg)
{
msg["MessageId"]=messageId;
std::string out=msg.toStyledString();
cout<<"send:"<<out<<endl;
m_socketManager.SendMsg(fd,(char*)out.c_str(),out.length());
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/michael_fengjun/CM-Server.git
git@gitee.com:michael_fengjun/CM-Server.git
michael_fengjun
CM-Server
CM-Server
master

搜索帮助