Data Coordination Between Server & Client

Started by
34 comments, last by roadkillguy 12 years, 9 months ago
I'm working on a game in c++. I've got all the connection stuff set up, client, server, etc., and it runs rather nicely.

Now, I need to know how to best not only coordinate player positions, but when to send what packets as well. So far, I tween player positions when the x,y,z coordinate is close enough, and do a hard set when it's far away. The player data is sent every several milliseconds. Should I be sending key presses to animate the players better? (My game has gravity) What if somebody forges packets and is able to warp to the other side of the map? I need a way to prevent this.

My game also involves bullets, and guns. (It's kind of a FPS) I'd like to make my game fast rolleyes.gif, so I'd like to avoid sqrt(x*x + y*y + z*z) for every bullet, every update. Is there any other way to get around this? I currently keep track of inventory, so packet forging on bullets wont be an issue. (I'm kind of paranoid of this due to the fact that other games have failed miserably from client side programming and cheaters)

Some general pointers or websites would be great.

Thanks.
Advertisement
I can help with one question.

When comparing two distances simply to see which is the larger, you don't need the square root. [font="Arial"]Use [/font][font="Arial"]if (x1*x1 + y1*y1 + z1*z1 > x2*x2 + y2*y2 + z2*z2) instead of [/font][font="Arial"]if (sqrt(x1*x1 + y1*y1 + z1*z1) > sqrt(x2*x2 + y2*y2 + z2*z2))

[/font]It may speed up some calculations - in theory. :)

As for forged packets, never trust the client. That is all.
Moving to the Networking forum, where you're likely to get much better answers :-)

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Sappharos is right: Never trust the client.

A mechanism that a lot of games use is to send a "baseline" snapshot every 5-10 seconds, and just sending keypresses in between, from the server to all viewing clients.
Meanwhile, for the controlling client to server, only send key presses, and have the server do the simulation and send out the results.
The client can also do the simulation and display to the user, to reduce lag; however, it needs to detect when the server came to a different conclusion than itself, and "snap" the player to the appropriate point.

Typically, this can be done with the periodic baseline updates, if they have a global step number timestep, for example.
enum Bool { True, False, FileNotFound };
Are your clients connecting only to one another (Peer-2-Peer)?

I'm not sure how far along you are with the packet sending but a good system to include is a simple debug slider that lets you alter how frequently the "base" update packets are sent (your player positions, etc). This makes it easy to observe how correct your game looks as you add more network traffic - ideally you want to find the lowest frequency of sending data that still looks good. It's possible you may want to have immediate sending of packets with crucial data in them (firing a rocket, end of a game round, etc).
I apologize for the long delay.. I actually got distracted working on my game :D

@Kazzahdrane No, it's not peer2peer, I believe server-client works better in my situation. I currently send packets every 500ms, and it seems to work ok. I'm not sure if this is fast or slow compared to the industry standard.

@hplus0603 What exactly do you mean by baseline? I have my data wrapped up for keypresses, but I'm not exactly sure what to do. Do i send a packet every time a key is pressed, or do I simply update every 500ms or so?

@ApochPIQ Thank you, I'm rather new here.

@Sappharos I've always tried to follow Never Trust the Client. Sometimes I feel as though I'm a bit paranoid. For example, can the client ignore packets? I currently implement a strike system. If someone were to use cheatengine (It's scary stuff for developers) to hack their inventory, and the player tries to use that item, they are dealt a strike. After 5 strikes, the server gives end of stream and disconnects the player. Is that okay?

Also, that algorithm is exellent.. I cant believe I didn't think of that.
What if somebody forges packets and is able to warp to the other side of the map?[/quote]

If you let them, it's safe to assume they will. Any data which could be manipulated to give the player an advantage in the game (position, health, ammo, money or other resources) is sensitive. My personal approach would be not to ban people for hacking the client (as you get more players this becomes less practical, and there's always a possibility of mistakes) but to simply make it impossible for it to confer an advantage to them. So yes, I'd just send a snapshot of the player position every few seconds for the server to check, if it's too far away then correct it. Don't allow the player's ammo to go up unless they've just picked up some, or they're in a shop and there is an appropriate deduction from their money. That sort of thing.
I am writing my own game server now as well, and I was thinking of going the same route as you with the strike system. I would log when a player tried to do something impossible, and after so many times, ban the account. However, after thinking about it more, I think that is a bad idea; let me explain why. If the server is checking all requests from clients (as it should), then a client should not be able to cheat. So, I would be banning accounts for probing my server for vulnerabilities. Yes, I would be a bit upset that someone is doing this, but as long as no damage occurs, I should continue to let the client probe --as long as its not causing damage to my system --after all, the client should be paying me money to play. So, as long as I am getting paid, and no damage to the server occurs, let them probe all they want, right? If I ban them, I am out money. If I let them continue, I get money. Better to get paid than not ... right?
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.
When clients do something they shouldn't, log it, and discard their input.

Then, patrol the logs periodically. You can even write tools to help with this. If you see a particular client doing suspicious stuff consistently, then you can think about taking action. Until then, the best policy is to just ignore things that shouldn't be done. That way, people can't tell the difference between sending garbage to the server (which should do nothing) and sending legitimate but "illegal" input to the server (which, if they knew it was legit but illegal, could tell them how to further exploit your system).

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

[color="#1C2837"]
When clients do something they shouldn't, log it, and discard their input.

Then, patrol the logs periodically. You can even write tools to help with this. If you see a particular client doing suspicious stuff consistently, then you can think about taking action. Until then, the best policy is to just ignore things that shouldn't be done. That way, people can't tell the difference between sending garbage to the server (which should do nothing) and sending legitimate but "illegal" input to the server (which, if they knew it was legit but illegal, could tell them how to further exploit your system). [color="#1C2837"][/quote]

[color="#1c2837"]Sort of like Darwinian Evolution...
[color="#1C2837"]That's what I do essentially. Maybe after x number of strikes the server should start making warnings to the admin.
[color="#1C2837"]Should I consider basic packet encryption, such as XOR?

This topic is closed to new replies.

Advertisement