Eris 1.3.18
Metaserver.h
00001 // TODO: Copyright stuff
00002 
00003 #ifndef ERIS_METASERVER_H
00004 #define ERIS_METASERVER_H
00005 
00006 #include <Eris/Types.h>
00007 #include <Eris/ServerInfo.h>
00008 
00009 #include <Atlas/Objects/Decoder.h>
00010 
00011 #include <sigc++/trackable.h>
00012 #include <sigc++/signal.h>
00013 #include <memory>
00014 
00015 #ifndef __WIN32__
00016 // pull in uint32_t on POSIX - is this generic?!
00017 #include <stdint.h>
00018 #else
00019 // Apparently not. [MW]
00020 #ifndef _STDINT_H_
00021 #define _STDINT_H_
00022 
00023 typedef unsigned char uint8_t;
00024 typedef unsigned short uint16_t;
00025 typedef unsigned int uint32_t;
00026 
00027 #endif  // _STDINT_H_
00028 
00029 #endif // __WIN32__
00030 
00031 // Forward decls
00032 class udp_socket_stream;
00033 class basic_socket_stream;
00034         
00035 namespace Eris {
00036         
00037 // Forward Declerations
00038 class MetaQuery;
00039 class BaseConnection;
00040 class Timeout;
00041 class PollData;
00042         
00043 #ifndef uint32_t
00044         /* WIN32 hack ...
00045         this is only true for 32bit machines but WIN64 is far ahead !! */
00046 
00047         #ifdef WINDOWS  
00048         typedef unsigned int uint32_t;
00049         #endif
00050         
00051         #ifdef MACOS
00052         #include <Types.h>
00053         // MacOS defines these anyway
00054         typedef Uint32  uint32_t;
00055         #endif
00056 #endif
00057 
00058 const int DATA_BUFFER_SIZE = 4096;
00059 
00061 typedef std::list<ServerInfo> ServerList;
00062 
00064 class Meta : virtual public sigc::trackable,
00065                 public Atlas::Objects::ObjectsDecoder
00066 {
00067 public:
00068     typedef enum
00069     {
00070         INVALID = 0,    
00071         VALID,          
00072         GETTING_LIST,   
00073         QUERYING        
00074     } MetaStatus;
00075 
00088     Meta(const std::string &msv, unsigned int maxQueries);
00089     virtual ~Meta();
00090     
00092     unsigned int getGameServerCount() const;
00093 
00097     const ServerInfo& getInfoForServer(unsigned int index) const;
00098 
00100     void queryServerByIndex(unsigned int index);
00101 
00108     void refresh();
00109 
00114     void cancel();
00115 
00116 // accessors
00117     MetaStatus getStatus() const {
00118         return m_status;
00119     }
00120 // signals
00121         
00123     sigc::signal<void, const ServerInfo&> ReceivedServerInfo;
00124 
00129     sigc::signal<void, int> CompletedServerList;
00130     
00132     sigc::signal<void> AllQueriesDone;
00133 
00138     sigc::signal<void, const std::string&> Failure;
00139         
00140 protected:
00141     friend class MetaQuery;
00142                 
00143     virtual void objectArrived(const Atlas::Objects::Root& obj);
00144 
00145     void doFailure(const std::string &msg);
00146     void queryFailure(MetaQuery *q, const std::string& msg);
00147 
00148     void query();
00149     void queryTimeout(MetaQuery *q);
00150     void metaTimeout();
00151     
00154     void connect();
00155     
00157     void disconnect();
00158         
00159 private:
00161     void recv();
00162     
00164     void recvCmd(uint32_t op);
00165 
00167     void processCmd();
00168 
00171     void listReq(int offset = 0);
00172 
00173     void setupRecvCmd();
00174     void setupRecvData(int words, uint32_t got);
00175         
00176     void deleteQuery(MetaQuery* query);
00177         
00178     void internalQuery(unsigned int index);
00179         
00180     const std::string m_clientName;     
00181     
00182     MetaStatus m_status;
00184     const std::string m_metaHost;       
00185         
00186     typedef std::set<MetaQuery*> QuerySet;
00187     QuerySet m_activeQueries;
00188                 
00189     unsigned int m_maxActiveQueries;
00190     unsigned int m_nextQuery;
00191 
00192     typedef std::vector<ServerInfo> ServerInfoArray;
00193     ServerInfoArray m_gameServers,
00194         m_lastValidList;
00195 
00196     // storage for the Metaserver protocol
00197     udp_socket_stream* m_stream;
00198     
00199         char _data[DATA_BUFFER_SIZE];
00200         char* _dataPtr; 
00201 
00202         std::streamsize _bytesToRecv; 
00203         unsigned int _totalServers,             
00204                 _packed;                
00205                 
00206         bool _recvCmd;          
00207         uint32_t _gotCmd;       
00208         
00209     std::auto_ptr<Timeout> m_timeout;   
00210         
00211     void gotData(PollData&);
00212 };
00213         
00214 } // of namespace Eris
00215 
00216 #endif