-1

The main "Ping"/"Time" function for the game is this:

Net Code For Server - Time/Ping

Outside of the switch, TimeSync() functions like this:

private void TimeSync()
{
    YGConnection.Send("Time", DateTime.UtcNow.Millisecond);
}

It sends the current UTCNow.Millisecond time to the server.

Finally, getTime() gets the current time + the offset of the server.

private double getTime()
{
    return Math.Round((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds) + Offset;
}

It all works, but the numbers produced are like this:

Version 0.1.5 : Server time offset: -486335789940 - Ping: -943491433067

The server basically sends getTime() back, but without the offset. I'm wondering what is going on with the negative numbers and if there is anything I can do to fix that.

Merlin
  • 929
  • 12
  • 33
  • Looks like you are mixing up units, that offset is not measured in milliseconds. – Ben Voigt Apr 23 '15 at 22:41
  • `DateTime.UtcNow.Millisecond` is value of milliseconds in timestamp (i.e. a number in range [0, 999]). Are you sure this is the number you need? – n0rd Apr 23 '15 at 22:43

1 Answers1

1

DateTime.Milliseconds is the number of milliseconds (thousandths of a second) within the current second. If you only compare Milliseconds, you will lose any information like the second it occurred in.

At midnight it is 00:00:00.000

500 milliseconds later it is 00:00:00.500

500 milliseconds later it is 00:00:01.000

If you are just comparing the milliseconds, the difference between the first two times will be 500.
The difference between the last two will be -500.

What you probably want to do is return a whole date/time value rather than just the milliseconds. If you subtract 2 DateTime objects, you get a TimeSpan object. From that you can find the TimeSpan.TotalMilliseconds will give you how many milliseconds there are between those two times regardless of how many seconds have elapsed.

Tremmors
  • 2,906
  • 17
  • 13
  • That did it, with a little work I've redone my lag/latency and transmitted DateTime directly instead of trying to convert to numerics. System is now responding in much smaller numbers and positive. – Merlin Apr 24 '15 at 00:38
  • Version 0.1.5 : Server time offset: 1474057869632 - Ping: 788.1565 – Merlin Apr 24 '15 at 00:44