ReplicaNet and RNLobby  1
NATResolver.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 _NATSERVER_H_
13 #define _NATSERVER_H_
14 #include "RNPlatform/Inc/MessageHelper.h"
15 #include "RNPlatform/Inc/Encryption.h"
16 #include "RNPlatform/Inc/ThreadClass.h"
17 #include "RNPlatform/Inc/Thread.h"
18 #include "RNXPSockets/Inc/XPSocket.h"
19 #include "RNPlatform/Inc/SysTime.h"
20 #include <set>
21 #include <list>
22 
23 namespace RNReplicaNet
24 {
25 
26 namespace RNLobby
27 {
28 
31 class NATResolver : private ThreadClass , private Thread , private SysTime
32 {
33 public:
34  NATResolver();
35  virtual ~NATResolver();
36 
37  enum
38  {
39  kDefaultPort = 4002
40  };
41 
44  bool Start(void);
45 
48  bool Stop(void);
49 
51  void SetAutomaticThreadStop(const bool allow = true);
52 
55  void SetAutomaticHostSocketReads(const bool allow = false);
56 
60  void SetEncryptionKey(const void *data,const int length);
61 
64  bool BeginHosting(t_XPSocket *socket);
65 
67  // CallbackExternalAddressKnown() can be called.
77  bool BeginAdvertise(t_XPSocket *socket,void *userPointer,const bool isAdvertised,const int globalID,const int sessionID,const int nonceID,const bool externalHost = true,const XPAddress *optionalHost=0,const bool persistant = false);
78 
90  bool BeginResolve(t_XPSocket *socket,void *userPointer,const bool isAdvertised,const int globalID,const int sessionID,const int nonceID,const bool tryExternalHost = true,const XPAddress *optionalHost=0,const XPAddress *optionalFirstTargetTry=0);
91 
93  void ForgetSocket(const t_XPSocket *socket);
94 
96  void ForgetUserPointer(const void *userPointer);
97 
99  void ForgetResolve(t_XPSocket *socket,const bool isAdvertised,const int globalID,const int sessionID,const int nonceID);
100 
105  bool CallbackParsePacketData(t_XPSocket *socket,const XPAddress &addr,void *data,size_t length);
106 
109  void SetServer(const XPAddress &address);
110 
112  virtual void CallbackResolveFailed(t_XPSocket *socket,void *userPointer,const int titleID,const bool isAdvertised,const int globalID,const int sessionID,const int nonceID);
113 
116  virtual void CallbackResolveResult(t_XPSocket *socket,void *userPointer,const XPAddress &targetAddress,const int titleID,const bool isAdvertised,const int globalID,const int sessionID,const int nonceID);
117 
119  virtual void CallbackExternalAddressKnown(t_XPSocket *socket,void *userPointer,const XPAddress &externalAddress,const int titleID,const bool isAdvertised,const int globalID,const int sessionID,const int nonceID);
120 
121 private:
122  int ThreadEntry(void);
123 
124 protected:
125 
126 private:
127  class NATState;
128  class PerHostState;
129 
130  bool InsertState(NATState *state);
131  void DeleteState(NATState * &state);
132  bool PackageAndSendData(t_XPSocket *socket,const DynamicMessageHelper &message,const XPAddress &address);
133 
134  Encryption::Key mCryptoKey;
135  t_XPSocket *mHost;
136 
137  // Compare functors
138  struct ltSocket
139  {
140  bool operator()(const NATState *a,const NATState *b) const;
141  };
142 
143  struct ltIDs
144  {
145  bool operator()(const NATState *a,const NATState *b) const;
146  };
147 
148  struct ltIDsWithSocket
149  {
150  bool operator()(const NATState *a,const NATState *b) const;
151  };
152 
153  struct ltUserPointer
154  {
155  bool operator()(const NATState *a,const NATState *b) const;
156  };
157 
158  std::multiset<NATState *,ltSocket> mSetBySocket;
159  std::set<NATState *,ltIDs> mSetByIDs; // There must be only one for each ID setting. Used mostly for advertised resolves.
160  std::set<NATState *,ltIDsWithSocket> mSetByIDsWithSocket; // There must be only one for each ID setting.
161  std::multiset<NATState *,ltUserPointer> mSetByUserPointer;
162  std::list<NATState *> mListByOrder;
163 
164  bool BuildAndSendAdvertisePacket(DynamicMessageHelper &message,NATState *state,PerHostState &hostState);
165 
166  bool mIsEmpty;
167  SysTimeType mBecameEmptyAt;
168  bool mAllowAutoThreadStop;
169  bool mAllowAutoHostSocketReads;
170 
171  XPAddress mServerAddress;
172  bool mServerAddressSet;
173 };
174 
175 } // namespace RNLobby
176 
177 } // namespace RNReplicaNet
178 
179 #endif
bool BeginResolve(t_XPSocket *socket, void *userPointer, const bool isAdvertised, const int globalID, const int sessionID, const int nonceID, const bool tryExternalHost=true, const XPAddress *optionalHost=0, const XPAddress *optionalFirstTargetTry=0)
void SetAutomaticThreadStop(const bool allow=true)
When the NATResolver becomes inactive for a period of time this allows the thread to stop processing...
void SetServer(const XPAddress &address)
void SetEncryptionKey(const void *data, const int length)
Definition: Encryption.h:27
Definition: XPSocket.h:91
virtual void CallbackResolveResult(t_XPSocket *socket, void *userPointer, const XPAddress &targetAddress, const int titleID, const bool isAdvertised, const int globalID, const int sessionID, const int nonceID)
void ForgetUserPointer(const void *userPointer)
Forgets all sockets with the same userPointer.
void SetAutomaticHostSocketReads(const bool allow=false)
void ForgetResolve(t_XPSocket *socket, const bool isAdvertised, const int globalID, const int sessionID, const int nonceID)
Removes the specific resolve request.
Definition: SysTime.h:55
Definition: NATResolver.h:31
virtual void CallbackExternalAddressKnown(t_XPSocket *socket, void *userPointer, const XPAddress &externalAddress, const int titleID, const bool isAdvertised, const int globalID, const int sessionID, const int nonceID)
A virtual callback that is triggered when an advertised socket it told the external address from bein...
bool BeginAdvertise(t_XPSocket *socket, void *userPointer, const bool isAdvertised, const int globalID, const int sessionID, const int nonceID, const bool externalHost=true, const XPAddress *optionalHost=0, const bool persistant=false)
Advertises a socket with a set of identifying numbers (isAdvertised, globalID, nonceID) which should ...
bool BeginHosting(t_XPSocket *socket)
void ForgetSocket(const t_XPSocket *socket)
Removes the socket from the NATResolver index.
Definition: XPSocket.h:167
bool CallbackParsePacketData(t_XPSocket *socket, const XPAddress &addr, void *data, size_t length)
Definition: MessageHelper.h:211
virtual void CallbackResolveFailed(t_XPSocket *socket, void *userPointer, const int titleID, const bool isAdvertised, const int globalID, const int sessionID, const int nonceID)
A virtual callback that is triggered when a NAT resolve attempt fails.
Definition: ThreadClass.h:96
Definition: Thread.h:29