Add infos into target window
[ryzomcore.git] / ryzom / server / src / frontend_service / client_id_lookup.h
blobbf14ee98039a54c4f285169845289e42bdadb656
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2015 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #ifndef NL_CLIENT_ID_LOOKUP_H
23 #define NL_CLIENT_ID_LOOKUP_H
25 #include "nel/misc/types_nl.h"
26 #include "game_share/ryzom_entity_id.h"
27 #include "fe_types.h"
30 /**
31 * CClientIdLookup.
32 * Allows to translate an entity CEntityId to a TClientId if the specified entity
33 * is played by a client connected on this frontend service, and
34 * also translates a TEntityIndex to a TClientId.
36 * \author Olivier Cado
37 * \author Nevrax France
38 * \date 2002
40 class CClientIdLookup
42 public:
44 /// Constructor
45 CClientIdLookup() {}
47 /// Init
48 void init( TDataSetIndex maxNbRows )
50 _EntityIndexToClient.resize( maxNbRows );
51 uint i;
52 for ( i=0; i!=maxNbRows; ++i )
54 _EntityIndexToClient[i] = INVALID_CLIENT;
58 /** Return the client id corresponding to the specified CEntityId,
59 * or INVALID_CLIENT if there is no such client on this frontend service.
61 TClientId getClientId( const NLMISC::CEntityId& id )
63 //nlinfo( "get: %u elements", _IdToClientMap.size() );
64 TIdToClientMap::const_iterator iecm = _IdToClientMap.find( id );
65 if ( iecm != _IdToClientMap.end() )
66 return (*iecm).second;
67 else
68 return INVALID_CLIENT;
71 /** Return the client id corresponding to the specified entity index (fast),
72 * or INVALID_CLIENT if there is no such client on this frontend service.
74 TClientId getClientId( const TEntityIndex& entityindex )
76 return _EntityIndexToClient[entityindex.getIndex()];
79 /// Test if an entity id is present
80 bool findEntityId( const NLMISC::CEntityId& id )
82 return _IdToClientMap.find( id ) != _IdToClientMap.end();
85 /// Add an id mapping
86 bool addId( const NLMISC::CEntityId& id, TClientId clientid )
88 nlassert( clientid <= MaxNbClients );
89 return _IdToClientMap.insert( std::make_pair( id, clientid ) ).second; // false if Entity Index added twice to CClientIdLookup
90 //nlinfo( "add: %u elements", _IdToClientMap.size() );
93 /// Add an entity index mapping for fast access
94 void addEntityIndex( const TEntityIndex& entityindex, TClientId clientid )
96 nlassert( clientid <= MaxNbClients );
97 if ( _EntityIndexToClient[entityindex.getIndex()] != INVALID_CLIENT )
98 nlwarning( "Trying to remap an entityindex to a different clientid");
99 _EntityIndexToClient[entityindex.getIndex()] = clientid;
102 /// Remove a id mapping
103 void removeEId( const NLMISC::CEntityId& id )
105 if ( _IdToClientMap.erase( id ) == 0 )
106 nlwarning ("Cannot find EntityId %s to remove in EntityToClient (multiple use of the same account?)", id.toString().c_str() );
109 /// Remove an entity index mapping
110 void removeEntityIndex( const TEntityIndex& entityindex )
112 _EntityIndexToClient[entityindex.getIndex()] = INVALID_CLIENT;
116 private:
118 typedef std::vector<TClientId> TEntityIndexToClient;
120 struct CIdHash
122 enum { bucket_size = 4, min_buckets = 8 };
123 size_t operator () (NLMISC::CEntityId id) const { return (uint32)id.getShortId(); }
124 bool operator() (const NLMISC::CEntityId& left, const NLMISC::CEntityId& right) { return left < right; }
127 typedef CHashMap<NLMISC::CEntityId, TClientId, CIdHash> TIdToClientMap;
129 /// Access TClientId indexed by TEntityIndex (if the entity is a client connected on this FS)
130 TEntityIndexToClient _EntityIndexToClient;
132 /// Access TClientId using a id
133 TIdToClientMap _IdToClientMap;
137 #endif // NL_CLIENT_ID_LOOKUP_H
139 /* End of client_id_lookup.h */