STL/Map for player/address association
Well for our Multiplayer Game I decided to use unconnected UDP. As the game should support a lot of clients this is the best solution for our needs. Now I have to associate the sockaddr_in struct with a specific player. I read something about using the stl::map container - As this was my idea too my problem is now the map key - what should I use for this? It has to contain address and port. This association is needed for every received message and i would like to make it as effective as possible....
Help would be appreciated
Live is just a RPG - But the detail level is really good...
Live is just a RPG - But the detail level is really good...
March 26, 2002 09:46 AM
Here is what i have used:
to loop thru each ip just do:
SOCKADDR_IN saddr; //<< received from recvfromstd::vector<SOCKADDR_IN> addrList;addrList.push_back(saddr);
to loop thru each ip just do:
std::vector<SOCKADDR_IN>::iterator iter = addrList.begin();for(;iter != addrList.end(); iter++){ SOCKADDR_IN saddrin = (*iter); //or whatever u want to do}
I just created a Host ID associated with each new computer attempts to connect. I then associate all the address information with that HostID. I inform the connecting host of it''s new HostID and that becomes part of the header it uses when sending information. This way, after connection is established I can refer to hosts by their ID''s. This solves the problem of firewalls that will occasionally change the port that they send data from. If you do the lookup every time you receive a new ip/port pair then your going to loose some of your players who are behind firewalls. (And potentially connecting new ones that don’t exist) because you will keep sending to the old IP/port pair. You can''t use IP address alone because anyone using Internet Connection sharing, router, firewall, etc is only going to be able to connect one of their computers to your game.
The only caveat is that you have to update the address information associated with a host ID if it changes so that you are sending to the right place.
Good Luck!
The only caveat is that you have to update the address information associated with a host ID if it changes so that you are sending to the right place.
Good Luck!
Use a map, or preferably a hash_map. The vector method is too slow. I don''t think you really want to iterate through all of your connections everytime you receive a message. It does work, but if you plan to have many connections you will want to use something else.
To use the hash_map you have to provide a hash function, and an equality function. See documentation for your compilers version of STL. STLPort has a great hash_map (www.stlport.org)and it''s free. I don''t think VC++ provides a hash_map in it''s implementation of STL.
To use the hash_map you have to provide a hash function, and an equality function. See documentation for your compilers version of STL. STLPort has a great hash_map (www.stlport.org)and it''s free. I don''t think VC++ provides a hash_map in it''s implementation of STL.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement