Planeshift
msgmanager.h
Go to the documentation of this file.
1 /*
2  * msgmanager.h
3  *
4  * Copyright (C) 2005 Atomic Blue (info@planeshift.it, http://www.atomicblue.org)
5  *
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation (version 2 of the License)
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */
19 #ifndef __MSGMANAGER_H__
20 #define __MSGMANAGER_H__
21 //=============================================================================
22 // Crystal Space Includes
23 //=============================================================================
24 #include <csutil/ref.h>
25 
26 //=============================================================================
27 // Project Includes
28 //=============================================================================
29 #include "net/subscriber.h" // Subscriber class
30 #include "net/message.h" // For msgtype typedef
31 #include "util/eventmanager.h"
32 #include "globals.h"
33 
34 //=============================================================================
35 // Local Includes
36 //=============================================================================
37 
38 class Client;
39 class gemObject;
40 class gemActor;
41 class MsgEntry;
42 class GEMSupervisor;
43 
48 #define NO_VALIDATION 0x00
51 #define REQUIRE_ANY_CLIENT 0x01
52 #define REQUIRE_READY_CLIENT 0x02
53 #define REQUIRE_ALREADY_READY_CLIENT 0x04
54 #define REQUIRE_ACTOR 0x08
55 #define REQUIRE_ALIVE 0x10
56 #define REQUIRE_TARGET 0x20
57 #define REQUIRE_TARGETACTOR 0x40
58 #define REQUIRE_TARGETNPC 0x80
59 
60 
68 {
69 public:
70 
71  virtual bool Verify(MsgEntry* pMsg, unsigned int flags, Client* &client);
72 
74  Client* FindPlayerClient(const char* name);
75 
84  csArray<csString> DecodeCommandArea(Client* client, csString target);
85 
95  gemObject* FindObjectByString(const csString &str, gemActor* me) const;
96 };
97 
105 template <class SubClass>
107 {
108 public:
109  typedef void(SubClass::*FunctionPointer)(MsgEntry*, Client*);
110 
112  virtual ~MessageManager()
113  {
114  UnsubscribeAll();
115  }
116 
126  inline void Subscribe(FunctionPointer fpt, msgtype type, uint32_t flags = 0x01)
127  {
128  CS::Threading::RecursiveMutexScopedLock lock(mutex);
129  if(!handlers.Contains(type))
130  {
131  GetEventManager()->Subscribe(this, type, flags);
132  }
133  handlers.Put(type, fpt);
134  }
135 
142  inline bool Unsubscribe(msgtype type)
143  {
144  CS::Threading::RecursiveMutexScopedLock lock(mutex);
145  if(handlers.Contains(type))
146  {
147  handlers.DeleteAll(type);
148  return GetEventManager()->Unsubscribe(this, type);
149  }
150  return false;
151  }
152 
160  inline bool Unsubscribe(FunctionPointer handler, msgtype type)
161  {
162  CS::Threading::RecursiveMutexScopedLock lock(mutex);
163  if(handlers.Contains(type))
164  {
165  handlers.Delete(type, handler);
166  if(!handlers.Contains(type))
167  {
168  return GetEventManager()->Unsubscribe(this, type);
169  }
170  else
171  {
172  return true;
173  }
174  }
175  else
176  {
177  return false;
178  }
179  }
180 
186  inline bool UnsubscribeAll()
187  {
188  CS::Threading::RecursiveMutexScopedLock lock(mutex);
189  handlers.Empty();
191  return GetEventManager()->UnsubscribeAll(this);
192  return false;
193  }
194 
202  void HandleMessage(MsgEntry* msg, Client* client)
203  {
204  csArray<FunctionPointer> msgHandlers;
205  {
206  CS::Threading::RecursiveMutexScopedLock lock(mutex);
207  msgHandlers = handlers.GetAll(msg->GetType());
208  }
209  SubClass* self = dynamic_cast<SubClass*>(this);
210  if(!self)
211  {
212  // fatal error, we aren't a base of our subclass!
213  return;
214  }
215 
216  for(size_t i = 0; i < msgHandlers.GetSize(); ++i)
217  {
218  (self->*msgHandlers[i])(msg, client);
219  }
220  }
221 
222 private:
223  CS::Threading::RecursiveMutex mutex;
225 
233  inline EventManager* GetEventManager() const
234  {
235  EventManager* eventManager = psserver->GetEventManager();
236  CS_ASSERT(eventManager);
237  return eventManager;
238  }
239 };
240 
243 #endif
csArray< csString > DecodeCommandArea(Client *client, csString target)
Decodes an area: expression.
bool Unsubscribe(FunctionPointer handler, msgtype type)
Unsubscribes a specific handler from a specific message type.
Definition: msgmanager.h:160
gemObject * FindObjectByString(const csString &str, gemActor *me) const
Find the object we are referring to in str.
uint8_t GetType()
Definition: message.h:1036
bool Unsubscribe(msgtype type)
Unsubscribes this manager from a specific message type.
Definition: msgmanager.h:142
virtual bool Verify(MsgEntry *pMsg, unsigned int flags, Client *&client)
The structure of 1 queue entry (pointer to a message)
Definition: message.h:143
void HandleMessage(MsgEntry *msg, Client *client)
Transfers the message to the manager specific function.
Definition: msgmanager.h:202
This interface must be implemented by objects that want to receive network messages.
Definition: subscriber.h:37
void Subscribe(FunctionPointer fpt, msgtype type, uint32_t flags=0x01)
Subscribes this manager to a specific message type with a custom callback.
Definition: msgmanager.h:126
Any semi-autonomous object, either a player or an NPC.
Definition: gem.h:1170
Base server-side class for subscriptions.
Definition: msgmanager.h:67
EventManager * GetEventManager()
Returns the event manager.
Definition: psserver.h:346
virtual ~MessageManager()
Unsubscribes all messages then destroys this object.
Definition: msgmanager.h:112
This class collects data of a netclient.
Definition: client.h:95
uint8_t msgtype
Definition: message.h:56
A gemObject is any solid, graphical object visible in PS with normal physics and normal collision det...
Definition: gem.h:314
This class holds the refs to the core factories, etc in CEL.
Definition: gem.h:126
bool UnsubscribeAll()
Unsubscribes this manager from all message types.
Definition: msgmanager.h:186
This class handles all queueing and invoking of timed events, such as combat, spells, NPC dialog responses, range weapons, or NPC respawning.
Definition: eventmanager.h:40
psServer * psserver
Client * FindPlayerClient(const char *name)
Finds Client* of character with given name.
Provides a manager to facilitate subscriptions.
Definition: msgmanager.h:106