Ejemplo n.º 1
0
/** Register this client with the STK server.
 */
void ConnectToServer::registerWithSTKServer()
{
    // Our public address is now known, register details with
    // STK server.
    const TransportAddress& addr = NetworkConfig::get()->getMyAddress();
    Online::XMLRequest *request  = new Online::XMLRequest();
    PlayerManager::setUserDetails(request, "set",
                                  Online::API::SERVER_PATH);
    request->addParameter("address", addr.getIP());
    request->addParameter("port", addr.getPort());
    request->addParameter("private_port",
                          NetworkConfig::get()->getClientPort());

    Log::info("ConnectToServer", "Registering addr %s",
              addr.toString().c_str());

    // This can be done blocking: till we are registered with the
    // stk server, there is no need to to react to any other 
    // network requests
    request->executeNow();

    const XMLNode * result = request->getXMLData();

    std::string success;
    if(result->get("success", &success) && success == "yes")
    {
        Log::debug("ConnectToServer", "Address registered successfully.");
    }
    else
    {
        Log::error("ConnectToServer", "Failed to register address.");
    }
    delete request;

}   // registerWithSTKServer
void ServerLobbyRoomProtocol::checkIncomingConnectionRequests()
{
    // first poll every 5 seconds
    static double last_poll_time = 0;
    if (StkTime::getRealTime() > last_poll_time+10.0)
    {
        last_poll_time = StkTime::getRealTime();
        TransportAddress addr = NetworkManager::getInstance()->getPublicAddress();
        Online::XMLRequest* request = new Online::XMLRequest();
        request->setServerURL("address-management.php");
        request->addParameter("id",Online::CurrentUser::get()->getProfile()->getID());
        request->addParameter("token",Online::CurrentUser::get()->getToken());
        request->addParameter("address",addr.ip);
        request->addParameter("port",addr.port);
        request->addParameter("action","poll-connection-requests");

        request->executeNow();
        assert(request->isDone());

        const XMLNode * result = request->getXMLData();
        std::string rec_success;
        if(result->get("success", &rec_success))
        {
            if(rec_success == "yes")
            {
                const XMLNode * users_xml = result->getNode("users");
                uint32_t id = 0;
                for (unsigned int i = 0; i < users_xml->getNumNodes(); i++)
                {
                    users_xml->getNode(i)->get("id", &id);
                    Log::debug("ServerLobbyRoomProtocol", "User with id %d wants to connect.", id);
                    m_incoming_peers_ids.push_back(id);
                }
            }
            else
            {
                Log::error("ServerLobbyRoomProtocol", "Error while reading the list.");
            }
        }
        else
        {
            Log::error("ServerLobbyRoomProtocol", "Cannot retrieve the list.");
        }
        delete request;
    }

    // now
    for (unsigned int i = 0; i < m_incoming_peers_ids.size(); i++)
    {
        m_listener->requestStart(new ConnectToPeer(m_incoming_peers_ids[i]));
    }
    m_incoming_peers_ids.clear();
}
Ejemplo n.º 3
0
/** Register this client with the STK server.
 */
void ConnectToServer::registerWithSTKServer()
{
    // Our public address is now known, register details with
    // STK server. If previous unjoin request is not finished, wait
    if (!m_previous_unjoin.expired())
    {
        if (ProtocolManager::lock()->isExiting())
            return;
        StkTime::sleep(1);
    }

    const TransportAddress& addr = STKHost::get()->getPublicAddress();
    Online::XMLRequest *request  = new Online::XMLRequest();
    NetworkConfig::get()->setServerDetails(request, "join-server-key");
    request->addParameter("server-id", m_server->getServerId());
    request->addParameter("address", addr.getIP());
    request->addParameter("port", addr.getPort());

    Crypto::initClientAES();
    request->addParameter("aes-key", Crypto::getClientKey());
    request->addParameter("aes-iv", Crypto::getClientIV());

    Log::info("ConnectToServer", "Registering addr %s",
        addr.toString().c_str());

    // This can be done blocking: till we are registered with the
    // stk server, there is no need to to react to any other 
    // network requests
    request->executeNow();

    const XMLNode* result = request->getXMLData();

    std::string success;
    if(result->get("success", &success) && success == "yes")
    {
        Log::debug("ConnectToServer", "Address registered successfully.");
    }
    else
    {
        irr::core::stringc error(request->getInfo().c_str());
        Log::error("ConnectToServer", "Failed to register client address: %s",
            error.c_str());
        m_state = DONE;
    }
    delete request;

}   // registerWithSTKServer
/** Register this server (i.e. its public address) with the STK server
 *  so that clients can find it. It blocks till a response from the
 *  stk server is received (this function is executed from the 
 *  ProtocolManager thread). The information about this client is added
 *  to the table 'server'.
 */
