Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: proper server exit cleanup  (Read 3133 times)

0 Members and 1 Guest are viewing this topic.

baccari

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
proper server exit cleanup
« on: February 22, 2013, 01:33:08 pm »
What I want to do is for the client to detect server disconnection and reconnect automatically when it starts listening again.
To test that, I start the client, I start the server then I stop it and try to start it again and see if the client reconnects.
The problem is that the server cannot rebind to the same port again.
What should I do ?

Client:
void SDP::RunNet()
{
    sf::TcpSocket CMDSocket;
    CMDSocket.setBlocking(false);

    sf::Time CMDSocketTimeout = sf::seconds(0.05f);

    std::cout<<"Connecting to CMD... "<<std::flush;

    mutexRunning.lock();
    bool stillRunning = Running;
    mutexRunning.unlock();
    while(stillRunning)
    {
        if(!connectedToCMD)
        {
            if(CMDSocket.connect("localhost", 55001,CMDSocketTimeout)==sf::Socket::Done)
            {
                connectedToCMD=true;
                std::cout<<"Connected to CMD."<<std::endl;
            }
            sf::sleep(CMDSocketTimeout);
        }
        else
        {
            sf::Packet packet;
            sf::Socket::Status CMDSocketStatus = CMDSocket.receive(packet);
            if(CMDSocketStatus==sf::Socket::Disconnected)
            {
                connectedToCMD=false;
                CMDSocket.disconnect();
                std::cout<<"CMD disconnected."<<std::endl<<"Trying to reconnect... "<<std::flush;
            }
        }
        mutexRunning.lock();
        stillRunning = Running;
        mutexRunning.unlock();
    }

    if(connectedToCMD)
    {
        CMDSocket.disconnect();
        std::cout<<"Disonnected from CMD."<<std::endl;
    }
 

Server:
void CMD::RunNet()
{
    sf::TcpListener listener;
    listener.setBlocking(false);
    listener.listen(55001);
    sf::SocketSelector selector;
    selector.add(listener);

    std::list<sf::TcpSocket*> clients;

    mutexRunning.lock();
    bool stillRunning = Running;
    mutexRunning.unlock();
    while (stillRunning)
    {
        if (selector.wait(sf::seconds(0.015f)))
        {
            if (selector.isReady(listener))
            {
                sf::TcpSocket* client = new sf::TcpSocket;
                if (listener.accept(*client) == sf::Socket::Done)
                {
                    std::cout<<"client accepted: "<<client->getRemoteAddress()<<std::endl;
                    std::cout<<"list size: "<<clients.size();
                    clients.push_back(client);
                    std::cout<<" then: "<<clients.size()<<std::endl;
                    selector.add(*client);
                }
                else
                {
                    delete client;
                }
            }
            else
            {
                for (std::list<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); ++it)
                {
                    sf::TcpSocket& client = **it;
                    if (selector.isReady(client))
                    {
                        sf::Packet packet;
                        sf::Socket::Status clientStatus = client.receive(packet);
                        if (clientStatus == sf::Socket::Done)
                        {
                            std::cout<<"Done"<<std::endl;
                        }
                        if (clientStatus == sf::Socket::Disconnected)
                        {
                            std::cout<<"Client disconnected"<<std::endl;
                            std::cout<<"list size: "<<clients.size();
                            selector.remove(client);
                            client.disconnect();
                            delete(&client);
                            clients.erase(it);
                            it--;
                            std::cout<<" then: "<<clients.size()<<std::endl;
                        }
                    }
                }
            }
        }
        mutexRunning.lock();
        stillRunning = Running;
        mutexRunning.unlock();
    }

    std::cout<<"Disconnecting from all clients."<<std::endl;
    for (std::list<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); ++it)
    {
        sf::TcpSocket& client = **it;
        std::cout<<"list size: "<<clients.size();
        selector.remove(client);
        client.disconnect();
        delete(&client);
        clients.erase(it);
        it--;
        std::cout<<" then: "<<clients.size()<<std::endl;
    }
    clients.clear();
    selector.remove(listener);
    listener.close();
}
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: proper server exit cleanup
« Reply #1 on: February 22, 2013, 02:37:13 pm »
Quote
What should I do ?
Nothing.

https://github.com/SFML/SFML/issues/150
Laurent Gomila - SFML developer

baccari

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
Re: proper server exit cleanup
« Reply #2 on: February 22, 2013, 05:04:11 pm »
Are you going to use shutdown() sometime soon ?  :(

It seems this problem does not occur with TcpSocket because I don't get the same error when I close and relaunch the client. Is this true ?
« Last Edit: February 22, 2013, 05:13:43 pm by wbaccari »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: proper server exit cleanup
« Reply #3 on: February 22, 2013, 05:51:04 pm »
Quote
Are you going to use shutdown() sometime soon ?
Probably.

Quote
It seems this problem does not occur with TcpSocket because I don't get the same error when I close and relaunch the client. Is this true ?
No, both types of sockets should behave the same way.
Laurent Gomila - SFML developer

baccari

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
Re: proper server exit cleanup
« Reply #4 on: February 22, 2013, 06:00:40 pm »
I don't get the same problem when I close the client and restart it while the server is still running.
I discovered that the call for disconnect() in the server cleanup is not working and this is causing the problem.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: proper server exit cleanup
« Reply #5 on: February 22, 2013, 06:11:41 pm »
Quote
I discovered that the call for disconnect() in the server cleanup is not working
What does "is not working" mean?
Laurent Gomila - SFML developer

baccari

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
Re: proper server exit cleanup
« Reply #6 on: February 22, 2013, 06:16:17 pm »
When I call disconnect from the server I should immediately get a message on the client output. This is not happening. I only get that after the listener.close() call.