// *************************************************************** // Function: GWLPDatabase // *************************************************************** #ifndef GWLP_DATABASE_H_ #define GWLP_DATABASE_H_ #include #include #include #ifdef WIN32 #pragma comment(lib, "mysqlclient.lib") #pragma comment(lib, "wsock32.lib") #include #endif #include "mysql.h" #include "hnl/nl.h" using namespace std; using namespace stdext; #define ALL_QUERIES 0 #define SHORT_NO_RESULT 1 #define LONG_NO_RESULT 2 #define SHORT_RESULT 3 #define LONG_RESULT 4 #define LENGTH_OF_LONG_QUERY 200 #define PIPE_THREAD_LIMIT 50 //Configuration data for connecting to the MySQL server. this info should be loaded from a file struct MYSQL_SERVER { char database_name[50]; //The name of the database, usually: gwlp char host[50]; //The host the database is on, ip usually char username[50]; //Username required for db login char password[50]; //Password required for db login unsigned int port; //Port that the database is listening on unsigned short maxPipes; //Max number of connections to db that can be used as pipes }; struct THREAD_INFO { void* pThreadHandle; MYSQL_RES* pResult; unsigned long referenceId; NLmutex* pMutex; MYSQL* pDb; char* pQuery; long queryLength; char* pErrorMessage; long numAffected; bool returnStatus; bool showStatus; }; struct PIPE { MYSQL* pDatabase; char pipeRuleSet; NLmutex* pMutex; list threads; PIPE() { pipeRuleSet=0; pMutex=0; } }; struct QUERY { char* pQuery; long queryLength; char boundRule; unsigned long referenceId; bool returnStatus; bool deleteMe; bool showStatus; }; struct QUERY_RESULT { MYSQL_RES* pResult; unsigned long referenceID; long affectedRows;//Only used for non-result queries, 0=fail }; class GWLPDatabase { public: GWLPDatabase(); ~GWLPDatabase(); bool EstablishConnections(char *host, char *username, char *pass, char *databaseName, long port, short numConnections); void Update(); void Init(); unsigned long ResultsWaiting(); unsigned long Query(char* pString, long length); unsigned long Query(char* pString, long length, bool returnStatus); QUERY_RESULT* StealResult(unsigned long reference); MYSQL_RES* StealMySQLResult(unsigned long reference); bool FreeResult(unsigned long reference); template void ProcessResults(T *hMap); private: unsigned long AddQuery(char* pQuery, long queryLength, bool returnStatus, bool showStatus); void PrepareForShutdown(); vector mPipes; list mQueue; hash_map mResults; unsigned long mFreeReferenceId; unsigned long mQueriesAdded; unsigned long mQueriesProcessed; }; //************************************ // Name: ProcessResults // // Calls the processing function for each result and handles cleanup of results. // // Parameters: // hMap - Local data hash map. // // Return: // //************************************ template void GWLPDatabase::ProcessResults(T *hMap) { hash_map::iterator itr; for (itr=hMap->begin(); itr!=hMap->end(); itr++) { //see if the player has a query in progress if ((itr)->second.queryId>0) { //Check the database to see if this query has been completed QUERY_RESULT *g=NULL; if ((g=StealResult((itr)->second.queryId))!=NULL) { //Save the old information and call the callback processing function void *td=(itr)->second.pTempData; unsigned long qid=(itr)->second.queryId; int ret=(*(itr)->second.QueryProcess)(g, &itr->second); if (ret<=0) { //If the data did not change we can delete it if ((itr)->second.pTempData==td) { if (td!=NULL){delete td;} (itr)->second.pTempData=NULL; } //If there is no new query we can set everything to zero if ((itr)->second.queryId==qid) { (itr)->second.QueryProcess=NULL; (itr)->second.queryId=0; } } //Free the result from the database FreeResult(qid); } } } } #endif