Shadowrun: Awakened 29 September 2011 - Build 871
Classes | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes
SraNetwork::ClientRegister Class Reference

#include <ClientRegister.h>

List of all members.

Classes

struct  SystemAddressCompare

Public Member Functions

void addClient (SystemAddress address, SraClientData client)
bool checkForValidCharacterInfo (const BasicCharacterInfo *info, RakString *resultMessage)
int createCharacterForPlayer (int raceId, SystemAddress address, int clientID)
const SystemAddressgetAddressFromClient (const SraClientData *data)
const SystemAddressgetAddressFromClientID (const int clientID)
const SraClientDatagetClientFromAddress (SystemAddress address)
void getPlayersForClient (const int clientID, const int *outPlayerIDs)
int loginClient (const string &username, const string &password, SystemAddress address)
void removeClient (const SystemAddress address)
void removeClient (const SraClientData *data)

Static Public Member Functions

static ClientRegistergetInstance ()

Private Member Functions

 ClientRegister (void)
 ~ClientRegister (void)

Private Attributes

tbb::concurrent_hash_map
< SystemAddress, SraClientData,
SystemAddressCompare
clientAddressRegister
int nextID

Static Private Attributes

static ClientRegisterinstance = new ClientRegister()

Detailed Description

Definition at line 49 of file ClientRegister.h.


Constructor & Destructor Documentation

SraNetwork::ClientRegister::ClientRegister ( void  ) [private]

Definition at line 7 of file ClientRegister.cpp.

References Singleton< DbConnectionFactory >::GetSingleton(), and nextID.

    {
        //Initalize db connection
        // factory("localhost:3306", "root", "SRA_Dev", "sra_dev");
        //static SraData::DbConnectionFactory factory("localhost", "root", "SRA_Dev", "sra_dev");
        SraData::DbConnectionFactory factory( "tcp://127.0.0.1:3306", "root", "SRA_Dev", "sra_dev");
        sql::Connection* connection = SraData::DbConnectionFactory::GetSingleton().getConnection();
        nextID = 42;
    }
SraNetwork::ClientRegister::~ClientRegister ( void  ) [private]

Definition at line 17 of file ClientRegister.cpp.

    {
    }

Member Function Documentation

void SraNetwork::ClientRegister::addClient ( SystemAddress  address,
SraClientData  client 
)

Add a client (i.e. SraclientData) with the given SystemAddress. TODO: Check if the client is already connected?

Definition at line 30 of file ClientRegister.cpp.

References clientAddressRegister, and test::s.

Referenced by loginClient().

    {
        SystemAddress s = address;
        tbb::concurrent_hash_map<SystemAddress, SraClientData, SystemAddressCompare>::accessor acc;
        clientAddressRegister.insert(acc,s);
        acc->second = client;
    }
bool SraNetwork::ClientRegister::checkForValidCharacterInfo ( const BasicCharacterInfo info,
RakString resultMessage 
)

Checks if the given character info is valid. This will check for existing characters with the same name and a valid attribute set

Definition at line 145 of file ClientRegister.cpp.

References SraNetwork::BasicCharacterInfo::charName, RakNet::RakString::IsEmpty(), and SraNetwork::BasicCharacterInfo::raceId.

Referenced by SraNetwork::CreateCharacterMessage::process().

    {
        // Create db factory ... until its fixed
        //SraData::DbConnectionFactory factory( "tcp://127.0.0.1:3306", "root", "SRA_Dev", "sra_dev");
        //TODO: Add some checks to the DB 
        if (info->charName.IsEmpty())
        {
            *resultMessage = "Can not create a character without a name!";
            return false;
        } else if (info->raceId < 0)
        {
            *resultMessage = "Invalid race, please try again";
            return false;
        }
        return true;
    }
int SraNetwork::ClientRegister::createCharacterForPlayer ( int  raceId,
SystemAddress  address,
int  clientID 
)

Create a new chracter for the player, currently thats only a race. Returns the newly created character ID when successfull.

Definition at line 116 of file ClientRegister.cpp.

References seqmission::charID.

Referenced by SraNetwork::CreateCharacterMessage::process().

    {
        if ( clientID == -1 )
        {
            return -1;
        }

        //int raceID = _wtoi(data);
        //if ( raceID == 0 )
        //{
        //  return;
        //}
        // Call database
        int charID, playID;
        
        // Create db factory ... until its fixed
        SraData::DbConnectionFactory factory( "tcp://127.0.0.1:3306", "root", "SRA_Dev", "sra_dev");
        SraData::DbProcedures::createPlayerCharacter( raceId, clientID, &playID, &charID);
        return charID;
    }
const SystemAddress * SraNetwork::ClientRegister::getAddressFromClient ( const SraClientData data)

Get SystemAddress associated with the given SraClientData.

Definition at line 76 of file ClientRegister.cpp.

