Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | Related Pages | Examples

serverframework.h

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 SERVERFRAMEWORK_H
00024 #define SERVERFRAMEWORK_H
00025 
00026 #include <ptypes.h>
00027 
00028 #include <vector>
00029 #include <map>
00030 
00031 #include "../common/framework.h"
00032 #include "serverdata.h"
00033 #include "client.h"
00034 #include "serverpacket.h"
00035 #include "../common/presendingchain.h"
00036 #include "../common/plugin.h"
00037 
00038 
00039 /**
00040  * @ingroup Server
00041  * @brief This struct is used to delete all messages for a certain client (when that client disconnects).
00042  */
00043 struct QueueDeletor
00044 {
00045    Client * client;
00046    QueueDeletor(Client * client) : client(client){};
00047    bool operator() (const ServerPacket * packet) const
00048    {
00049       return packet->peer.host == client->getAddress() && packet->peer.port == client->getPort();
00050    }
00051    bool operator() (const unsigned int user_id) const
00052    {
00053       return client->getId() == user_id;
00054    }
00055 };
00056 
00057 /// @example testserver/testserverframework.h
00058 /**
00059  * @ingroup Server
00060  * @brief This class is the framework on the server.
00061  * it contains a list of connected clients,  a sender, a reciever, pending-ack-list and all the data used to represent the gameworld.
00062  *
00063  * @author Lars Langer and Emanuel Greisen
00064  */
00065 class ServerFramework : public Framework<ServerPacket,ipmsgserver>
00066 {
00067 friend class ServerData;
00068 
00069 private:
00070    unsigned int connection_timeout_length;
00071    /// All the clients currently talking to the server.
00072    std::map<unsigned int, Client *> clients;
00073    /// All clients really connected to the server.
00074    std::map<unsigned int, Client *> connected_clients;
00075    typedef std::map<unsigned int, Client *>::const_iterator ConstClientIterator;
00076    /// The socket used to send packets.
00077    //ost::UDPSocket socket_send;
00078    /// Contains all the data on the server.
00079    std::map<unsigned int, ServerData *> server_data;
00080    /// Contains a list of pre-sending plugin-chains
00081    std::map<std::string, PreSendingChain<ServerPacket> *> pre_sending_chains;
00082    PreSendingChain<ServerPacket> * current_pre_send_chain;
00083    PreSendingChain<ServerPacket> * default_pre_send_chain;
00084 
00085 
00086 public:
00087    ServerFramework(int port);
00088    ~ServerFramework();
00089 
00090 private: // Should be private
00091    void registerClient(Client * client);
00092    /// Will be called when a new client initiates a connection with the server.
00093    void unregisterClient(Client * client);
00094    /// This method will look up the client based on sender infomation in the packet.
00095    Client * getClientFromPacket(ServerPacket * packet);
00096    /// Insert an object of type ServerData to the framework.
00097    void registerData(ServerData * data);
00098    /// Will unregister an object of type ServerData from the framework.
00099    void unregisterData(FrameworkData * data);
00100 
00101 public:
00102    /// Return the object with the given id, or NULL if none exists.
00103    ServerData * getDataObject(unsigned int objid);
00104    /// Register a chain of plugins used to process packets before they are added in the outqueue.
00105    void registerPreSendingChain(const std::string & chainname, const std::vector<Plugin<ServerPacket> *> & stations);
00106    /// Set the current chain of plugins.
00107    void setPreSendingChain(const std::string &chainname);
00108    /// Set the current chain of plugins to the default (no plugins used, strait to the outqueue).
00109    void setDefaultPreSendingChain( );
00110    /// This method will "purge" the active presending chain.
00111    void purgeCurrentPreSendingChain();
00112    /// Change the number of milliseconds that the server/clients will use to determine if a silent disconnect has occured.
00113    inline void setConnectionTimeout(unsigned int ct) { connection_timeout_length = ct; };
00114 
00115 private:
00116    /// This method should handle system packets.
00117    void handleSystemPacket(ServerPacket * p);
00118 public:
00119    /// This will handle any incomming packets, along with other keep-alive-stuff.
00120    virtual void keepAlive(unsigned int sleepmillis = 0);
00121    /// This method will test if a packet is in order,
00122    /// and if not buffer the packet for later delivery.
00123    virtual bool isPacketReadyForDelivery(ServerPacket * packet);
00124    /// This method delivers all packets, and returns any
00125    /// buffered packets waiting for the packet.
00126    virtual ServerPacket * deliverPacket(ServerPacket * packet);
00127    /// Called when we want to insert a pending ack into the pendingacklist.
00128    virtual void addPendingAck(ServerPacket * packet, bool haslock);
00129    /// Returns total count of packets buffered on all clients, due to deliver-in-order.
00130    virtual unsigned int bufferedPackets() const;
00131    /// This method will be called by the sender when ever it successfully sends a
00132    /// reliable packet to the socket, we will look up the client and register this
00133    /// on him/her.
00134    virtual void sentReliablePacket(ServerPacket * packet);
00135 
00136 public:
00137    /// Get all the well registered clients
00138    inline std::map<unsigned int, Client *> & getAllClients() { return connected_clients; };
00139    /// Get all server data objects.
00140    inline const std::map<unsigned int, ServerData *> & getServerDataObjects() const { return server_data; };
00141 
00142 public:
00143    /// This method will copy the packet and enqueue a copy for each client.
00144    void enqueuePacket(ServerPacket * packet, const std::map< unsigned int, Client * > & clientlist );
00145    /// This method will just enqueue the packet for this single client.
00146    void enqueuePacket(ServerPacket * packet, Client * client );
00147 
00148 public:
00149    void start();
00150 
00151 protected: // These method should be implemented on the GameServer
00152    /// This method should write it's reply in the reply-packet, and return true
00153    /// or false depending on wether it want's us to continue the handshaking.
00154    virtual bool onInitialHandshake(ServerPacket * handshake, ServerPacket * reply) = 0;
00155    /// This method will be invoked when a new client has passed the handshaking-phase.
00156    virtual void onClientConnected(Client * client) = 0;
00157    /// This method will be invoked when a client has timed out.
00158    virtual void onClientDisconnected(Client * client) = 0;
00159    /// This method should return some dirivative of the Client-class.
00160    virtual Client * createClient(const ipaddress &addr, int port) = 0;
00161    /// This method should add a sequence number to the pluginpacket if it does not already have one.
00162    virtual void addPluginSequenceNumber( ServerPacket * packet);
00163 
00164 };
00165 
00166 #endif

Generated on Mon Feb 6 12:24:50 2006 for Ganef by  doxygen 1.4.4