mas-bandwidth / yojimbo

A network library for client/server games written in C++
BSD 3-Clause "New" or "Revised" License
2.48k stars 244 forks source link

Not able to connect from my client app to any yojimbo server #106

Closed fosheus closed 6 years ago

fosheus commented 6 years ago

Hello there,

Firstly, thank you to Glenn for his incredible work on yojimbo library and the differents articles for gafferongames.

Secondly, I'm trying to work with yojimbo for 2 days and I can't make it work in my SFML project. I've a simple game that permit clients to connect to a host client and every body see each other on the screen. I

My application is composed of a server and a client. If the player wants to host the game then a server is instantiated in addition to the client.

The problem I have is when I run my code as host or client, nothing happens. Both server and client seam to initialize correctly and I have no error message nor timeout from the client and no connection detected. I've tried to run the server and client separatly using the test client and server provided in the yojimbo solution. It appears that the server is able to accept the yojimbo client but the yojimbo server does not accept the client of my application. I deduced the problem was coming from my client. I tried running my client with minimal code but nothing happens. He still tries to connect indefinitely without timeout.

I really don't understand what I did wrong so if anyone could help me on that I would be very glade.

Here you can find my minimal code of the client : My shared.h really looks like the one in USAGE file, so just add 1 or 2 messages types and bam, you have my shared.h

GameState.h

#include <SFML\Graphics.hpp>
#include <SFML/Network/IpAddress.hpp>
#include <yojimbo.h>

#include "shared.h"
#include "Game.h"
#include "State.h"
#include "DEFINITIONS.h"
#include "GameServer.h"
#include "MainMenuState.h"
#include <string>
#include <iostream>
#include <string>

using namespace yojimbo;

class GameState : public State
{

public:
    GameState(GameDataRef data,sf::String address,sf::String pseudo,bool local);
    ~GameState();

    void Init();
    void HandleInput();
    void Update(float dt);
    void Draw(float dt);

private :
    void processMessages();
    void processMessage(yojimbo::Message* message);
    void processLevelStateMessage(LevelStateMessage* message);

    void quit();

private :
    enum State {
        DISCONNECTED,
        CONNECTED,
    };

private:
    GameDataRef _data;
    GameConnectionConfig connectionConfig;
    GameAdapter adapter;
    yojimbo::Client client;
    yojimbo::Address endpoint;
    uint64_t clientId;
    bool hasFocus;
    float sendClock;
};

GameState.cpp

#include "GameState.h"

using namespace yojimbo;

GameState::GameState(GameDataRef data,sf::String address,sf::String pseudo,bool local) :_data(data),
    client(yojimbo::GetDefaultAllocator(),yojimbo::Address("0.0.0.0"),connectionConfig,adapter,0.0f)
{

    this->endpoint = Address("127.0.0.1", ServerPort);

    /*
    hasFocus = true;
    this->pseudo = pseudo;
    identified = false;
    sendTimeout = 0;
    pingDt = 0;
    identifier = 0;
    currentState = State::DISCONNECTED;*/
    yojimbo::random_bytes((uint8_t*)&clientId, 8);

}

GameState::~GameState()
{
    quit();
}

void GameState::Init()
{
    client.InsecureConnect(DEFAULT_PRIVATE_KEY, clientId, endpoint);

    char addressString[256];
    client.GetAddress().ToString(addressString, sizeof(addressString));
    printf("client address is %s\n", addressString);
}

void GameState::HandleInput()
{

    sf::Event event;

    while (this->_data->window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed) {
            quit();
            this->_data->machine.AddState(StateRef(new MainMenuState(this->_data)), true);
        }
        if (event.type == sf::Event::GainedFocus) {
            hasFocus = true;
        }
        if (event.type == sf::Event::LostFocus) {
            hasFocus = false;
        }

    }
}

void GameState::Update(float dt)
{
    sendClock += dt;

    if (sendClock >= (4.0f / 60.0f)) {
        client.SendPackets();
        sendClock = 0;
    }

    client.ReceivePackets();

    client.AdvanceTime(dt);

    if (client.ConnectionFailed() || client.IsDisconnected()) {
        quit();
        this->_data->machine.AddState(StateRef(new MainMenuState(this->_data)), true);
    }

    if (client.IsConnecting()) {
        std::cout << "client connecting... "+std::to_string(sendClock) << std::endl;
    }   
}

void GameState::Draw(float dt)
{
    //CLEAR AND SET WINDOW//

    this->_data->window.clear(sf::Color(0x33,0x66,0xff));

    this->_data->window.display();
}

void GameState::processMessages()
{
    for (int i = 0; i < connectionConfig.numChannels; i++) {
        Message* message = client.ReceiveMessage(i);
        while (message != NULL) {
            processMessage(message);
            client.ReleaseMessage(message);
            message = client.ReceiveMessage(i);
        }
    }
}

void GameState::processMessage(yojimbo::Message * message)
{
    switch (message->GetType())
    {
    case (int)GameMessageType::LEVEL_STATE_MESSAGE: 
        processLevelStateMessage((LevelStateMessage*)message);
        break;
    case (int)GameMessageType::PLAYER_WON_MESSAGE:
        break;
    default:
        break;
    }
}

void GameState::processLevelStateMessage(LevelStateMessage * message)
{
    std::cout << "LevelStateMessage received" << std::endl;
}

void GameState::quit()
{
    client.Disconnect();
    if (server != nullptr) {
        server->stopServer();
        delete server;
        server = nullptr;
    }
}
gafferongames commented 6 years ago

Try using a loop back server. This is what it’s designed for. See loopback.cpp for an example

fosheus commented 6 years ago

I found why the client wasn't connecting to the server. Actually when I entered in my Update method for each server and client, I gave the delta time since last frame to the AdvanceTime instead of the total time passed since the start of the application. I miss understood how to use this method. I found it while implemening the loopback server 😅. Thank you Glenn for your quick response.

gafferongames commented 6 years ago

No worries! Glad you found it!