References clientAddressRegister.

Referenced by removeClient().

    {
        tbb::concurrent_hash_map<SystemAddress, SraClientData, SystemAddressCompare>::const_iterator it;
        for (  it = clientAddressRegister.begin(); it != clientAddressRegister.end(); ++it)
        {
            if ( it->second == *client ) 
            {
                return &it->first;
            }
        }
        return NULL;
    }
const SystemAddress * SraNetwork::ClientRegister::getAddressFromClientID ( const int  clientID)

Get SystemAddress associated with a SraClientData that has the given clientID.

Definition at line 57 of file ClientRegister.cpp.

References clientAddressRegister.

Referenced by SraNetwork::WorldServer::ZoneServerConnectionThread().

    {
        tbb::concurrent_hash_map<SystemAddress, SraClientData, SystemAddressCompare>::const_iterator it;
        for (  it = clientAddressRegister.begin(); it != clientAddressRegister.end(); ++it)
        {
            if ( it->second.clientID == clientID ) 
            {
                return &it->first;
            }
        }
        return NULL;
    }
const SraClientData * SraNetwork::ClientRegister::getClientFromAddress ( SystemAddress  address)

Get SraClientData associated with the given adress.

Definition at line 69 of file ClientRegister.cpp.

References clientAddressRegister.

Referenced by SraNetwork::LeaveChannelMessage::process(), SraNetwork::JoinChannelMessage::process(), SraNetwork::IncommingConnectionMessage::process(), SraNetwork::GetClientCharacterListMessage::process(), and SraNetwork::CreateCharacterMessage::process().

    {
        tbb::concurrent_hash_map<SystemAddress, SraClientData, SystemAddressCompare>::const_accessor acc;
        clientAddressRegister.find(acc,address);
        return acc.empty() ? NULL : &(acc->second);
    }
ClientRegister * SraNetwork::ClientRegister::getInstance ( ) [static]
void SraNetwork::ClientRegister::getPlayersForClient ( const int  clientID,
const int *  outPlayerIDs 
)

Get players of current client.

Definition at line 137 of file ClientRegister.cpp.

Referenced by SraNetwork::GetClientCharacterListMessage::process().

    {
        //TODO: query DB for players
        //int playerIndex = 0;

    }
int SraNetwork::ClientRegister::loginClient ( const string &  username,
const string &  password,
SystemAddress  address 
)

Check login credentials and add user to register. Returns the clientID if the login data is correct.

Definition at line 91 of file ClientRegister.cpp.

References addClient(), SraNetwork::SraClientData::clientID, and SraNetwork::SraClientData::zoneID.

Referenced by SraNetwork::ClientLoginMessage::process().

    {
        // Create db factory ... until its fixed
        SraData::DbConnectionFactory factory( "tcp://127.0.0.1:3306", "root", "SRA_Dev", "sra_dev");

        int pID = -1;

        // Call database
        SraData::DbProcedures::createPlayer(username, password, &pID);
        
        // If we have found a player, process it and return the player ID
        if ( pID != -1 ) {
            //Create the struct
            SraClientData client = {0};
            client.clientID = pID;
            //client.lastKeepAlive = keepAlive.sec;
            //Read the current zone, we can only have one zone, so ignore the query id.
            int queryID;
            SraData::DbProcedures::getPcZone( pID, &client.zoneID, &queryID);

            addClient( address, client);
        }
        return pID;
    }
void SraNetwork::ClientRegister::removeClient ( const SraClientData data)

Remove client.

Definition at line 46 of file ClientRegister.cpp.

References getAddressFromClient(), and removeClient().

    {
        const SystemAddress* addr = getAddressFromClient(data);
        if (addr != NULL) 
        {
            removeClient(*addr);
        }
    }
void SraNetwork::ClientRegister::removeClient ( const SystemAddress  address)

Remove client wit the given SystemAddress.

Definition at line 38 of file ClientRegister.cpp.

References clientAddressRegister.

Referenced by removeClient().

    {
        tbb::concurrent_hash_map<SystemAddress, SraClientData, SystemAddressCompare>::accessor acc;
        if (clientAddressRegister.find(acc, address) ) {
            clientAddressRegister.erase(acc);
        }
    }

Member Data Documentation

The clients are stored in a concurrent hash map, because many 'PacketProcessingTask' tasks may access this register concurrently. We are using SystemAddresses as key values, because they are unique (are they?) and we need those addresses to send data to the clients.

Definition at line 75 of file ClientRegister.h.

Referenced by addClient(), getAddressFromClient(), getAddressFromClientID(), getClientFromAddress(), and removeClient().

Definition at line 77 of file ClientRegister.h.

Referenced by getInstance().

Definition at line 131 of file ClientRegister.h.

Referenced by ClientRegister().


The documentation for this class was generated from the following files:

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net