|
SuperTuxKart
|
A class to execute requests in a separate thread. More...
#include <request_manager.hpp>

Public Types | |
| enum | InternetPermission { IPERM_NOT_ASKED = 0 , IPERM_ALLOWED = 1 , IPERM_NOT_ALLOWED = 2 } |
| If stk has permission to access the internet (for news server etc). More... | |
Public Member Functions | |
| void | addRequest (std::shared_ptr< Online::Request > request) |
| Inserts a request into the queue of all requests. More... | |
| void | startNetworkThread () |
| Start the actual network thread. More... | |
| void | stopNetworkThread () |
| This function inserts a high priority request to quit into the request queue of the network thead, and also aborts any ongoing download. More... | |
| bool | getAbort () |
| bool | getPaused () |
| void | setPaused (bool val) |
| void | update (float dt) |
| Should be called every frame and takes care of processing the result queue and polling the database server if a user is signed in. | |
| void | setMenuPollingInterval (float polling_interval) |
| Sets the interval with which poll requests are send to the server. More... | |
| void | setGamePollingInterval (float polling_interval) |
| Sets the interval with which poll requests are send to the server. More... | |
Public Member Functions inherited from CanBeDeleted | |
| CanBeDeleted () | |
| Set this instance to be not ready to be deleted. | |
| void | setCanBeDeleted () |
| Sets this instance to be ready to be deleted. | |
| void | resetCanBeDeleted () |
| bool | canBeDeletedNow () |
| bool | waitForReadyToDeleted (float waiting_time) |
| Waits at most t seconds for this class to be ready to be deleted. More... | |
Static Public Member Functions | |
| static RequestManager * | get () |
| Singleton access function. More... | |
| static void | deallocate () |
| Deletes the http manager. | |
| static bool | isRunning () |
| Checks if the http manager is running. | |
Static Public Attributes | |
| static bool | m_disable_polling = false |
| static const int | HTTP_MAX_PRIORITY = 9999 |
Private Member Functions | |
| void | addResult (std::shared_ptr< Online::Request > request) |
| Inserts a request into the queue of results. More... | |
| void | handleResultQueue () |
| Takes a request out of the result queue, if any is present. More... | |
| RequestManager () | |
| Constructor. More... | |
Static Private Member Functions | |
| static void | mainLoop (void *obj) |
| The actual main loop, which is started as a separate thread from the constructor. More... | |
Private Attributes | |
| float | m_time_since_poll |
| Time passed since the last poll request. | |
| std::shared_ptr< Online::Request > | m_current_request |
| The current requested being worked on. | |
| std::condition_variable | m_condition_variable |
| A conditional variable to wake up the main loop. | |
| Synchronised< bool > | m_abort |
| Signal an abort in case that a download is still happening. | |
| std::atomic_bool | m_paused |
| Signal an pause before STK goes into background in iOS. | |
| float | m_game_polling_interval |
| The polling interval while a game is running. | |
| float | m_menu_polling_interval |
| The polling interval while the menu is shown. | |
| std::thread | m_thread |
| Thread id of the thread running in this object. | |
| Synchronised< std::priority_queue< std::shared_ptr< Online::Request >, std::vector< std::shared_ptr< Online::Request > >, Online::Request::Compare > > | m_request_queue |
| The list of pointers to all requests that still need to be handled. | |
| Synchronised< std::queue< std::shared_ptr< Online::Request > > > | m_result_queue |
| The list of pointers to all requests that are already executed by the networking thread, but still need to be processed by the main thread. | |
Static Private Attributes | |
| static RequestManager * | m_request_manager = NULL |
A class to execute requests in a separate thread.
Typically the requests involve a http(s) requests to be sent to the stk server, and receive an answer (e.g. to sign in; or to download an addon). The requests are sorted by priority (e.g. sign in and out have higher priority than downloading addon icons). A request is created and initialised from the main thread. When it is moved into the request queue, it must not be handled by the main thread anymore, only the RequestManager thread can handle it. Once the request is finished, it is put in a separate ready queue. The main thread regularly checks the ready queue for any ready request, and executes a callback. So there is no need to protect any functions or data members in requests, since they will either be handled by the main thread, or RequestManager thread, never by both. On exit, if necessary a high priority sign-out or client-quit request is put into the queue, and a flag is set which causes libcurl to abort any ongoing download. Then an additional 'quit' event with same priority as the sign-out is added to the queue (since it will be added later, the sign-out will be executed first, making sure that a logged in user is logged out (or its session saved). Once this is done, most of stk is deleted (except a few objects like the file manager which might be accessed if a download just finished before the abort). On executing the quit request, the request manager will set a flag that it is ready to be deleted (using the CanBeDeleted class). The main thread will wait for a certain amount of time for the RequestManager to be ready to be deleted (i.e. the sign-out and quit request have been processes), before deleting the RequestManager. Typically the RequestManager will finish while the rest of stk is shutting down, so the user will not experience any waiting time. Only on first start of stk (which will trigger downloading of all addon icons) is it possible that actually a download request is running, which might take a bit before it can be deleted.
If stk has permission to access the internet (for news server etc).
IPERM_NOT_ASKED: The user needs to be asked if he wants to grant permission IPERM_ALLOWED: STK is allowed to access server. IPERM_NOT_ALLOWED: STK must not access external servers.
|
private |
Constructor.
It only initialised values, it does not start the actual thread.
| void Online::RequestManager::addRequest | ( | std::shared_ptr< Online::Request > | request | ) |
Inserts a request into the queue of all requests.
The request will be sorted by priority.
| request | The pointer to the new request to insert. |
|
private |
Inserts a request into the queue of results.
| request | The pointer to the request to insert. |
|
inlinestatic |
Singleton access function.
Creates the RequestManager if necessary.
|
private |
Takes a request out of the result queue, if any is present.
Calls the callback method of the request and takes care of memory management if necessary.
|
staticprivate |
The actual main loop, which is started as a separate thread from the constructor.
After testing for a new server, fetching news, the list of packages to download, it will wait for commands to be issued.
| obj | A pointer to this object, passed on by pthread_create |
|
inline |
Sets the interval with which poll requests are send to the server.
This can happen from the news manager (i.e. info contained in the news.xml file), or a poll request.
|
inline |
Sets the interval with which poll requests are send to the server.
This can happen from the news manager (i.e. info contained in the news.xml file), or a poll request.
| void Online::RequestManager::startNetworkThread | ( | ) |
Start the actual network thread.
This can not be done as part of the constructor, since the assignment to the global network_http variable has not been assigned at that stage, and the thread might use network_http - a very subtle race condition. So the thread can only be started after the assignment (in main) has been done.
| void Online::RequestManager::stopNetworkThread | ( | ) |
This function inserts a high priority request to quit into the request queue of the network thead, and also aborts any ongoing download.
Separating this allows more time for the thread to finish cleanly, before it gets cancelled in the destructor.