00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
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
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
00169 std::string mBroadcastURL;
00170 std::string mBroadcastData;
00171 std::map<int,XPAddress> mGotReplyFrom;
00172 int mBroadcastServerID;
00173 XPAddress mCurrentReply;
00174 int mCurrentNOnce;
00175 float mRequestListingTime;
00176 std::list<std::pair<float,int> > mRequestRulesTimes;
00177 std::list<std::pair<float,int> > mRequestPlayersTimes;
00178
00179 struct ServerInfo
00180 {
00181 ServerInfo();
00182
00183 void StartLatencyCalculate(void);
00184
00185
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;
00210 std::multimap<XPAddress,int> mServerIndexByAddress;
00211 std::list<int> mServerIDsPingFinished;
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 }
00228
00229 }
00230
00231 #endif