Planeshift
|
00001 /* 00002 * msghandler.h by Matze Braun <[email protected]> 00003 * 00004 * Copyright (C) 2001 Atomic Blue ([email protected], http://www.atomicblue.org) 00005 * 00006 * 00007 * This program is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU General Public License 00009 * as published by the Free Software Foundation (version 2 of the License) 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 * 00018 */ 00019 #ifndef __MSGHANDLER_H__ 00020 #define __MSGHANDLER_H__ 00021 00022 #include <csutil/parray.h> 00023 #include <csutil/refcount.h> 00024 #include <csutil/threading/thread.h> 00025 #include <csutil/threading/rwmutex.h> 00026 00027 #include "net/message.h" 00028 #include "net/netbase.h" 00029 00030 // forward decls 00031 struct iNetSubscriber; 00032 class NetBase; 00033 class Client; 00034 00039 //----------------------------------------------------------------------------- 00040 00041 00047 struct Subscription 00048 { 00049 uint32_t flags; 00050 iNetSubscriber* subscriber; 00058 Subscription(iNetSubscriber *nSubscriber, uint32_t nFlags = 0x01/*REQUIRE_READY_CLIENT*/) 00059 : flags(nFlags), subscriber(nSubscriber) 00060 { 00061 } 00062 00063 // comparison operator required for usage in csHash 00064 bool operator<(const Subscription& other) const 00065 { 00066 return subscriber < other.subscriber; 00067 } 00068 }; 00069 00070 00071 //----------------------------------------------------------------------------- 00072 00073 00080 struct OrderedMessageChannel 00081 { 00082 int nextSequenceNumber; 00083 csRefArray<MsgEntry> pendingMessages; 00084 00085 OrderedMessageChannel() : nextSequenceNumber(0) { } 00086 00087 int GetCurrentSequenceNumber() { return nextSequenceNumber; } 00088 00089 int IncrementSequenceNumber() 00090 { 00091 nextSequenceNumber++; 00092 if (nextSequenceNumber > 63) // must be clamped to 6 bit value 00093 nextSequenceNumber = 1; // can't use zero because that means unsequenced 00094 00095 return nextSequenceNumber; 00096 } 00097 }; 00098 00099 //----------------------------------------------------------------------------- 00100 00106 class MsgHandler : public csRefCount 00107 { 00108 public: 00109 MsgHandler(); 00110 virtual ~MsgHandler(); 00111 00113 bool Initialize(NetBase *nb, int queuelen = 500); 00114 00125 virtual void Subscribe(iNetSubscriber *subscriber, msgtype type, uint32_t flags = 0x01/*REQUIRE_READY_CLIENT*/); 00126 00136 virtual bool Unsubscribe(iNetSubscriber *subscriber , msgtype type); 00137 00144 virtual bool UnsubscribeAll(iNetSubscriber *subscriber); 00145 00147 void Publish(MsgEntry *msg); 00148 00150 typedef NetBase::broadcasttype broadcasttype; 00151 00153 virtual void SendMessage(MsgEntry *msg) 00154 { netbase->SendMessage(msg); } 00155 00157 virtual void Broadcast(MsgEntry* msg, broadcasttype scope = NetBase::BC_EVERYONEBUTSELF, int guildID=-1) 00158 { netbase->Broadcast(msg, scope, guildID); } 00159 00176 virtual void Multicast(MsgEntry* msg, csArray<PublishDestination>& multi, uint32_t except, float range) 00177 { netbase->Multicast(msg, multi, except, range); } 00178 00179 void AddToLocalQueue(MsgEntry *me) { netbase->QueueMessage(me); } 00180 00181 csTicks GetPing() { return netbase->GetPing(); } 00182 00184 bool Flush() { return netbase->Flush(queue); } 00185 00186 protected: 00187 NetBase *netbase; 00188 MsgQueue *queue; 00189 00193 csHash<Subscription, msgtype> subscribers; 00194 CS::Threading::ReadWriteMutex mutex; 00195 }; 00196 00199 #endif 00200