diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..1507357 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "functional": "cpp" + } +} \ No newline at end of file diff --git a/testmuduo/muduo_server.cpp b/testmuduo/muduo_server.cpp index fb25fcd..d8428c9 100644 --- a/testmuduo/muduo_server.cpp +++ b/testmuduo/muduo_server.cpp @@ -18,6 +18,15 @@ using namespace muduo; using namespace muduo::net; using namespace placeholders; +/* +基于muduo网络库开发服务器程序 +1.组合TcpServer对象 +2.创建事件循环对象的指针 +3.明确TcpServer构造函数需要什么参数,输出ChatServer的构造函数 +4.在当前服务器类的构造函数当中,注册处理连接的回调函数和处理读写事件的回调函数 +5.设置合适的服务端线程数量,muduo库会自己分配I/O线程和worker线程 +*/ + class ChatServer{ public: ChatServer(EventLoop* loop, @@ -27,17 +36,26 @@ public: { //给服务器注册用户连接的创建和断开回调 _server.setConnectionCallback(std::bind(&ChatServer::onConnection, this, _1)); + //给服务器注册用户读写事件的回调 - //_server.setMessageCallback(); + _server.setMessageCallback(std::bind(&ChatServer::onMessage, this, _1, _2, _3)); + + //设置服务器端的线程数量 1个I/O线程 3个worker线程 + _server.setThreadNum(4); } + void start(){ + _server.start(); + } private: void onConnection(const TcpConnectionPtr&) { } - void Message(const MessageCallback& cb) + void onMessage(const TcpConnectionPtr& conn, + Buffer* buffer, + Timestamp time) { }