diff options
author | Machiavelli <none@none> | 2010-08-18 19:48:51 +0200 |
---|---|---|
committer | Machiavelli <none@none> | 2010-08-18 19:48:51 +0200 |
commit | d845a903b10b0788b03131a0cb1a2e72f55adc07 (patch) | |
tree | 44c8e0ea6ae88217167d05203378a3324572cee5 /src/server/shared/Threading/Callback.h | |
parent | 2ffe785765e3812e442d246f9561cebd83a008cb (diff) |
DBLayer:
- Use ACE_Future and ACE_Future_Set for async SQL queries with callback
* Callbacks will now be executed from the thread and object that scheduled the request, instead of the world runnable thread (and thus are no longer dependent on the 50ms forced sleep time).
* This design gets rid of a potential DOS loophole in the resultqueue system - unique requests will be cancelled when re-requested.
- Drop now redundant SQLQueryTask, SQLResultQueue, SQLResultQueueTask operations.
- Drop now redundant CharacterHandler class
- Change static callback functions in WorldSession to normal functions.
Thanks to Derex and Zor for advice along the way.
--HG--
branch : trunk
Diffstat (limited to 'src/server/shared/Threading/Callback.h')
-rw-r--r-- | src/server/shared/Threading/Callback.h | 427 |
1 files changed, 62 insertions, 365 deletions
diff --git a/src/server/shared/Threading/Callback.h b/src/server/shared/Threading/Callback.h index d2e2c36851a..921741bbed4 100644 --- a/src/server/shared/Threading/Callback.h +++ b/src/server/shared/Threading/Callback.h @@ -1,6 +1,4 @@ /* - * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> - * * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/> * * This program is free software; you can redistribute it and/or modify @@ -10,377 +8,76 @@ * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#ifndef TRINITY_CALLBACK_H -#define TRINITY_CALLBACK_H - -/// ------------ BASE CLASSES ------------ - -namespace Trinity -{ - template < class Class, typename ParamType1 = void, typename ParamType2 = void, typename ParamType3 = void, typename ParamType4 = void > - class _Callback - { - protected: - typedef void (Class::*Method)(ParamType1, ParamType2, ParamType3, ParamType4); - Class *m_object; - Method m_method; - ParamType1 m_param1; - ParamType2 m_param2; - ParamType3 m_param3; - ParamType4 m_param4; - void _Execute() { (m_object->*m_method)(m_param1, m_param2, m_param3, m_param4); } - public: - _Callback(Class *object, Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3, ParamType4 param4) - : m_object(object), m_method(method), m_param1(param1), m_param2(param2), m_param3(param3), m_param4(param4) {} - _Callback(_Callback < Class, ParamType1, ParamType2, ParamType3, ParamType4> const& cb) - : m_object(cb.object), m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2), m_param3(cb.m_param3), m_param4(cb.m_param4) {} - }; - - template < class Class, typename ParamType1, typename ParamType2, typename ParamType3 > - class _Callback < Class, ParamType1, ParamType2, ParamType3 > - { - protected: - typedef void (Class::*Method)(ParamType1, ParamType2, ParamType3); - Class *m_object; - Method m_method; - ParamType1 m_param1; - ParamType2 m_param2; - ParamType3 m_param3; - void _Execute() { (m_object->*m_method)(m_param1, m_param2, m_param3); } - public: - _Callback(Class *object, Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3) - : m_object(object), m_method(method), m_param1(param1), m_param2(param2) {} - _Callback(_Callback < Class, ParamType1, ParamType2, ParamType3 > const& cb) - : m_object(cb.object), m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2), m_param3(cb.m_param3) {} - }; - - template < class Class, typename ParamType1, typename ParamType2 > - class _Callback < Class, ParamType1, ParamType2 > - { - protected: - typedef void (Class::*Method)(ParamType1, ParamType2); - Class *m_object; - Method m_method; - ParamType1 m_param1; - ParamType2 m_param2; - void _Execute() { (m_object->*m_method)(m_param1, m_param2); } - public: - _Callback(Class *object, Method method, ParamType1 param1, ParamType2 param2) - : m_object(object), m_method(method), m_param1(param1), m_param2(param2) {} - _Callback(_Callback < Class, ParamType1, ParamType2 > const& cb) - : m_object(cb.m_object), m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2) {} - }; - - template < class Class, typename ParamType1 > - class _Callback < Class, ParamType1 > - { - protected: - typedef void (Class::*Method)(ParamType1); - Class *m_object; - Method m_method; - ParamType1 m_param1; - void _Execute() { (m_object->*m_method)(m_param1); } - public: - _Callback(Class *object, Method method, ParamType1 param1) - : m_object(object), m_method(method), m_param1(param1) {} - _Callback(_Callback < Class, ParamType1 > const& cb) - : m_object(cb.m_object), m_method(cb.m_method), m_param1(cb.m_param1) {} - }; - - template < class Class > - class _Callback < Class > - { - protected: - typedef void (Class::*Method)(); - Class *m_object; - Method m_method; - void _Execute() { (m_object->*m_method)(); } - public: - _Callback(Class *object, Method method) - : m_object(object), m_method(method) {} - _Callback(_Callback < Class > const& cb) - : m_object(cb.m_object), m_method(cb.m_method) {} - }; - - /// ---- Statics ---- - - template < typename ParamType1 = void, typename ParamType2 = void, typename ParamType3 = void, typename ParamType4 = void > - class _SCallback - { - protected: - typedef void (*Method)(ParamType1, ParamType2, ParamType3, ParamType4); - Method m_method; - ParamType1 m_param1; - ParamType2 m_param2; - ParamType3 m_param3; - ParamType4 m_param4; - void _Execute() { (*m_method)(m_param1, m_param2, m_param3, m_param4); } - public: - _SCallback(Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3, ParamType4 param4) - : m_method(method), m_param1(param1), m_param2(param2), m_param3(param3), m_param4(param4) {} - _SCallback(_SCallback < ParamType1, ParamType2, ParamType3, ParamType4> const& cb) - : m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2), m_param3(cb.m_param3), m_param4(cb.m_param4) {} - }; - - template < typename ParamType1, typename ParamType2, typename ParamType3 > - class _SCallback < ParamType1, ParamType2, ParamType3 > - { - protected: - typedef void (*Method)(ParamType1, ParamType2, ParamType3); - Method m_method; - ParamType1 m_param1; - ParamType2 m_param2; - ParamType3 m_param3; - void _Execute() { (*m_method)(m_param1, m_param2, m_param3); } - public: - _SCallback(Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3) - : m_method(method), m_param1(param1), m_param2(param2), m_param3(param3) {} - _SCallback(_SCallback < ParamType1, ParamType2, ParamType3 > const& cb) - : m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2), m_param3(cb.m_param3) {} - }; - - template < typename ParamType1, typename ParamType2 > - class _SCallback < ParamType1, ParamType2 > - { - protected: - typedef void (*Method)(ParamType1, ParamType2); - Method m_method; - ParamType1 m_param1; - ParamType2 m_param2; - void _Execute() { (*m_method)(m_param1, m_param2); } - public: - _SCallback(Method method, ParamType1 param1, ParamType2 param2) - : m_method(method), m_param1(param1), m_param2(param2) {} - _SCallback(_SCallback < ParamType1, ParamType2 > const& cb) - : m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2) {} - }; - - template < typename ParamType1 > - class _SCallback < ParamType1 > - { - protected: - typedef void (*Method)(ParamType1); - Method m_method; - ParamType1 m_param1; - void _Execute() { (*m_method)(m_param1); } - public: - _SCallback(Method method, ParamType1 param1) - : m_method(method), m_param1(param1) {} - _SCallback(_SCallback < ParamType1 > const& cb) - : m_method(cb.m_method), m_param1(cb.m_param1) {} - }; - - template < > - class _SCallback < > - { - protected: - typedef void (*Method)(); - Method m_method; - void _Execute() { (*m_method)(); } - public: - _SCallback(Method method) - : m_method(method) {} - _SCallback(_SCallback <> const& cb) - : m_method(cb.m_method) {} - }; -} - -/// --------- GENERIC CALLBACKS ---------- - -namespace Trinity -{ - class ICallback - { - public: - virtual void Execute() = 0; - virtual ~ICallback() {} - }; - - template < class CB > - class _ICallback : public CB, public ICallback - { - public: - _ICallback(CB const& cb) : CB(cb) {} - void Execute() { CB::_Execute(); } - }; - - template < class Class, typename ParamType1 = void, typename ParamType2 = void, typename ParamType3 = void, typename ParamType4 = void > - class Callback : - public _ICallback< _Callback < Class, ParamType1, ParamType2, ParamType3, ParamType4 > > - { - private: - typedef _Callback < Class, ParamType1, ParamType2, ParamType3, ParamType4 > C4; - public: - Callback(Class *object, typename C4::Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3, ParamType4 param4) - : _ICallback< C4 >(C4(object, method, param1, param2, param3, param4)) {} - }; - - template < class Class, typename ParamType1, typename ParamType2, typename ParamType3 > - class Callback < Class, ParamType1, ParamType2, ParamType3 > : - public _ICallback< _Callback < Class, ParamType1, ParamType2, ParamType3 > > - { - private: - typedef _Callback < Class, ParamType1, ParamType2, ParamType3 > C3; - public: - Callback(Class *object, typename C3::Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3) - : _ICallback< C3 >(C3(object, method, param1, param2, param3)) {} - }; - - template < class Class, typename ParamType1, typename ParamType2 > - class Callback < Class, ParamType1, ParamType2 > : - public _ICallback< _Callback < Class, ParamType1, ParamType2 > > - { - private: - typedef _Callback < Class, ParamType1, ParamType2 > C2; - public: - Callback(Class *object, typename C2::Method method, ParamType1 param1, ParamType2 param2) - : _ICallback< C2 >(C2(object, method, param1, param2)) {} - }; - - template < class Class, typename ParamType1 > - class Callback < Class, ParamType1 > : - public _ICallback< _Callback < Class, ParamType1 > > - { - private: - typedef _Callback < Class, ParamType1 > C1; - public: - Callback(Class *object, typename C1::Method method, ParamType1 param1) - : _ICallback< C1 >(C1(object, method, param1)) {} - }; +#ifndef _CALLBACK_H +#define _CALLBACK_H - template < class Class > - class Callback < Class > : public _ICallback< _Callback < Class > > - { - private: - typedef _Callback < Class > C0; - public: - Callback(Class *object, typename C0::Method method) - : _ICallback< C0 >(C0(object, method)) {} - }; -} +#include <ace/Future.h> +#include <ace/Future_Set.h> -/// ---------- QUERY CALLBACKS ----------- +#include "DatabaseEnv.h" -#include "QueryResult.h" -class QueryResult; +typedef ACE_Future<QueryResult_AutoPtr> QueryResultFuture; -namespace Trinity +/*! A simple template using ACE_Future to manage callbacks from the thread and object that + issued the request. <ParamType> is variable type of parameter that is used as parameter + for the callback function. +*/ +template <typename ParamType> +class QueryCallback { - class IQueryCallback - { - public: - virtual void Execute() = 0; - virtual ~IQueryCallback() {} - virtual void SetResult(QueryResult_AutoPtr result) = 0; - virtual QueryResult_AutoPtr GetResult() = 0; - }; - - template < class CB > - class _IQueryCallback : public CB, public IQueryCallback - { - public: - _IQueryCallback(CB const& cb) : CB(cb) {} - void Execute() { CB::_Execute(); } - void SetResult(QueryResult_AutoPtr result) { CB::m_param1 = result; } - QueryResult_AutoPtr GetResult() { return CB::m_param1; } - }; - - template < class Class, typename ParamType1 = void, typename ParamType2 = void, typename ParamType3 = void > - class QueryCallback : - public _IQueryCallback< _Callback < Class, QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3 > > - { - private: - typedef _Callback < Class, QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3 > QC3; - public: - QueryCallback(Class *object, typename QC3::Method method, QueryResult_AutoPtr result, ParamType1 param1, ParamType2 param2, ParamType3 param3) - : _IQueryCallback< QC3 >(QC3(object, method, result, param1, param2, param3)) {} - }; - - template < class Class, typename ParamType1, typename ParamType2 > - class QueryCallback < Class, ParamType1, ParamType2 > : - public _IQueryCallback< _Callback < Class, QueryResult_AutoPtr, ParamType1, ParamType2 > > - { - private: - typedef _Callback < Class, QueryResult_AutoPtr, ParamType1, ParamType2 > QC2; - public: - QueryCallback(Class *object, typename QC2::Method method, QueryResult_AutoPtr result, ParamType1 param1, ParamType2 param2) - : _IQueryCallback< QC2 >(QC2(object, method, result, param1, param2)) {} - }; - - template < class Class, typename ParamType1 > - class QueryCallback < Class, ParamType1 > : - public _IQueryCallback< _Callback < Class, QueryResult_AutoPtr, ParamType1 > > - { - private: - typedef _Callback < Class, QueryResult_AutoPtr, ParamType1 > QC1; - public: - QueryCallback(Class *object, typename QC1::Method method, QueryResult_AutoPtr result, ParamType1 param1) - : _IQueryCallback< QC1 >(QC1(object, method, result, param1)) {} - }; - - template < class Class > - class QueryCallback < Class > : public _IQueryCallback< _Callback < Class, QueryResult_AutoPtr > > - { - private: - typedef _Callback < Class, QueryResult_AutoPtr > QC0; - public: - QueryCallback(Class *object, typename QC0::Method method, QueryResult_AutoPtr result) - : _IQueryCallback< QC0 >(QC0(object, method, result)) {} - }; - - /// ---- Statics ---- - - template < typename ParamType1 = void, typename ParamType2 = void, typename ParamType3 = void > - class SQueryCallback : - public _IQueryCallback< _SCallback < QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3 > > - { - private: - typedef _SCallback < QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3 > QC3; - public: - SQueryCallback(typename QC3::Method method, QueryResult_AutoPtr result, ParamType1 param1, ParamType2 param2, ParamType3 param3) - : _IQueryCallback< QC3 >(QC3(method, result, param1, param2, param3)) {} - }; - - template < typename ParamType1, typename ParamType2 > - class SQueryCallback < ParamType1, ParamType2 > : - public _IQueryCallback< _SCallback < QueryResult_AutoPtr, ParamType1, ParamType2 > > - { - private: - typedef _SCallback < QueryResult_AutoPtr, ParamType1, ParamType2 > QC2; - public: - SQueryCallback(typename QC2::Method method, QueryResult_AutoPtr result, ParamType1 param1, ParamType2 param2) - : _IQueryCallback< QC2 >(QC2(method, result, param1, param2)) {} - }; - - template < typename ParamType1 > - class SQueryCallback < ParamType1 > : - public _IQueryCallback< _SCallback < QueryResult_AutoPtr, ParamType1 > > - { - private: - typedef _SCallback < QueryResult_AutoPtr, ParamType1 > QC1; - public: - SQueryCallback(typename QC1::Method method, QueryResult_AutoPtr result, ParamType1 param1) - : _IQueryCallback< QC1 >(QC1(method, result, param1)) {} - }; - - template < > - class SQueryCallback < > : public _IQueryCallback< _SCallback < QueryResult_AutoPtr > > - { - private: - typedef _SCallback < QueryResult_AutoPtr > QC0; - public: - SQueryCallback(QC0::Method method, QueryResult_AutoPtr result) - : _IQueryCallback< QC0 >(QC0(method, result)) {} - }; -} - -#endif - + public: + QueryCallback() {} + + void SetFutureResult(QueryResultFuture value) + { + result = value; + } + + QueryResultFuture GetFutureResult() + { + return result; + } + + int IsReady() + { + return result.ready(); + } + + void GetResult(QueryResult_AutoPtr res) + { + result.get(res); + } + + void FreeResult() + { + result.cancel(); + } + + void SetParam(ParamType value) + { + param = value; + } + + ParamType GetParam() + { + return param; + } + + private: + QueryResultFuture result; + ParamType param; +}; + +/*! ACE_Future_Set to store a bunch of unique async queries with callbacks. + (ie name queries) +*/ +typedef ACE_Future_Set<QueryResult_AutoPtr> QueryResultFutureSet; +#endif
\ No newline at end of file |