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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - barisdoga

Pages: [1]
1
I have investigated the issue in more depth;

It seems like calling disconnect() causes TcpSocket to go INVALID_SOCKET therefore wait() function return false due to winapi select() returning error(-1) because it still sees a socket is valid and returning error.

Calling TcpSocket::disconnect() first and then calling SocketSelector::remove() also doesn't remove it because the socket goes INVALID_SOCKET state resulting wait() return false.

The conculusion is to remove the socket from selector before disconnecting it. But then you can not receive the disconnection data from SocketSelector...

2
One more thing to note here is that, after TcpSocket::disconnect() is called, the wait(1000) starts to return without waiting the timeout value.

3
With a simple modification I have tested the same problem on client side disconnecting tcpSocket the result is same. Client SocketSelector can't detect the disconnect but servers SocketSelector can.

sf::TcpListener tcpListener;
sf::SocketSelector socketSelector;
sf::TcpSocket clientSocket;
std::mutex lock;
bool disconnectClient = false;

void Server()
{
        sf::Socket::Status tcpStatus = tcpListener.listen(LOGIN_SERVER_PORT);
        if (tcpStatus != sf::Socket::Done)
        {
                std::cout << "[Server] TcpListener couldn&#39;t be initialized!" << std::endl;
                return;
        }
        else
        {
                std::cout << "[Server] TcpListener initialized!" << std::endl;
        }

        sf::Socket::Status acceptStatus = tcpListener.accept(clientSocket);
        if (acceptStatus == sf::Socket::Done)
        {
                std::cout << "[Server] A new client is accepted!" << std::endl;
                socketSelector.add(clientSocket);

                std::cout << "[Server] Starting to listen with socket selector!" << std::endl;
                while (true)
                {
                        lock.lock();
                        if (socketSelector.wait(sf::milliseconds(1000)))
                        {
                                std::cout << "[Server] socketSelector.wait() returns true" << std::endl;

                                if (socketSelector.isReady(clientSocket))
                                {
                                        std::cout << "[Server] socketSelector.isReady(clientSocket) returns true" << std::endl;
                                }
                                else
                                {
                                        std::cout << "[Server] socketSelector.isReady(clientSocket) returns false" << std::endl;
                                }
                        }
                        else
                        {
                                std::cout << "[Server] socketSelector.wait() returns false" << std::endl;
                        }
                        lock.unlock();
                        Sleep(100);
                }
        }

}

void Client()
{
        sf::SocketSelector selector;
        sf::TcpSocket tcpSocket;
        sf::Socket::Status status = tcpSocket.connect(GSO_SERVER_IP, LOGIN_SERVER_PORT, sf::milliseconds(1000));
        if (status != sf::Socket::Done)
        {
                std::cout << "[Client] Connection to server failed!" << std::endl;
                return;
        }
        else
        {
                std::cout << "[Client] Connected to server!" << std::endl;
        }

        selector.add(tcpSocket);

        bool waiting = true;
        while (waiting)
        {
                lock.lock();
                if (selector.wait(sf::milliseconds(1000)))
                {
                        std::cout << "[Client] SocketSelector returns true!" << std::endl;
                        if (selector.isReady(tcpSocket))
                        {
                                std::cout << "[Client] SocketSelector tcpSocket is ready!" << std::endl;
                                waiting = false;
                        }
                        else
                        {
                                std::cout << "[Client] SocketSelector tcpSocket is not ready!" << std::endl;
                        }
                }
                else
                {
                        std::cout << "[Client] SocketSelector returns false!" << std::endl;
                }
                lock.unlock();
                Sleep(100);
                if (disconnectClient)
                {
                        std::cout << "[Client] Disconnecting tcp socket" << std::endl;
                        tcpSocket.disconnect();
                        disconnectClient = false;
                }
        }
}

int main()
{
        std::thread s(Server);
        Sleep(1000);
        std::thread c(Client);

        while (true)
        {
                int i;
                std::cin >> i;
                if(i == 0)
                {
                        lock.lock();
                        std::cout << "[MainThread] Lock is locked. Sending disconnect command!" << std::endl;
                        disconnectClient = true;
                        lock.unlock();
                }
        }

        return 0;
}

[Server] TcpListener initialized!
[Client] Connected to server!
[Server] A new client is accepted!
[Server] Starting to listen with socket selector!
[Client] SocketSelector returns false!
[Server] socketSelector.wait() returns false
[Client] SocketSelector returns false!
[Server] socketSelector.wait() returns false
[Client] SocketSelector returns false!
[Server] socketSelector.wait() returns false
[Client] SocketSelector returns false!
[Server] socketSelector.wait() returns false
0
[Client] SocketSelector returns false!
[Server] socketSelector.wait() returns false
[MainThread] Lock is locked. Sending disconnect command!
[Client] SocketSelector returns false!
[Client] Disconnecting tcp socket
[Server] socketSelector.wait() returns true
[Server] socketSelector.isReady(clientSocket) returns true
[Client] SocketSelector returns false!
[Client] SocketSelector returns false!
[Server] socketSelector.wait() returns true
[Server] socketSelector.isReady(clientSocket) returns true
[Client] SocketSelector returns false!
[Server] socketSelector.wait() returns true
[Server] socketSelector.isReady(clientSocket) returns true

