放码过去
多余的废话就不多说了,直接放码上来吧。
网络层代码
chatserver.hpp,位于include/server目录下、
#ifndef CHATSERVER_H_
#define CHATSERVER_H_
#include<muduo/net/TcpServer.h>
#include<muduo/net/EventLoop.h>
#include<iostream>
using namespace std;
using namespace muduo;
using namespace muduo::net;
class ChatServer{
private:
TcpServer _server;
EventLoop* _loop;
public:
ChatServer(EventLoop* loop,
const InetAddress& listenAddr,
const string& nameArg);
void start();
private:
void onConnection(const TcpConnectionPtr& conn);
void onMessage(const TcpConnectionPtr& conn,Buffer* buff,Timestamp time);
};
#endif
chatserver.cpp,位于 src/server下
#include "chatserver.hpp"
#include <functional>
#include <string>
#include "chatservice.hpp"
#include "json.hpp"
using json = nlohmann::json;
using namespace std;
using namespace placeholders;
ChatServer::ChatServer(EventLoop *loop,
const InetAddress &listenAddr,
const string &nameArg) : _server(loop, listenAddr, nameArg),
_loop(loop)
{
_server.setConnectionCallback(std::bind(&ChatServer::onConnection, this, _1));
_server.setMessageCallback(std::bind(&ChatServer::onMessage, this, _1, _2, _3));
_server.setThreadNum(5);
}
void ChatServer::start()
{
_server.start();
}
void ChatServer::onMessage(const TcpConnectionPtr &conn, Buffer *buff, Timestamp time){
string buf = buff->retrieveAllAsString();
json js = json::parse(buf);
auto msgHandler = ChatService::instance()->getHandle(js["msgid"].get<int>());
msgHandler(conn,js,time);
}
void ChatServer::onConnection(const TcpConnectionPtr &conn){
if(!conn->connected()){
conn->shutdown();
}
}
一切尽在不言中。
业务层代码
chatservice.hpp,位于include/server下
#ifndef CHATSERVICE_H_
#define CHATSERVICE_H_
#include<muduo/net/TcpConnection.h>
#include<unordered_map>
#include<functional>
#include "json.hpp"
using json = nlohmann::json;
using namespace std;
using namespace muduo;
using namespace muduo::net;
using MsgHandler = std::function<void(const TcpConnectionPtr &conn,json &js,Timestamp time)>;
class ChatService{
public:
static ChatService* instance();
void login(const TcpConnectionPtr &conn,json &js,Timestamp time);
void reg(const TcpConnectionPtr &conn,json &js,Timestamp time);
MsgHandler getHandle(int msgid);
private:
ChatService();
unordered_map<int,MsgHandler> _msgHanderMap;
};
#endif
public.hpp,一个客户端和服务端公用的头,那就放在include下吧,存储各种状态
#ifndef PUBLIC_H
#define PUBLIC_H
enum EnMsgType{
LOGIN_TYPE = 1,
REG_TYPE
};
#endif
chatserver.cpp,置于src/server之下:
#include"chatservice.hpp"
#include"public.hpp"
#include<string>
#include<muduo/base/Logging.h>
using namespace std;
using namespace muduo;
ChatService* ChatService::instance(){
static ChatService service;
return &service;
}
ChatService::ChatService(){
_msgHanderMap.insert({LOGIN_TYPE,std::bind(&ChatService::login,this,_1,_2,_3)});
_msgHanderMap.insert({REG_TYPE,std::bind(&ChatService::reg,this,_1,_2,_3)});
}
MsgHandler ChatService::getHandle(int msgid){
auto it = _msgHanderMap.find(msgid);
if(it == _msgHanderMap.end()){
return [=](const TcpConnectionPtr &conn,json &js,Timestamp time){
LOG_ERROR<<"msgid:"<<msgid<<"can not find handle!";
};
}
else{
return _msgHanderMap[msgid];
}
}
void ChatService::login(const TcpConnectionPtr &conn,json &js,Timestamp time){
LOG_INFO<<"login";
}
void ChatService::reg(const TcpConnectionPtr &conn,json &js,Timestamp time){
LOG_INFO<<"regist";
}
主函数
main.cpp,放在src/server之下:
#include<iostream>
#include "chatserver.hpp"
using namespace std;
int main(){
EventLoop loop;
InetAddress addr("127.0.0.lsof -i :8083 1234561",7000);
ChatServer server(&loop,addr,"ChatServer");
server.start();
loop.loop();
return 0;
}
CMake
这里记得将 json.hpp 放置在 thirdparty之下。
最外层的 CMake文件进行一波修改,然后建议写一个运行的 shell 脚本
cmake_minimum_required(VERSION 3.0) # 最低版本
project(main) # 给这个工程一个名字,这不是可执行文件的名字,是工程的名字
# 设置编译选项,不知道最后能不能过
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -g)
# 设置可执行文件最后的输出目录
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
# 配置头文件的搜索路径
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/include/server)
include_directories(${PROJECT_SOURCE_DIR}/thirdparty/json_test)
# 加载子目录
add_subdirectory(src)
运行脚本
以前从没写过,抱着试一试的心态,就很简单,五分钟写一个。
命令:
su root
cd CHAT
touch mk.sh
chmod 757 mk.sh
vim mk.sh
i
mk.sh:
#!/bin/sh
rm -rf build/* bin/*
cd build
cmake ..
make
继续命令:
esc
:
wq
回车
./mk.sh
一猜就是这么用的。
|