00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00041
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
00058
00059
00060
00061
00062
00063
00064
00065 class ServerFramework : public Framework<ServerPacket,ipmsgserver>
00066 {
00067 friend class ServerData;
00068
00069 private:
00070 unsigned int connection_timeout_length;
00071
00072 std::map<unsigned int, Client *> clients;
00073
00074 std::map<unsigned int, Client *> connected_clients;
00075 typedef std::map<unsigned int, Client *>::const_iterator ConstClientIterator;
00076
00077
00078
00079 std::map<unsigned int, ServerData *> server_data;
00080
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:
00091 void registerClient(Client * client);
00092
00093 void unregisterClient(Client * client);
00094
00095 Client * getClientFromPacket(ServerPacket * packet);
00096
00097 void registerData(ServerData * data);
00098
00099 void unregisterData(FrameworkData * data);
00100
00101 public:
00102
00103 ServerData * getDataObject(unsigned int objid);
00104
00105 void registerPreSendingChain(const std::string & chainname, const std::vector<Plugin<ServerPacket> *> & stations);
00106
00107 void setPreSendingChain(const std::string &chainname);
00108
00109 void setDefaultPreSendingChain( );
00110
00111 void purgeCurrentPreSendingChain();
00112
00113 inline void setConnectionTimeout(unsigned int ct) { connection_timeout_length = ct; };
00114
00115 private:
00116
00117 void handleSystemPacket(ServerPacket * p);
00118 public:
00119
00120 virtual void keepAlive(unsigned int sleepmillis = 0);
00121
00122
00123 virtual bool isPacketReadyForDelivery(ServerPacket * packet);
00124
00125
00126 virtual ServerPacket * deliverPacket(ServerPacket * packet);
00127
00128 virtual void addPendingAck(ServerPacket * packet, bool haslock);
00129
00130 virtual unsigned int bufferedPackets() const;
00131
00132
00133
00134 virtual void sentReliablePacket(ServerPacket * packet);
00135
00136 public:
00137
00138 inline std::map<unsigned int, Client *> & getAllClients() { return connected_clients; };
00139
00140 inline const std::map<unsigned int, ServerData *> & getServerDataObjects() const { return server_data; };
00141
00142 public:
00143
00144 void enqueuePacket(ServerPacket * packet, const std::map< unsigned int, Client * > & clientlist );
00145
00146 void enqueuePacket(ServerPacket * packet, Client * client );
00147
00148 public:
00149 void start();
00150
00151 protected:
00152
00153
00154 virtual bool onInitialHandshake(ServerPacket * handshake, ServerPacket * reply) = 0;
00155
00156 virtual void onClientConnected(Client * client) = 0;
00157
00158 virtual void onClientDisconnected(Client * client) = 0;
00159
00160 virtual Client * createClient(const ipaddress &addr, int port) = 0;
00161
00162 virtual void addPluginSequenceNumber( ServerPacket * packet);
00163
00164 };
00165
00166 #endif