Added code to establish the time delta between the client and server

clocks immediately following authentication.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1398 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2002-05-28 21:56:38 +00:00
parent f9980f9353
commit 2f1a67077a
7 changed files with 390 additions and 28 deletions
@@ -1,5 +1,5 @@
//
// $Id: BootstrapData.java,v 1.4 2002/02/04 01:47:20 mdb Exp $
// $Id: BootstrapData.java,v 1.5 2002/05/28 21:56:38 mdb Exp $
package com.threerings.presents.net;
@@ -18,4 +18,7 @@ public class BootstrapData extends DObject
/** The oid to which to send invocation requests. */
public int invOid;
/** The time from which server ticks are incrementing. */
public long serverStartStamp;
}
@@ -1,8 +1,12 @@
//
// $Id: PingRequest.java,v 1.5 2001/10/11 04:07:53 mdb Exp $
// $Id: PingRequest.java,v 1.6 2002/05/28 21:56:38 mdb Exp $
package com.threerings.presents.net;
import java.io.IOException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
public class PingRequest extends UpstreamMessage
{
/** The code for a ping request. */
@@ -16,6 +20,46 @@ public class PingRequest extends UpstreamMessage
super();
}
/**
* Returns a timestamp that was obtained when this packet was encoded
* by the low-level networking code.
*/
public long getPackStamp ()
{
return _packStamp;
}
/**
* Returns a timestamp that was obtained when this packet was decoded
* by the low-level networking code.
*/
public long getUnpackStamp ()
{
return _unpackStamp;
}
// documentation inherited
public void writeTo (DataOutputStream out)
throws IOException
{
super.writeTo(out);
// grab a timestamp noting when we were encoded into a raw buffer
// for delivery over the network
_packStamp = System.currentTimeMillis();
}
// documentation inherited
public void readFrom (DataInputStream in)
throws IOException
{
super.readFrom(in);
// grab a timestamp noting when we were decoded from a raw buffer
// after being received over the network
_unpackStamp = System.currentTimeMillis();
}
public short getType ()
{
return TYPE;
@@ -25,4 +69,12 @@ public class PingRequest extends UpstreamMessage
{
return "[type=PING, msgid=" + messageId + "]";
}
/** A time stamp obtained when we serialize this object. */
protected long _packStamp;
/** A time stamp obtained when we unserialize this object (the intent
* is to get a timestamp as close as possible to when the packet was
* received on the network). */
protected long _unpackStamp;
}
@@ -1,8 +1,14 @@
//
// $Id: PongResponse.java,v 1.6 2001/10/11 04:07:53 mdb Exp $
// $Id: PongResponse.java,v 1.7 2002/05/28 21:56:38 mdb Exp $
package com.threerings.presents.net;
import java.io.IOException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import com.threerings.presents.Log;
public class PongResponse extends DownstreamMessage
{
/** The code for a pong response. */
@@ -16,6 +22,82 @@ public class PongResponse extends DownstreamMessage
super();
}
/**
* Constructs a pong response which will use the supplied ping time to
* establish the end-to-end processing delay introduced by the server.
*/
public PongResponse (long pingStamp)
{
// save this for when we are serialized in preparation for
// delivery over the network
_pingStamp = pingStamp;
}
/**
* Returns the time at which this packet was packed for delivery in
* the time frame of the server that sent the packet.
*/
public long getPackStamp ()
{
return _packStamp;
}
/**
* Returns the number of milliseconds that elapsed between the time
* that the ping which instigated this pong was read from the network
* and the time that this pong was written to the network.
*/
public int getProcessDelay ()
{
return _processDelay;
}
/**
* Returns a timestamp that was obtained when this packet was decoded
* by the low-level networking code.
*/
public long getUnpackStamp ()
{
return _unpackStamp;
}
// documentation inherited
public void writeTo (DataOutputStream out)
throws IOException
{
super.writeTo(out);
// make a note of the time at which we were packed
_packStamp = System.currentTimeMillis();
out.writeLong(_packStamp);
// the time spent between unpacking the ping and packing the pong
// is the processing delay
if (_pingStamp == 0L) {
Log.warning("Pong response written that was not constructed " +
"with a valid ping stamp [rsp=" + this + "].");
_processDelay = 0;
} else {
_processDelay = (int)(_packStamp - _pingStamp);
}
out.writeInt(_processDelay);
}
// documentation inherited
public void readFrom (DataInputStream in)
throws IOException
{
super.readFrom(in);
// grab a timestamp noting when we were decoded from a raw buffer
// after being received over the network
_unpackStamp = System.currentTimeMillis();
// read in our time stamps
_packStamp = in.readLong();
_processDelay = in.readInt();
}
public short getType ()
{
return TYPE;
@@ -25,4 +107,23 @@ public class PongResponse extends DownstreamMessage
{
return "[type=PONG, msgid=" + messageId + "]";
}
/** The ping unpack stamp provided at construct time to this pong
* response; only valid on the sending process, not the receiving
* process. */
protected long _pingStamp;
/** The timestamp obtained immediately before this packet was sent out
* over the network. */
protected long _packStamp;
/** The delay in milliseconds between the time that the ping request
* was read from the network and the time the pong response was
* written to the network. */
protected int _processDelay;
/** A time stamp obtained when we unserialize this object (the intent
* is to get a timestamp as close as possible to when the packet was
* received on the network). */
protected long _unpackStamp;
}