Sign in to follow this  
savail

Finding specific data on server provided from client

Recommended Posts

Hey,

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!

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this