当前位置:网站首页>集群聊天服务器:网络模块ChatServer
集群聊天服务器:网络模块ChatServer
2022-07-23 21:17:00 【_索伦】
chatServer.hpp
#ifndef CHATSERVER_H
#define CHATSERVER_H
#include <muduo/net/TcpServer.h>
#include <muduo/net/EventLoop.h>
#include <string>
using namespace muduo;
using namespace muduo::net;
// 聊天服务器的主类
class ChatServer
{
public:
// 初始化聊天服务器对象
ChatServer(EventLoop* loop,
const InetAddress& listenAddr,
const string& nameArg);
// 启动服务
void start();
private:
// 上报链接相关信息的回调函数
void onConnection(const TcpConnectionPtr&);
// 上报读写事件相关信息的回调函数
void onMessage(const TcpConnectionPtr&,
Buffer*,
Timestamp);
TcpServer _server; // 组合的muduo库,实现服务器功能的类对象
EventLoop* _loop; // 指向事件循环对象的指针
};
#endif
chatServer.cpp
#include "chatServer.hpp"
#include "chatService.hpp"
#include "json.hpp"
#include <functional>
using namespace std;
using namespace placeholders;
using json = nlohmann::json;
// 初始化聊天服务器对象
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(4);
}
// 启动服务
void ChatServer::start()
{
_server.start();
}
// 上报链接相关信息的回调函数
void ChatServer::onConnection(const TcpConnectionPtr& conn)
{
// 客户端断开链接
if (!conn->connected())
{
// 处理异常断开?
ChatService::instance()->clientCloseException(conn);
conn->shutdown();
}
}
// 上报读写事件相关信息的回调函数
void ChatServer::onMessage(const TcpConnectionPtr& conn,
Buffer* buffer,
Timestamp time)
{
string buf = buffer->retrieveAllAsString();
// 数据的反序列化
json js = json::parse(buf);
// 达到的目的:完全解耦网络模块的代码和业务模块的代码
// 通过js["msgid"](一个ID绑定一个模块)获取-》业务handler-》 conn js time
auto msgHandler = ChatService::instance()->getHandler(js["msgid"].get<int>());
// 回调消息绑定好的事件处理器,来执行相应的业务处理
msgHandler(conn, js, time);
}
边栏推荐
- Green-Tao 定理 (3): 反一致函数及其生成的 Sigma-代数
- Basic syntax of MySQL DDL and DML and DQL
- High numbers | calculation of double integral 4 | high numbers | handwritten notes
- Unity solves that animation is not available: the animationclip 'xxx' used by the animation component 'xxx' must be marked as legacy
- 最小生成树:Kruskal
- 手机股票开户安全吗?
- 1061 Dating
- Stm32c8t6 driving lidar actual combat (II)
- Failed to introspect Class FeignClientFactoryBean 异常排查
- 高数下|二重积分的计算4|高数叔|手写笔记
猜你喜欢

The common interfaces of Alipay are uniformly encapsulated and can be used directly for payment parameters (applicable to H5, PC, APP)

One of QT desktop whiteboard tools (to solve the problem of unsmooth curve -- Bezier curve)

Protocol buffers 的问题和滥用

Edge cloud | 1. overview

2022-7-23 12点 程序爱生活 小时线顶背离出现,保持下跌趋势,等待反弹信号出现。

flink原理及开发总结(详细)

STM32C8t6 驱动激光雷达实战(二)

LeetCode热题 HOT52-100

Jetson nano recording stepping on the pit (it will definitely solve your problem)

TypeScript基础
随机推荐
Problems and abuse of protocol buffers
Pay more attention to which securities company has the lowest commission? Is it safe to open an account online?
LU_ Asr01 voice module usage
Chapter 2 回归
【创建 Birthday Card 应用】
Protocol buffers 的问题和滥用
Scala Programming (Junior)
221. 最大正方形 ●● & 1277. 统计全为 1 的正方形子矩阵 ●●
支付宝常用接口统一封装,可直接支付参数使用(适用于H5、PC、APP)
Green-Tao 定理的证明 (2): Von Neumann 定理的推广
Trial record of ModelBox end cloud collaborative AI development kit (rk3568) (II)
网络学习型红外模块,8路发射独立控制
"Pulse" to the future! Huawei cloud Mrs helps smooth migration to the cloud
模块化开发
Minimum spanning tree: Kruskal
TCP half connection queue and full connection queue (the most complete in History)
Opencv image processing Laplace pyramid
【愚公系列】2022年06月 .NET架构班 084-微服务专题 Abp vNext微服务通信
最小生成树:Kruskal
OpenCV图像处理——拉普拉斯金字塔