00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef _NATSERVER_H_
00012 #define _NATSERVER_H_
00013 #include "RNPlatform/Inc/DLLExportAPI.h"
00014 #include "RNPlatform/Inc/MessageHelper.h"
00015 #include "RNPlatform/Inc/Encryption.h"
00016 #include "RNPlatform/Inc/ThreadClass.h"
00017 #include "RNPlatform/Inc/Thread.h"
00018 #include "RNXPSockets/Inc/XPSocket.h"
00019 #include "RNPlatform/Inc/SysTime.h"
00020 #include <set>
00021 #include <list>
00022
00023 namespace RNReplicaNet
00024 {
00025
00026 namespace RNLobby
00027 {
00028
00031 class NATResolver : private ThreadClass , private Thread , private SysTime
00032 {
00033 public:
00034 NATResolver();
00035 virtual ~NATResolver();
00036
00037 enum
00038 {
00039 kDefaultPort = 3001
00040 };
00041
00044 bool Start(void);
00045
00048 bool Stop(void);
00049
00051 void SetAutomaticThreadStop(const bool allow = true);
00052
00055 void SetAutomaticHostSocketReads(const bool allow = false);
00056
00060 void SetEncryptionKey(const void *data,const int length);
00061
00063 bool BeginHosting(t_XPSocket *socket);
00064
00066 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);
00067
00069 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);
00070
00071 void ForgetSocket(const t_XPSocket *socket);
00072
00073 void ForgetUserPointer(const void *userPointer);
00074
00076 void ForgetResolve(t_XPSocket *socket,const bool isAdvertised,const int globalID,const int sessionID,const int nonceID);
00077
00082 bool CallbackParsePacketData(t_XPSocket *socket,const XPAddress &addr,void *data,size_t length);
00083
00086 void SetServer(const XPAddress &address);
00087
00088 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);
00089
00091 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);
00092
00093 private:
00094 int ThreadEntry(void);
00095
00096 protected:
00097
00098 private:
00099 class NATState;
00100 class PerHostState;
00101
00102 bool InsertState(NATState *state);
00103 void DeleteState(NATState * &state);
00104 bool PackageAndSendData(t_XPSocket *socket,const DynamicMessageHelper &message,const XPAddress &address);
00105
00106 Encryption::Key mCryptoKey;
00107 t_XPSocket *mHost;
00108
00109
00110 struct ltSocket
00111 {
00112 bool operator()(const NATState *a,const NATState *b) const;
00113 };
00114
00115 struct ltIDs
00116 {
00117 bool operator()(const NATState *a,const NATState *b) const;
00118 };
00119
00120 struct ltIDsWithSocket
00121 {
00122 bool operator()(const NATState *a,const NATState *b) const;
00123 };
00124
00125 struct ltUserPointer
00126 {
00127 bool operator()(const NATState *a,const NATState *b) const;
00128 };
00129
00130 std::multiset<NATState *,ltSocket> mSetBySocket;
00131 std::set<NATState *,ltIDs> mSetByIDs;
00132 std::set<NATState *,ltIDsWithSocket> mSetByIDsWithSocket;
00133 std::multiset<NATState *,ltUserPointer> mSetByUserPointer;
00134 std::list<NATState *> mListByOrder;
00135
00136 bool BuildAndSendAdvertisePacket(DynamicMessageHelper &message,NATState *state,PerHostState &hostState);
00137
00138 bool mIsEmpty;
00139 float mBecameEmptyAt;
00140 bool mAllowAutoThreadStop;
00141 bool mAllowAutoHostSocketReads;
00142
00143 XPAddress mServerAddress;
00144 bool mServerAddressSet;
00145 };
00146
00147 }
00148
00149 }
00150
00151 #endif