void ServerLobbyRoomProtocol::registerServer()
{
    Online::XMLRequest *request = new Online::XMLRequest();
    const TransportAddress& addr = NetworkConfig::get()->getMyAddress();
#ifdef NEW_PROTOCOL
    PlayerManager::setUserDetails(request, "register", Online::API::SERVER_PATH);
#else
    PlayerManager::setUserDetails(request, "start", Online::API::SERVER_PATH);
#endif
    request->addParameter("address",      addr.getIP()                    );
    request->addParameter("port",         addr.getPort()                  );
    request->addParameter("private_port",
                                    NetworkConfig::get()->getPrivatePort());
    request->addParameter("name",   NetworkConfig::get()->getServerName() );
    request->addParameter("max_players", 
                          UserConfigParams::m_server_max_players          );
    Log::info("RegisterServer", "Showing addr %s", addr.toString().c_str());
    
    request->executeNow();

    const XMLNode * result = request->getXMLData();
    std::string rec_success;

    if (result->get("success", &rec_success) && rec_success == "yes")
    {
        Log::info("RegisterServer", "Server is now online.");
        STKHost::get()->setRegistered(true);
    }
    else
    {
        irr::core::stringc error(request->getInfo().c_str());
        Log::error("RegisterServer", "%s", error.c_str());
        STKHost::get()->setErrorMessage(_("Failed to register server"));
    }

}   // registerServer
/** Query the STK server for connection requests. For each connection request
 *  start a ConnectToPeer protocol.
 */
void ServerLobbyRoomProtocol::checkIncomingConnectionRequests()
{
    // First poll every 5 seconds. Return if no polling needs to be done.
    const float POLL_INTERVAL = 5.0f;
    static double last_poll_time = 0;
    if (StkTime::getRealTime() < last_poll_time + POLL_INTERVAL)
        return;

    // Now poll the stk server
    last_poll_time = StkTime::getRealTime();
    Online::XMLRequest* request = new Online::XMLRequest();
    PlayerManager::setUserDetails(request, "poll-connection-requests",
                                  Online::API::SERVER_PATH);

    const TransportAddress &addr = NetworkConfig::get()->getMyAddress();
    request->addParameter("address", addr.getIP()  );
    request->addParameter("port",    addr.getPort());

    request->executeNow();
    assert(request->isDone());

    const XMLNode *result = request->getXMLData();
    std::string success;

    if (!result->get("success", &success) || success != "yes")
    {
        Log::error("ServerLobbyRoomProtocol", "Cannot retrieve the list.");
        return;
    }

    // Now start a ConnectToPeer protocol for each connection request
    const XMLNode * users_xml = result->getNode("users");
    uint32_t id = 0;
    for (unsigned int i = 0; i < users_xml->getNumNodes(); i++)
    {
        users_xml->getNode(i)->get("id", &id);
        Log::debug("ServerLobbyRoomProtocol",
                   "User with id %d wants to connect.", id);
        Protocol *p = new ConnectToPeer(id);
        p->requestStart();
    }        
    delete request;
}   // checkIncomingConnectionRequests