AdvertiseClient.h

00001 /* START_LICENSE_HEADER
00002 
00003 Copyright (C) 2000 Martin Piper, original design and program code
00004 Copyright (C) 2001 Replica Software
00005 
00006 This program file is copyright (C) Replica Software and can only be used under license.
00007 For more information visit: http://www.replicanet.com/
00008 Or email: info@replicanet.com
00009 
00010 END_LICENSE_HEADER */
00011 #ifndef _ADVERTISECLIENT_H_
00012 #define _ADVERTISECLIENT_H_
00013 #include "RNPlatform/Inc/DLLExportAPI.h"
00014 #include "RNLobby/Inc/BaseTransportServer.h"
00015 #include "RNXPSockets/Inc/XPSocket.h"
00016 #include "RNLobby/Inc/UserClient.h"
00017 #include <list>
00018 
00019 namespace RNReplicaNet
00020 {
00021 
00022 namespace RNLobby
00023 {
00024 
00026 class AdvertiseClient : public BaseTransportServer
00027 {
00028 public:
00029     AdvertiseClient();
00030     virtual ~AdvertiseClient();
00031 
00033     bool Start(void);
00034 
00036     bool StartLAN(void);
00037 
00040     void SetServer(const XPAddress &address);
00041 
00047     bool AdvertiseSession(const char *url,const char *userData = "");
00048 
00050     bool RulesChanged(void);
00051 
00053     bool PlayersChanged(void);
00054 
00056     bool StopAdvertiseSession(void);
00057 
00061     bool GetListings(const size_t maxResults = 10,const size_t offset = 0,const char *filter = 0);
00062 
00064     bool StopListings(void);
00065 
00067     bool RequestRules(const int serverIndex);
00068 
00070     bool RequestPlayers(const int serverIndex);
00071 
00074     bool RequestLatency(const int serverIndex);
00075 
00077     bool GetAnyActivePings(void) const;
00078 
00080     bool AddRule(const char *rule,const char *userData = "");
00081 
00085     bool AddPlayer(const char *name,const char *userData = "",const int loginID = UserInfo::kAnyLoginID);
00086 
00089     bool SendReliableMessage(const int serverIndex,const void *data,const int length);
00090 
00091 protected:
00092 
00094 
00096     virtual void CallbackConnectionEstablished(Transport *transport) {}
00097 
00099     virtual void CallbackWantRules(void) {}
00100 
00102     virtual void CallbackWantPlayers(void) {}
00103 
00105     virtual void CallbackGotServerIndex(const int serverIndex) {}
00106 
00108     virtual void CallbackListingsBegin(void) {}
00109 
00111     virtual void CallbackListingsEntry(const int serverIndex,const int hostLoginID,const char *url, const char *userData, const float age, const XPAddress &address) {}
00112 
00114     virtual void CallbackListingsComplete(void) {}
00115 
00117     virtual void CallbackRulesBegin(const int serverIndex) {}
00118 
00120     virtual void CallbackRule(const int serverIndex,const char *rule, const char *userData) {}
00121 
00123     virtual void CallbackRulesComplete(const int serverIndex) {}
00124 
00126     virtual void CallbackPlayersBegin(const int serverIndex) {}
00127 
00129     virtual void CallbackPlayer(const int serverIndex,const char *name, const char *userData, const int loginID) {}
00130 
00132     virtual void CallbackPlayersComplete(const int serverIndex) {}
00133 
00137     virtual void CallbackLatencyTo(const int serverIndex,const float latency,const float packetLossPercent) {}
00138 
00142     virtual void CallbackMessageReceived(const int sourceLoginID,const void *data,const int length) {}
00143 
00144 protected:
00145     // These callbacks from BaseTransportServer are implemented by this class. If they are implemented by the user this implementation must be called.
00146     void CallbackTidy(void);
00147 
00148     bool CallbackPoll(void);
00149 
00150     bool CallbackParseValidMessage(Transport *transport,DynamicMessageHelper &message,const size_t length);
00151 
00152     XPAddress mServerAddress;
00153 
00154     bool mIsAdvertising;
00155 
00156     std::string mCurrentURL;
00157     std::string mCurrentUserData;
00158 
00159     bool mDoingListing;
00160 
00161     bool mWantRules;
00162     bool mDoingRules;
00163 
00164     bool mWantPlayers;
00165     bool mDoingPlayers;
00166 
00167     XPSocketUrgent *mBroadcast;
00168 //  int mSerial;
00169     std::string mBroadcastURL;                      // Protected by the mutex
00170     std::string mBroadcastData;                     // Protected by the mutex
00171     std::map<int,XPAddress> mGotReplyFrom;          // Protected by the mutex
00172     int mBroadcastServerID;
00173     XPAddress mCurrentReply;
00174     int mCurrentNOnce;
00175     float mRequestListingTime;
00176     std::list<std::pair<float,int> > mRequestRulesTimes;    // Protected by the mutex
00177     std::list<std::pair<float,int> > mRequestPlayersTimes;  // Protected by the mutex
00178 
00179     struct ServerInfo
00180     {
00181         ServerInfo();
00182 
00183         void StartLatencyCalculate(void);
00184 
00185         // If the latency has not been calculated then it returns a value less than 0.0f
00186         float GetLatency(void);
00187 
00188         float GetPacketLoss(void);
00189 
00190         std::string mURL;
00191         std::string mData;
00192         int mLoginID;
00193         XPAddress mExternalIP;
00194         bool mWantPing;
00195         size_t mPingTries;
00196         float mLastPingSentTime;
00197         size_t mPacketsLost;
00198         size_t mReplies;
00199         float mTotalRoundTripTime;
00200         bool mPingAcked;
00201     };
00202 
00203     class PingServer : public ThreadClass , public Thread
00204     {
00205     public:
00206         PingServer();
00207         ~PingServer();
00208 
00209         std::map<int,ServerInfo> mLocalServerInfo;      // Protected by the mutex
00210         std::multimap<XPAddress,int> mServerIndexByAddress; // Protected by the mutex
00211         std::list<int> mServerIDsPingFinished;          // Protected by the mutex
00212         bool mAnyActivePings;
00213 
00214         int ThreadEntry(void);
00215 
00216     private:
00217         void ThreadExiting(const int returnCode);
00218 
00219         t_XPSocket *mPingSocket;
00220     };
00221 
00222     PingServer mPingServer;
00223 
00224     bool mServerAddressSet;
00225 };
00226 
00227 } // namespace RNLobby
00228 
00229 } // namespace RNReplicaNet
00230 
00231 #endif

Generated on Sat Jun 28 22:02:16 2008 for ReplicaNet and RNLobby by  doxygen 1.5.3