2024-12-23 20:55:55 +08:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
muduo网络库给用户提供了两个主要的类
|
|
|
|
|
TcpServer :用于编写服务器程序的
|
|
|
|
|
TcpClient :用于编写客户端程序的
|
|
|
|
|
|
|
|
|
|
epoll+线程池
|
|
|
|
|
好处 :能够把网络I/O的代码和业务代码区分开
|
|
|
|
|
用户的连接和断开 用户的可读写事件
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <muduo/net/TcpServer.h>
|
|
|
|
|
#include <muduo/net/EventLoop.h>
|
|
|
|
|
#include <iostream>
|
2024-12-25 20:01:55 +08:00
|
|
|
|
#include <functional>
|
2024-12-23 20:55:55 +08:00
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace muduo;
|
|
|
|
|
using namespace muduo::net;
|
2024-12-25 20:01:55 +08:00
|
|
|
|
using namespace placeholders;
|
2024-12-23 20:55:55 +08:00
|
|
|
|
|
|
|
|
|
class ChatServer{
|
|
|
|
|
public:
|
|
|
|
|
ChatServer(EventLoop* loop,
|
|
|
|
|
const InetAddress& listenAddr,
|
|
|
|
|
const string& nameArg)
|
|
|
|
|
:_server(loop,listenAddr,nameArg),_loop(loop)
|
|
|
|
|
{
|
2024-12-25 20:01:55 +08:00
|
|
|
|
//给服务器注册用户连接的创建和断开回调
|
|
|
|
|
_server.setConnectionCallback(std::bind(&ChatServer::onConnection, this, _1));
|
|
|
|
|
//给服务器注册用户读写事件的回调
|
|
|
|
|
//_server.setMessageCallback();
|
2024-12-23 20:55:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2024-12-25 20:01:55 +08:00
|
|
|
|
void onConnection(const TcpConnectionPtr&)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Message(const MessageCallback& cb)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-23 20:55:55 +08:00
|
|
|
|
TcpServer _server;
|
|
|
|
|
EventLoop* _loop;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|