Finding specific data on server provided from client

Started by
4 comments, last by savail 11 years, 2 months ago

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!

Advertisement

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.

How would I generate a unique ID then? I have few ideas but none of them seems 100% efficient. Can you give me some glimpse on that?

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.

You can store a unique IPv4 address and port combination in a single 64 bit integer:
__int64 id = __int64(ip) << 16 | port;

alright, thanks for answers

This topic is closed to new replies.

Advertisement