4
Hello, I have written a simple code to show the situation and also attaching console output. On the server side I just call TcpSocket::disconnect() for a client socket but SocketSelector wait always returns false but the client SocketSelector can detect the disconnection.

I am not sure this is an expected behaviour.

Thanks in advance!

Using SFML 2.6.0 official release on Windows 10 Machine

sf::TcpListener tcpListener;
sf::SocketSelector socketSelector;
sf::TcpSocket clientSocket;
std::mutex lock;
bool disconnectClientSocket = false;

void Server()
{

        sf::Socket::Status tcpStatus = tcpListener.listen(LOGIN_SERVER_PORT);
        if (tcpStatus != sf::Socket::Done)
        {
                std::cout << "[Server] TcpListener couldn&#39;t be initialized!" << std::endl;
                return;
        }
        else
        {
                std::cout << "[Server] TcpListener initialized!" << std::endl;
        }

        sf::Socket::Status acceptStatus = tcpListener.accept(clientSocket);
        if (acceptStatus == sf::Socket::Done)
        {
                std::cout << "[Server] A new client is accepted!" << std::endl;
                socketSelector.add(clientSocket);

                std::cout << "[Server] Starting to listen with socket selector!" << std::endl;
                while (true)
                {
                        lock.lock();
                        if (socketSelector.wait(sf::milliseconds(1000)))
                        {
                                std::cout << "[Server] socketSelector.wait() returns true" << std::endl;

                                if (socketSelector.isReady(clientSocket))
                                {
                                        std::cout << "[Server] socketSelector.isReady(clientSocket) returns true" << std::endl;
                                }
                                else
                                {
                                        std::cout << "[Server] socketSelector.isReady(clientSocket) returns false" << std::endl;
                                }
                        }
                        else
                        {
                                std::cout << "[Server] socketSelector.wait() returns false" << std::endl;
                        }
                        lock.unlock();

                        Sleep(100);

                        if (disconnectClientSocket)
                        {
                                std::cout << "[Server] Disconnecting client socket" << std::endl;
                                clientSocket.disconnect();
                                disconnectClientSocket = false;
                        }
                }
        }
       
}

void Client()
{
        sf::SocketSelector selector;
        sf::TcpSocket tcpSocket;
        sf::Socket::Status status = tcpSocket.connect(GSO_SERVER_IP, LOGIN_SERVER_PORT, sf::milliseconds(1000));
        if (status != sf::Socket::Done)
        {
                std::cout << "[Client] Connection to server failed!" << std::endl;
                return;
        }
        else
        {
                std::cout << "[Client] Connected to server!" << std::endl;
        }

        selector.add(tcpSocket);
       
        bool waiting = true;
        while (waiting)
        {
                if (selector.wait())
                {
                        std::cout << "[Client] SocketSelector returns true!" << std::endl;
                        if (selector.isReady(tcpSocket))
                        {
                                std::cout << "[Client] SocketSelector tcpSocket is ready!" << std::endl;
                                waiting = false;
                        }
                        else
                        {
                                std::cout << "[Client] SocketSelector tcpSocket is not ready!" << std::endl;
                        }
                }
                else
                {
                        std::cout << "[Client] SocketSelector returns false!" << std::endl;
                }
                Sleep(100);    
        }
}


int main()
{
        std::thread s(Server);
        Sleep(1000);
        std::thread c(Client);

        while (true)
        {
                int i;
                std::cin >> i;
                if(i == 0)
                {
                        lock.lock();
                        std::cout << "[MainThread] Lock is locked. Sending disconnect command!" << std::endl;
                        disconnectClientSocket = true;
                        lock.unlock();
                }
        }

        return 0;
}


[Server] TcpListener initialized!
[Server] A new client is accepted!
[Server] Starting to listen with socket selector!
[Client] Connected to server!
[Server] socketSelector.wait() returns false
[Server] socketSelector.wait() returns false
[Server] socketSelector.wait() returns false
[Server] socketSelector.wait() returns false
0
[Server] socketSelector.wait() returns false
[MainThread] Lock is locked. Sending disconnect command!
[Server] Disconnecting client socket
[Server] socketSelector.wait() returns false
[Client] SocketSelector returns true!
[Client] SocketSelector tcpSocket is ready!
[Server] socketSelector.wait() returns false
[Server] socketSelector.wait() returns false
[Server] socketSelector.wait() returns false
[Server] socketSelector.wait() returns false
[Server] socketSelector.wait() returns false
[Server] socketSelector.wait() returns false
[Server] socketSelector.wait() returns false

Pages: [1]
anything