00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef _CODESERVER_H_
00012 #define _CODESERVER_H_
00013 #include "RNPlatform/Inc/DLLExportAPI.h"
00014 #include "RNPlatform/Inc/RegistryManagerMap.h"
00015 #include "RNPlatform/Inc/Encryption.h"
00016 #include "RNPlatform/Inc/ThreadClass.h"
00017 #include "RNPlatform/Inc/Thread.h"
00018 #include "RNXPSockets/Inc/XPSockets.h"
00019
00020 namespace RNReplicaNet
00021 {
00022
00023 namespace RNLobby
00024 {
00025
00028 class CodeServer : private ThreadClass , private Thread
00029 {
00030 public:
00031 class CodeEntry
00032 {
00033 public:
00034 struct ThisLess
00035 {
00036 bool operator()(const std::string &s1, const std::string &s2) const
00037 {
00038 return strcmp(s1.c_str(), s2.c_str()) < 0;
00039 }
00040 };
00041 CodeEntry();
00042
00043
00044 XPAddress mLastAddress;
00045 float mLastTime;
00046 int mUserSuppliedReply;
00047 int mLastNOnce1,mLastNOnce2;
00048
00049
00050 std::string mTheCode;
00051 };
00052
00053 enum Reply
00054 {
00055 kReply_ValidKey = 0,
00056 kReply_UnspecifiedError,
00057 kReply_InvalidKey,
00058 kReply_KeyInUse,
00059 kReply_Timeout,
00060 kReply_CorruptReply
00061 };
00062 enum
00063 {
00064 kDefaultPort = 3002
00065 };
00066
00067
00068 CodeServer();
00069 virtual ~CodeServer();
00070
00076 bool ReadCodeFile(const char *filename, const bool duplicateCodeCheck = false);
00077
00084 bool AddCode(const char *code, const bool duplicateCodeCheck = false,const int userSuppliedReply = 0);
00085
00090 bool RemoveCode(const char *code = 0);
00091
00094 bool Start(const XPAddress &address = XPAddress(kDefaultPort));
00095
00099 bool Stop(bool hardware = false);
00100
00105 void SetEncryptionKey(const void *data,const int length);
00106
00110 void SetCodeTimeout(const float time = (60.0f * 30.0f));
00111
00114 float GetCodeTimeout(void) const;
00115
00121 virtual void CallbackDataReceived(void *data,int &length,const int maxLength,XPAddress &address) {}
00122
00125 virtual void CallbackVersion(const int version) {}
00126
00134 virtual void CallbackKeyData(char *keyData,const int maxLength,int &nOnce1,int &nOnce2,bool &updateDatabase) {}
00135
00141 virtual void CallbackQueryReply(Reply &reply,int &userSuppliedReply,int &nOnce1,int &nOnce2) {}
00142
00148 virtual void CallbackDataSend(void *data,int &length,const int maxLength,XPAddress &address) {}
00149
00152 virtual void CallbackQueryReplyTime(const float seconds) {}
00153
00155 int GetNumRequests(void) const;
00156
00158 int GetNumFailed(void) const;
00159
00160 private:
00161 int ThreadEntry(void);
00162
00163 protected:
00164 RegistryManagerMap<std::string,CodeEntry,CodeEntry::ThisLess> mCodes;
00165 MutexClass mCodesMutex;
00166 private:
00167 XPSocketUrgent *mListen;
00168
00169 Encryption::Key mCryptoKey;
00170 float mCodeTimeout;
00171 int mNumRequests;
00172 int mNumFailed;
00173 };
00174
00175 }
00176
00177 }
00178
00179 #endif