Planeshift
msghandler.h
Go to the documentation of this file.
1 /*
2  * msghandler.h by Matze Braun <MatzeBraun@gmx.de>
3  *
4  * Copyright (C) 2001 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 __MSGHANDLER_H__
20 #define __MSGHANDLER_H__
21 
22 #include <csutil/parray.h>
23 #include <csutil/refcount.h>
24 #include <csutil/threading/thread.h>
25 #include <csutil/threading/rwmutex.h>
26 
27 #include "net/message.h"
28 #include "net/netbase.h"
29 
30 // forward decls
31 struct iNetSubscriber;
32 class NetBase;
33 class Client;
34 
39 //-----------------------------------------------------------------------------
40 
41 
48 {
49  uint32_t flags;
58  Subscription(iNetSubscriber *nSubscriber, uint32_t nFlags = 0x01/*REQUIRE_READY_CLIENT*/)
59  : flags(nFlags), subscriber(nSubscriber)
60  {
61  }
62 
63  // comparison operator required for usage in csHash
64  bool operator<(const Subscription& other) const
65  {
66  return subscriber < other.subscriber;
67  }
68 };
69 
70 
71 //-----------------------------------------------------------------------------
72 
73 
81 {
83  csRefArray<MsgEntry> pendingMessages;
84 
85  OrderedMessageChannel() : nextSequenceNumber(0) { }
86 
87  int GetCurrentSequenceNumber() { return nextSequenceNumber; }
88 
90  {
91  nextSequenceNumber++;
92  if (nextSequenceNumber > 63) // must be clamped to 6 bit value
93  nextSequenceNumber = 1; // can't use zero because that means unsequenced
94 
95  return nextSequenceNumber;
96  }
97 };
98 
99 //-----------------------------------------------------------------------------
100 
106 class MsgHandler : public csRefCount
107 {
108 public:
109  MsgHandler();
110  virtual ~MsgHandler();
111 
113  bool Initialize(NetBase *nb, int queuelen = 500);
114 
125  virtual void Subscribe(iNetSubscriber *subscriber, msgtype type, uint32_t flags = 0x01/*REQUIRE_READY_CLIENT*/);
126 
136  virtual bool Unsubscribe(iNetSubscriber *subscriber , msgtype type);
137 
144  virtual bool UnsubscribeAll(iNetSubscriber *subscriber);
145 
147  void Publish(MsgEntry *msg);
148 
151 
153  virtual void SendMessage(MsgEntry *msg)
154  { netbase->SendMessage(msg); }
155 
157  virtual void Broadcast(MsgEntry* msg, broadcasttype scope = NetBase::BC_EVERYONEBUTSELF, int guildID=-1)
158  { netbase->Broadcast(msg, scope, guildID); }
159 
176  virtual void Multicast(MsgEntry* msg, csArray<PublishDestination>& multi, uint32_t except, float range)
177  { netbase->Multicast(msg, multi, except, range); }
178 
179  void AddToLocalQueue(MsgEntry *me) { netbase->QueueMessage(me); }
180 
181  csTicks GetPing() { return netbase->GetPing(); }
182 
184  bool Flush() { return netbase->Flush(queue); }
185 
186 protected:
189 
193  csHash<Subscription, msgtype> subscribers;
194  CS::Threading::ReadWriteMutex mutex;
195 };
196 
199 #endif
200 
int GetCurrentSequenceNumber()
Definition: msghandler.h:87
void AddToLocalQueue(MsgEntry *me)
Definition: msghandler.h:179
iNetSubscriber * subscriber
The actual subscriber that wants to be notified.
Definition: msghandler.h:50
int IncrementSequenceNumber()
Definition: msghandler.h:89
bool operator<(const Subscription &other) const
Definition: msghandler.h:64
The structure of 1 queue entry (pointer to a message)
Definition: message.h:143
csHash< Subscription, msgtype > subscribers
Stores the hash of all subscribers and the message type they are subscribed to.
Definition: msghandler.h:193
Manages a iNetSubscriber watching a certain message type.
Definition: msghandler.h:47
virtual void SendMessage(MsgEntry *msg)
Allows subscribers to respond with new messages.
Definition: msghandler.h:153
MsgQueue * queue
Definition: msghandler.h:188
Subscription(iNetSubscriber *nSubscriber, uint32_t nFlags=0x01)
Constructor without a callback.
Definition: msghandler.h:58
This interface must be implemented by objects that want to receive network messages.
Definition: subscriber.h:37
virtual void Broadcast(MsgEntry *msg, broadcasttype scope=NetBase::BC_EVERYONEBUTSELF, int guildID=-1)
Send messages to many clients with one func call.
Definition: msghandler.h:157
csRefArray< MsgEntry > pendingMessages
Definition: msghandler.h:83
virtual void Multicast(MsgEntry *msg, csArray< PublishDestination > &multi, uint32_t except, float range)
Sends the given message me to all the clients in the list.
Definition: msghandler.h:176
This class acts as a base for client/server net classes.
Definition: netbase.h:116
This class collects data of a netclient.
Definition: client.h:95
uint8_t msgtype
Definition: message.h:56
void void Initialize(iObjectRegistry *object_reg)
A queue of smart pointers with locking facilties for multi-threading.
Definition: genrefqueue.h:39
NetBase::broadcasttype broadcasttype
import the broadcasttype
Definition: msghandler.h:150
This class is the client&#39;s and server&#39;s main interface for either sending network messages out or get...
Definition: msghandler.h:106
NetBase * netbase
Definition: msghandler.h:187
bool Flush()
Flush the connected output queue.
Definition: msghandler.h:184
This class holds the structure for guaranteed inbound ordering of certain message types...
Definition: msghandler.h:80
CS::Threading::ReadWriteMutex mutex
Protects subscribers.
Definition: msghandler.h:194
uint32_t flags
Additional flags for detecting if the subscriber should be notified.
Definition: msghandler.h:49
broadcasttype
Definition: netbase.h:241
csTicks GetPing()
Definition: msghandler.h:181