Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kingpeter2015 opened this issue Jul 15, 2023 · 0 comments
Open

Comments

@kingpeter2015
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant