core/Database.h
author Borja Ferrer <faluco@alliedmods.net>
Sat Feb 23 20:18:49 2008 +0000 (2008-02-23)
changeset 1894 e6efc8ffa47a
parent 1656 b0b86d57530c
child 1947 e73a1e51833b
permissions -rw-r--r--
Executable memory is now provided by MM:S's allocator, this should reduce virtual memory usage.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_DATABASE_MANAGER_H_
    33 #define _INCLUDE_DATABASE_MANAGER_H_
    34 
    35 #include <IDBDriver.h>
    36 #include "sm_globals.h"
    37 #include <sh_vector.h>
    38 #include <sh_string.h>
    39 #include <sh_list.h>
    40 #include <ITextParsers.h>
    41 #include "sm_memtable.h"
    42 #include <IThreader.h>
    43 #include "sm_simple_prioqueue.h"
    44 #include "PluginSys.h"
    45 
    46 using namespace SourceHook;
    47 
    48 struct ConfDbInfo
    49 {
    50 	ConfDbInfo() : name(-1), driver(-1), host(-1), user(-1), pass(-1), 
    51 		database(-1), realDriver(NULL)
    52 	{
    53 	}
    54 	int name;
    55 	int driver;
    56 	int host;
    57 	int user;
    58 	int pass;
    59 	int database;
    60 	IDBDriver *realDriver;
    61 	DatabaseInfo info;
    62 };
    63 
    64 class DBManager : 
    65 	public IDBManager,
    66 	public SMGlobalClass,
    67 	public IHandleTypeDispatch,
    68 	public ITextListener_SMC,
    69 	public IThread,
    70 	public IThreadWorkerCallbacks,
    71 	public IPluginsListener
    72 {
    73 public:
    74 	DBManager();
    75 public:
    76 	const char *GetInterfaceName();
    77 	unsigned int GetInterfaceVersion();
    78 public: //SMGlobalClass
    79 	void OnSourceModAllInitialized();
    80 	void OnSourceModLevelChange(const char *mapName);
    81 	void OnSourceModIdentityDropped(IdentityToken_t *pToken);
    82 	void OnSourceModShutdown();
    83 public: //IHandleTypeDispatch
    84 	void OnHandleDestroy(HandleType_t type, void *object);
    85 public: //IDBManager
    86 	void AddDriver(IDBDriver *pDrivera);
    87 	void RemoveDriver(IDBDriver *pDriver);
    88 	const DatabaseInfo *FindDatabaseConf(const char *name);
    89 	bool Connect(const char *name, IDBDriver **pdr, IDatabase **pdb, bool persistent, char *error, size_t maxlength);
    90 	unsigned int GetDriverCount();
    91 	IDBDriver *GetDriver(unsigned int index);
    92 	Handle_t CreateHandle(DBHandleType type, void *ptr, IdentityToken_t *pToken);
    93 	HandleError ReadHandle(Handle_t hndl, DBHandleType type, void **ptr);
    94 	HandleError ReleaseHandle(Handle_t hndl, DBHandleType type, IdentityToken_t *token);
    95 public: //ITextListener_SMC
    96 	void ReadSMC_ParseStart();
    97 	SMCResult ReadSMC_NewSection(const SMCStates *states, const char *name);
    98 	SMCResult ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value);
    99 	SMCResult ReadSMC_LeavingSection(const SMCStates *states);
   100 	void ReadSMC_ParseEnd(bool halted, bool failed);
   101 public: //IThread
   102 	void RunThread(IThreadHandle *pThread);
   103 	void OnTerminate(IThreadHandle *pThread, bool cancel);
   104 public: //IThreadWorkerCallbacks
   105 	void OnWorkerStart(IThreadWorker *pWorker);
   106 	void OnWorkerStop(IThreadWorker *pWorker);
   107 public: //IPluginsListener
   108 	void OnPluginUnloaded(IPlugin *plugin);
   109 public:
   110 	ConfDbInfo *GetDatabaseConf(const char *name);
   111 	IDBDriver *FindOrLoadDriver(const char *name);
   112 	IDBDriver *GetDefaultDriver();
   113 	const char *GetDefaultDriverName();
   114 	bool AddToThreadQueue(IDBThreadOperation *op, PrioQueueLevel prio);
   115 	void LockConfig();
   116 	void UnlockConfig();
   117 	void RunFrame();
   118 	inline HandleType_t GetDatabaseType()
   119 	{
   120 		return m_DatabaseType;
   121 	}
   122 private:
   123 	void KillWorkerThread();
   124 private:
   125 	CVector<IDBDriver *> m_drivers;
   126 
   127 	/* Threading stuff */
   128 	PrioQueue<IDBThreadOperation *> m_OpQueue;
   129 	Queue<IDBThreadOperation *> m_ThinkQueue;
   130 	CVector<bool> m_drSafety;			/* which drivers are safe? */
   131 	IThreadWorker *m_pWorker;			/* Worker thread object */
   132 	IMutex *m_pConfigLock;				/* Configuration lock */
   133 	IMutex *m_pQueueLock;				/* Queue safety lock */
   134 	IMutex *m_pThinkLock;				/* Think-queue lock */
   135 
   136 	List<ConfDbInfo> m_confs;
   137 	HandleType_t m_DriverType;
   138 	HandleType_t m_DatabaseType;
   139 	String m_DefDriver;
   140 	BaseStringTable m_StrTab;
   141 	char m_Filename[PLATFORM_MAX_PATH];
   142 	unsigned int m_ParseLevel;
   143 	unsigned int m_ParseState;
   144 	IDBDriver *m_pDefault;
   145 };
   146 
   147 extern DBManager g_DBMan;
   148 
   149 #endif //_INCLUDE_DATABASE_MANAGER_H_