Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am working on a network game project in python which we want to keep synchronized. I would assume we should use Network Time Protocol to cater for different levels of lag.

That being the case, is there an implementation for Network Time Protocol in python?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

You probably won't be able to use NTP directly -- for one, you need to have a server that is guaranteed to be available to all of your clients. Second, it's really overkill for what you need. What you want to really do is come up with some sort of common time stamp that all of the clients can agree on.

Based on the method from http://www.mine-control.com/zack/timesync/timesync.html, here is what I would do (modified slightly):

  1. Client stamps current local time on a "time request" packet and sends to server
  2. Upon receipt by server, server stamps server-time and returns
  3. Upon receipt by client, client subtracts current time from sent time and divides by two to compute latency. It subtracts current time from server time to determine client-server time delta and adds in the half-latency to get the correct clock delta. (So far this algothim is very similar to SNTP)
  4. The client repeats steps 1 through 3 five or more times, pausing a few seconds each time. Other traffic may be allowed in the interim, but should be minimized for best results The results of the packet receipts are accumulated and sorted in lowest-latency to highest-latency order. The median latency is determined by picking the mid-point sample from this ordered list.
  5. All samples above approximately 1 standard-deviation from the median are discarded and the remaining samples are averaged using an arithmetic mean.

The only difference is that you shouldn't try to set the system time -- most users would consider this to be bad behavior and besides, it's most likely something that would require admin privileges and your game shouldn't need to do anything that requires admin privileges.

I would probably add that you should do some time synchronization periodically within the game as well, especially if it's been going on for a while. You should be able to find a point in the game where the action isn't as heavy and it's a good time to sync.

Alternatively, you could time stamp all packets from the client and the server and build your time synchronization code so that any packet can be used to determine the current latency and what the common game clock should be.

share|improve this answer

You didn't mention if a single server was an authority or not, but have a single time that every client syncs off of.

In my experience with City of Heroes, latency can be streaky, so a running average (root mean square is easy) is a good way to approximate. We also timestamp all roundtrips as part of the packet header.

Truth be told, since the cases that throw things off aren't predictable, there isn't much you can do when the hiccups come your way, and otherwise things are pretty smooth, so I wouldn't put too much effort into it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.