当前位置:网站首页>Muduo simple usage

Muduo simple usage

2022-06-23 01:39:00 For financial freedom!

#include <muduo/net/TcpServer.h>
#include <muduo/net/EventLoop.h>
#include <iostream>
#include <functional>
#include <string>
using namespace std;
using namespace muduo;
using namespace muduo::net;
using namespace placeholders;

/* 1. Combine TcpServer object  2. establish EventLoop Pointer to the time loop object  3. clear TcpServer What parameters do constructors need , Output ChatServer Constructor for  4. In the constructor of the current server class , Register the callback function to handle the connection and the callback function to handle the read-write event  5. Set the appropriate number of server threads ,muduo The library will divide itself i/o Threads and worker Threads  */


class ChatServer
{
    
public:
    ChatServer(EventLoop* loop,// The event loop 
                const InetAddress& listenAddr,//IP+port
                const string& nameArg)// Name of the server 
        :_server(loop,listenAddr,nameArg),_loop(loop)
    {
    
        // Register the server   User connection creation and   To break off   Callback 
        _server.setConnectionCallback(std::bind(&ChatServer::onConnection,this,_1));
        // Register user read / write event callback for the server 
        _server.setMessageCallback(std::bind(&ChatServer::onMessage,this,_1,_2,_3));
        // Set the number of threads on the server side  1 individual i/o Threads  3 individual worker Threads 
        _server.setThreadNum(4);
    }
    // Turn on the event loop 
    void start()
    {
    
        _server.start();
    }
private:
    // Dedicated to dealing with user connection creation and disconnection epoll listenfd accept
    void onConnection(const TcpConnectionPtr& conn)
    {
     
        if(conn->connected())
        {
    
            cout << conn->peerAddress().toIpPort() << "->" 
                << conn->localAddress().toIpPort()
                << " state:online" << endl;
        }
        else{
    
            cout << conn->peerAddress().toIpPort() << "->" 
                << conn->localAddress().toIpPort()
                << " state:offline" << endl;
            conn->shutdown();//close(fd);
            //_loop->quit();
        }
    }
    // Specifically handle user read and write events 
    void onMessage(const TcpConnectionPtr& conn,// Connect 
                    Buffer* buffer,// buffer 
                    Timestamp time)// Time information of receiving data 
    {
    
        string buf = buffer->retrieveAllAsString();
        cout << "recv data:" << buf << "time:" << time.toString() << endl;
        conn->send(buf);
    }
    TcpServer _server;
    EventLoop* _loop;
};


int main()
{
    
    EventLoop loop;//epoll
    InetAddress addr("127.0.0.1",6000);
    ChatServer server(&loop,addr,"CharServer");
    server.start();//listenfd  adopt epoll_ctl Add to epoll object  
    loop.loop();//epoll_wait Waiting for new users to connect in a blocked way , Read / write events of connected users, etc 

    return 0;
}

 Insert picture description here
Enter to execute :
 Insert picture description here
 Insert picture description here
 Insert picture description here

原网站

版权声明
本文为[For financial freedom!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220514414621.html