TCP - Keeping clients synchronized

Started by
4 comments, last by hplus0603 12 years, 1 month ago
I'm working on a networked flash game using TCP sockets (forced to use TCP b/c of flash). The game is pretty simple as it is mostly a text based game. The only real synchronization that must be enforced is a countdown timer for each client. This timer will start at 60 seconds and countdown to 0 and at 0 some game state change will occur, so it is important that all clients get to 0 at a similar time.

I could send the "start countdown" command from my server to all clients and use the clients system clock, but if the packet is dropped or delayed then a client could get out of sync with the others. I could also enforce some client/server protocol where the client must "check in" with the server every second (or less) and the server enforces the sync of all clients. My concern with this is efficiency. My game is a slow paced text game so it is very possible that a client may not have any data to send to the server for up to 30 seconds at a time.

It may not be possible to avoid the "constantly checking in" situation, but I'm hoping someone can give me some pointers to an efficient solution for keeping a timer in sync between multiple clients in a flash application.

Thanks in advance.

P.S. I don't think NTP is possible with flash.
Advertisement
What's stopping you from implementing NTP? I don't understand that limitation. If you can create arbitrary TCP conversations, you can do NTP. (Yes, it'll be potentially less reliable because of TCP's guaranteed delivery mechanisms, but there are ways to mitigate that.)

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

What matters is that all clients see the same events in the same order. The exact timing doesn't matter as much. If you time-stamp events with "ticks" (where you define how long a tick is) then the game will be sufficiently synchronized.
enum Bool { True, False, FileNotFound };
This timer will start at 60 seconds and countdown to 0 and at 0 some game state change will occur, so it is important that all clients get to 0 at a similar time.[/quote]


Start the timer on the server
Send start timer command to clients to do locally
When the server timer hits send your state change to the clients
The clients will be lagged behind by ms most likely but by the sounds that won't be a big deal

You see this often in games like World of Warcraft when crafting or gathering materials that run a progress bar. With high lag the progress bar is complete but the window doesn't come up yet. I can only imagine it's because my local timer for my progress bar completed (no lag locally) but the command from the server to open the loot window and show me my loot is taking longer to get to me. It doesn't screw things up just looks funny in high lag spikes. Then again most things do look funny in high lag spikes. The lowest lag person will always have an advantage in any game because they'll see data first. Most of the time this will be in MS so it won't be much of an advantage is slow paced games like your game sounds. If it was like 3 seconds it could mean a big difference however, but if someone has a 3 second lag that's pretty significant and shouldn't be playing online games anyway. :)
Thanks guys. I'm thinking that rpiller's option may work as long as the server is smart enough to ignore client requests that happen past the end of the timer count down. I'm still not exactly sure what hplus0603 about using time stamps to keep everything in sync. If you wouldn't mind going into a bit more detail on that I would appreciate it!

Thanks all!
The idea is simple: Each simulation "event" happens at a particular "tick number." If you run at 60 Hz, you have 60 ticks per second. If you run at 1 Hz, you have 1 tick per second. Events are tagged with the tick number they happen. Each client (and server) calculates the tick/time relation on their own. Clients that provide input tell the server "this was input for tick #N" and the server discards it if it's too old.

You can occasionally send the last tick number received, and what server tick number it was received at, from the server to the client. That way, the client knows how far to adjust the tick offset in relative terms, without needing to know what the clock is on the server.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement