Fix. Asynchronous calls can now have callbacks. Thead-safeness improved.
git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/uni@13291 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
@@ -146,14 +146,13 @@ namespace Online{
|
||||
|
||||
// ============================================================================
|
||||
|
||||
void CurrentUser::requestSignIn( const irr::core::stringw &username,
|
||||
const irr::core::stringw &password,
|
||||
bool save_session)
|
||||
CurrentUser::SignInRequest * CurrentUser::requestSignIn( const irr::core::stringw &username,
|
||||
const irr::core::stringw &password,
|
||||
bool save_session)
|
||||
{
|
||||
assert(m_state == SIGNED_OUT);
|
||||
m_save_session = save_session;
|
||||
XMLRequest * request = new XMLRequest((std::string)UserConfigParams::m_server_multiplayer + "client-user.php");
|
||||
request->setListener(this, CurrentUser::SIGN_IN_REQUEST);
|
||||
CurrentUser::SignInRequest * request = new CurrentUser::SignInRequest((std::string)UserConfigParams::m_server_multiplayer + "client-user.php");
|
||||
request->setParameter("action",std::string("connect"));
|
||||
request->setParameter("username",username);
|
||||
request->setParameter("password",password);
|
||||
@@ -162,9 +161,10 @@ namespace Online{
|
||||
assert(false);
|
||||
}
|
||||
m_state = SIGNING_IN;
|
||||
return request;
|
||||
}
|
||||
|
||||
bool CurrentUser::signIn(XMLRequest * input)
|
||||
void CurrentUser::signIn(SignInRequest * input)
|
||||
{
|
||||
const XMLNode * xml = input->getResult();
|
||||
bool success = false;
|
||||
@@ -194,8 +194,14 @@ namespace Online{
|
||||
info = _("Unable to connect to the server. Check your internet connection or try again later.");
|
||||
}
|
||||
input->setInfo(info);
|
||||
input->setSuccess(success);
|
||||
if(!success) m_state = SIGNED_OUT;
|
||||
return success;
|
||||
}
|
||||
|
||||
void CurrentUser::SignInRequest::callback()
|
||||
{
|
||||
CurrentUser::acquire()->signIn(this);
|
||||
CurrentUser::release();
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@@ -310,21 +316,4 @@ namespace Online{
|
||||
return _("Currently not signed in");
|
||||
}
|
||||
|
||||
|
||||
// ============================================================================
|
||||
|
||||
|
||||
void CurrentUser::onHTTPCallback(HTTPRequest * finished_request)
|
||||
{
|
||||
XMLRequest * input = (XMLRequest *) finished_request;
|
||||
|
||||
switch(finished_request->getListenerTarget())
|
||||
{
|
||||
case CurrentUser::SIGN_IN_REQUEST:
|
||||
signIn(input);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
} // namespace Online
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace Online{
|
||||
* \brief Class that represents an online registered user
|
||||
* \ingroup online
|
||||
*/
|
||||
class CurrentUser : public User, HTTPListener
|
||||
class CurrentUser : public User
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -47,17 +47,21 @@ namespace Online{
|
||||
SIGNING_OUT
|
||||
};
|
||||
|
||||
private:
|
||||
enum RequestType{
|
||||
SIGN_IN_REQUEST
|
||||
class SignInRequest : public XMLRequest
|
||||
{
|
||||
protected :
|
||||
virtual void callback ();
|
||||
public :
|
||||
SignInRequest(const std::string &url) : XMLRequest(url) {}
|
||||
};
|
||||
std::map<int, std::pair<bool, irr::core::stringw> > m_http_results;
|
||||
|
||||
private:
|
||||
std::string m_token;
|
||||
bool m_save_session;
|
||||
UserState m_state;
|
||||
CurrentUser();
|
||||
|
||||
bool signIn(XMLRequest * input);
|
||||
void signIn(SignInRequest * input);
|
||||
|
||||
|
||||
public:
|
||||
@@ -70,9 +74,9 @@ namespace Online{
|
||||
|
||||
bool trySavedSession();
|
||||
// Login
|
||||
void requestSignIn( const irr::core::stringw &username,
|
||||
const irr::core::stringw &password,
|
||||
bool save_session);
|
||||
SignInRequest * requestSignIn( const irr::core::stringw &username,
|
||||
const irr::core::stringw &password,
|
||||
bool save_session);
|
||||
|
||||
// Register
|
||||
bool signUp( const irr::core::stringw &username,
|
||||
|
||||
@@ -32,6 +32,7 @@ namespace Online{
|
||||
m_priority = priority;
|
||||
m_manage_memory = manage_memory;
|
||||
m_cancel = false;
|
||||
m_done.setAtomic(false);
|
||||
} // Request
|
||||
|
||||
Request::~Request()
|
||||
@@ -45,6 +46,11 @@ namespace Online{
|
||||
afterOperation();
|
||||
}
|
||||
|
||||
void Request::afterOperation()
|
||||
{
|
||||
m_done.setAtomic(true);
|
||||
}
|
||||
|
||||
// =========================================================================================
|
||||
|
||||
QuitRequest::QuitRequest()
|
||||
@@ -62,25 +68,13 @@ namespace Online{
|
||||
m_parameters = new Parameters;
|
||||
m_progress.setAtomic(0);
|
||||
m_added = false;
|
||||
m_done.setAtomic(false);
|
||||
m_listener.setAtomic(NULL);
|
||||
m_listener_target = 0;
|
||||
m_info = "";
|
||||
m_success = false;
|
||||
}
|
||||
|
||||
HTTPRequest::~HTTPRequest()
|
||||
{
|
||||
delete m_parameters;
|
||||
m_listener.lock();
|
||||
if (m_listener.getData() != NULL)
|
||||
{
|
||||
delete m_listener.getData();
|
||||
}
|
||||
m_listener.unlock();
|
||||
}
|
||||
void HTTPRequest::setListener(Synchronised<HTTPListener *> listener, int target)
|
||||
{
|
||||
m_listener = listener;
|
||||
m_listener_target = 0;
|
||||
}
|
||||
|
||||
bool HTTPRequest::isAllowedToAdd()
|
||||
@@ -95,10 +89,8 @@ namespace Online{
|
||||
|
||||
void HTTPRequest::afterOperation()
|
||||
{
|
||||
if (m_listener != NULL)
|
||||
{
|
||||
m_listener->onHTTPCallback(this);
|
||||
}
|
||||
callback();
|
||||
Request::afterOperation();
|
||||
}
|
||||
|
||||
std::string HTTPRequest::downloadPage()
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
#include "utils/string_utils.hpp"
|
||||
#include "utils/synchronised.hpp"
|
||||
#include "io/file_manager.hpp"
|
||||
#include "online/http_listener.hpp"
|
||||
|
||||
namespace Online{
|
||||
|
||||
@@ -47,9 +46,11 @@ namespace Online{
|
||||
* be freed by the calling function. */
|
||||
bool m_manage_memory;
|
||||
|
||||
Synchronised<bool> m_done;
|
||||
|
||||
virtual void beforeOperation() {}
|
||||
virtual void operation() = 0;
|
||||
virtual void afterOperation() {}
|
||||
virtual void afterOperation();
|
||||
|
||||
public:
|
||||
|
||||
@@ -75,6 +76,8 @@ namespace Online{
|
||||
* by network_http (i.e. freed once the request is handled). */
|
||||
bool manageMemory() const { return m_manage_memory; }
|
||||
|
||||
bool isDone(){return m_done.getAtomic();}
|
||||
|
||||
virtual bool isAllowedToAdd() {return true;}
|
||||
|
||||
/** This class is used by the priority queue to sort requests by priority.
|
||||
@@ -116,11 +119,9 @@ namespace Online{
|
||||
* packet is downloaded. At the end eithe -1 (error) or 1
|
||||
* (everything ok) at the end. */
|
||||
Synchronised<float> m_progress;
|
||||
Synchronised<bool> m_done;
|
||||
std::string m_url;
|
||||
bool m_added;
|
||||
Synchronised<HTTPListener *> m_listener;
|
||||
int m_listener_target;
|
||||
bool m_success;
|
||||
/**
|
||||
* info to show on screen if necessary
|
||||
*/
|
||||
@@ -139,6 +140,7 @@ namespace Online{
|
||||
size_t size,
|
||||
size_t nmemb,
|
||||
void *userp);
|
||||
virtual void callback() {}
|
||||
|
||||
public :
|
||||
|
||||
@@ -167,13 +169,13 @@ namespace Online{
|
||||
void setInfo(const irr::core::stringw & info) {m_info = info;}
|
||||
const irr::core::stringw & getInfo() {return m_info;}
|
||||
|
||||
void setSuccess(bool success = true){ m_success = success; }
|
||||
bool isSuccess(){ return m_success; }
|
||||
|
||||
const std::string &getURL() const {return m_url;}
|
||||
|
||||
virtual bool isAllowedToAdd() OVERRIDE;
|
||||
|
||||
virtual void setListener(Synchronised<HTTPListener *> listener, int target = 0);
|
||||
int getListenerTarget(){ return m_listener_target; }
|
||||
|
||||
};
|
||||
|
||||
class XMLRequest : public HTTPRequest
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include "utils/translation.hpp"
|
||||
#include "utils/string_utils.hpp"
|
||||
#include "states_screens/dialogs/registration_dialog.hpp"
|
||||
#include "online/messages.hpp"
|
||||
|
||||
|
||||
using namespace GUIEngine;
|
||||
@@ -39,6 +40,8 @@ LoginDialog::LoginDialog(const Message message_type) :
|
||||
{
|
||||
m_self_destroy = false;
|
||||
m_open_registration_dialog = false;
|
||||
m_load_timer = 0;
|
||||
m_sign_in_request = NULL;
|
||||
loadFromFile("online/login_dialog.stkgui");
|
||||
|
||||
m_info_widget = getWidget<LabelWidget>("info");
|
||||
@@ -93,6 +96,7 @@ LoginDialog::LoginDialog(const Message message_type) :
|
||||
|
||||
LoginDialog::~LoginDialog()
|
||||
{
|
||||
delete m_sign_in_request;
|
||||
}
|
||||
|
||||
|
||||
@@ -102,9 +106,8 @@ void LoginDialog::login()
|
||||
{
|
||||
const stringw username = m_username_widget->getText().trim();
|
||||
const stringw password = m_password_widget->getText().trim();
|
||||
Online::CurrentUser::acquire()->requestSignIn(username,password, m_remember_widget->getState());
|
||||
m_sign_in_request = Online::CurrentUser::acquire()->requestSignIn(username,password, m_remember_widget->getState());
|
||||
Online::CurrentUser::release();
|
||||
m_self_destroy = true;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
@@ -149,7 +152,29 @@ void LoginDialog::onEnterPressedInternal()
|
||||
|
||||
void LoginDialog::onUpdate(float dt)
|
||||
{
|
||||
|
||||
if(m_sign_in_request != NULL)
|
||||
{
|
||||
if(m_sign_in_request->isDone())
|
||||
{
|
||||
if(m_sign_in_request->isSuccess())
|
||||
{
|
||||
m_self_destroy = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
sfx_manager->quickSound( "anvil" );
|
||||
m_message_widget->setColor(irr::video::SColor(255, 255, 0, 0));
|
||||
m_message_widget->setText(m_sign_in_request->getInfo(), false);
|
||||
}
|
||||
delete m_sign_in_request;
|
||||
m_sign_in_request = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_load_timer += dt;
|
||||
m_message_widget->setText(Online::Messages::signingIn(m_load_timer), false);
|
||||
}
|
||||
}
|
||||
//If we want to open the registration dialog, we need to close this one first
|
||||
m_open_registration_dialog && (m_self_destroy = true);
|
||||
|
||||
|
||||
@@ -37,6 +37,8 @@ private:
|
||||
|
||||
bool m_self_destroy;
|
||||
bool m_open_registration_dialog;
|
||||
float m_load_timer;
|
||||
Online::CurrentUser::SignInRequest * m_sign_in_request;
|
||||
|
||||
GUIEngine::LabelWidget * m_info_widget;
|
||||
GUIEngine::TextBoxWidget * m_username_widget;
|
||||
|
||||
@@ -107,13 +107,13 @@ void OnlineScreen::beforeAddingWidget()
|
||||
m_register_widget->setVisible(false);
|
||||
m_sign_in_widget->setVisible(false);
|
||||
}
|
||||
else if (m_recorded_state == CurrentUser::SIGNED_OUT || CurrentUser::SIGNING_IN || CurrentUser::SIGNING_OUT)
|
||||
else if (m_recorded_state == CurrentUser::SIGNED_OUT || m_recorded_state == CurrentUser::SIGNING_IN || m_recorded_state == CurrentUser::SIGNING_OUT)
|
||||
{
|
||||
m_quick_play_widget->setDeactivated();
|
||||
m_find_server_widget->setDeactivated();
|
||||
m_create_server_widget->setDeactivated();
|
||||
m_sign_out_widget->setVisible(false);
|
||||
if(CurrentUser::SIGNING_IN || CurrentUser::SIGNING_OUT)
|
||||
if(m_recorded_state == CurrentUser::SIGNING_IN || m_recorded_state == CurrentUser::SIGNING_OUT)
|
||||
{
|
||||
m_register_widget->setDeactivated();
|
||||
m_sign_in_widget->setDeactivated();
|
||||
|
||||
Reference in New Issue
Block a user