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 - Clyx

Pages: [1]
1
Thanks for your answer ! That works ! I have just a little problem how to properly disconnect a client from the server ?

When I have to remove a client I do this :

//NB : ClientConnection class is just a class that holds an id, the socket and some methods.
ClientConnection *client = m_activesConnections.at(id);
//m_activesConnections is a map<unsigned short, ClientConnection*>
delete client;
 

I have done this in the destructor of ClientConnection:

ClientConnection::~ClientConnection() {
    std::cout << "Destroyed ClientConnection " << m_id <<  std::endl;
    m_socket->disconnect();
}
 

But when I remove a client, and the I try to reconnect a new client it doesn' work it doesn't detect new client..

Here is a part of my server (inspired from the doc):

void Server::start() {

    if(m_port == 0){
        std::cerr << "Error the server must have a port !" << std::endl;
        return;
    }

    if(m_listener.listen(m_port) != sf::Socket::Done){
        std::cerr << "Error while starting server, port is already in use" << std::endl;
        return;
    }

    m_selector.add(m_listener);

    m_active = true;
    std::cout << "Server started on port " << m_port << " waiting for new client..." << std::endl;

    while(m_active){

        if(m_selector.wait()){
            if(m_selector.isReady(m_listener)){
                sf::TcpSocket *client = new sf::TcpSocket;
                if(m_listener.accept(*client) == sf::Socket::Done){
                    ClientConnection *clientConnection = new ClientConnection(*client);
                    clientConnection->setActive(true);
                    addClient(clientConnection);
                    m_selector.add(*clientConnection->getSocket());
                    std::cout << "new client detected, waiting for handshaking.." << std::endl;
                }else{
                    delete client;
                }
            }else{
                for(std::map<unsigned short, ClientConnection*>::iterator it = m_activesConnections.begin(); it != m_activesConnections.end(); it++) {
                    if(&it == nullptr){
                        std::cerr << "Error" << std::endl;
                        continue;
                    }
                    if(it->second != nullptr) {
                        sf::TcpSocket *client = it->second->getSocket();
                        if (m_selector.isReady(*client)) {
                            it->second->listenPackets();
                        }
                    }
                }
            }
        }
    }
}
 

What am I doing wrong ?

Thanks you!

Clyx

2
Thanks for your answer,

Yeah I forgot to comment the line which call the function synchronously, what do you mean by checking the value of the receive() ? Could you show me an example on how to handle packet correctly ?

I'm troubled, tou say that starting a thread for each client is questionnable, but how to do it otherwise ? I thought it was a must to keep the connexion with the client alive. Could you explain ?

The ClientConnection class is not really important it has just some getter for now.

Thanks.

Clyx

3
Network / Help about creating server which handles many client connections
« on: December 08, 2016, 07:12:42 pm »
Hi, I'm working on a game working in multiplayer mode, but I'm facing some problems trying to set up a working server which can accepts more than one connection.

How the server should work to my mind

The server starts, the he wait for new client connection, So I have a loop that is active until the server stop.
For each new connection I start a new thread which willl handle comunication with the client, each client are stored in a map width an id and an object representing the connection. So in the thread method I have a loop which runs while the connection is still alive, in this method I handle pakets.

I have to use packets to tranfers data because the project it is more easier. I use TCP packet.

So I would like to know how this kind of server should be done ? How handle connection/disconnection, receiving packets ...

My problem
My server receive the packet but use almost 20% of the processor, I receive 0 in the packet but it shouldn't because I send 42.

This is what I've done (which doesn't work):

Client
bool Client::connect() {
    bool success = m_socket.connect(m_host, m_port) == sf::Socket::Done;

    if(success){
        std::cout << "Connected !" << std::endl;
        m_active = true;
        std::thread thread(sendData, this);
        thread.detach();
    }else{
        std::cerr << "Unable to connect to server" << std::endl;
    }

    return success;
}

//Method which send data
void Client::sendData() {
    sf::Packet packet;
    int i = 42;
    packet << i;
    m_socket.send(packet);
}
 

Server

void Server::start() {

    if(m_port == 0){
        std::cerr << "Error the server must have a port !" << std::endl;
        return;
    }

    if(m_listener.listen(m_port) != sf::Socket::Done){
        std::cerr << "Error while starting server, port is already in use" << std::endl;
        return;
    }

    m_active = true;
    std::cout << "Server started on port " << m_port << " waiting for new client..." << std::endl;

    sf::TcpSocket clientSocket;

    while(m_active){

        if(m_listener.accept(clientSocket) != sf::Socket::Done){
            std::cerr << "Error while receiving new connection" << std::endl;
        }

        unsigned short id = generateClientId();
        ClientConnection *clientConnection = new ClientConnection(0, clientSocket);
        clientConnection->setActive(true);
        addClient(id, clientConnection);
        clientConnection->listenPackets();
        std::cout << "New client connected, starting thread !" << std::endl;
        std::thread thread(&ClientConnection::listenPackets, clientConnection);
        thread.detach();
    }

    std::cout << "Server stopped" << std::endl;

}

//Method which receive packets
void ClientConnection::listenPackets() {
    sf::Packet packet;
    int a;
    while(m_active){
        m_socket->receive(packet);
        while(!packet.endOfPacket()){
            packet >> a;
            std::cout << "Recu " << a << std::endl;
        }
    }
}
 

So here are most interesting parts, any help is welcomed.

Thanks :)

Clyx.

4
General / Re: Hexagonal grid with vertexarray
« on: October 10, 2016, 04:12:56 pm »
Thanks for your answer,

but I don't understand how can I tell that a new fan starts? I'm totally confused about that..


5
General / Re: Hexagonal grid with vertexarray
« on: October 10, 2016, 04:02:20 pm »
Yeah.. I tried again and again.. But I'm not able to figure out :/

My code :

(click to show/hide)

I don't know why it doesn't work, it seems that the second hex starts from the origin of the previous.

This is what I get :



Any idea?

Thanks you

6
General / Re: Hexagonal grid with vertexarray
« on: October 07, 2016, 11:41:35 am »
Thanks for your answer !

What I want to do is basically setup my vertices variable which is a VertexArray and fill it with the good hexagon's corner and then simply draw it with the RenderWindow.

So I'd like to know what do I have to put for each hex hex[0] hex[1] ...
I'm using the tutorial http://www.sfml-dev.org/tutorials/2.0/graphics-vertex-array.php

Also I don't know which drawing mode I have to user, do I use Triangles, TrianglesFan , TrianlesStrip or something else?

I'm sorry for my english , I do my best to be understandable.

Thanks.

7
General / Hexagonal grid with vertexarray
« on: October 06, 2016, 09:15:34 pm »
Hi,

I'm stucked for about 1 week trying to make an algorithm which will allow me to render an hexagonal tilemap.

I would like to know how I can implement this system through SFML in an optimzed way.

For now I have a class which extends Drawable and that's all...

Here is what I've done :

(click to show/hide)

PS: I woud like to have pointy hexagon.

Any help would be appreciated.

Thanks in advance.

Pages: [1]
anything