trantor icon indicating copy to clipboard operation
trantor copied to clipboard

How To Exit TcpServer in trantor/trantor/test/KickoffTest.cc ?

Open kingpeter2015 opened this issue 1 year ago • 0 comments

In KickoffTest.cc, a Tcpserver Object is created. I wanted to exit this server when there is no client connection. The following is my code:

int main()
{
    /**** omit for same code ********/
    ....
    server.setConnectionCallback([&n](const TcpConnectionPtr &connPtr) {
        if (connPtr->connected())
        {
            ++n;
            if (n % 2 == 0)
            {
                connPtr->keepAlive();
            }
            LOG_DEBUG << "New connection";
        }
        else if (connPtr->disconnected())
        {
            LOG_DEBUG << "connection disconnected";
            /*** Begin: My Added Code****/
            --n;
            if(n == 0) 
            {
               loop.runInLoop([&server]() {
                 server.stop();
               });
            }
            /*** End: My Added Code****/
        }
    });
    server.setIoLoopNum(3);
    server.start();
    loop.loop();
}

My Added Code marked the codes that i added. And the server got stucked in line server.stop();.

How to solve it and how to exit a tcp server gracefully?

kingpeter2015 avatar Jul 15 '23 10:07 kingpeter2015