00001 /*************************************************************************** 00002 * The contents of this file are subject to the Mozilla Public * 00003 * License Version 1.1 (the "License"); you may not use this file * 00004 * except in compliance with the License. You may obtain a copy of * 00005 * the License at http://www.mozilla.org/MPL/ * 00006 * * 00007 * Software distributed under the License is distributed on an "AS * 00008 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * 00009 * implied. See the License for the specific language governing * 00010 * rights and limitations under the License. * 00011 * * 00012 * The Original Code is Game Network Framework (GaNeF). * 00013 * * 00014 * The Initial Developers of the Original Code are * 00015 * Lars Langer and Emanuel Greisen * 00016 * Copyright (C) 2005. Lars Langer & Emanuel Greisen * 00017 * All Rights Reserved. * 00018 * * 00019 * Contributor(s): * 00020 * none yet.... * 00021 * * 00022 ***************************************************************************/ 00023 #ifndef SERVERDATA_H 00024 #define SERVERDATA_H 00025 00026 #include <map> 00027 00028 #include <ptypes.h> 00029 00030 #include "serverpacket.h" 00031 #include "../common/frameworkdata.h" 00032 00033 template<class PacketType, class SocketType> 00034 class PendingAck; 00035 class ServerFramework; 00036 class Client; 00037 00038 /// @example testserver/testserversnakebite.h 00039 /** 00040 * @ingroup Server 00041 * @brief This is the interface implemented by all data residing on the server. 00042 * When your data-classes inherit from this class they must implement the fillXXX methods. This is how the packets used to update/construct a client version of the object are created. 00043 * 00044 * <p> 00045 * OPTIMIZE: we could enqueue the packet directly if there is only one receiver in the client-list 00046 * 00047 * @author Lars Langer & Emanuel Greisen 00048 */ 00049 class ServerData : public FrameworkData 00050 { 00051 private: 00052 ServerFramework * server_frame_work; 00053 const char _packet_priority; 00054 bool _packet_reliable; 00055 static unsigned int next_data_id; 00056 static mutex next_data_id_mutex; 00057 static unsigned int nextDataId(); 00058 00059 public: 00060 ServerData(ServerFramework * f, char packet_priority, bool packet_reliable); 00061 ~ServerData(); 00062 00063 public: 00064 /// Will send a create-object packet to all clients. 00065 void sendCreateObjectToAll() const; 00066 /// Creates and sends a create-object packet to a list of clients. 00067 void sendCreateObject(const std::map<unsigned int, Client *> & clientlist) const; 00068 void sendCreateObject( Client * client) const; 00069 00070 /// Will send an update packet to all clients. 00071 void sendUpdateToAll(unsigned char type, bool reliable) const; 00072 /// Creates and sends an update packet to a list of clients. 00073 void sendUpdate(const std::map<unsigned int, Client *> & clientlist, unsigned char type, bool reliable) const; 00074 void sendUpdate( Client * client, unsigned char type, bool reliable) const; 00075 00076 /// Will send a destroy/delete-object packet to all clients. 00077 void sendDestroyToAll() const; 00078 /// Create and sends a destroy-object packet to a list of clients. 00079 void sendDestroy(const std::map<unsigned int, Client *> & clientlist) const; 00080 void sendDestroy( Client * client ) const; 00081 00082 public: 00083 /// This must produce a unique id for this class, it will be used on the client side to construct new objects. 00084 virtual const unsigned int getClassId() const = 0; 00085 /// Here the data-class should fill in data according to update type `type`. 00086 virtual void fillUpdatePacket(ServerPacket * packet, unsigned char type) const = 0; 00087 /// Here the data-class should fill in all data required by the constructor on the client side. 00088 virtual void fillCreateObjectPacket(ServerPacket * packet) const = 0; 00089 /// Called when ever we receive a packet for this object from a client. 00090 virtual void clientPacket(Client * client, unsigned char type, ServerPacket * packet) = 0; 00091 }; 00092 00093 #endif