Exemple #1
0
void FSClient::Receive(Networking::Message& m, s32 id)
{
  if(id == serverhConn)
  {
    HandleServerMessage(m, id);
  }
  else
  {
    HandleServerMessage(m, id);
  }
}
void App::KeepAliveThread()
{
    int singleSleepTime = 10; //[s]
    int sleepsBeforeSend = 6; // so keep alive interval == 1 min
    while (true)
    {
        Sleep(singleSleepTime);
        ++mKeepAliveCounter;
        ZLOGD() << "Keep alive thread checking: " << (int)mKeepAliveCounter << "/" << sleepsBeforeSend;
        if (mKeepAliveCounter >= sleepsBeforeSend) // TODO execute iff the connection is valid?
        {
            ZLOGI() << "Keep alive thread tries to send status";
            JSONServerMessage msg;
            msg.InitManually(LuaModule::MODULE_STATUS, "");
            HandleServerMessage(msg);
            mKeepAliveCounter = 0;
        }
    }
}
int App::Logic()
{
    auto configResult = SZ::Config::Me()->LoadConfig("./config.json");
    if (!EnsureCorrectness(*configResult, "App config error", ""))
    {
        delete configResult;
        return -1;
    }
    delete configResult;

    RegisterCommonLuaFunctions();

    std::thread keepAlive(std::bind(&App::KeepAliveThread, this));
    keepAlive.detach();

    int connectionFailures = 0;

    while (true) // currently never leaves this loop
    {
        if (!mConn)
        {
            mConn = new SSLSocketClient();
        }
        else
        {
            mConn->Close(); // to restart client from scratch
        }

        if (!EnsureCorrectness(mConn->Init(), "Connection error", ""))
        {
            Sleep(3);
            ++connectionFailures;
            if (connectionFailures >= FAILURE_COUNT_TO_RECONNECT_SCRIPT)
            {
                connectionFailures = 0;
                SZ::LuaModule module;
                if (!EnsureCorrectness(module.Init(LuaModule::MODULE_MODEM_CONN), "Reconnecting script init error", "", nullptr))
                {
                    continue;
                }
                if (!EnsureCorrectness(module.Call("", ""), "Reconnecting script Lua call error", "", nullptr))
                {
                    continue;
                }
            }
            continue;
        }
        connectionFailures = 0;

        SZ::JSONClientMessage initialMessage;
        initialMessage.Init(LuaModule::MODULE_INIT, "", BoardId(), "OK", "");
        if (!EnsureCorrectness(mConn->Send(initialMessage.Create()), "Initial sending error", "", mConn))
        {
            Sleep(3);
            continue;
        }

        mConn->Listen([&](const std::string &command)
        {
            SZ::JSONServerMessage message;
            if (!EnsureCorrectness(message.Decode(command), "JSON decode error", "", mConn))
            {
                return;
            }

            HandleServerMessage(message);
            mKeepAliveCounter = 0;
        });

        delete mConn;
        mConn = nullptr;
    }

    keepAlive.join();
    SZ::LuaStateControl::Destroy();
    return 0;
}