I have a lobby server and I'm adding data about online users in a vector. So far, when it comes to activities in which I must set a state for only one particular user I've been iterating through my vector to find the specific user by the nick. I'm doing so everytime and I'm wondering if there's any better solution? I don't want to iterate through all my users each time I have to set something to only one of them. If a client could send his genuine index of his place in my server's vector that would be cool but once client sent a message with his index, other users could disconnect - server's vector would be shortened and therefore the index wouldn't be correct anymore. I can't think on how to implement such mechanism. It needs a lot of synchronisation but maybe there are better means?
I will be grateful for any suggestions!
Finding specific data on server provided from client
Members - Reputation: 333
Posted 31 January 2013 - 08:58 AM
Members - Reputation: 277
Posted 31 January 2013 - 07:19 PM
You should take a look at a map container (or however a hashed table/tree is called in the coding language you are using, std::map is c++).
If you are using c++ take a look at this: http://www.cplusplus.com/reference/map/map/
When you search in a map It does something similar to looking in a vector but it scales better (it's faster when you have a lot of entries (a lot being a few usualy)).
You could use the nikname as a key or (faster search usually) generate a unique ID (uint) for every player and use that.
btw, i dont think this is a networking question but w/e.
Moderators - Reputation: 10552
Posted 02 February 2013 - 06:23 AM
First of all, you cannot trust any identifier found in a packet, because the client might be spoofing*. The good news is that each client already has a unique identifier! The IP address and port number uniquely identify that connection. You can use this combination as a key to lookup a map data structure, or because the number of clients is not large you could use a linear search of a client array, too.
* Exceptions include something like a username and password, because the password is a shared secret that malicious users should not be capable of spoofing.