
00001 #include "ClientRegister.h" 00002 namespace SraNetwork { 00003 00004 ClientRegister* SraNetwork::ClientRegister::instance = new ClientRegister(); 00005 00006 ClientRegister::ClientRegister(void) 00007 { 00008 //Initalize db connection 00009 //SraData::DbConnectionFactory factory( "tcp://127.0.0.1:3306", "root", "SRA_Dev", "sra_dev"); 00010 nextID = 42; 00011 } 00012 00013 ClientRegister::~ClientRegister(void) 00014 { 00015 } 00016 00017 ClientRegister* ClientRegister::getInstance() 00018 { 00019 return instance; 00020 } 00021 00022 void ClientRegister::addClient( SystemAddress address, SraClientData client) 00023 { 00024 printf("New client !\n"); 00025 SystemAddress s = address; 00026 tbb::concurrent_hash_map<SystemAddress, SraClientData, SystemAddressCompare>::accessor acc; 00027 clientAddressRegister.insert(acc,s); 00028 acc->second = client; 00029 } 00030 00031 const SystemAddress* ClientRegister::getAddressFromClientID( const int clientID) 00032 { 00033 tbb::concurrent_hash_map<SystemAddress, SraClientData, SystemAddressCompare>::const_iterator it; 00034 for ( it = clientAddressRegister.begin(); it != clientAddressRegister.end(); ++it) 00035 { 00036 if ( it->second.clientID == clientID ) 00037 { 00038 return &it->first; 00039 } 00040 } 00041 return NULL; 00042 } 00043 const SraClientData* ClientRegister::getClientFromAddress( SystemAddress address) 00044 { 00045 tbb::concurrent_hash_map<SystemAddress, SraClientData, SystemAddressCompare>::const_accessor acc; 00046 clientAddressRegister.find(acc,address); 00047 return &(acc->second); 00048 } 00049 00050 const SystemAddress* ClientRegister::getAddressFromClient( const SraClientData* client) 00051 { 00052 tbb::concurrent_hash_map<SystemAddress, SraClientData, SystemAddressCompare>::const_iterator it; 00053 for ( it = clientAddressRegister.begin(); it != clientAddressRegister.end(); ++it) 00054 { 00055 if ( it->second == *client ) 00056 { 00057 return &it->first; 00058 } 00059 } 00060 return NULL; 00061 } 00062 00063 00064 void ClientRegister::removeClient(const SystemAddress address) 00065 { 00066 tbb::concurrent_hash_map<SystemAddress, SraClientData, SystemAddressCompare>::accessor acc; 00067 if (clientAddressRegister.find(acc, address) ) { 00068 clientAddressRegister.erase(acc); 00069 } 00070 } 00071 00072 void ClientRegister::removeClient( const SraClientData* data) 00073 { 00074 const SystemAddress* addr = getAddressFromClient(data); 00075 if (addr != NULL) 00076 { 00077 removeClient(*addr); 00078 } 00079 } 00080 00081 int ClientRegister::loginClient( wchar_t* data, SystemAddress address, int userLen) 00082 { 00083 if (userLen >= PACKAGE_DATA_SIZE) 00084 { 00085 printf("Client login corrupted, length of user name >= PACKAGE_DATA_SIZE!\n"); 00086 return -1; 00087 } 00088 //Split user & pw 00089 wchar_t *user = new wchar_t[userLen+1]; 00090 //Since we have used a 00091 wcsncpy_s(user, userLen+1, data, _TRUNCATE); 00092 int rLen = wcslen(data)-(userLen); 00093 wchar_t *pw = new wchar_t[rLen+1]; 00094 wcsncpy_s(pw, rLen+1, data+userLen, _TRUNCATE); 00095 printf("Check login for u:%ls ; p:%ls",user, pw); 00096 int pID; 00097 //Check login 00098 //There seems to be a bug here: 00099 //dbManager.loginPlayer(user, pw, &pID); 00100 pID = nextID++; 00101 if ( pID != -1 ) { 00102 //if it has been successfull, we will add the client now: 00103 SraClientData client = {0}; 00104 client.clientID = pID; 00105 //client.lastKeepAlive = keepAlive.sec; 00106 //Read additional character data from DB, e.g. current zone etc. 00107 client.zoneID = 124; 00108 addClient( address, client); 00109 } 00110 return pID; 00111 00112 } 00113 }
Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.