ReplicaNet and RNLobby  1
AdvertiseClient.h
1 /* START_LICENSE_HEADER
2 
3 Copyright (C) 2000 Martin Piper, original design and program code
4 Copyright (C) 2001 Replica Software
5 
6 This program file is copyright (C) Replica Software and can only be used under license.
7 For more information visit: http://www.replicanet.com/
8 Or email: info@replicanet.com
9 
10 END_LICENSE_HEADER */
11 #include "RNPlatform/Inc/MemoryTracking.h"
12 #ifndef _ADVERTISECLIENT_H_
13 #define _ADVERTISECLIENT_H_
14 #include "RNLobby/Inc/BaseTransportServer.h"
15 #include "RNXPSockets/Inc/XPSocket.h"
16 #include "RNLobby/Inc/UserClient.h"
17 #include <list>
18 
19 namespace RNReplicaNet
20 {
21 
22 namespace RNLobby
23 {
24 
27 {
28 public:
30  virtual ~AdvertiseClient();
31 
33  bool Start(void);
34 
36  bool StartLAN(void);
37 
40  void SetServer(const XPAddress &address);
41 
48  bool AdvertiseSession(const char *url,const char *userData = "");
49 
51  bool RulesChanged(void);
52 
54  bool PlayersChanged(void);
55 
58  bool StopAdvertiseSession(void);
59 
66  bool GetListings(const unsigned int maxResults = 10,const unsigned int offset = 0,const char *filter = 0);
67 
69  bool StopListings(void);
70 
72  bool RequestRules(const int serverIndex);
73 
75  bool RequestPlayers(const int serverIndex);
76 
79  bool RequestLatency(const int serverIndex);
80 
82  bool GetAnyActivePings(void) const;
83 
87  bool AddRule(const char *rule,const char *userData = "");
88 
94  bool AddPlayer(const char *name,const char *userData = "",const int loginID = UserInfo::kAnyLoginID);
95 
98  bool SendReliableMessage(const int serverIndex,const void *data,const int length);
99 
100 protected:
101 
103 
105  virtual void CallbackConnectionEstablished(Transport *transport) {}
106 
108  virtual void CallbackWantRules(void) {}
109 
111  virtual void CallbackWantPlayers(void) {}
112 
114  virtual void CallbackGotServerIndex(const int serverIndex) {}
115 
117  virtual void CallbackListingsBegin(void) {}
118 
126  virtual void CallbackListingsEntry(const int serverIndex,const int hostLoginID,const char *url, const char *userData, const float age, const XPAddress &address) {}
127 
129  virtual void CallbackListingsComplete(void) {}
130 
132  virtual void CallbackRulesBegin(const int serverIndex) {}
133 
138  virtual void CallbackRule(const int serverIndex,const char *rule, const char *userData) {}
139 
142  virtual void CallbackRulesComplete(const int serverIndex) {}
143 
146  virtual void CallbackPlayersBegin(const int serverIndex) {}
147 
153  virtual void CallbackPlayer(const int serverIndex,const char *name, const char *userData, const int loginID) {}
154 
157  virtual void CallbackPlayersComplete(const int serverIndex) {}
158 
163  virtual void CallbackLatencyTo(const int serverIndex,const float latency,const float packetLossPercent) {}
164 
169  virtual void CallbackMessageReceived(const int sourceLoginID,const void *data,const int length) {}
170 
171 protected:
172  // These callbacks from BaseTransportServer are implemented by this class. If they are implemented by the user this implementation must be called.
173  void CallbackTidy(void);
174 
175  bool CallbackPoll(void);
176 
177  bool CallbackParseValidMessage(Transport *transport,DynamicMessageHelper &message,const size_t length);
178 
179  XPAddress mServerAddress;
180 
181  bool mIsAdvertising;
182 
183  std::string mCurrentURL;
184  std::string mCurrentUserData;
185 
186  bool mDoingListing;
187 
188  bool mWantRules;
189  bool mDoingRules;
190 
191  bool mWantPlayers;
192  bool mDoingPlayers;
193 
194  XPSocketUrgent *mBroadcast;
195 // int mSerial;
196  std::string mBroadcastURL; // Protected by the mutex
197  std::string mBroadcastData; // Protected by the mutex
198  std::map<int,XPAddress> mGotReplyFrom; // Protected by the mutex
199  int mBroadcastServerID;
200  XPAddress mCurrentReply;
201  int mCurrentNOnce;
202  SysTimeType mRequestListingTime;
203  std::list<std::pair<SysTimeType,int> > mRequestRulesTimes; // Protected by the mutex
204  std::list<std::pair<SysTimeType,int> > mRequestPlayersTimes; // Protected by the mutex
205 
206  struct ServerInfo
207  {
208  ServerInfo();
209 
210  void StartLatencyCalculate(void);
211 
212  // If the latency has not been calculated then it returns a value less than 0.0f
213  float GetLatency(void);
214 
215  float GetPacketLoss(void);
216 
217  std::string mURL;
218  std::string mData;
219  int mLoginID;
220  XPAddress mExternalIP;
221  bool mWantPing;
222  int mPingTries;
223  SysTimeType mLastPingSentTime;
224  size_t mPacketsLost;
225  int mReplies;
226  SysTimeType mTotalRoundTripTime;
227  bool mPingAcked;
228  };
229 
230  class PingServer : public ThreadClass , public Thread
231  {
232  public:
233  PingServer();
234  ~PingServer();
235 
236  std::map<int,ServerInfo> mLocalServerInfo; // Protected by the mutex
237  std::multimap<XPAddress,int> mServerIndexByAddress; // Protected by the mutex
238  std::list<int> mServerIDsPingFinished; // Protected by the mutex
239  bool mAnyActivePings;
240 
241  int ThreadEntry(void);
242 
243  private:
244  void ThreadExiting(const int returnCode);
245 
246  t_XPSocket *mPingSocket;
247  bool mAPingSent;
248  };
249 
250  PingServer mPingServer;
251 
252  bool mServerAddressSet;
253 };
254 
255 } // namespace RNLobby
256 
257 } // namespace RNReplicaNet
258 
259 #endif
virtual void CallbackRulesComplete(const int serverIndex)
Definition: AdvertiseClient.h:142
virtual void CallbackLatencyTo(const int serverIndex, const float latency, const float packetLossPercent)
Definition: AdvertiseClient.h:163
Definition: Transport.h:35
bool SendReliableMessage(const int serverIndex, const void *data, const int length)
A server base class that handles encrypted messages to and from a transport.
Definition: BaseTransportServer.h:30
virtual void CallbackWantRules(void)
This is called by AdvertiseClient when the user needs to supply rules for the currently advertised se...
Definition: AdvertiseClient.h:108
void SetServer(const XPAddress &address)
Definition: XPSocket.h:91
bool StartLAN(void)
This starts this class to only advertise and find sessions on the LAN.
bool AddPlayer(const char *name, const char *userData="", const int loginID=UserInfo::kAnyLoginID)
virtual void CallbackListingsBegin(void)
This is called at the start when the listings are refreshed.
Definition: AdvertiseClient.h:117
virtual void CallbackListingsEntry(const int serverIndex, const int hostLoginID, const char *url, const char *userData, const float age, const XPAddress &address)
Definition: AdvertiseClient.h:126
bool RulesChanged(void)
The user may call this notify the server that the session rules have changed. This then causes a new ...
bool RequestPlayers(const int serverIndex)
Request the players for a specific server. CallbackPlayersBegin etc will be called when the players h...
virtual void CallbackListingsComplete(void)
This is called when there are no more listings.
Definition: AdvertiseClient.h:129
virtual void CallbackConnectionEstablished(Transport *transport)
User extensible callbacks.
Definition: AdvertiseClient.h:105
bool RequestRules(const int serverIndex)
Request the rules for a specific server. CallbackRulesBegin etc will be called when the rules have be...
virtual void CallbackMessageReceived(const int sourceLoginID, const void *data, const int length)
Definition: AdvertiseClient.h:169
bool StopListings(void)
Stop the current request for listings.
virtual void CallbackGotServerIndex(const int serverIndex)
This is called when the server index is known for the currently advertised server.
Definition: AdvertiseClient.h:114
Maintains a connection to the AdvertiseServer.
Definition: AdvertiseClient.h:26
virtual void CallbackRule(const int serverIndex, const char *rule, const char *userData)
Definition: AdvertiseClient.h:138
bool CallbackParseValidMessage(Transport *transport, DynamicMessageHelper &message, const size_t length)
bool Start(void)
Because this uses a TransportUDP connection it will call XPURL::RegisterDefaultTransports() ...
virtual void CallbackRulesBegin(const int serverIndex)
This is called the start when the rules for a server index are updated.
Definition: AdvertiseClient.h:132
Definition: XPSocket.h:167
bool RequestLatency(const int serverIndex)
bool PlayersChanged(void)
The user may call this notify the server that the session players list has changed. This then causes a new players list to be requested by CallbackWantPlayers.
Definition: MessageHelper.h:211
bool AdvertiseSession(const char *url, const char *userData="")
virtual void CallbackPlayer(const int serverIndex, const char *name, const char *userData, const int loginID)
Definition: AdvertiseClient.h:153
bool GetListings(const unsigned int maxResults=10, const unsigned int offset=0, const char *filter=0)
virtual void CallbackPlayersComplete(const int serverIndex)
Definition: AdvertiseClient.h:157
bool GetAnyActivePings(void) const
Returns true if there are any pings active.
virtual void CallbackPlayersBegin(const int serverIndex)
Definition: AdvertiseClient.h:146
bool AddRule(const char *rule, const char *userData="")
Definition: XPSocketUrgentClass.h:26
virtual void CallbackWantPlayers(void)
This is called by AdvertiseClient when the user needs to supply players for the currently advertised ...
Definition: AdvertiseClient.h:111
Definition: ThreadClass.h:96
Definition: Thread.h:29