How to get current ServerTime value on... server?

Options
ddudek
edited March 2013 in Photon Server
Hi there,

The client's API gives you access to nice PhotonPeer::getServerTime() property, which gives me synchronized timeline on all clients. But how to get this value on server?

I.e. I'd like to server to decide when the gameplay starts, and I tried to send the mesage like this:
        protected virtual void PublishStartGameEvent()
        {
            string levelName = "Level1";

            int serverTime = ???
            int zeroTime = serverTime + 5000; // + 5s
            var startGameEvent = new StartGameEvent(levelName, zeroTime);

            this.PublishEvent(startGameEvent, this.Actors, new SendParameters() { Unreliable = false });
        }

Comments

  • Sergiy
    Options
    Maybe I didn't understand your question correctly, but.... DateTime.Now ? ;)
  • Sergiy wrote:
    Maybe I didn't understand your question correctly, but.... DateTime.Now ? ;)
    Well, according to the documentation, client's PhotonPeer::getServerTime() gives me server machine uptime in miliseconds, so its not date. It's an int number, and is relatively small. Does anyone know how to get the corresponding value on the server? :)

    The case would be to know how it is calculated.. documentations says that it is set shortly after the connection is established, maybe it's related to sending ping and calculating RoundTripTime?
  • Philip
    Options
    The equivalent on the server would be:

    int serverTime = System.Environment.TickCount;
  • Philip wrote:
    The equivalent on the server would be:

    int serverTime = System.Environment.TickCount;
    It works, thanks :)
  • Philip wrote:
    The equivalent on the server would be:

    int serverTime = System.Environment.TickCount;

    Hi, this question made me wonder, if there is any way to get the time on the MasterServer from one of the SubServers that connect to it instead of manually sending echo requests often, i mean the sub server has(must) to send echo request often to keep itself from disconnecting?
  • Philip
    Options
    Philip wrote:
    The equivalent on the server would be:

    int serverTime = System.Environment.TickCount;

    Hi, this question made me wonder, if there is any way to get the time on the MasterServer from one of the SubServers that connect to it instead of manually sending echo requests often, i mean the sub server has(must) to send echo request often to keep itself from disconnecting?

    Well our S2S api doesn't provide that - not very elegant but if it helps: you could use the client library.
    Maybe better just synchronize the time of your servers.
  • thank you, and yes the client library isn't very elegant and opens door for hackers to trust the client with time. I ended up manually sending a ping request from client every now and then and synchronized it with the server.
  • Tobias
    Options
    How will you use the game-server's time on the master?
  • i am not, I am using client's time on the sub-server to calculate lag for lag compensation in movement
  • Tobias
    Options
    If you can do that, you can also compensate lag of the event/RPC "i won the game, end in X seconds", no?
    How do you use the client time? Did you check if it's working with clients running on different machines (with different client times)?
  • Should've been more clear, what I meant to say was, I send Server's Current Time to the server from the client at the time of the movement request. Then the master server forwards the movement packet to the zone server. It is from the zone server that I calculate the lag. Right now I basically send a ping request to the master once its connected to the master. When the master receives the request it basically sends its current time(Environment.TickCount) to the requested server as a ping response. The zone server then calculates the offset using RTT , current zone server tick count, and the sent time.

    // I calculate the offset using the following formula in the zone server
    _offset = response.MasterTimeStamp + (this.RoundTripTime >> 1) - Environment.TickCount

    Is this the best way to do it or would you suggest a better one?

    [Edit] : The above method seems to deviate a little. Any clue why?
    [Edit]: Found the problem, the RTT is 0 in the zone server, any idea? (ZoneServer is a ServerPeerBase which is connected to the Master)
  • I scratched the RTT idea because it was giving me errors and decided to sync the server time myself. Currently I am sending about 10 ping requests to the master from the sub server after it is registered. And when the master sends the response I calculate the latency by

    var latency = (Environment.TickCount - response.SentServerTimeStamp) >> 1;

    then add these latencies to a list until I finish the 10th ping response. Once I get enough ping responses I omit latencies which are above one standard deviations and use the rest to calculate the average latency and use this to sync the time between master and the sub server. Can anyone suggest a better approach or should it work fine?